(個人メモ)kubernetes tips

# はじめに
kubernetesで便利な何かを個人的にまとめているものになります。整理したら、改めて記事を公開しなおすかもしれません

# tips
## 既存podからyamlを出力
```
kubectl get pods -o yaml nginx-test-58586b9f9c-4qqk7
```
```
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: 2018-07-28T18:19:18Z
  generateName: nginx-test-58586b9f9c-
  labels:
    app: nginx-test
    pod-template-hash: "1414265957"
  name: nginx-test-58586b9f9c-4qqk7
  namespace: default
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: nginx-test-58586b9f9c
    uid: b8ba595b-91c5-11e8-b0cf-525400139e17
  resourceVersion: "240803"
  selfLink: /api/v1/namespaces/default/pods/nginx-test-58586b9f9c-4qqk7
  uid: bded4294-9292-11e8-b0cf-525400139e17
spec:
  containers:
  - image: nginx
    imagePullPolicy: Always
    name: master
    ports:
    - containerPort: 80
      protocol: TCP
    resources:
      requests:
        cpu: 100m
        memory: 100Mi
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-x2nfm
      readOnly: true
  dnsPolicy: ClusterFirst
  nodeName: ntw-k8s-nodegpu01
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-x2nfm
    secret:
      defaultMode: 420
      secretName: default-token-x2nfm
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: 2018-07-28T18:19:18Z
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: 2018-07-28T18:19:23Z
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: null
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: 2018-07-28T18:19:18Z
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://1fc84fb7cf9f39188a9a0c9140231ce59a8ee8a3c30b3983f53acfeb3183f5b3
    image: nginx:latest
    imageID: docker-pullable://nginx@sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424
    lastState: {}
    name: master
    ready: true
    restartCount: 0
    state:
      running:
        startedAt: 2018-07-28T18:19:22Z
  hostIP: 10.44.194.85
  phase: Running
  podIP: 192.168.15.4
  qosClass: Burstable
  startTime: 2018-07-28T18:19:18Z
```

## Manifest Template (LoadBalancer)
Nginxのマニフェストテンプレート

```
cat <<'EOF' > deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: LoadBalancer
  ports:
  - name: http
    port: 80
    targetPort: 80
    protocol: TCP
  selector:
    app: nginx
EOF
```

## Manifest Template (NodePort)
Nginxのマニフェストテンプレート

```
cat <<'EOF' > deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    targetPort: 80
    nodePort: 30080
    protocol: TCP
  selector:
    app: nginx
EOF
```

## CentOSをDeploy
```
cat <<'EOF' > ~/manifests/centos_deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: centos-deployment
spec:
  selector:
    matchLabels:
      app: centos
  replicas: 1
  template:
    metadata:
      labels:
        app: centos
    spec:
      containers:
      - name: centos
        image: centos:latest
        command: [ "sleep", "3600000" ]
EOF

kubectl apply -f ~/manifests/centos_deployment.yaml
```

## Namespaceのすべてのリソースを表示
**kubectl get all は全てではないので注意**

```
kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind -o name
```

## 既存のBlockStorageを、PVとして定義する方法
### AWS
https://kubernetes.io/docs/concepts/storage/volumes/#awselasticblockstore
awsElasticBlockStoreオプションで指定

### GCP
https://kubernetes.io/docs/concepts/storage/volumes/#gcepersistentdisk
gcePersistentDiskオプションで指定

### Azure
https://github.com/kubernetes/examples/blob/master/staging/volumes/azure_disk/README.md

azureDiskオプションで指定



## yamlで指定できる項目の確認

```
kubectl explain hogehoge
```

## kubectlコマンド自体のdebug

todo update

```
kubectl hogehoge -v=9 ?
```

## secretの上書き

secretの新規作成をこう作った時に

```
kubectl create secret tls ssl-certificate-secret --key privkey.pem --cert cert.pem
```

kubectl create を再度実行するとエラーになるので、上書きはこうすると便利。

```
kubectl create secret tls ssl-certificate-secret --key tls.key --cert tls.crt --dry-run -o yaml | kubectl apply -f -
```