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
  1. cat > myapp-ds.yaml <<EOF
  2. apiVersion: apps/v1
  3. kind: DaemonSet
  4. metadata:
  5. name: myapp-ds
  6. labels:
  7. app: myapp
  8. spec:
  9. selector:
  10. matchLabels:
  11. app: myapp
  12. template:
  13. metadata:
  14. name: myapp-pod
  15. labels:
  16. app: myapp
  17. spec:
  18. containers:
  19. - name: myapp
  20. image: ikubernetes/myapp:v1
  21. ports:
  22. - name: http
  23. containerPort: 80
  24. EOF
  1. kubectl apply -f myapp-ds.yaml
  1. [root@master01 ~]# kubectl get pods -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. myapp-ds-4vzxc 1/1 Running 0 36s 10.244.140.94 node02 <none> <none>
  4. myapp-ds-98t2b 1/1 Running 0 36s 10.244.186.231 node03 <none> <none>
  5. myapp-ds-zxcxq 1/1 Running 0 36s 10.244.196.165 node01 <none> <none>

查看DS

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

查看DS具体信息

  1. [root@master01 ~]# kubectl describe daemonsets.apps myapp-ds
  2. Name: myapp-ds
  3. Selector: app=myapp
  4. Node-Selector: <none>
  5. Labels: app=myapp
  6. Annotations: deprecated.daemonset.template.generation: 1
  7. kubectl.kubernetes.io/last-applied-configuration:
  8. {"apiVersion":"apps/v1","kind":"DaemonSet","metadata":{"annotations":{},"labels":{"app":"myapp"},"name":"myapp-ds","namespace":"default"},...
  9. Desired Number of Nodes Scheduled: 3
  10. Current Number of Nodes Scheduled: 3
  11. Number of Nodes Scheduled with Up-to-date Pods: 3
  12. Number of Nodes Scheduled with Available Pods: 3
  13. Number of Nodes Misscheduled: 0
  14. Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
  15. Pod Template:
  16. Labels: app=myapp
  17. Containers:
  18. myapp:
  19. Image: ikubernetes/myapp:v1
  20. Port: 80/TCP
  21. Host Port: 0/TCP
  22. Environment: <none>
  23. Mounts: <none>
  24. Volumes: <none>
  25. Events:
  26. Type Reason Age From Message
  27. ---- ------ ---- ---- -------
  28. Normal SuccessfulCreate 2m16s daemonset-controller Created pod: myapp-ds-zxcxq
  29. Normal SuccessfulCreate 2m16s daemonset-controller Created pod: myapp-ds-98t2b
  30. Normal SuccessfulCreate 2m16s daemonset-controller Created pod: myapp-ds-4vzxc

查看labels

  1. [root@master01 ~]# kubectl get pods --show-labels
  2. NAME READY STATUS RESTARTS AGE LABELS
  3. myapp-ds-4vzxc 1/1 Running 0 26m app=myapp,controller-revision-hash=6ccfbbfc49,pod-template-generation=1
  4. myapp-ds-98t2b 1/1 Running 0 26m app=myapp,controller-revision-hash=6ccfbbfc49,pod-template-generation=1
  5. 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