DaemonSet,简称ds.要在所有集群节点上运行一个pod ,需要创建一个DaemonSet 对象,这很像一个ReplicationController 或ReplicaSet ,除了由DaemonSet 创建的pod ,已经有一个指定的目标节点并跳过Kubernetes 调度程序。它们不是随机分布在集群上的。

DaemonSet 确保创建足够的pod ,并在自己的节点上部署每个pod 。尽管ReplicaSet或ReplicationController确保集群中存在期望数量的pod副本,但DaemonSet 并没有期望的副本数的概念。它不需要,因为它的工作是确保一个pod 匹配它的选择器并在每个节点上运行。如果节点下线,DaemonSet 不会在其他地方重新创建pod 。但是, 当将一个新节点添加到集群中时, DaemonSet 会立刻部署一个新的pod 实例。

创建DS

  1. [root@master01 ~]# kubectl explain DaemonSet
  2. KIND: DaemonSet
  3. VERSION: apps/v1
  4. DESCRIPTION:
  5. DaemonSet represents the configuration of a daemon set.
  6. FIELDS:
  7. apiVersion <string>
  8. APIVersion defines the versioned schema of this representation of an
  9. object. Servers should convert recognized schemas to the latest internal
  10. value, and may reject unrecognized values. More info:
  11. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
  12. kind <string>
  13. Kind is a string value representing the REST resource this object
  14. represents. Servers may infer this from the endpoint the client submits
  15. requests to. Cannot be updated. In CamelCase. More info:
  16. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
  17. metadata <Object>
  18. Standard object's metadata. More info:
  19. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
  20. spec <Object>
  21. The desired behavior of this daemon set. More info:
  22. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
  23. status <Object>
  24. The current status of this daemon set. This data may be out of date by some
  25. window of time. Populated by the system. Read-only. More info:
  26. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
cat > myapp-ds.yaml <<EOF
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: myapp-ds
  labels:
    app: myapp
spec:
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        ports:
        - name: http
          containerPort: 80
EOF
kubectl apply -f myapp-ds.yaml
[root@master01 ~]# kubectl get pods -o wide
NAME             READY   STATUS    RESTARTS   AGE   IP               NODE     NOMINATED NODE   READINESS GATES
myapp-ds-4vzxc   1/1     Running   0          36s   10.244.140.94    node02   <none>           <none>
myapp-ds-98t2b   1/1     Running   0          36s   10.244.186.231   node03   <none>           <none>
myapp-ds-zxcxq   1/1     Running   0          36s   10.244.196.165   node01   <none>           <none>

查看DS

[root@master01 ~]# kubectl get ds -o wide
NAME       DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE   CONTAINERS   IMAGES                 SELECTOR
myapp-ds   3         3         3       3            3           <none>          84s   myapp        ikubernetes/myapp:v1   app=myapp

查看DS具体信息

[root@master01 ~]# kubectl describe daemonsets.apps myapp-ds
Name:           myapp-ds
Selector:       app=myapp
Node-Selector:  <none>
Labels:         app=myapp
Annotations:    deprecated.daemonset.template.generation: 1
                kubectl.kubernetes.io/last-applied-configuration:
                  {"apiVersion":"apps/v1","kind":"DaemonSet","metadata":{"annotations":{},"labels":{"app":"myapp"},"name":"myapp-ds","namespace":"default"},...
Desired Number of Nodes Scheduled: 3
Current Number of Nodes Scheduled: 3
Number of Nodes Scheduled with Up-to-date Pods: 3
Number of Nodes Scheduled with Available Pods: 3
Number of Nodes Misscheduled: 0
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=myapp
  Containers:
   myapp:
    Image:        ikubernetes/myapp:v1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                  Message
  ----    ------            ----   ----                  -------
  Normal  SuccessfulCreate  2m16s  daemonset-controller  Created pod: myapp-ds-zxcxq
  Normal  SuccessfulCreate  2m16s  daemonset-controller  Created pod: myapp-ds-98t2b
  Normal  SuccessfulCreate  2m16s  daemonset-controller  Created pod: myapp-ds-4vzxc

查看labels

[root@master01 ~]# kubectl get pods --show-labels 
NAME             READY   STATUS    RESTARTS   AGE   LABELS
myapp-ds-4vzxc   1/1     Running   0          26m   app=myapp,controller-revision-hash=6ccfbbfc49,pod-template-generation=1
myapp-ds-98t2b   1/1     Running   0          26m   app=myapp,controller-revision-hash=6ccfbbfc49,pod-template-generation=1
myapp-ds-zxcxq   1/1     Running   0          26m   app=myapp,controller-revision-hash=6ccfbbfc49,pod-template-generation=1

修改labels

可以看到labels被修改后,会立即创建新的pod

[root@master01 ~]# kubectl label pod myapp-ds-4vzxc app=app --overwrite 
pod/myapp-ds-4vzxc labeled

[root@master01 ~]# kubectl get pods --show-labels  -o wide
NAME             READY   STATUS    RESTARTS   AGE   IP               NODE     NOMINATED NODE   READINESS GATES   LABELS
myapp-ds-4vzxc   1/1     Running   0          31m   10.244.140.94    node02   <none>           <none>            app=app,controller-revision-hash=6ccfbbfc49,pod-template-generation=1
myapp-ds-98t2b   1/1     Running   0          31m   10.244.186.231   node03   <none>           <none>            app=myapp,controller-revision-hash=6ccfbbfc49,pod-template-generation=1
myapp-ds-wqmdx   1/1     Running   0          7s    10.244.140.95    node02   <none>           <none>            app=myapp,controller-revision-hash=6ccfbbfc49,pod-template-generation=1
myapp-ds-zxcxq   1/1     Running   0          31m   10.244.196.165   node01   <none>           <none>            app=myapp,controller-revision-hash=6ccfbbfc49,pod-template-generation=1

删除DS

kubectl delete ds myapp-ds