Crea un daemonset in Kubernetes
Su questa pagina
- Prerequisiti
- Cosa faremo?
- Crea un Daemonset
- Conclusione
Un DaemonSet assicura che tutti i nodi nel cluster Kubernetes eseguano una copia di un pod. Ogni nodo avrà una copia del pod. Quando i nodi vengono aggiunti al cluster, vengono aggiunti i pod e quando i nodi vengono rimossi dal cluster, i pod vengono rimossi. Se eliminiamo un DaemonSet pulirà i pod che ha creato.
Un DaemonSet assicura che tutti i nodi eseguano una copia di un pod. Normalmente, il nodo su cui viene eseguito un pod viene selezionato dallo scheduler, ma i pod DaemonSet vengono creati e pianificati dal controller DaemonSet.
Daemonset può essere utilizzato:
- Per eseguire l'archiviazione cluster su ciascun nodo, ad esempio: glusterd, ceph
- Per eseguire la raccolta dei log su ciascun nodo, ad esempio: fluentd, logstash
- Per eseguire il monitoraggio dei nodi in qualsiasi momento, ad esempio: Prometheus Node Exporter, collectd, Datadog agent
Per saperne di più su Daemonset, visita kubernetes.io la documentazione ufficiale di Kubernetes.
In questo articolo, creeremo un Daemonset di \fluentd_elasticsearch\. Questo creerà pod di \fluentd_elasticsearch\ su ciascun nodo del cluster. Il nostro file di definizione Daemonset avrà Tolleranza per Taint dei nodi master per consentire al pod di essere pianificato anche sul nodo master.
Prerequisiti
- Cluster Kubernetes con almeno 1 nodo di lavoro.
Se vuoi imparare a creare un cluster Kubernetes, fai clic qui. Questa guida ti aiuterà a creare un cluster Kubernetes con 1 master e 2 nodi su istanze AWS Ubuntu EC2.
Che cosa faremo?
- Crea un Daemonset
Crea un demone
Controlla se esiste un daemonset nello spazio dei nomi predefinito e in tutti gli spazi dei nomi.
kubectl get daemonsets #Get daemonsets from the default namespace
kubectl get daemonsets --all-namespaces #Get daemonsets from all namespace using --all-namespace option
Nello screenshot qui sopra, puoi vedere che ci sono alcuni Daemonset disponibili. Tutti questi Daemonset sono per i componenti Cluster.
Ora prendi i pod che appartengono allo spazio dei nomi \kube-system\.
kubectl get pods -n kube-system #Get pods from the "kube-system" namespace
Tutti questi pod visti nello screenshot sopra appartengono al Daemonset dei componenti del cluster.
Ottieni un elenco di pod proxy.
kubectl get pods -n kube-system | grep proxy #Get pods from the "kube-system" namespace and grep for proxy
Controlla cosa controlla i pod proxy.
kubectl describe pod kube-proxy-s5vzp -n kube-system #Describe the pod from the "kube-system" namespace
Ottieni i dettagli del daemonset che controlla i pod proxy.
kubectl describe daemonset kube-proxy -n kube-system #Describe the daemonset from the "kube-system" namespace
Crea un file con la seguente definizione di daemonset al suo interno.
vim my-daemonset.yml
apiVersion: apps/v1 kind: DaemonSet metadata: name: my-fluentd-elasticsearch-daemonset namespace: kube-system labels: k8s-app: fluentd-logging spec: selector: matchLabels: name: fluentd-elasticsearch template: metadata: labels: name: fluentd-elasticsearch spec: tolerations: - key: node-role.kubernetes.io/master effect: NoSchedule containers: - name: fluentd-elasticsearch image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2 resources: limits: memory: 200Mi requests: cpu: 100m memory: 200Mi volumeMounts: - name: varlog mountPath: /var/log - name: varlibdockercontainers mountPath: /var/lib/docker/containers readOnly: true terminationGracePeriodSeconds: 30 volumes: - name: varlog hostPath: path: /var/log - name: varlibdockercontainers hostPath: path: /var/lib/docker/containers
Nella definizione di cui sopra, abbiamo una tolleranza alla contaminazione del nodo principale. Ciò consentirà al pod di essere posizionato anche sul nodo principale.
Crea un daemonset utilizzando il file di definizione creato nel passaggio precedente.
kubectl create -f my-daemonset.yml #Create a daemonset
kubectl get daemonset -n kube-system #Get daemonset from the "kube-system" namespace
Questo insieme di demoni è stato creato nello spazio dei nomi \kube-system\.
Descrivi il daemonset che abbiamo appena creato nello spazio dei nomi \kube-system\.
kubectl describe daemonset my-fluentd-elasticsearch-daemonset -n kube-system #Describe the daemonset from the "kube-system" namespace
Nello screenshot sopra, si può vedere che i pod sono stati distribuiti su 2 nodi.
Ora possiamo ottenere i dettagli dei pod distribuiti come daemonset su 2 nodi.
kubectl get pods -n kube-system | grep my-fluentd-elasticsearch-daemonset #Get pods from the "kube-system" namespace and grep
kubectl describe pod my-fluentd-elasticsearch-daemonset-4t9vs -n kube-system | grep Node #Describe the pods from the "kube-system" namespace and grep
kubectl describe pod my-fluentd-elasticsearch-daemonset-kxfjj -n kube-system | grep Node #Describe the pod from the "kube-system" namespace and grep
Nello screenshot sopra, puoi vedere che i pod sono stati distribuiti sul nodo di lavoro \node01\ e sul nodo master \master\. Il motivo per cui il pod è stato pianificato sul nodo master è Toleration to the Taint of the master node.
Conclusione
In questo articolo, abbiamo visto i passaggi per creare un set di daemon e abbiamo visto come i pod nel set di daemon vengono distribuiti su ogni nodo nel cluster Kubernetes.