Kubernetes ---- Pod控制器之Deployment
Deployments
官方介绍:https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
一个Deployment控制器为Pods和ReplicaSets提供描述性的更新方式。
使用Deploy创建Pods,实际上Deployment控制器用来控制ReplicaSet控制器,然后ReplicaSet来进行控制Pod,是个三层的结构.
描述 Deployment 中的 desired state,并且 Deployment 控制器以受控速率更改实际状态,以达到期望状态。可以定义 Deployments 以创建新的 ReplicaSets ,或删除现有 Deployments ,并通过新的 Deployments 使用其所有资源。
deployment控制器常用于管理多个无状态应用。
下列内容纯属个人观点,等于把官方的一些内容翻译了一遍同时做了一遍.有问题可随时提出,感谢!
一.编写yaml文件创建ReplicatSet控制器,且同时创建Pod。
apiVersion: apps/v1
kind: ReplicatSet
metadata:
name: rs-demo
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: myapp
release: canary
template:
metadata:
name: rs-pod
namespace: default
labels:
app: myapp
release: canary
spec:
containers:
- name: rs-container
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
livenessProbe:
exec:
command: ["/bin/sh","-c","ps aux| grep root"]
initialDelaySeconds: 2
periodSeconds: 3
readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 2
periodSeconds: 3
1. 命令行创建及验证
$ kubectl create -f rs-demo.yaml $ kubectl get rs NAME DESIRED CURRENT READY AGE rs-demo 2 2 2 42m
# 此时查看是有两个副本Pod $ kubectl get pods NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 9 20h rs-demo-wklsw 1/1 Running 0 42m rs-demo-hnfhw 1/1 Running 0 42m
# 将pod-demo这个pod增加一个rs-demo控制器能匹配到的标签; $ kubectl label pod pod-demo release=canary
# 此时发现会减少一个rs-demo开头的Pod,增加了pod-demo这个Pod(标签不要冲突,否则会出现如下情况); $ kubectl get pods NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 9 20h rs-demo-wklsw 1/1 Running 0 42m
2.修改副本数目
$ kubectl edit -f rs-demo.yaml .... replicas: 3 .... : wq $ kubectl get pods NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 10 20h rs-demo-8l82h 1/1 Running 0 78s rs-demo-hnfhw 1/1 Running 0 53m
3.滚动升级
# 手动修改镜像名称 $ kubectl edit -f rs-demo.yaml .... image: ikubernetes/myapp:v2 .... : wq # 此时发现rs的images已经有了改变 $ kubectl get rs -o wide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR rs-demo 3 3 3 59m myapp-container ikubernetes/myapp:v2 app=myapp,release=canary # 手动进行灰度升级:依次删除Pod,删除后rs再创建的Pod就会使用新镜像 $ kubectl delete pod pod-demo
二. 创建Deployment及创建Pod
apiVersion: apps/v1 kind: Deployment metadata: name: deploy-demo namespace: default spec: replicas: 2 selector: matchLabels: app: myapp release: canary template: metadata: labels: app: myapp release: canary spec: containers: - name: myapp-container image: ikubernetes/myapp:v1 imagePullPolicy: IfNotPresent ports: - containerPort: 80 livenessProbe: exec: command: ["/bin/sh","-c","ps aux | grep root"] initialDelaySeconds: 2 periodSeconds: 3 readinessProbe: tcpSocket: port: 80 initialDelaySeconds: 2 periodSeconds: 3
1. 命令行创建及验证
$ kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE deploy-demo 2/2 2 2 6m38s # 在创建了一个deployment控制器的同时,还创建了一个replicaset的控制器,说明deployment为三层结构 $ kubectl get rs NAME DESIRED CURRENT READY AGE deploy-demo-f5dfc4cf4 2 2 2 8m22s $ kubectl get pods NAME READY STATUS RESTARTS AGE deploy-demo-f5dfc4cf4-cgkks 1/1 Running 0 7m31s deploy-demo-f5dfc4cf4-nz99x 1/1 Running 0 7m31s
2.动态增加副本数或减少副本数:
$ vim deploy-demo.yaml .... replicas: 3 # 手动指定副本数量 .... $ kubectl apply -f deploy-demo.yaml $ kubectl get pods NAME READY STATUS RESTARTS AGE deploy-demo-f5dfc4cf4-cgkks 1/1 Running 0 13m deploy-demo-f5dfc4cf4-jpvxs 1/1 Running 0 12s deploy-demo-f5dfc4cf4-nz99x 1/1 Running 0 13m 使用打补丁方法: kubectl patch (-f FILENAME | TYPE NAME) -p PATCH [options] $ kubectl patch deployment deploy-demo -p '{"spec":{"replicas":5}}'
3.动态更新
$ vim deploy-demo.yaml .... image: ikubernetes/myapp:v2 .... # 跟踪pod的更新过程(内容如下) $ kubectl get pods -w -l app=myapp deploy-demo-854b57c687-j4xfv 0/1 Pending 0 0s deploy-demo-854b57c687-j4xfv 0/1 Pending 0 0s deploy-demo-854b57c687-j4xfv 0/1 ContainerCreating 0 0s deploy-demo-854b57c687-j4xfv 0/1 Running 0 1s deploy-demo-854b57c687-j4xfv 1/1 Running 0 5s deploy-demo-f5dfc4cf4-jpvxs 1/1 Terminating 0 5m11s deploy-demo-854b57c687-87g55 0/1 Pending 0 0s deploy-demo-854b57c687-87g55 0/1 Pending 0 0s deploy-demo-854b57c687-87g55 0/1 ContainerCreating 0 0s deploy-demo-f5dfc4cf4-jpvxs 0/1 Terminating 0 5m12s deploy-demo-f5dfc4cf4-jpvxs 0/1 Terminating 0 5m13s deploy-demo-f5dfc4cf4-jpvxs 0/1 Terminating 0 5m13s deploy-demo-854b57c687-87g55 0/1 Running 0 2s deploy-demo-854b57c687-87g55 1/1 Running 0 4s deploy-demo-f5dfc4cf4-cgkks 1/1 Terminating 0 18m deploy-demo-854b57c687-hw5zz 0/1 Pending 0 0s deploy-demo-854b57c687-hw5zz 0/1 Pending 0 0s deploy-demo-854b57c687-hw5zz 0/1 ContainerCreating 0 0s deploy-demo-f5dfc4cf4-cgkks 0/1 Terminating 0 18m deploy-demo-854b57c687-hw5zz 0/1 Running 0 1s deploy-demo-f5dfc4cf4-cgkks 0/1 Terminating 0 18m deploy-demo-f5dfc4cf4-cgkks 0/1 Terminating 0 18m deploy-demo-854b57c687-hw5zz 1/1 Running 0 5s deploy-demo-f5dfc4cf4-nz99x 1/1 Terminating 0 18m deploy-demo-f5dfc4cf4-nz99x 0/1 Terminating 0 18m deploy-demo-f5dfc4cf4-nz99x 0/1 Terminating 0 18m deploy-demo-f5dfc4cf4-nz99x 0/1 Terminating 0 18m # 另开一终端 $ kubectl apply -f deploy-demo.yaml # 查看rs控制器,发现多了一个rs,也就是第一个行的那个,第二行的rs也依旧存在,在等待用户随时回滚 $ kubectl get rs NAME DESIRED CURRENT READY AGE deploy-demo-854b57c687 3 3 3 27m deploy-demo-f5dfc4cf4 0 0 0 45m
4.回滚:
# 回滚deployment的容器 $ kubectl rollout undo deployment deploy-demo # 会发现可用的pods又都到最初的rs上了 $ kubectl get rs NAME DESIRED CURRENT READY AGE deploy-demo-854b57c687 0 0 0 27m deploy-demo-f5dfc4cf4 3 3 3 45m
5.金丝雀模式发布:
管理Deployment控制Pod的副本最大超出数及最大出现错误的Pod数:
$ kubectl expain deploy.spec.strategy.rollingUpdate maxSurge <string> : Pod副本可临时超过几个 ... maxUnavailable <string>: Pod副本容错率 ... $ kubectl patch deployment deploy-demo -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurget":1,"maxUnavailable":0}}}}' # 经查,发现策略已经更改,默认都是25%(不够1个Pod的按1个Pod算) $ kubectl describe deploy deploy-demo .... StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 0 max unavailable, 1 max surge .... # 开始升级版本,在升级完第一个Pod后,deployment控制器将会暂停; $ kubectl set image deployment deploy-demo myapp-container=ikubernetes/myapp:v3 && kubectl rollout pause deployment deploy-demo # 此时查看会发现有6个Pod,因为我们定义了"maxSurget"的值为1,所以可以临时超出事先定义好的5个副本数(此时还处于暂停状态) $ kubectl get pods NAME READY STATUS RESTARTS AGE deploy-demo-7fdb69d588-7srcs 1/1 Running 0 4m46s deploy-demo-7fdb69d588-b567d 1/1 Running 0 4m53s deploy-demo-7fdb69d588-c7p4v 1/1 Running 0 4m38s deploy-demo-7fdb69d588-cn69j 1/1 Running 0 7m41s deploy-demo-7fdb69d588-mr5k2 1/1 Running 0 4m35s # 查看rs控制器,发现又多了一个,但只有一个Pod准备就绪; $ kubectl get rs NAME DESIRED CURRENT READY AGE deploy-demo-7fdb69d588 1 1 1 15m deploy-demo-854b57c687 5 5 5 79m deploy-demo-f5dfc4cf4 0 0 0 97m # 监控升级过程 $ kubectl rollout status deployment deploy-demo 或 $ kubectl get pods -w # 过了一段儿时间发现客户没有反馈问题的时候,则继续升级; $ kubectl rollout resume deployment deploy-demo # 此时再次查看rs控制器,发现新增的控制器上可用的Pod已经变为了5(说明所有Pod已经升级完成) $ kubectl get rs NAME DESIRED CURRENT READY AGE deploy-demo-7fdb69d588 5 5 5 17m deploy-demo-854b57c687 0 0 0 81m deploy-demo-f5dfc4cf4 0 0 0 99m # 当发现问题时,可才用undo回滚策略,默认回滚至上一版本 $ kubectl rollout undo deployment deploy-demo # 查看现在的版本,及之前的版本,现在的版本是10 $ kubectl rollout history deployment deploy-demo REVISION CHANGE-CAUSE 8 <none> 9 <none> 10 <none> # 回滚至第8版本. $ kubectl rollout undo deployment deploy-demo --to-revision=8 # 发现此时所有Pod都在第一个rs上工作了 $ kubectl get rs NAME DESIRED CURRENT READY AGE deploy-demo-7fdb69d588 0 0 0 17m deploy-demo-854b57c687 0 0 0 81m deploy-demo-f5dfc4cf4 5 5 5 99m