kubernetes yamlを書く最初のとっかかり

# これは？

kubernetesについての概要は目にするけど…チートシートはあるけど…
で、**yamlどうやって書くんじゃい！！！**
って取っ掛かりの開設があまりなかったので、手っ取り早くyamlのひな形を作成する例

# 結論
以下の二通りで私は作っている
- vscodeに拡張機能を入れる
- kubectl create `{{マニュフェスト種別}}`　`{{名前}}` -o yaml --dry-run=client > temp.yaml

## vscodeに拡張機能を入れる

何はともあれ入れるが吉

https://marketplace.visualstudio.com/items?itemName=ms-kubernetes-tools.vscode-kubernetes-tools
<!--
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3472791/f532efb9-96a3-0b86-ed6e-34bcf4ac8f87.png)
-->

こんな風に瞬く間にひな形ができる

![Animation.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3472791/f8c332ee-ed79-4ddf-2c07-136eead4ab5a.gif)


## コマンドの例

マニュフェストはいっぱいあるけど、一つ一つについて覚える必要はなく、都度`-h`で確認すればOK

デプロイメントだと下記のようになる

~~~
# 下のyamlが生成される
# --dry-run=clientをつけなければ、この内容でdeployが作成される

$ kubectl  create deployment mydeployment --image=nginx -o yaml --dry-run=client
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mydeployment
  name: mydeployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mydeployment
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: mydeployment
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
~~~

configmapが作りたいと思ったら、たとえば…


~~~
$ kubectl create -h | grep Available -A18
Available Commands:
  clusterrole           Create a cluster role
  clusterrolebinding    Create a cluster role binding for a particular cluster role
  configmap             Create a config map from a local file, directory or literal value
  cronjob               Create a cron job with the specified name
  deployment            Create a deployment with the specified name
  ingress               Create an ingress with the specified name
  job                   Create a job with the specified name
  namespace             Create a namespace with the specified name
  poddisruptionbudget   Create a pod disruption budget with the specified name
  priorityclass         Create a priority class with the specified name
  quota                 Create a quota with the specified name
  role                  Create a role with single rule
  rolebinding           Create a role binding for a particular role or cluster role
  secret                Create a secret using specified subcommand
  service               Create a service using a specified subcommand
  serviceaccount        Create a service account with the specified name
  token                 Request a service account token
~~~

解説とサンプルコマンドがいっぱい出てくるので、コピペすればOK

~~~
$ kubectl create configmap -h
Create a config map based on a file, directory, or specified literal value.

 A single config map may package one or more key/value pairs.

 When creating a config map based on a file, the key will default to the basename of the file, and
the value will default to the file content.  If the basename is an invalid key, you may specify an
alternate key.

 When creating a config map based on a directory, each file whose basename is a valid key in the
directory will be packaged into the config map.  Any directory entries except regular files are
ignored (e.g. subdirectories, symlinks, devices, pipes, etc).

Aliases:
configmap, cm

Examples:
  # Create a new config map named my-config based on folder bar
  kubectl create configmap my-config --from-file=path/to/bar

  # Create a new config map named my-config with specified keys instead of file basenames on disk
  kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt
--from-file=key2=/path/to/bar/file2.txt

  # Create a new config map named my-config with key1=config1 and key2=config2
  kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2

  # Create a new config map named my-config from the key=value pairs in the file
  kubectl create configmap my-config --from-file=path/to/bar

  # Create a new config map named my-config from an env file
  kubectl create configmap my-config --from-env-file=path/to/foo.env --from-env-file=path/to/bar.env

...

~~~

<!--
# 我が家のdnsmasqの例

kubernetesにdnsmasqをデプロイするだけ
k8とは関係なく、LAN内にVM作りすぎて名前引けるようにしたかった。


~~~
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mydnsmasq
spec:
  selector:
    matchLabels:
      app: mydnsmasq
  template:
    metadata:
      labels:
        app: mydnsmasq
    spec:
      containers:
      - name: mydnsmasq
        image: alpine
        command: [ "sh","-c"]
        args:
            - |
                apk update && \
                apk upgrade && \
                apk add bash dnsmasq && \
                rm -rf /var/cache/apk/* && \
                mkdir -p /etc/default/ && \
                echo -e "ENABLED=1\nIGNORE_RESOLVCONF=yes" > /etc/default/dnsmasq && \
                exec dnsmasq --no-daemon
        volumeMounts:
          - mountPath: /etc/dnsmasq.conf
            subPath: dnsmasq.conf
            name: config
        resources: {}
        ports:
         - containerPort: 53
           protocol: UDP
         - containerPort: 53
           protocol: TCP
      volumes:
         - name: config
           configMap:
            name: mydnsmasq-config
            items:
              - key: dnsmasq.conf
                path: dnsmasq.conf
~~~

~~~
apiVersion: v1
kind: Service
metadata:
  name: mydnsmasq-service
spec:
  selector:
    app: mydnsmasq
  type: LoadBalancer
  ports:
  - port: 53
    protocol: UDP
    targetPort: 53
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mydnsmasq-config
data:
  dnsmasq.conf: |
          port=53
          address=/epgstation.tv/192.168.0.151
~~~

~~~
-->