4 Kubernetes资源-Pod控制器(2)
4.3 DaemonSet
DaemonSet确保Pod在所有Node节点运行一个副本,当有Node节点添加时会自动在新的节点创建Pod。常用于每个Node上的日志收集、监控、集群存储管理等。
- 编写YAML文件,通过app: tomcat标签将DaemonSet与Pod资源关联:
[root@k8smaster43-11 CSDN]# cat daemonset.yaml
apiVersion: apps/v1 # API版本
kind: DaemonSet # 资源类型
metadata:
name: tomcat-daemons # DS资源名称
spec:
selector:
matchLabels:
app: tomcat # 关联Pod标签
template:
metadata:
name: tomcat-pod # Pod资源名称
labels:
app: tomcat # Pod资源标签
spec:
containers:
- name: demo-tomcat # Pod容器名称
image: tomcat:8.0 # Pod容器镜像
imagePullPolicy: Never # 容器下载模式
[root@k8smaster43-11 CSDN]#
- 创建DaemonSet资源:
[root@k8smaster43-11 CSDN]# kubectl create -f daemonset.yaml
daemonset.apps/tomcat-daemons created
[root@k8smaster43-11 CSDN]#
[root@k8smaster43-11 CSDN]# kubectl get ds,pod -o wide --show-labels
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE CONTAINERS IMAGES SELECTOR LABELS
daemonset.apps/tomcat-daemons 2 2 2 2 2 <none> 39s demo-tomcat tomcat:8.0 app=tomcat <none>
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pod/tomcat-daemons-dqfn4 1/1 Running 0 39s 10.244.2.81 k8sworker43-22 <none> <none> app=tomcat,controller-revision-hash=7f5c98c9cc,pod-template-generation=1
pod/tomcat-daemons-tpz7z 1/1 Running 0 39s 10.244.1.98 k8sworker43-21 <none> <none> app=tomcat,controller-revision-hash=7f5c98c9cc,pod-template-generation=1
[root@k8smaster43-11 CSDN]#
- 将1台工作节点从Kubernetes集群中剔除,则DaemonSet的Pod会从该节点删除:
[root@k8smaster43-11 CSDN]# kubectl delete nodes/k8sworker43-21
node "k8sworker43-21" deleted
[root@k8smaster43-11 CSDN]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8smaster43-11 Ready master 107d v1.19.4
k8sworker43-22 Ready <none> 107d v1.19.4
[root@k8smaster43-11 CSDN]#
[root@k8smaster43-11 CSDN]# kubectl get ds,pod -o wide --show-labels
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE CONTAINERS IMAGES SELECTOR LABELS
daemonset.apps/tomcat-daemons 1 1 1 1 1 <none> 8m19s demo-tomcat tomcat:8.0 app=tomcat <none>
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pod/tomcat-daemons-dqfn4 1/1 Running 0 8m19s 10.244.2.81 k8sworker43-22 <none> <none> app=tomcat,controller-revision-hash=7f5c98c9cc,pod-template-generation=1
[root@k8smaster43-11 CSDN]#
- 将1台工作节点加入Kubernetes集群中,则DaemonSet会自动从该节点启动Pod:
# 在工作节点执行kubeadm join命令加入集群
[root@k8sworker43-21 ~]# kubeadm join 192.168.43.11:6443 --token d1ncs0.we7r79976ndaf3k4 --discovery-token-ca-cert-hash sha256:28d33e6003840de19700fbba569f32557c57841ff94516720eadc072e4b3a099
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
[root@k8sworker43-21 ~]#
#在Master节点查看DaemonSet资源情况
[root@k8smaster43-11 CSDN]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8smaster43-11 Ready master 107d v1.19.4
k8sworker43-21 Ready <none> 12s v1.19.4
k8sworker43-22 Ready <none> 107d v1.19.4
[root@k8smaster43-11 CSDN]#
[root@k8smaster43-11 CSDN]# kubectl get ds,pod -o wide --show-labels
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE CONTAINERS IMAGES SELECTOR LABELS
daemonset.apps/tomcat-daemons 2 2 2 2 2 <none> 20m demo-tomcat tomcat:8.0 app=tomcat <none>
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pod/tomcat-daemons-cvxrb 1/1 Running 0 10s 10.244.1.2 k8sworker43-21 <none> <none> app=tomcat,controller-revision-hash=7f5c98c9cc,pod-template-generation=1
pod/tomcat-daemons-dqfn4 1/1 Running 0 20m 10.244.2.81 k8sworker43-22 <none> <none> app=tomcat,controller-revision-hash=7f5c98c9cc,pod-template-generation=1
[root@k8smaster43-11 CSDN]#
- 删除DaemonSet和对应Pod的资源:
[root@k8smaster43-11 CSDN]# kubectl delete -f daemonset.yaml
daemonset.apps "tomcat-daemons" deleted
[root@k8smaster43-11 CSDN]#
4.4 Job/CronJob
Job负责批处理任务,即仅执行一次的任务,它保证批处理的一个或多个Pod成功结束。
- 编写YAML文件,设置任务的执行次数 completions为4次、同时执行的任务数量parallelism为2个,因为任务是执行完就结束所以Pod的重启策略restartPolicy:为Never:
[root@k8smaster43-11 CSDN]# cat job.yaml
apiVersion: batch/v1 # API版本
kind: Job # 资源类型
metadata:
name: perl-job # Job资源名称
spec:
completions: 4 # Job执行次数
parallelism: 2 # Job并发数量
template:
metadata:
name: perl-pod # Pod资源标签
spec:
containers:
- name: perl # Pod容器名称
image: perl # Pod容器镜像
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never # Pod重启策略
[root@k8smaster43-11 CSDN]#
- 创建Job资源并查看任务执行状态,Pod为Running表示正在执行、Completed表示执行结束:
[root@k8smaster43-11 CSDN]# kubectl create -f job.yaml
job.batch/perl-job created
[root@k8smaster43-11 CSDN]# kubectl get job,pod -o wide
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
job.batch/perl-job 0/4 19s 19s perl perl controller-uid=517a4293-107a-4c05-8480-611b63dc8de5
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/perl-job-ggspc 1/1 Running 0 19s 10.244.2.86 k8sworker43-22 <none> <none>
pod/perl-job-p796m 1/1 Running 0 19s 10.244.2.87 k8sworker43-22 <none> <none>
# 有2个任务同时执行,任务执行结束Pod状态变为Completed
[root@k8smaster43-11 CSDN]# kubectl get job,pod -o wide
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
job.batch/perl-job 2/4 30s 30s perl perl controller-uid=517a4293-107a-4c05-8480-611b63dc8de5
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/perl-job-ggspc 0/1 Completed 0 30s 10.244.2.86 k8sworker43-22 <none> <none>
pod/perl-job-gkrsf 1/1 Running 0 2s 10.244.2.89 k8sworker43-22 <none> <none>
pod/perl-job-p796m 0/1 Completed 0 30s 10.244.2.87 k8sworker43-22 <none> <none>
pod/perl-job-slr82 1/1 Running 0 4s 10.244.2.88 k8sworker43-22 <none> <none>
- 删除Job和对应Pod的资源:
[root@k8smaster43-11 CSDN]# kubectl delete -f job.yaml
job.batch "perl-job" deleted
[root@k8smaster43-11 CSDN]#
CronJob负责在给定时间调度Job,或者按照周期运行Job。常用于定时任务脚本、数据库备份、定时邮件发送等。
- 编写YAML文件,通过schedule字段设置定时任务的时间间隔:
[root@k8smaster43-11 CSDN]# cat cronjob.yaml
apiVersion: batch/v1beta1 # API版本
kind: CronJob # 资源类型
metadata:
name: busybox-cronjob # CronJob资源名称
spec:
schedule: "*/1 * * * *" # 批处理时间间隔60s
jobTemplate:
spec:
completions: 1 # Job执行次数
parallelism: 1 # Job并发数量
template:
metadata:
app: busybox # Pod资源标签
spec:
containers:
- name: busybox # Pod容器名称
image: busybox # Pod容器镜像
command: ["sh", "-c", "date; echo Hello from the kubernetes cluster"]
restartPolicy: Never # Pod重启策略
[root@k8smaster43-11 CSDN]#
- 创建CronJob资源并观察执行过程,通过successfulJobsHistoryLimit字段可以设置需要保留的成功完成任务的Job个数,超过该数值的Job会被删除:
[root@k8smaster43-11 CSDN]# kubectl get cronjob,job,pod -o wide
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE CONTAINERS IMAGES SELECTOR
cronjob.batch/busybox-cronjob */1 * * * * False 0 31s 9m21s busybox busybox <none>
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
job.batch/busybox-cronjob-1658563620 1/1 18s 2m30s busybox busybox controller-uid=87d090c6-bbe4-48ca-8849-fc3a941d7bf4
job.batch/busybox-cronjob-1658563680 1/1 19s 90s busybox busybox controller-uid=673942e5-18fc-445e-b203-a264d271d5cb
job.batch/busybox-cronjob-1658563740 1/1 17s 29s busybox busybox controller-uid=c54ceea2-93e8-4b7e-83a3-b40e29113005
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/busybox-cronjob-1658563620-7d88g 0/1 Completed 0 2m30s 10.244.2.93 k8sworker43-22 <none> <none>
pod/busybox-cronjob-1658563680-528dd 0/1 Completed 0 90s 10.244.1.10 k8sworker43-21 <none> <none>
pod/busybox-cronjob-1658563740-h25ff 0/1 Completed 0 29s 10.244.2.94 k8sworker43-22 <none> <none>
[root@k8smaster43-11 CSDN]#
- 删除CornJob和对应Job、Pod的资源:
[root@k8smaster43-11 CSDN]# kubectl delete -f cronjob.yaml
cronjob.batch "busybox-cronjob" deleted
[root@k8smaster43-11 CSDN]#
4.5 StatefulSet
StatefulSet负责有状态服务问题,为每个Pod提供唯一的标识{StatefulSetName}-{0-N},保证部署和扩容的有序性,而其他控制器生成的Pod标识符存在随机值。
应用场景(案例在后续Service和PV/PVC部分):
- 稳定持久化存储,Pod重新调度后还能访问到相同的持久化数据,基于PVC实现的为每个Pod匹配一个PVC;
- 稳定的网络标志,Pod重新调度后其PodName和HostName不变,基于Headless Service实现的可以在容器内部ping PodName.HeadlessServiceName,而其他类型的Service只能ping ServiceName;
- 有序部署有序扩展,在部署和扩展的时候要依据定义的顺序依次运行,在下一个Pod运行之前所有前序Pod必须都是Running和Ready状态,基于init container实现;
- 有序收缩有序删除。