Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
96
Help us understand the problem. What is going on with this article?
@tkusumi

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

More than 1 year has passed since last update.

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
96
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
zlab
技術で新しい世界へシフトする。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
96
Help us understand the problem. What is going on with this article?