DaemonSet典型应用

Deployment部署的副本Pod会分布在各个Node上,每个Node都可能运行好几个副本。DaemonSet的不同之处在于:每个Node上最多只能运行一个副本。

DaemonSet的典型应用场景有:

·在集群的每个节点上运行存储Daemon,比如glusterd或ceph。
·在每个节点上运行日志收集Daemon,比如flunentd或logstash。
·在每个节点上运行监控Daemon,比如Prometheus Node ExPorter或collectd。

其实Kubernetes自己就在用DaemonSet运行系统组件。

主机查看系统的daemonset组件:

[root@master ~]# kubectl get daemonsets.apps  -n  kube-system
NAME                      DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR                 AGE
kube-flannel-ds-amd64     3         3         3       3            3           <none>                        7d1h
kube-flannel-ds-arm       0         0         0       0            0           <none>                        7d1h
kube-flannel-ds-arm64     0         0         0       0            0           <none>                        7d1h
kube-flannel-ds-ppc64le   0         0         0       0            0           <none>                        7d1h
kube-flannel-ds-s390x     0         0         0       0            0           <none>                        7d1h
kube-proxy                3         3         3       3            3           beta.kubernetes.io/os=linux   7d1h

DaemonSet kube-flannel-ds和kube-proxy分别负责在每个节点上运行flannel和kube-proxy组件。

master主机编写YAML配置文件,然后通过文件创建DaemonSet,最后查看DaemonSet的状态:

案例:

[root@master ~]# cat busybox.yml 
apiVersion: apps/v1
kind: DaemonSet
metadata:
    name: busybox-daemonset
spec:
   selector:
     matchLabels:
        app: busybox
   template:
     metadata:
        labels:
         app: busybox
     spec:
       containers:
       - name : test
         image: busybox
         command:
          - sh
          - -c
          - while true;do echo "test";sleep 10;done;
kubectl apply -f busybox.yml

查看:

[root@master ~]# kubectl get daemonsets.apps
NAME                DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
busybox-daemonset   2         2         2       2            2           <none>          48m
[root@master ~]# kubectl get pod -o wide
NAME                      READY   STATUS    RESTARTS   AGE   IP            NODE    NOMINATED NODE   READINESS GATES
busybox-daemonset-lcz4g   1/1     Running   0          49m   10.244.1.15   node1   <none>           <none>
busybox-daemonset-mz82h   1/1     Running   0          49m   10.244.2.20   node2   <none>           <none>
[root@master ~]# kubectl logs -f busybox-daemonset-lcz4g

 

 

 

案例:部署exporter

[root@master caoyi]# cat node-exporter.yml 
apiVersion: apps/v1
kind: DaemonSet
metadata:
    name: exporter-daemonset
spec:
   selector:
     matchLabels:
        app: node-exporter
   template:
     metadata:
        labels:
         app: node-exporter
     spec:
        hostNatwork: true
     spec:
       containers:
       - name : node-exporter
         image: prom/node-exporter
         command:
          - /bin/node_exporter
          - --path.procfs
          - /host/proc
          - --path.sysfs
          - /host/sys
          - --collector.filesystem.ignored-mount-points
          - ^/(sys|proc|dev|host|etc|rootfs/var/lib/docker/containers|rootfs/var/lib/docker/overlay2|rootfs/run/docker/netns|rootfs/var/lib/docker/devicemapper|rootfs/var/lib/docker/aufs)($$|/)
         volumeMounts:
          - name: proc
            mountPath: /host/proc
          - name: sys
            mountPath: /host/sys
          - name: root
            mountPath: /rootfs
       volumes:
        - name: proc
          hostPath:
                path: /proc
        - name: sys
          hostPath:
               path: /sys
        - name: root
          hostPath:
               path: /
[root@master caoyi]# kubectl apply -f node-exporter.yml 

查看:

[root@master caoyi]# kubectl get pod -o wide
NAME                       READY   STATUS    RESTARTS   AGE    IP            NODE    NOMINATED NODE   READINESS GATES
busybox-daemonset-lcz4g    1/1     Running   0          55m    10.244.1.15   node1   <none>           <none>
busybox-daemonset-mz82h    1/1     Running   0          55m    10.244.2.20   node2   <none>           <none>
exporter-daemonset-7qw72   1/1     Running   0          118s   10.244.1.16   node1   <none>           <none>
exporter-daemonset-pvzjv   1/1     Running   0          118s   10.244.2.21   node2   <none>           <none>

job

 容器按照持续运行的时间分为两类:服务类容器和工作类容器

 服务类容器通常持续提供了服务,需要一直运行,比如http server,daemon等。工作类容器则是一次性任务,比如批处理程序,完成后容器就退出了。

kubernetes的deployment、replicaSet 和 DaemonSet都用于管理服务类容器:对于工作类容器,我们用job.

案例:容器不重启

 模拟job正确的

[root@master caoyi]# cat myjob.yml 
apiVersion: batch/v1
kind: Job    ##指明当前资源的类型为Job
metadata:
   name: myjob
spec:
  template:
    metadata:
      name: myjob
    spec:
      containers:
      - name: hello
        image: busybox
        command: ["echo","hello"]
      restartPolicy: Never   ##restartPolicy 指定什么情况下需要重启容器,对于job,只能设置为never或者ONFailure,对于其他的controller(比如deployment)可以设置为Always
[root@master caoyi]# kubectl get pod -o wide
myjob-6ll52                0/1     Completed   0          23m   10.244.1.17   node1   <none>           <none>

模拟失败的job

在command 处修改一个错误的命令,如果输出一个错误的不存在的命令,启动的时候起不起来,隔一段时间会重新创建一个,直到启动了才会停止

当第一个pod启动时,容器失败退出,由于设置restartpolicy:Never,此容器不会被重启,但job desired的pod是目前successful为0,达不到要求。所以会启动新的pod知道successful为1。为了终止这个行为,只能删除job。

案例:容器重启

[root@master caoyi]# cat myjob.yml 
apiVersion: batch/v1
kind: Job
metadata:
   name: myjob
spec:
  template:
    metadata:
      name: myjob
    spec:
      containers:
      - name: hello
        image: busybox
        command: ["xxxxx","hello"]
      restartPolicy: OnFailure    ##只有在运行成功才会停止进行重启,只要有错误,它会一直进行重启

查看

[root@master caoyi]# kubectl get pod -o wide
NAME                       READY   STATUS              RESTARTS   AGE    IP            NODE    NOMINATED NODE   READINESS GATES
busybox-daemonset-lcz4g    1/1     Running             0          109m   10.244.1.15   node1   <none>           <none>
busybox-daemonset-mz82h    1/1     Running             0          109m   10.244.2.20   node2   <none>           <none>
exporter-daemonset-7qw72   1/1     Running             0          56m    10.244.1.16   node1   <none>           <none>
exporter-daemonset-pvzjv   1/1     Running             0          56m    10.244.2.21   node2   <none>           <none>
myjob-tk5br                0/1     RunContainerError   2          50s    10.244.1.18   node1   <none>           <none>





[root@master caoyi]# kubectl get pod -o wide
NAME                       READY   STATUS             RESTARTS   AGE     IP            NODE    NOMINATED NODE   READINESS GATES
busybox-daemonset-lcz4g    1/1     Running            0          111m    10.244.1.15   node1   <none>           <none>
busybox-daemonset-mz82h    1/1     Running            0          111m    10.244.2.20   node2   <none>           <none>
exporter-daemonset-7qw72   1/1     Running            0          57m     10.244.1.16   node1   <none>           <none>
exporter-daemonset-pvzjv   1/1     Running            0          57m     10.244.2.21   node2   <none>           <none>
myjob-tk5br                0/1     CrashLoopBackOff   4          2m19s   10.244.1.18   node1   <none>           <none>

同时执行多个Job

[root@master caoyi]# cat myjob.yml 
apiVersion: batch/v1
kind: Job
metadata:
   name: myjob
spec:
  completions: 6      ##每次运行两个pod,直到共运行6个pod成功完成
  parallelism: 2
  template:
    metadata:
      name: myjob
    spec:
      containers:
      - name: hello
        image: busybox
        command: ["echo","hello"]
      restartPolicy: OnFailure
[root@master caoyi]# kubectl get pod -o wide
NAME          READY   STATUS      RESTARTS   AGE    IP            NODE    NOMINATED NODE   READINESS GATES
myjob-98wf9   0/1     Completed   0          98s    10.244.2.25   node2   <none>           <none>
myjob-cvq4n   0/1     Completed   0          103s   10.244.2.22   node2   <none>           <none>
myjob-hgvkb   0/1     Completed   0          101s   10.244.2.23   node2   <none>           <none>
myjob-kvf4g   0/1     Completed   0          96s    10.244.2.27   node2   <none>           <none>
myjob-lzckc   0/1     Completed   0          103s   10.244.1.19   node1   <none>           <none>
myjob-mvv4k   0/1     Completed   0          99s    10.244.1.20   node1   <none>           <none>

定时执行job

linux中有cron程序定时执行任务,kubernetes的cronjob提供了类似的功能,可以定时执行job,

[root@master caoyi]# cat cronjob.yml 
apiVersion: batch/v1beta1
kind: CronJob
metadata:
   name: cronjob
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
   spec:
    template:
     spec:
       containers:
       - name: hello
         image: busybox
         command: ["echo","hello"]
       restartPolicy: OnFailure



[root@master caoyi]# kubectl apply -f cronjob.yml

查看

[root@master caoyi]# kubectl get pod -o wide
No resources found in default namespace.

当前是没有的

1分钟以后


[root@master caoyi]# kubectl get pod -o wide
NAME                       READY   STATUS      RESTARTS   AGE   IP            NODE    NOMINATED NODE   READINESS GATES
cronjob-1594910220-ddfvc   0/1     Completed   0          16s   10.244.1.23   node1   <none>           <none>



两分钟后



[root@master caoyi]# kubectl get pod -o wide
NAME                       READY   STATUS      RESTARTS   AGE     IP            NODE    NOMINATED NODE   READINESS GATES
cronjob-1594910280-2fqdr   0/1     Completed   0          2m44s   10.244.1.25   node1   <none>           <none>
cronjob-1594910340-msnxz   0/1     Completed   0          104s    10.244.1.27   node1   <none>           <none>