工作负载控制器(Workload Controllers)时k8s的一个抽象的概念,用于更高级层次对象,部署和管理Pod,常用于工作负载控制器:
Deployment:无状态应用部署
StatefulSet:有状态应用部署
DaemonSet:确保所有node运行同一个pod
job:一次性任务
Cronjob:定时任务
控制器的作用:
管理pod对象
使用标签与pod关联
控制器实现了pod的运维,列如滚动更新、伸缩、副本管理、维护pod状态等。
Deployment的功能:
管理pod和ReplicaSet
具体由上线部署、副本设定、滚动升级、回滚等功能
提供声明时更新、列如只更新的一个新的lmage
流程:
应用程序—部署—升级—回滚—下线
Deployment部署
[root@master manifest]# cat test2.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: default
spec:
replicas: 5
selector:
matchLabels:
app: apache
template:
metadata:
labels:
app: apache
spec:
containers:
- image: sktystwd/apache:v0.1
imagePullPolicy: IfNotPresent
name: httpd
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web-6bcd5cffd7-8jfjx 1/1 Running 0 6s
web-6bcd5cffd7-gmmqq 1/1 Running 0 6s
web-6bcd5cffd7-lnfl4 1/1 Running 0 6s
web-6bcd5cffd7-x67cb 1/1 Running 0 6s
web-6bcd5cffd7-zh869 1/1 Running 0 6s
Deployment滚动升级
[root@master manifest]# cat test2.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: default
spec:
replicas: 5
strategy:
rollingUpdate:
maxSurge: 30%
maxUnavailable: 30%
type: RollingUpdate
selector:
matchLabels:
app: apache
template:
metadata:
labels:
app: apache
spec:
containers:
- image: sktystwd/apache:v0.2
imagePullPolicy: IfNotPresent
name: httpd
maxSurge:滚动更新过程中最大pod副本数,确保在更新时启动的pod数量比期望(replicas)pod数量最大多出25%
maxUnavailable:滚动更新过程中最大不可用pod副本,确保在更新时最大25%pod数量不可用,确保75%pod数量时可用状态。
查看
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web-5dc777c866-f8cfv 1/1 Running 0 17s
web-5dc777c866-gqp4j 1/1 Running 0 17s
web-5dc777c866-hvvwm 1/1 Running 0 18s
web-5dc777c866-nssgb 1/1 Running 0 18s
web-5dc777c866-vxrzz 1/1 Running 0 18s
web-6bcd5cffd7-8jfjx 1/1 Terminating 0 9m58s
web-6bcd5cffd7-gmmqq 1/1 Terminating 0 9m58s
web-6bcd5cffd7-lnfl4 1/1 Terminating 0 9m58s
web-6bcd5cffd7-x67cb 1/1 Terminating 0 9m58s
web-6bcd5cffd7-zh869 1/1 Terminating 0 9m58s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web-5dc777c866-f8cfv 1/1 Running 0 78s
web-5dc777c866-gqp4j 1/1 Running 0 78s
web-5dc777c866-hvvwm 1/1 Running 0 79s
web-5dc777c866-nssgb 1/1 Running 0 79s
web-5dc777c866-vxrzz 1/1 Running 0 79s
水平扩容(启动多实列,提高并发)
修改yaml里replicas值,在apply
kubectl scale deployment web --replicas=10
[root@master manifest]# cat test2.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: default
spec:
replicas: 10
strategy:
rollingUpdate:
maxSurge: 60%
maxUnavailable: 50%
type: RollingUpdate
selector:
matchLabels:
app: apache
template:
metadata:
labels:
app: apache
spec:
containers:
- image: sktystwd/apache:v0.1
imagePullPolicy: IfNotPresent
name: httpd
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web-5dc777c866-7cztl 0/1 Terminating 0 4s
web-5dc777c866-8zn7q 0/1 Terminating 0 4s
web-5dc777c866-fg49j 1/1 Running 0 2m48s
web-5dc777c866-k98nr 0/1 ContainerCreating 0 4s
web-5dc777c866-l5j72 1/1 Running 0 2m48s
web-5dc777c866-l6kcf 0/1 Terminating 0 4s
web-5dc777c866-qr9dl 0/1 ContainerCreating 0 4s
web-5dc777c866-x79lc 1/1 Running 0 2m47s
web-6bcd5cffd7-4cmpp 0/1 ContainerCreating 0 4s
web-6bcd5cffd7-697fx 0/1 Pending 0 4s
web-6bcd5cffd7-hc4h6 0/1 ContainerCreating 0 4s
web-6bcd5cffd7-hl7w7 0/1 Pending 0 4s
web-6bcd5cffd7-j8ks6 0/1 ContainerCreating 0 4s
web-6bcd5cffd7-msnq8 0/1 ContainerCreating 0 4s
web-6bcd5cffd7-v7k2t 0/1 ContainerCreating 0 4s
web-6bcd5cffd7-znvxs 0/1 Pending 0 4s
web-6bcd5cffd7-zvssw 0/1 ContainerCreating 0 4s
web-6bcd5cffd7-zwv86 0/1 Pending 0 4s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web-6bcd5cffd7-4cmpp 1/1 Running 0 53s
web-6bcd5cffd7-697fx 1/1 Running 0 53s
web-6bcd5cffd7-hc4h6 1/1 Running 0 53s
web-6bcd5cffd7-hl7w7 1/1 Running 0 53s
web-6bcd5cffd7-j8ks6 1/1 Running 0 53s
web-6bcd5cffd7-msnq8 1/1 Running 0 53s
web-6bcd5cffd7-v7k2t 1/1 Running 0 53s
web-6bcd5cffd7-znvxs 1/1 Running 0 53s
web-6bcd5cffd7-zvssw 1/1 Running 0 53s
web-6bcd5cffd7-zwv86 1/1 Running 0 53s
回滚(发布失败回复正常版本)
查看历史发布版本
[root@master ~]# kubectl rollout history deployment/web
deployment.apps/web
REVISION CHANGE-CAUSE
9
10
回滚到上一个版本(当前位置时12)
[root@master ~]# kubectl rollout undo deployment/web
deployment.apps/web rolled back
[root@master ~]# kubectl rollout history deployment/web
deployment.apps/web
REVISION CHANGE-CAUSE
10
11
回滚到指定版本
[root@master ~]# kubectl rollout history deployment/web
deployment.apps/web
REVISION CHANGE-CAUSE
10
11 (当前12)
[root@master ~]# kubectl rollout undo deployment/web --to-revision=10
deployment.apps/web rolled back
[root@master ~]# kubectl rollout history deployment/web
deployment.apps/web
REVISION CHANGE-CAUSE
11
12 (当前13)
revisionHistoryLimit RS历史版本保存数量
[root@master manifest]# cat test2.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: default
spec:
replicas: 3
revisionHistoryLimit: 10
strategy:
rollingUpdate:
maxSurge: 51%
maxUnavailable: 50%
type: RollingUpdate
selector:
matchLabels:
app: apache
template:
metadata:
labels:
app: apache
spec:
containers:
- image: sktystwd/apache:v0.2 #可以多换几个镜像
imagePullPolicy: IfNotPresent
name: httpd
只有使用2个以上的镜像才会显现出来
[root@master ~]# kubectl rollout history deployment/web
deployment.apps/web
REVISION CHANGE-CAUSE
11
12
13
14
ReplicaSet控制器用途
pod 副本数量管理,不断对比当前pod数量与期望pod数量
deployment每次发布都会创建一个RS作为记录,用于实现回滚
查看rs记录
[root@master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
web-5dc777c866 0 0 0 86m
web-5ff867458d 2 2 2 5m55s
web-6bcd5cffd7 0 0 0 96m
web-77588656d8 3 3 0 5m21s
DaemonSet功能:
在每一个node上运行一个pod
新加入的node也同样自动运行一个pod
[root@master manifest]# cat daemon.yaml
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: filebeat
namespace: kube-system
spec:
selector:
matchLabels:
name: filebeat
template:
metadata:
labels:
app: filebeat
spec:
containers:
- image: elastic/filebeat:7.16.2
imagePullPolicy: IfNotPresent
name: log
job普通任务(job)和定时任务(CronJob)
job使用
[root@master ~]# kubectl explain job
....
[root@master manifest]# cat 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 #(重试次数)
在容器里运行那条命令为一次性任务(job)做完结束。
定时任务(CronJob)像linux里的crontab一样
定时任务
应用场景:停止备份
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: test
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: test
image: busybox
args:
- /bin/sh
- "-c"
- "date;echo hello tom"
imagePullPolicy: IfNotPresent
restartPolicy: OnFailure
[root@master manifest]# kubectl create -f cronjob.yaml
cronjob.batch/test created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
test-1640362140-x9hq7 0/1 Completed 0 20s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
test-1640362140-x9hq7 0/1 Completed 0 2m38s
test-1640362200-5v657 0/1 Completed 0 98s
test-1640362260-4rn5x 0/1 Completed 0 38s