文章目录
控制器
Pod 的分类:
• 自主式 Pod:Pod 退出后不会被创建
• 控制器管理的 Pod:在控制器的生命周期里,始终要维持 Pod 的副本数目
• 控制器类型:
• Replication Controller和ReplicaSet
• Deployment
• DaemonSet
• StatefulSet
• Job
• CronJob
• HPA全称Horizontal Pod Autoscaler
ReplicaSet控制器
设定副本数
[root@k8s1 ~]# vim replicaset-example.yaml
[root@k8s1 ~]# kubectl apply -f replicaset-example.yaml
replicaset.apps/replicaset-example created
[root@k8s1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
replicaset-example-5dm96 1/1 Running 0 8s
replicaset-example-q5fn2 1/1 Running 0 8s
replicaset-example-tnl9v 1/1 Running 0 8s
replicas:
设定副本数,可修改
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicaset-example
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
标签的重要性
修改标签之后,会自动拉取一个新的符合旧标签条件的pod容器
[root@k8s1 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-example-5dm96 1/1 Running 0 4m47s app=nginx
replicaset-example-q5fn2 1/1 Running 0 4m47s app=nginx
replicaset-example-tnl9v 1/1 Running 0 4m47s app=nginx
[root@k8s1 ~]# kubectl label pod
poddisruptionbudgets.policy pods podsecuritypolicies.policy podtemplates
[root@k8s1 ~]# kubectl label pod replicaset-example-5dm96 app=myapp --overwrite
pod/replicaset-example-5dm96 labeled
[root@k8s1 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-example-4cdxx 1/1 Running 0 4s app=nginx
replicaset-example-5dm96 1/1 Running 0 5m23s app=myapp
replicaset-example-q5fn2 1/1 Running 0 5m23s app=nginx
replicaset-example-tnl9v 1/1 Running 0 5m23s app=nginx
[root@k8s1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
replicaset-example 3 3 3 5m39s
deployment控制器
deploymnet控制器会去先拉取replicaset控制器,通过replicaset控制器去拉取Pod容器
[root@k8s1 ~]# vim deployment.yaml
[root@k8s1 ~]# kubectl apply -f deployment.yaml
deployment.apps/deployment-example created
[root@k8s1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/deployment-example-85b98978db-2d4c4 1/1 Running 0 12s
pod/deployment-example-85b98978db-5jmk6 1/1 Running 0 12s
pod/deployment-example-85b98978db-bgs5g 1/1 Running 0 12s
pod/replicaset-example-5dm96 1/1 Running 0 10m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11d
service/liveness-http ClusterIP 10.99.7.186 <none> 80/TCP 21m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deployment-example 3/3 3 3 12s
NAME DESIRED CURRENT READY AGE
replicaset.apps/deployment-example-85b98978db 3 3 3 12s
[root@k8s1 ~]#
更新控制器版本
ps:myapp镜像一定要先上传到harbor仓库
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
replicas: 6
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: myapp:v1
版本更新
[root@k8s1 ~]# vim deployment.yaml
[root@k8s1 ~]# kubectl apply -f deployment.yaml
deployment.apps/deployment-example configured
[root@k8s1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/deployment-example-7d84d7dccb-4tp7k 0/1 ContainerCreating 0 2s
pod/deployment-example-7d84d7dccb-mjf45 1/1 Running 0 6s
pod/deployment-example-7d84d7dccb-s857v 1/1 Running 0 2s
pod/deployment-example-7d84d7dccb-tjxm4 1/1 Running 0 6s
pod/deployment-example-7d84d7dccb-v8wxt 1/1 Running 0 6s
pod/deployment-example-7d84d7dccb-zdlzq 1/1 Running 0 2s
pod/deployment-example-85b98978db-5jmk6 1/1 Terminating 0 8m10s
pod/deployment-example-85b98978db-bgs5g 1/1 Terminating 0 8m10s
pod/deployment-example-85b98978db-fm6v4 0/1 Terminating 0 6s
pod/replicaset-example-5dm96 1/1 Running 0 18m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11d
service/liveness-http ClusterIP 10.99.7.186 <none> 80/TCP 28m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deployment-example 5/6 6 5 8m10s
NAME DESIRED CURRENT READY AGE
replicaset.apps/deployment-example-7d84d7dccb 6 6 5 6s
replicaset.apps/deployment-example-85b98978db 0 0 0 8m10s
版本更新与回滚
[root@k8s1 ~]# vim deployment.yaml
[root@k8s1 ~]# kubectl apply -f deployment.yaml
deployment.apps/deployment-example configured
[root@k8s1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-example-56c698c49c-78kql 1/1 Running 0 3s
deployment-example-56c698c49c-9qgdj 0/1 ContainerCreating 0 1s
deployment-example-56c698c49c-gpvmq 1/1 Running 0 3s
deployment-example-56c698c49c-hc7fq 0/1 ContainerCreating 0 1s
deployment-example-56c698c49c-nvpgf 1/1 Running 0 3s
deployment-example-56c698c49c-xbhmm 1/1 Running 0 1s
deployment-example-7d84d7dccb-4tp7k 0/1 Terminating 0 139m
deployment-example-7d84d7dccb-mjf45 1/1 Terminating 0 139m
deployment-example-7d84d7dccb-s857v 1/1 Running 0 139m
deployment-example-7d84d7dccb-tjxm4 1/1 Terminating 0 139m
deployment-example-7d84d7dccb-zdlzq 1/1 Running 0 139m
replicaset-example-5dm96 1/1 Running 0 157m
版本回滚回myapp:v1
[root@k8s1 ~]# vim deployment.yaml
[root@k8s1 ~]# kubectl apply -f deployment.yaml
deployment.apps/deployment-example configured
[root@k8s1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-example-7d84d7dccb-527fj 1/1 Running 0 9s
deployment-example-7d84d7dccb-6sgxg 1/1 Running 0 8s
deployment-example-7d84d7dccb-f2jpv 1/1 Running 0 9s
deployment-example-7d84d7dccb-fchwf 1/1 Running 0 8s
deployment-example-7d84d7dccb-jlr7n 1/1 Running 0 8s
deployment-example-7d84d7dccb-wjq8k 1/1 Running 0 9s
replicaset-example-5dm96 1/1 Running 0 159m
控制器,回滚频率
strategy:
rollingUpdate:
maxSurge: 1 ##最大
maxUnavailable: 0
一个一个的平稳更新
[root@k8s1 ~]# vim deployment.yaml
[root@k8s1 ~]# kubectl apply -f deployment.yaml
deployment.apps/deployment-example configured
[root@k8s1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-example-56c698c49c-lqrgt 1/1 Running 0 2s
deployment-example-56c698c49c-tx7ns 0/1 Pending 0 0s
deployment-example-7d84d7dccb-prhcm 1/1 Running 0 68s
deployment-example-7d84d7dccb-pxdkj 1/1 Running 0 70s
deployment-example-7d84d7dccb-s7z74 1/1 Running 0 73s
deployment-example-7d84d7dccb-szzh7 1/1 Running 0 70s
deployment-example-7d84d7dccb-t94sw 1/1 Running 0 72s
deployment-example-7d84d7dccb-vg8ql 1/1 Terminating 0 68s
replicaset-example-5dm96 1/1 Running 0 3h15m
[root@k8s1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-example-56c698c49c-lqrgt 1/1 Running 0 4s
deployment-example-56c698c49c-mbwb7 0/1 ContainerCreating 0 0s
deployment-example-56c698c49c-r95vn 1/1 Running 0 1s
deployment-example-56c698c49c-tx7ns 1/1 Running 0 2s
deployment-example-7d84d7dccb-pxdkj 1/1 Running 0 72s
deployment-example-7d84d7dccb-s7z74 1/1 Running 0 75s
deployment-example-7d84d7dccb-szzh7 1/1 Terminating 0 72s
deployment-example-7d84d7dccb-t94sw 1/1 Running 0 74s
replicaset-example-5dm96 1/1 Running 0 3h15m
[root@k8s1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-example-56c698c49c-229ks 1/1 Running 0 2s
deployment-example-56c698c49c-lqrgt 1/1 Running 0 7s
deployment-example-56c698c49c-mbwb7 1/1 Running 0 3s
deployment-example-56c698c49c-r95vn 1/1 Running 0 4s
deployment-example-56c698c49c-sr58g 0/1 ContainerCreating 0 1s
deployment-example-56c698c49c-tx7ns 1/1 Running 0 5s
deployment-example-7d84d7dccb-pxdkj 1/1 Running 0 75s
deployment-example-7d84d7dccb-s7z74 1/1 Terminating 0 78s
replicaset-example-5dm96 1/1 Running 0 3h15m
[root@k8s1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-example-56c698c49c-229ks 1/1 Running 0 3s
deployment-example-56c698c49c-lqrgt 1/1 Running 0 8s
deployment-example-56c698c49c-mbwb7 1/1 Running 0 4s
deployment-example-56c698c49c-r95vn 1/1 Running 0 5s
deployment-example-56c698c49c-sr58g 1/1 Running 0 2s
deployment-example-56c698c49c-tx7ns 1/1 Running 0 6s
replicaset-example-5dm96 1/1 Running 0 3h15m
[root@k8s1 ~]#
检查 Deployment 是否已创建
运行 kubectl get deployments 检查 Deployment 是否已创建。 如果已经创建 Deployment,则输出类似于:
[root@k8s1 ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
deployment-example 6/6 6 6 3h8m
检查 Deployment 上线历史
[root@k8s1 ~]# kubectl rollout history deployment/deployment-example
deployment.apps/deployment-example
REVISION CHANGE-CAUSE
1 <none>
6 <none>
7 <none>
DaemonSet控制器
DaemonSet控制器可以在每一个节点上部署应用
以harbor仓库有的镜像nginx为例:
[root@k8s1 ~]# vim daemonset.yaml
[root@k8s1 ~]# kubectl apply -f daemonset.yaml
daemonset.apps/daemonset-example created
可以发现只有k8s2和k8s3两个节点部署,这是因为为了保持稳定性,master节点上有一个污点
[root@k8s1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
daemonset-example-dtnjs 1/1 Running 0 19s
daemonset-example-t2ww2 1/1 Running 0 19s
[root@k8s1 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s1 Ready control-plane,master 11d v1.23.12
k8s2 Ready <none> 11d v1.23.12
k8s3 Ready <none> 11d v1.23.12
[root@k8s1 ~]# kubectl describe nodes k8s1 | grep Tain
Taints: node-role.kubernetes.io/master:NoSchedule
容忍参数
要解决这个,控制器需要设置容忍
这也是flannel可以全部部署的原因,如下图
给daemonset控制器加上容忍参数后,master节点成功部署
[root@k8s1 ~]# vim daemonset.yaml
[root@k8s1 ~]# kubectl apply -f daemonset.yaml
daemonset.apps/daemonset-example configured
[root@k8s1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
daemonset-example-7rrk2 1/1 Running 0 1s
daemonset-example-dtnjs 1/1 Terminating 0 10m
daemonset-example-r5b82 1/1 Running 0 8s
Job控制器
用于离线任务
此处的perl镜像需要提前上传到harbor仓库
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl:5.34.0
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
[root@k8s1 ~]# vim job.yaml
[root@k8s1 ~]# kubectl apply -f job.yaml
job.batch/pi created
[root@k8s1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pi-ljtqn 0/1 ContainerCreating 0 19s
[root@k8s1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pi-ljtqn 1/1 Running 0 25s
[root@k8s1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pi-ljtqn 0/1 Completed 0 26s
查看pi-ljtqn 日志
[root@k8s1 ~]# kubectl logs pi-ljtqn
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
Cronjob控制器
CronJob通过创建job,然后由job来创建控制器
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: cronjob-example
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: cronjob
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from k8s cluster
restartPolicy: OnFailure
schedule
每隔一分钟创建一个pod
[root@k8s1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/cronjob-example-27781315-hgxvk 0/1 Completed 0 11s
pod/replicaset-example-5dm96 1/1 Running 0 4h3m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11d
service/liveness-http ClusterIP 10.99.7.186 <none> 80/TCP 4h14m
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
cronjob.batch/cronjob-example * * * * * False 0 11s 22s
NAME COMPLETIONS DURATION AGE
job.batch/cronjob-example-27781315 1/1 7s 11s
[root@k8s1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/cronjob-example-27781315-hgxvk 0/1 Completed 0 12s
pod/replicaset-example-5dm96 1/1 Running 0 4h4m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11d
service/liveness-http ClusterIP 10.99.7.186 <none> 80/TCP 4h14m
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
cronjob.batch/cronjob-example * * * * * False 0 12s 23s
NAME COMPLETIONS DURATION AGE
job.batch/cronjob-example-27781315 1/1 7s 12s
[root@k8s1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/cronjob-example-27781315-hgxvk 0/1 Completed 0 61s
pod/cronjob-example-27781316-2ghqw 0/1 ContainerCreating 0 1s
pod/replicaset-example-5dm96 1/1 Running 0 4h4m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11d
service/liveness-http ClusterIP 10.99.7.186 <none> 80/TCP 4h15m
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
cronjob.batch/cronjob-example * * * * * False 1 1s 72s
NAME COMPLETIONS DURATION AGE
job.batch/cronjob-example-27781315 1/1 7s 2m45s
job.batch/cronjob-example-27781316 1/1 6s 105s
job.batch/cronjob-example-27781317 1/1 6s 45s