k8sではkubectl run
やkubectl create
で各リソースのyamlを生成できる。
例えばPodのyamlを生成するならこう。
kubectl run <pod名> --image=<image名> --dry-run=client -o yaml > pod.yaml
Deploymentのyamlを生成するならこう。
kubectl create deployment <pod名> --image=<image名> --replicas=<レプリカ数> --dry-run=client -o yaml > deployment.yaml
その他、作成できるリソースは kubectl create --help
で確認できる。
$ kubectl create --help Create a resource from a file or from stdin. JSON and YAML formats are accepted. ... Available Commands: clusterrole Create a ClusterRole. clusterrolebinding Create a ClusterRoleBinding for a particular ClusterRole configmap Create a configmap from a local file, directory or literal value cronjob Create a cronjob 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 priorityclass with the specified name. quota Create a quota with the specified name. role Create a role with single rule. rolebinding Create a RoleBinding for a particular Role or ClusterRole secret Create a secret using specified subcommand service Create a service using specified subcommand. serviceaccount Create a service account with the specified name ...
もしくは kubectl CLI | Kubernetes で確認できる。
この中にはdaemonset
がない。
kubectl create
でDaemonSetのリソースを作成することはできないようで、yamlの作成もできないらしい。
とはいえ、DaemonSet用のyamlを用意するにしてもイチから書くはもちょっと面倒くさい。
どうすればいいのか?
Depolymentのyamlを生成して、そのyamlを一部編集してDaemonSet用のyamlを用意することになるらしい。
実際にやってみる
こんなノードを予め用意。
DaemonSetを利用してsample-worker
/sample-worker2
/sample-worker3
に1つずつnginxのPodを配置してみる。
$ kubectl get nodes NAME STATUS ROLES AGE VERSION sample-control-plane Ready control-plane,master 25m v1.21.1 sample-worker Ready <none> 24m v1.21.1 sample-worker2 Ready <none> 24m v1.21.1 sample-worker3 Ready <none> 24m v1.21.1
kubectl createでdeploymentのyamlを作成する。
$ kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > nginx-deployment.yaml $ cat nginx-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nginx name: nginx spec: replicas: 1 selector: matchLabels: app: nginx strategy: {} template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx name: nginx resources: {} status: {}
nginx-deployment.yaml
をコピーしてnginx-daemonset.yaml
を用意。
以下のように変更する。
ポイントはkindのDeploymentをDaemonSetに置き換えることと、replicasを削除すること。
その他不要な行も削除する。
$ diff -u nginx-deployment.yaml nginx-daemonset.yaml --- nginx-deployment.yaml 2022-05-21 22:24:18.000000000 +0900 +++ nginx-daemonset.yaml 2022-05-21 22:25:30.000000000 +0900 @@ -1,24 +1,18 @@ apiVersion: apps/v1 -kind: Deployment +kind: DaemonSet metadata: - creationTimestamp: null labels: app: nginx name: nginx spec: - replicas: 1 selector: matchLabels: app: nginx - strategy: {} template: metadata: - creationTimestamp: null labels: app: nginx spec: containers: - image: nginx name: nginx - resources: {} -status: {}
これでapplyできる。
$ kubectl apply -f nginx-daemonset.yaml daemonset.apps/nginx created $ kubectl get daemonset NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE nginx 3 3 3 3 3 <none> 33s $ kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-bs4nc 1/1 Running 0 39s 10.244.3.2 sample-worker2 <none> <none> nginx-j5h7m 1/1 Running 0 39s 10.244.2.2 sample-worker3 <none> <none> nginx-zlfq5 1/1 Running 0 39s 10.244.1.2 sample-worker <none> <none>