12

I took the CKA exam and I needed to work with Daemonsets for quite a while there. Since it is much faster to do everything with kubectl instead of creating yaml manifests for k8s resources, I was wondering if it is possible to create Daemonset resources using kubectl.

I know that it is NOT possible to create it using regular kubectl create daemonset at least for now. And there is no description of it in the documentation. But maybe there is a way to do that in some different way?

The best thing I could do right now is to create Deployment first like kubectl create deployment and edit it's output manifest. Any options here?

Adilet Maratov
  • 1,312
  • 2
  • 14
  • 24

9 Answers9

17

The fastest hack is to create a deployment file using

kubectl create deploy nginx --image=nginx --dry-run -o yaml > nginx-ds.yaml

Now replace the line kind: Deployment with kind: DaemonSet in nginx-ds.yaml and remove the line replicas: 1

However, the following command will give a clean daemonset manifest considering that "apps/v1" is the api used for DaemonSet in your cluster

kubectl create deploy nginx --image=nginx --dry-run -o yaml | \
    sed '/null\|{}\|replicas/d;/status/,$d;s/Deployment/DaemonSet/g' > nginx-ds.yaml

You have your nginx DaemonSet.

zaman sakib
  • 851
  • 11
  • 18
  • 1
    It also comes with different apiversion and some unwanted fields. change api version to extensions/v1beta1 and use "--validate=false" with the kubectl apply to suppress warnings. – Vaibhav Jain Jul 20 '19 at 10:05
  • The examples in the documents are more extensive than necessary – Joost Döbken Oct 28 '19 at 07:42
  • 1
    An improvement - you also need to remove **strategy: {}** property from generated deployment's yaml – jack_t Aug 05 '20 at 21:01
9

CKA allows access to K8S documentation. So, it should be possible to get a sample YAML for different resources from there. Here is the one for the Daemonset from K8S documentation.

Also, not sure if the certification environment has access to resources in the kube-system namespace. If yes, then use the below command to get a sample yaml for Daemonset.

kubectl get daemonsets kube-flannel-ds-amd64 -o yaml -n=kube-system > daemonset.yaml

Praveen Sripati
  • 32,799
  • 16
  • 80
  • 117
  • this command may produce a YAML which is a bit cumbersome as it will have way too many things in it from the current daemonset and you will have to make sure you check every attribute to not cause any kind of undesired overlap. I think @zamansakib 's way ( https://stackoverflow.com/a/56097744/636762 ) of creating a dry-run deployment yaml and then just changing the kind gives a cleaner new yaml – Rakib Jun 21 '20 at 07:00
5

The fastest way to create

kubectl create deploy nginx --image=nginx --dry-run -o yaml > nginx-ds.yaml

Now replace the line kind: Deployment with kind: DaemonSet in nginx-ds.yaml and remove the line replicas: 1 , strategy {} and status {} as well. Otherwise it shows error for some required fields like this

error: error validating "nginx-ds.yaml": error validating data: [ValidationError(DaemonSet.spec): unknown field "strategy" in io.k8s.api.apps.v1.DaemonSetSpec, ValidationError(DaemonSet.status): missing required field "currentNumberScheduled" in io.k8s.api.apps.v1.DaemonSetStatus,ValidationError(DaemonSet.status): missing required field "numberMisscheduled" in io.k8s.api.apps.v1.DaemonSetStatus, ValidationError(DaemonSet.status): missing required field "desiredNumberScheduled" in io.k8s.api.apps.v1.DaemonSetStatus, ValidationError(DaemonSet.status): missing required field "numberReady" in io.k8s.api.apps.v1.DaemonSetStatus]; if you choose to ignore these errors, turn validation off with --validate=false
araza
  • 51
  • 1
  • 1
4

It's impossible. At least for Kubernetes 1.12. The only option is to get a sample Daemonset yaml file and go from there.

Adilet Maratov
  • 1,312
  • 2
  • 14
  • 24
0

There is no such option to create a DaemonSet using kubectl. But still, you can prepare a Yaml file with basic configuration for a DaemonSet, e.g. daemon-set-basic.yaml, and create it using kubectl create -f daemon-set-basic.yaml

You can edit new DaemonSet using kubectl edit daemonset <name-of-the-daemon-set>. Or modify the Yaml file and apply changes by kubectl apply -f daemon-set-basic.yaml. Note, if you want to update configuration modifying file and using apply command, it is better to use apply instead of create when you create the DaemonSet.

Here is the example of a simple DaemonSet:

kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
  labels:
    k8s-app: fluentd-logging
spec:
  selector:
    matchLabels:
      name: fluentd-elasticsearch
  template:
    metadata:
      labels:
        name: fluentd-elasticsearch
    spec:
      containers:
      - name: fluentd-elasticsearch
        image: k8s.gcr.io/fluentd-elasticsearch:1.20
Artem Golenyaev
  • 2,568
  • 12
  • 20
  • thanks for the answer. Yes, it is easy to create a daemonset using yaml, this is what I do most of the time. The problem with that is that I should create that file manually, which I'm too lazy to do :) . Seems like I should create a PR to kubernetes repo and try to implement that. Thanks for the answer – Adilet Maratov Sep 06 '18 at 16:48
0

You could take advantage of Kubernetes architecture to obtain definition of DaemonSet from existing cluster. Have a look at kube-proxy, which is a network component that runs on each node in your cluster. enter image description here kube-proxy is deployed as DaemonSet so you can extract its definition with below command.

$ kubectl get ds kube-proxy -n kube-system -o yaml > kube-proxy.ds.yaml

Warning! By extracting definition of DaemonSet from kube-proxy be aware that:

  1. You will have to do pliantly of clean up!
  2. You will have to change apiVersion from extensions/v1beta1 to apps/v1
Lukasz Dynowski
  • 11,169
  • 9
  • 81
  • 124
0

I used this by the following commands:

  1. Either create Replicaset or deployment from Kubernetes imperative command

kubectl create deployment <daemonset_name> --image= --dry-run -o yaml > file.txt

  1. Edit the kind and replace DaemonSet, remove replicas and strategy fields into it.

  2. kubectl apply -f file.txt

Deepak Mourya
  • 340
  • 2
  • 10
0

During CKA examination you are allowed to access Kubernetes Documentation for DaemonSets. You could use the link and get examples of DaemonSet yaml files. However you could use the way you mentioned, change a deployment specification to DaemonSet specification. You need to change the kind to Daemonset, remove strategy, replicas and status fields. That would do.

Aditya Bhuyan
  • 328
  • 6
  • 10
-1

Using command to deployment create and modifying it, one can create daemonset very quickly. Below is one line command to create daemonset

kubectl create deployment elasticsearch --namespace=kube-system  --image=k8s.gcr.io/fluentd-elasticsearch:1.20 --dry-run -o yaml | grep -v "creationTimestamp\|status" | awk '{gsub(/Deployment/, "DaemonSet"); print }'
Shambu
  • 2,612
  • 1
  • 21
  • 16