<>1、控制器简介

<>2、 Replicaset控制器
[root@server2 ~]# vim rs.yaml apiVersion: apps/v1 kind: ReplicaSet metadata:
name: replicaset-example spec: replicas: 3 运行了三个副本 selector:
表示选者器,控制器和pod之间的联系是啥,因为不同的pod有不同的控制器 matchLabels: app: nginx
如果pod上有app:nginx标签,此pod就属于此控制器 template: 模板 metadata: labels: app: nginx
表示模板下面创建的pod都会有app: nginx 标签 spec: containers: - name: nginx image: nginx
[root@server2 ~]# kubectl apply -f rs.yaml 运行
replicaset.apps/replicaset-example created [root@server2 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE replicaset-example 3 3 3 40s [root@server2 ~]#
kubectl get pod 运行了三个副本 NAME READY STATUS RESTARTS AGE replicaset-example-9qr92
1/1 Running 0 2m13s replicaset-example-c7zpv 1/1 Running 0 2m13s
replicaset-example-hdrqc 1/1 Running 0 2m13s [root@server2 ~]# kubectl get pod
--show-labels 查看pod的标签 ,3个副本都有 app=myapp 标签 NAME READY STATUS RESTARTS AGE
LABELS replicaset-example-9qr92 1/1 Running 0 3m22s app=myapp
replicaset-example-c7zpv 1/1 Running 0 3m22s app=myapp replicaset-example-hdrqc
1/1 Running 0 3m22s app=myapp
标签对控制器的影响
[root@server2 ~]# kubectl label pods replicaset-example-9qr92 app=nginx
--overwrite 将原有的标签覆盖换成app=nginx,overwrite 表示覆盖 pod/replicaset-example-9qr92
labeled [root@server2 ~]# kubectl get pod --show-labels 查看标签 NAME READY STATUS
RESTARTS AGE LABELS replicaset-example-9qr92 1/1 Running 0 13m app=nginx
其中这个不匹配,控制器会维护副本数,会重新拉伸一个出来 replicaset-example-c7zpv 1/1 Running 0 13m
app=myapp replicaset-example-hdrqc 1/1 Running 0 13m app=myapp
replicaset-example-sjxss 1/1 Running 0 2m46s app=myapp [root@server2 ~]#
kubectl delete -f rs.yaml 删除 replicaset.apps "replicaset-example" deleted
<>3、deployment 控制器

deployment 和 Replicaset 书写一样
[root@server2 ~]# vim rs.yaml apiVersion: apps/v1 kind: Deployment metadata:
name: deployment spec: replicas: 3 selector: matchLabels: app: myapp template:
metadata: labels: app: myapp spec: containers: - name: myapp image: myapp:v1
[root@server2 ~]# kubectl apply -f rs.yaml deployment.apps/deployment created
[root@server2 ~]# kubectl get all

<>deployment控制器可以进行版本更新,版本退回,rs是做不到的,rs只控制副本数,如下实验:
[root@server2 ~]# vim rs.yaml apiVersion: apps/v1 kind: ReplicaSet metadata:
name: deployment spec: replicas: 3 selector: matchLabels: app: myapp template:
metadata: labels: app: myapp spec: containers: - name: myapp image: myapp:v1
[root@server2 ~]# vim rs.yaml [root@server2 ~]# kubectl apply -f rs.yaml 运行
[root@server2 ~]# vim rs.yaml apiVersion: apps/v1 kind: ReplicaSet metadata:
name: deployment spec: replicas: 3 selector: matchLabels: app: myapp template:
metadata: labels: app: myapp spec: containers: - name: myapp image: myapp:v2
版本变成v2 [root@server2 ~]# kubectl apply -f rs.yaml [root@server2 ~]# kubectl get
pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS
GATES deployment-895ql 1/1 Running 0 2m28s 10.244.2.17 server4 <none> <none>
deployment-bsgpw 1/1 Running 0 2m28s 10.244.1.24 server3 <none> <none>
deployment-wmw2x 1/1 Running 0 2m28s 10.244.1.25 server3 <none> <none>
replicaset-example-9qr92 1/1 Running 0 77m 10.244.2.15 server4 <none>
[root@server2 ~]# curl 10.244.2.17 版本并没有更改。rs是做不到版本更新的 Hello MyApp | Version:
v1 | <a href="hostname.html">Pod Name</a> [root@server2 ~]# kubectl delete pod
--all 删除所有副本,重新拉起三个副本 pod "deployment-895ql" deleted pod "deployment-bsgpw"
deleted pod "deployment-wmw2x" deleted pod "replicaset-example-9qr92" deleted
[root@server2 ~]# kubectl get pod -o wide 查看ip NAME READY STATUS RESTARTS AGE
IP NODE NOMINATED NODE READINESS GATES deployment-7dvsd 1/1 Running 0 57s
10.244.2.20 server4 <none> <none> deployment-prlpx 1/1 Running 0 56s
10.244.1.29 server3 <none> <none> deployment-wtvkx 1/1 Running 0 56s
10.244.1.28 server3 <none> <none> [root@server2 ~]# curl 10.244.1.28
访问,可以看出新拉起来的,版本就更新成了v2 Hello MyApp | Version: v2 | <a href="hostname.html">Pod
Name</a> [root@server2 ~]# kubectl delete -f rs.yaml 删除 replicaset.apps
"deployment" deleted
<>deployment 控制器
[root@server2 ~]# vim rs.yaml 变回deployment控制器 apiVersion: apps/v1 kind:
Deployment 变回deployment控制器 metadata: name: deployment spec: replicas: 3
selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec:
containers: - name: myapp image: myapp:v1 [root@server2 ~]# kubectl apply -f
rs.yaml 运行 deployment.apps/deployment created [root@server2 ~]# kubectl get pod
查看pod节点,总共3个副本 NAME READY STATUS RESTARTS AGE deployment-678fcbc488-8nhmf 1/1
Running 0 38s deployment-678fcbc488-9brd7 1/1 Running 0 38s
deployment-678fcbc488-xkr8x 1/1 Running 0 38s [root@server2 ~]# vim rs.yaml
拉伸为6个副本 apiVersion: apps/v1 kind: Deployment metadata: name: deployment spec:
replicas: 6 拉伸为6个副本 selector: matchLabels: app: myapp template: metadata:
labels: app: myapp spec: containers: - name: myapp image: myapp:v1
[root@server2 ~]# kubectl apply -f rs.yaml 运行 deployment.apps/deployment
configured [root@server2 ~]# kubectl get pod
可以发现生成了6个副本,deployment控制器里面也有rs控制器,所以可以拉伸 NAME READY STATUS RESTARTS AGE
deployment-678fcbc488-57q4x 1/1 Running 0 75s deployment-678fcbc488-6l24d 1/1
Running 0 75s deployment-678fcbc488-8nhmf 1/1 Running 0 3m35s
deployment-678fcbc488-9brd7 1/1 Running 0 3m35s deployment-678fcbc488-cmggh 1/1
Running 0 75s deployment-678fcbc488-xkr8x 1/1 Running 0 3m35s [root@server2 ~]#
vim rs.yaml apiVersion: apps/v1 kind: Deployment metadata: name: deployment
spec: replicas: 3 下降为3个副本 selector: matchLabels: app: myapp template: metadata:
labels: app: myapp spec: containers: - name: myapp image: myapp:v1
[root@server2 ~]# kubectl apply -f rs.yaml 运行 [root@server2 ~]# kubectl get pod
3个副本 NAME READY STATUS RESTARTS AGE deployment-678fcbc488-57q4x 1/1 Running 0
6m50s deployment-678fcbc488-9brd7 1/1 Running 0 9m10s
deployment-678fcbc488-cmggh 1/1 Running 0 6m50s [root@server2 ~]# vim rs.yaml
版本更新 apiVersion: apps/v1 kind: Deployment metadata: name: deployment spec:
replicas: 3 selector: matchLabels: app: myapp template: metadata: labels: app:
myapp spec: containers: - name: myapp image: myapp:v2 将版本更新为v2 [root@server2
~]# kubectl apply -f rs.yaml deployment.apps/deployment configured
[root@server2 ~]# kubectl get pod -o wide 查看ip NAME READY STATUS RESTARTS AGE
IP NODE NOMINATED NODE READINESS GATES deployment-57c78c68df-d5bbz 1/1 Running
0 70s 10.244.1.33 server3 <none> <none> deployment-57c78c68df-v69cg 1/1 Running
0 66s 10.244.2.24 server4 <none> <none> deployment-57c78c68df-xq7wp 1/1 Running
0 61s 10.244.1.34 server3 <none> <none> [root@server2 ~]# curl 10.244.1.34
访问,可以发现版本变成v2 deployment控制器负责版本切换 Hello MyApp | Version: v2 | <a
href="hostname.html">Pod Name</a>
<>deployment控制器版本更新回退原理

[root@server2 ~]# kubectl get all

如果更新了镜像,deployment控制器新建一个rs,原来的rs不动保留,之前rs控制的pod会被回收,新的rs应用新的副本
如果退回原来的版本v1,v2上的rs直接切换到以前保留的版本v1,不用新建,v2上rs控制的pod会被回收

上述操作也可用命令操作

<>3、DaemonSet控制器(针对所有节点都要部署一个pod)
[root@server2 ~]# kubectl delete -f rs.yaml deployment.apps "deployment"
deleted [root@server2 ~]# cp rs.yaml ds.yaml [root@server2 ~]# vim ds.yaml
apiVersion: apps/v1 kind: DaemonSet metadata: name: daemonset spec: selector:
matchLabels: app: myapp template: metadata: labels: app: myapp spec:
containers: - name: myapp image: myapp:v2 [root@server2 ~]# kubectl apply -f
ds.yaml 运行 daemonset.apps/daemonset created [root@server2 ~]# kubectl get ds
部署了两个节点,其中master节点不部署 NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE
SELECTOR AGE daemonset 2 2 2 2 2 <none> 39s [root@server2 ~]# kubectl get node
daemonset控制器是根据集群节点数来部署的,每个节点部署一个pod NAME STATUS ROLES AGE VERSION server2
Ready control-plane,master 5d7h v1.23.4 server3 Ready <none> 4d22h v1.23.4
server4 Ready <none> 4d22h v1.23.4 [root@server2 ~]# vim ds.yaml 如果变更镜像版本
apiVersion: apps/v1 kind: DaemonSet metadata: name: daemonset spec: selector:
matchLabels: app: myapp template: metadata: labels: app: myapp spec:
containers: - name: myapp image: myapp:v1 版本变成v1 [root@server2 ~]# kubectl
apply -f ds.yaml 运行 [root@server2 ~]# kubectl get pod -o wide 查看ip NAME READY
STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES daemonset-2df5d 1/1
Running 0 68s 10.244.2.26 server4 <none> <none> daemonset-ksv2h 1/1 Running 0
72s 10.244.1.36 server3 <none> <none> [root@server2 ~]# curl 10.244.1.36
访问,可以发现版本变成v1,所以daemonset控制器也只成版本更新 Hello MyApp | Version: v1 | <a
href="hostname.html">Pod Name</a> [root@server2 ~]# kubectl delete -f ds.yaml
删除 daemonset.apps "daemonset" deleted
<>4、job控制器(仅执行一次任务)
[root@server1 harbor]# docker pull perl 网上拉取perl镜像 [root@server1 harbor]#
docker tag perl:latest reg.westos.org/library/perl:latest 改标签 [root@server1
harbor]# docker push reg.westos.org/library/perl:latest 上传到私有仓库 [root@server2
~]# vim job.yaml apiVersion: batch/v1 kind: Job metadata: name: pi spec:
template: spec: containers: - name: pi image: perl command: ["perl",
"-Mbignum=bpi", "-wle", "print bpi(2000)"] 容器内运行的指令 ,保留小数点多少位 restartPolicy:
Never 不重启 backoffLimit: 4 出错次数最大上限为4次 [root@server2 ~]# kubectl apply -f
job.yaml 创建 job.batch/pi created [root@server2 ~]# kubectl get pod
可以发现运行完一次之后直接退出 NAME READY STATUS RESTARTS AGE pi-v27nx 0/1 Completed 0 2m10s
[root@server2 ~]# kubectl logs pi-v27nx 输出的信息在日志里保存
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096
[root@server2 ~]# kubectl delete -f job.yaml 删除 job.batch "pi" deleted
<>5、cronjob 控制器

Cron Job 创建基于时间调度的 Jobs。
一个 CronJob 对象就像 crontab (cron table) 文件中的一行,它用 Cron 格式进行编写,并周期性地在给定的调度时间执行
Job。
Cron 时间表语法 # ┌───────────── 分钟 (0 - 59) # │ ┌───────────── 小时 (0 - 23) # │ │
┌───────────── 月的某天 (1 - 31) # │ │ │ ┌───────────── 月份 (1 - 12) # │ │ │ │
┌───────────── 周的某天 (0 - 6) (周日到周一;在某些系统上,7 也是星期日) # │ │ │ │ │ # │ │ │ │ │ # │
│ │ │ │ # * * * * * [root@server2 ~]# vim cronjob.yaml apiVersion:
batch/v1beta1 kind: CronJob metadata: name: hello spec: schedule: "* * * * *"
表示每分钟 jobTemplate: job模板 spec: template: spec: containers: - name: hello image:
busybox imagePullPolicy: IfNotPresent args: - /bin/sh - -c - date; echo Hello
from the Kubernetes cluster 在shell里面执行date命令 restartPolicy: OnFailure
表示只要出错,就重启 [root@server2 ~]# kubectl apply -f cronjob.yaml 运行 Warning:
batch/v1beta1 CronJob is deprecated in v1.21+, unavailable in v1.25+; use
batch/v1 CronJob 版本 太低 [root@server2 ~]# kubectl api-versions 查看版本

[root@server2 ~]# kubectl get pod 每分钟运行 NAME READY STATUS RESTARTS AGE
hello-27465276-mtdsr 0/1 Completed 0 2m5s hello-27465277-9pnb9 0/1 Completed 0
65s hello-27465278-5qbsj 0/1 Completed 0 5s [root@server2 ~]# kubectl logs
hello-27465278-5qbsj 查看日志里的输出 Tue Mar 22 02:38:01 UTC 2022 Hello from the
Kubernetes cluster [root@server2 ~]# kubectl delete -f cronjob.yaml 删除

技术
下载桌面版
GitHub
Gitee
SourceForge
百度网盘(提取码:draw)
云服务器优惠
华为云优惠券
腾讯云优惠券
阿里云优惠券
Vultr优惠券
站点信息
问题反馈
邮箱:[email protected]
吐槽一下
QQ群:766591547
关注微信