LoginSignup
101
102

More than 3 years have passed since last update.

Kubernetes Tips: kubectl でマニフェストの雛形を作る

Last updated at Posted at 2018-06-21

kubectl run のドライラン(--dry-run)と yaml 表示(-o yaml)を組み合わせると生成されたマニフェストを標準出力に表示することができます。シェルの履歴等を使えば簡単に実行できるので、Kubernetes で yaml のマニフェストを素早く作成したい場合に便利です。

$ kubectl run mydeploy --image nginx -o yaml --dry-run
# 若干ゴミ(creationTimestamp など)が残るで削除する
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: mydeploy
  name: mydeploy
spec:
  replicas: 1
  selector:
    matchLabels:
      run: mydeploy
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: mydeploy
    spec:
      containers:
      - image: nginx
        name: mydeploy
        resources: {}
status: {}

なお各リソースの細かい設定値を調べたいときは kubectl explain が便利です。

$ kubectl explain deployment.spec.template.spec
KIND:     Deployment
VERSION:  extensions/v1beta1

RESOURCE: spec <Object>

DESCRIPTION:
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

     PodSpec is a description of a pod.

FIELDS:
   activeDeadlineSeconds        <integer>
     Optional duration in seconds the pod may be active on the node relative to
     StartTime before the system will actively try to mark it failed and kill
     associated containers. Value must be a positive integer.
...

各リソースの雛形用コマンド

Deployment

kubectl run mydeploy --image nginx -o yaml --dry-run

Pod

# --restart=Never を付けると Pod になる
kubectl run mypod --restart=Never --image nginx -o yaml --dry-run

Job

# --restart=OnFailure を付けると Job になる
kubectl run myjob --restart=OnFailure --image ubuntu -o yaml --dry-run -- echo hello

CronJob

# --schedule を付けると CronJob になる
kubectl run mycron --schedule "1 * * * *" --image nginx -o yaml --dry-run  

Service

kubectl create svc clusterip myapp --tcp 80 -o yaml --dry-run

ConfigMap

kubectl create cm mycm --from-literal mykey=myval -o yaml --dry-run

# --from-file でファイルを指定した場合ちゃんとインデントしてくれる
kubectl create cm mycm --from-file myfile.yaml -o yaml --dry-run

Secret

# 値は base64 エンコードされているので編集に注意
kubectl create secret generic mysecret --from-literal mykey=myval -o yaml --dry-run

ServiceAccount

kubectl create serviceaccount mysc -o yaml --dry-run

ClusterRoleBinding

kubectl create clusterrolebinding myclusterrolebinding --clusterrole=edit --serviceaccount default:mysc -o yaml --dry-run

RoleBinding

kubectl create rolebinding cluster-admin-binding --role=edit --serviceaccount default:mysc -o yaml --dry-run

PodDisruptionBudget

kubectl create pdb my-pdb --selector=app=nginx --min-available=1 -o yaml --dry-run
101
102
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
101
102