1. RS与RC与Deployment关联
- RC(Replication Controller)主要作用就是用来确保容器应用的副本数始终保持在用户定义的副本数。即如果有容器异常退出,会自动创建新的pod来替代;而如果异常多出来的容器也会自动回收。
- K8S官方建议使用RS(ReplicaSet)替代RC(Replication Controller)进行部署,RS跟RC没有本质的不同,只是名字不一样,并且RS支持集合式的selector.
2. 实例:
- 使用deployment来创建pod
vim nginx-rs.yaml
apiVersion: apps/v1 #指定要使用的 Kubernetes API 版本,这里使用的是 apps/v1
kind: ReplicaSet #指定要创建的对象类型,这里是 ReplicaSet
metadata: #元数据信息,包括对象的名称、标签等信息
name: my-nginx #对象的名称,这里是 my-nginx
spec: #指定 ReplicaSet 对象的配置信息
replicas: 3 #指定需要运行的副本数,这里是 3
selector: #指定选择器,用于选择需要管理的 Pod
matchLabels: #匹配标签,这里是 app: nginx-www,表示选择具有 app=nginx-www 标签的Pod
app: nginx-www
template: #指定 Pod 的模板,用于创建和管理 Pod
metadata: #Pod 元数据信息,包括标签等信息
labels: #Pod 标签,这里是 app: nginx-www
app: nginx-www
spec: #Pod 配置信息
containers: #容器列表,这里只有一个 Nginx 容器
- name: my-nginx #容器名称,这里是 my-nginx
image: nginx:latest #容器镜像名称,这里是 nginx:latest,表示使用最新版本的 Nginx 镜像
imagePullPolicy: IfNotPresent #本地不存在镜像时才尝试拉取新镜像
ports: #容器对外暴露的端口号,这里是 80,表示将容器的 80 端口映射到集群中的一个随机端口上。
- containerPort: 80
2.1. 创建RS
kubectl apply -f nginx-rs.yaml
----过几分钟查看一下pod状态:
kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-5pbm6 1/1 Running 0 5m47s
my-nginx-9q8hh 1/1 Running 0 5m47s
my-nginx-cp6st 1/1 Running 0 5m47s
--查看RS my-nginx的状态:
[root@master1 ~]# kubectl get rs my-nginx
NAME DESIRED CURRENT READY AGE
my-nginx 3 3 3 8m19s
3. RS与Deployment的关联
3.1. Deployment
-
Deployment为pod和RS提供了一个声明式定义(declarative)方法,用来替代以前的RC来方便的管理应用。典型的应用场景包括:
1. 定义Deployment来创建pod和RS
2. 滚动升级和回滚应用
3. 扩容和缩容
4. 暂停和继续Deployment -
使用Deployment部署一个简单的Nginx应用:
vim nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx-deployment
template:
metadata:
labels:
app: nginx-deployment
spec:
containers:
- name: my-nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
- 创建deployment:
kubectl apply -f nginx-deployment.yaml --record
--record 参数可以记录命令,我们可以很方便的查看每次 revision的变化。
- 查看状态:
[root@master1 ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 3m52s
[root@master1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-86c547cdd4 3 3 3 3m59s
[root@master1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-86c547cdd4-b5d9t 1/1 Running 0 4m4s
nginx-deployment-86c547cdd4-d4sxp 1/1 Running 0 4m4s
nginx-deployment-86c547cdd4-n8t9g 1/1 Running 0 4m4s
3.2. 扩容
[root@master1 ~]# kubectl scale deployment nginx-deployment --replicas=5
deployment.apps/nginx-deployment scaled
[root@master1 ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 5/5 5 5 6m14s
[root@master1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-86c547cdd4 5 5 5 6m23s
[root@master1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-86c547cdd4-b5d9t 1/1 Running 0 6m26s
nginx-deployment-86c547cdd4-d4sxp 1/1 Running 0 6m26s
nginx-deployment-86c547cdd4-flzxw 1/1 Running 0 20s
nginx-deployment-86c547cdd4-n8t9g 1/1 Running 0 6m26s
nginx-deployment-86c547cdd4-t5hzg 1/1 Running 0 20s
3.2.1. 如果集群支持HPA的话,还可以为Deployment设置自动扩展
kubectl autoscale deployment nginx-deployment --min=5 --max=10 --cpu-percent=80
3.3. 缩容
[root@master1 ~]# kubectl scale deployment nginx-deployment --replicas=2
deployment.apps/nginx-deployment scaled
[root@master1 ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 7m32s
[root@master1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-86c547cdd4 2 2 2 7m37s
[root@master1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-86c547cdd4-b5d9t 1/1 Running 0 7m40s
nginx-deployment-86c547cdd4-d4sxp 1/1 Running 0 7m40s
3.4. 更新镜像
kubectl set image deployment/nginx-deployment my-nginx=nginx:1.9.1
--查看是否更新成功
[root@master1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-6867c57f58-hdhqc 1/1 Running 0 9m29s
nginx-deployment-6867c57f58-z2smx 1/1 Running 0 8m8s
[root@master1 ~]# kubectl exec nginx-deployment-6867c57f58-hdhqc -- nginx -v
nginx version: nginx/1.9.1
3.5. 回滚
回滚到上一个版本
[root@master1 ~]# kubectl rollout undo deployment/nginx-deployment
deployment.apps/nginx-deployment rolled back
查看回滚状态
[root@master1 ~]# kubectl rollout status deployment nginx-deployment
deployment "nginx-deployment" successfully rolled out
[root@master1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-86c547cdd4-52llc 1/1 Running 0 15s
nginx-deployment-86c547cdd4-jv25d 1/1 Running 0 9s
[root@master1 ~]# kubectl exec nginx-deployment-86c547cdd4-52llc -- nginx -v
nginx version: nginx/1.25.1
3.6. 回退
查看回滚历史版本
[root@master1 ~]# kubectl rollout history deployment nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
2 kubectl apply --filename=nginx-deployment.yaml --record=true
3 kubectl apply --filename=nginx-deployment.yaml --record=true
回退到版本2
[root@master1 ~]# kubectl rollout undo deployment/nginx-deployment --to-revision=2
deployment.apps/nginx-deployment rolled back
[root@master1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-6867c57f58-g8bw2 1/1 Running 0 19s
nginx-deployment-6867c57f58-hbwng 1/1 Running 0 16s
[root@master1 ~]# kubectl exec nginx-deployment-6867c57f58-g8bw2 -- nginx -v
nginx version: nginx/1.9.1