DaemonSet,简称ds.要在所有集群节点上运行一个pod ,需要创建一个DaemonSet 对象,这很像一个ReplicationController 或ReplicaSet ,除了由DaemonSet 创建的pod ,已经有一个指定的目标节点并跳过Kubernetes 调度程序。它们不是随机分布在集群上的。
DaemonSet 确保创建足够的pod ,并在自己的节点上部署每个pod 。尽管ReplicaSet或ReplicationController确保集群中存在期望数量的pod副本,但DaemonSet 并没有期望的副本数的概念。它不需要,因为它的工作是确保一个pod 匹配它的选择器并在每个节点上运行。如果节点下线,DaemonSet 不会在其他地方重新创建pod 。但是, 当将一个新节点添加到集群中时, DaemonSet 会立刻部署一个新的pod 实例。
创建DS
[root@master01 ~]# kubectl explain DaemonSetKIND: DaemonSetVERSION: apps/v1DESCRIPTION:DaemonSet represents the configuration of a daemon set.FIELDS:apiVersion <string>APIVersion defines the versioned schema of this representation of anobject. Servers should convert recognized schemas to the latest internalvalue, and may reject unrecognized values. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resourceskind <string>Kind is a string value representing the REST resource this objectrepresents. Servers may infer this from the endpoint the client submitsrequests to. Cannot be updated. In CamelCase. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kindsmetadata <Object>Standard object's metadata. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadataspec <Object>The desired behavior of this daemon set. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-statusstatus <Object>The current status of this daemon set. This data may be out of date by somewindow of time. Populated by the system. Read-only. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
cat > myapp-ds.yaml <<EOFapiVersion: apps/v1kind: DaemonSetmetadata:name: myapp-dslabels:app: myappspec:selector:matchLabels:app: myapptemplate:metadata:name: myapp-podlabels:app: myappspec:containers:- name: myappimage: ikubernetes/myapp:v1ports:- name: httpcontainerPort: 80EOF
kubectl apply -f myapp-ds.yaml
[root@master01 ~]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESmyapp-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 wideNAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE CONTAINERS IMAGES SELECTORmyapp-ds 3 3 3 3 3 <none> 84s myapp ikubernetes/myapp:v1 app=myapp
查看DS具体信息
[root@master01 ~]# kubectl describe daemonsets.apps myapp-dsName: myapp-dsSelector: app=myappNode-Selector: <none>Labels: app=myappAnnotations: deprecated.daemonset.template.generation: 1kubectl.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: 3Current Number of Nodes Scheduled: 3Number of Nodes Scheduled with Up-to-date Pods: 3Number of Nodes Scheduled with Available Pods: 3Number of Nodes Misscheduled: 0Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 FailedPod Template:Labels: app=myappContainers:myapp:Image: ikubernetes/myapp:v1Port: 80/TCPHost Port: 0/TCPEnvironment: <none>Mounts: <none>Volumes: <none>Events:Type Reason Age From Message---- ------ ---- ---- -------Normal SuccessfulCreate 2m16s daemonset-controller Created pod: myapp-ds-zxcxqNormal SuccessfulCreate 2m16s daemonset-controller Created pod: myapp-ds-98t2bNormal SuccessfulCreate 2m16s daemonset-controller Created pod: myapp-ds-4vzxc
查看labels
[root@master01 ~]# kubectl get pods --show-labelsNAME READY STATUS RESTARTS AGE LABELSmyapp-ds-4vzxc 1/1 Running 0 26m app=myapp,controller-revision-hash=6ccfbbfc49,pod-template-generation=1myapp-ds-98t2b 1/1 Running 0 26m app=myapp,controller-revision-hash=6ccfbbfc49,pod-template-generation=1myapp-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
