Adilet Maratov
Adilet Maratov

Reputation: 1382

Create Daemonset using kubectl?

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?

Upvotes: 14

Views: 17124

Answers (9)

araza
araza

Reputation: 61

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 an 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

Upvotes: 6

Aditya Bhuyan
Aditya Bhuyan

Reputation: 448

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.

Upvotes: 0

Deepak Mourya
Deepak Mourya

Reputation: 430

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

Upvotes: 0

Shambu
Shambu

Reputation: 2832

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 }'

Upvotes: -1

Lukasz Dynowski
Lukasz Dynowski

Reputation: 13570

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

Upvotes: 1

zaman sakib
zaman sakib

Reputation: 957

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.

Upvotes: 21

Adilet Maratov
Adilet Maratov

Reputation: 1382

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

Upvotes: 4

Praveen Sripati
Praveen Sripati

Reputation: 33495

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

Upvotes: 10

Artem Golenyaev
Artem Golenyaev

Reputation: 2747

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

Upvotes: 1

Related Questions