Create Daemonset using kubectl?

9/4/2018

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
daemonset
kubectl
kubernetes

6 Answers

11/25/2019

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
Source: StackOverflow

9/4/2018

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
Source: StackOverflow

10/15/2018

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
Source: StackOverflow

5/12/2019

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
Source: StackOverflow

10/17/2018

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
Source: StackOverflow

12/14/2019

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
Source: StackOverflow