# 官方提供@yunTaoScripts DEPLOYMENT 🔥🔥
# 创建DEPLOYMENT
RC、RS、Deployment的区别
- Replica Set目前与RC的区别只是支持的selector不同,后续肯定会加入更多功能。
- Deployment使用了Replica Set,是更高一层的概念。
- 除非需要自定义升级功能或根本不需要升级Pod,所以推荐使用Deployment而不直接使用Replica Set。
# 命令行管理
# 创建deployment
kubectl get deployments
kubectl create deployment web1 --image=nginx --dry-run=client -o yaml > cc.yaml kubectl run nginx --image=nginx --dry-run -o yaml
kubectl run nginx --image=nginx --replicas=5
kubectl run name --image=nginx --port=80
kubectl run name --image=nginx --env="env1=v2" --env="env2=v2"
kubectl run name --image=nginx --labels="app=hazelcast,env=prod"
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web
spec:
containers:
- image: nginx
name: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
resources: {}
status: {}
kubectl explain pod.spec.containers.ports
ports
- List of ports to expose from the container. Exposing a port here gives the system additional information about the network connections a container uses, but is primarily informational. Not specifying a port here DOES NOT prevent that port from being exposed. Any port which is listening on the default "0.0.0.0" address inside a container will be accessible from the network. Cannot be updated.
kubectl get pod --show-labels
- 通过delployment创建的pod,除了app=web的标签外,还有一个hash标签,保证了POD选择的准确性。
NAME | READY | STATUS | RESTARTS | AGE | LABELS |
---|---|---|---|---|---|
web | 1/1 | Running | 0 | 3m47s | run=web |
web-7cc55f78db-4x5bl | 1/1 | Running | 0 | 21m | app=web,pod-template-hash=7cc55f78db |
web-7cc55f78db-5v85d | 1/1 | Running | 0 | 21m | app=web,pod-template-hash=7cc55f78db |
web-7cc55f78db-mjzxr | 1/1 | Running | 0 | 21m | app=web,pod-template-hash=7cc55f78db |
# 修改副本数
kubectl scale deployment nginx --replicas=20
kubectl edit deployment nginx
kubectl apply -f xxxx.yaml
# HPA
- HPA(horizontal pod autoscalers)水平自动伸缩 通过检测pod CPU的负载,解决deployment里某pod负载太重,动态伸缩pod的数量来负载均衡。
# 配置HPA
- 如果把副本数设置为大于5个,运行个数也是5个,设置副本数是小于2个,运行为2个,副本数受hpa控制。
kubectl autoscale deployment nginx --min=2 --max=10
kubectl autoscale deployment nginx --max=5 --cpu-percent=80
- 如果某pod的负载太重,则会调整pod数目,
kubectl get hpa
kubectl delete hpa nginx
# 解决当前cpu的使用量为unknown
- 负载需要增加资源限制
spec:
terminationGracePeriodSeconds: 0
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
ports:
- containerPort: 80
resources: #####需要增加资源限制
requests:
cpu: 400m
# HPA测试
# 进程测试
创建一个deployment,副本数为1 修改deployment。
设置HPA:
kubectl autoscale deployment web --min=2 --max=6 --cpu-percent=40
,hpa会自动修改副本为2.进入到某pod里,执行多个
cat /dev/zero > /dev/null &
,观察pod的数目变化,及hpa的cpu使用量。kubectl get hpa -w
NAME | REFERENCE | TARGETS | MINPODS | MAXPODS | REPLICAS | AGE |
---|---|---|---|---|---|---|
web | Deployment/web | 0%/40% | 2 | 6 | 2 | 10m |
web | Deployment/web | 0%/40% | 2 | 6 | 2 | 11m |
web | Deployment/web | 0%/40% | 2 | 6 | 2 | 11m |
web | Deployment/web | 44%/40% | 2 | 6 | 2 | 11m |
web | Deployment/web | 124%/40% | 2 | 6 | 3 | 11m |
web | Deployment/web | 122%/40% | 2 | 6 | 6 | 12m |
web | Deployment/web | 80%/40% | 2 | 6 | 6 | 12m |
web | Deployment/web | 49%/40% | 2 | 6 | 6 | 12m |
web | Deployment/web | 41%/40% | 2 | 6 | 6 | 12m |
web | Deployment/web | 41%/40% | 2 | 6 | 6 | 13m |
在物理机里
killall -9 cat
观察pod的数目,差不多5分钟自动下降。kubectl get hpa -w
NAME | REFERENCE | TARGETS | MINPODS | MAXPODS | REPLICAS | AGE |
---|---|---|---|---|---|---|
web | Deployment/web | 41%/40% | 2 | 6 | 6 | 13m |
web | Deployment/web | 17%/40% | 2 | 6 | 6 | 13m |
web | Deployment/web | 0%/40% | 2 | 6 | 6 | 14m |
web | Deployment/web | 0%/40% | 2 | 6 | 6 | 16m |
web | Deployment/web | 0%/40% | 2 | 6 | 6 | 18m |
web | Deployment/web | 0%/40% | 2 | 6 | 3 | 18m |
web | Deployment/web | 0%/40% | 2 | 6 | 2 | 19m |
# service访问测试
- 安装测试工具
yum install httpd-tools -y
ab -t 600 -n 1000000 -c 1000 http://10.211.55.111:30093/index.html
- -n:在测试会话中所执行的请求个数。默认时,仅执行一个请求。
- -c:一次产生的请求个数。默认是一次一个。
- -t:测试所进行的最大秒数
请提前创建好service
- service创建方法见后面章节。
- http://10.211.55.111:30093/index.html service类型为NodePort。
NAME | REFERENCE | TARGETS | MINPODS | MAXPODS | REPLICAS | AGE |
---|---|---|---|---|---|---|
web | Deployment/web | unknown/10% | 1 | 10 | 0 | 9s |
web | Deployment/web | 0%/10% | 1 | 10 | 6 | 15s |
web | Deployment/web | 0%/10% | 1 | 10 | 1 | 60s |
web | Deployment/web | 0%/10% | 1 | 10 | 4 | 75s |
web | Deployment/web | 0%/10% | 1 | 10 | 6 | 90s |
web | Deployment/web | 62%/10% | 1 | 10 | 6 | 105s |
web | Deployment/web | 61%/10% | 1 | 10 | 10 | 2m |
# POD健壮性测试
kubectl get pod -owide | sort -rk 7
NAME | READY | STATUS | RESTARTS | AGE | IP | NODE |
---|---|---|---|---|---|---|
web-55899bcb7b-xxn94 | 1/1 | Running | 0 | 4m49s | 10.244.207.211 | ctsn1 |
web-55899bcb7b-tqgsp | 1/1 | Running | 0 | 4m49s | 10.244.207.210 | ctsn1 |
web-55899bcb7b-r8l75 | 1/1 | Running | 0 | 4m49s | 10.244.207.208 | ctsn1 |
web-55899bcb7b-r2tpb | 1/1 | Running | 0 | 4m49s | 10.244.207.209 | ctsn1 |
web-55899bcb7b-5vtld | 1/1 | Running | 0 | 4m49s | 10.244.207.212 | ctsn1 |
web-55899bcb7b-xshn7 | 1/1 | Running | 0 | 4m49s | 10.244.59.138 | ctsm1 |
web-55899bcb7b-vc6r9 | 1/1 | Running | 0 | 4m49s | 10.244.59.139 | ctsm1 |
web-55899bcb7b-hgpnv | 1/1 | Running | 0 | 4m49s | 10.244.59.144 | ctsm1 |
web-55899bcb7b-cj6ts | 1/1 | Running | 0 | 4m49s | 10.244.59.136 | ctsm1 |
web-55899bcb7b-5pqxd | 1/1 | Running | 0 | 4m49s | 10.244.59.156 | ctsm1 |
- 把ctsn1关机,等一段时间就会发现,pod都会在ctsm1上运行
NAME | READY | STATUS | RESTARTS | AGE | IP | NODE |
---|---|---|---|---|---|---|
web-55899bcb7b-5pqxd | 1/1 | Running | 0 | 11h | 10.244.59.156 | ctsm1 |
web-55899bcb7b-8nxwm | 1/1 | Running | 0 | 10h | 10.244.59.157 | ctsm1 |
web-55899bcb7b-c756r | 1/1 | Running | 0 | 10h | 10.244.59.151 | ctsm1 |
web-55899bcb7b-cj6ts | 1/1 | Running | 0 | 11h | 10.244.59.136 | ctsm1 |
web-55899bcb7b-hgpnv | 1/1 | Running | 0 | 11h | 10.244.59.144 | ctsm1 |
web-55899bcb7b-qdzss | 1/1 | Running | 0 | 10h | 10.244.59.148 | ctsm1 |
web-55899bcb7b-sbbwp | 1/1 | Running | 0 | 10h | 10.244.59.147 | ctsm1 |
web-55899bcb7b-vc6r9 | 1/1 | Running | 0 | 11h | 10.244.59.139 | ctsm1 |
web-55899bcb7b-xshn7 | 1/1 | Running | 0 | 11h | 10.244.59.138 | ctsm1 |
web-55899bcb7b-xx5jg | 1/1 | Running | 0 | 10h | 10.244.59.149 | ctsm1 |
- 当ctsn1重新启动,pod并不会返回到ctsn1上运行
# 升级镜像与回退
kubectl set image deployment/nginx nginx=nginx:1.9 < --record>
kubectl set image deployment/nginx nginx=nginx:1.9 busybox=buxybox:v1
kubectl rollout undo deployment nginx
# 查看历史记录
kubectl rollout history deployment/nginx
切换到某指定版本
kubectl rollout undo deployment/nginx --to-revision=2
# 滚动升级
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web
name: web
spec:
replicas: 10
selector:
matchLabels:
app: web
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: web
spec:
containers:
- image: nginx:1.15.8
imagePullPolicy: IfNotPresent
name: nginx
ports:
- containerPort: 80
resources: {}
status: {}
rollingUpdate 🍉
- maxSurge
- 在升级过程中一次升级几个。
- maxUnavailable
- 在升级过程中,有几个不可用。一次性删除多少个pod.
← 快速链接