当前位置: 代码迷 >> 综合 >> k8s核心概念之Replication Controller(RC)、Replica Set(RS)和Deployment
  详细解决方案

k8s核心概念之Replication Controller(RC)、Replica Set(RS)和Deployment

热度:59   发布时间:2023-09-22 12:34:30.0

一、Controller基础参数及命令

1. Replication Controller

ReplicationController定义了一个期望的场景,即声明某种Pod的副本数量在任意时刻都符合某个预期值,所以RC的定义包含以下几个部分:

  • Pod期待的副本数(replicas)
  • 用于筛选目标Pod的Label Selector
  • 当Pod的副本数量小于预期数量时,用于创建新Pod的Pod模板(template)

kind:表示要新建对象的类型
spec.selector:表示需要管理的Pod的label,这里表示包含app: nginx的label的Pod都会被该RC管理
spec.replicas:表示受此RC管理的Pod需要运行的副本数
spec.template:表示用于定义Pod的模板,比如Pod名称、拥有的label以及Pod中运行的应用等

apiVersion: v1
kind: ReplicationController
metadata:name: nginx-rc    #设置rc的元数据
spec:               #设置rc的具体规格replicas: 3       #设置Pod的具体数量selector:         #通过selector来匹配相应的Pod的labelapp: my-nginxtemplate:         #设置Pod的模板metadata:name: nginxlabels:app: my-nginxspec:containers:- name: nginximage: nginximagePullPolicy: Always #镜像拉取策略,分为Always,Never,IfNotPresent,默认是Alwaysports:- containerPort: 80

rc基础操作

# 创建rc
[root@master01 k8s]# kubectl apply -f nginx_rc.yaml
replicationcontroller/nginx-rc created
# 查询rc
[root@master01 k8s]# kubectl get rc nginx-rc
NAME       DESIRED   CURRENT   READY   AGE
nginx-rc   3         3         3       74s
# 查询pod运行情况
[root@master01 k8s]# kubectl get pod -l app  # -l 指定selector的key
NAME             READY   STATUS    RESTARTS   AGE
nginx-rc-dsn65   1/1     Running   0          3m30s
nginx-rc-f5d48   1/1     Running   0          3m30s
nginx-rc-gvn97   1/1     Running   0          3m30s
# 删除rc
[root@master01 k8s]# kubectl delete -f nginx_rc.yaml
replicationcontroller "nginx-rc" deleted

rc特性

# 删除指定pod
[root@master01 k8s]# kubectl delete pod nginx-rc-dsn65
pod "nginx-rc-dsn65" deleted
# 当删除其中一个Pod或者删除全部Pod的时候,RC会自动再次创建Pod,直到满足配置文件中定义的个数
[root@master01 k8s]# kubectl get pod -l app
NAME             READY   STATUS              RESTARTS   AGE
nginx-rc-8r9mf   0/1     ContainerCreating   0          13s
nginx-rc-f5d48   1/1     Running             0          6m22s
nginx-rc-gvn97   1/1     Running             0          6m22s

2. Replica Set

RS与RC唯一的区别是:RS支持基于集合的Label Selector(Set-based selector),而RC只支持基于等式的Label Selector(equality-based selector),这使得Replica Set的功能更强

apiVersion: apps/v1
kind: ReplicaSet
metadata:name: nginx-rs    #设置rs的元数据
spec:               #设置rs的具体规格replicas: 3       #设置Pod的具体数量selector:matchExpressions:- key: appoperator: Invalues: [nginx-rs]template:         #设置Pod的模板metadata:name: nginxlabels:app: nginx-rsspec:containers:- name: nginximage: nginximagePullPolicy: Always #镜像拉取策略,分为Always,Never,IfNotPresent,默认是Alwaysports:- containerPort: 80

k8s核心概念之Replication Controller(RC)、Replica Set(RS)和Deployment
一般情况下,我们很少单独使用Replica Set,它主要是被Deployment这个更高的资源对象所使用,从而形成一整套Pod创建、删除、更新的编排机制。当我们使用Deployment时,无须关心它是如何创建和维护Replica Set的,这一切都是自动发生的。同时,无需担心跟其他机制的不兼容问题(比如ReplicaSet不支持rolling-update但Deployment支持)。

3. Deployment

Deployment相对RC最大的一个升级就是我们可以随时知道当前Pod“部署”的进度。
创建一个Deployment对象来生成对应的Replica Set并完成Pod副本的创建过程
检查Deploymnet的状态来看部署动作是否完成(Pod副本的数量是否达到预期的值)

apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploymentlabels:app: nginx
spec:replicas: 3selector:matchLabels:app: nginxtemplate:metadata:name: nginxlabels:app: nginxspec:containers:- name: nginximage: nginx:1.7.9ports:- containerPort: 80

跟踪pod部署

# 启动pod
[root@master01 k8s]# kubectl apply -f nginx_deployment.yaml --record
deployment.apps/nginx-deployment created
[root@master01 k8s]# kubectl get pods -o wide
NAME                                READY   STATUS              RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
nginx-deployment-6dc77f4d96-652rh   0/1     ContainerCreating   0          0s    <none>   work01   <none>           <none>
nginx-deployment-6dc77f4d96-82bp5   0/1     ContainerCreating   0          0s    <none>   work02   <none>           <none>
nginx-deployment-6dc77f4d96-gkjt5   0/1     ContainerCreating   0          0s    <none>   work01   <none>           <none>
[root@master01 k8s]# kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   0/3     3            0           0s
[root@master01 k8s]# kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-6dc77f4d96   3         3         0       1s
[root@master01 k8s]# kubectl get deployment -o wide
NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES        SELECTOR
nginx-deployment   0/3     3            0           2s    nginx        nginx:1.7.9   app=nginx

二、弹性伸缩

弹性伸缩就是在现有环境不能满足业务需求的时候,进行的扩容或缩容

# ReplicationController 扩缩容
[root@master01 k8s]# kubectl scale rc nginx-rc --replicas=6
replicationcontroller/nginx-rc scaled
[root@master01 k8s]# kubectl get pods
NAME             READY   STATUS              RESTARTS   AGE
nginx-rc-265zs   0/1     ContainerCreating   0          5s
nginx-rc-29bb9   1/1     Running             0          58s
nginx-rc-2rjrw   1/1     Running             0          58s
nginx-rc-hxhjd   1/1     Running             0          5s
nginx-rc-m27vb   1/1     Running             0          58s
nginx-rc-zs7dv   0/1     ContainerCreating   0          5s
# ReplicationController 判断副本数是否正确,正确并修改数量
[root@master01 k8s]# kubectl scale rc nginx-rc --current-replicas=3 --replicas=1
error: Expected replicas to be 3, was 6
[root@master01 k8s]# kubectl scale rc nginx-rc --current-replicas=6 --replicas=1
replicationcontroller/nginx-rc scaled
[root@master01 k8s]# kubectl get pods -owide
NAME             READY   STATUS    RESTARTS   AGE   IP               NODE     NOMINATED NODE   READINESS GATES
nginx-rc-m27vb   1/1     Running   0          17h   10.244.205.228   work01   <none>           <none># deployment扩缩容把rc改成deployment即可
[root@master01 k8s]# kubectl scale deployment nginx-deployment --replicas=5
deployment.apps/nginx-deployment scaled

三、滚动升级【deployment】

升级pod

[root@master01 k8s]# kubectl set image deployment nginx-deployment nginx=nginx:1.9.1
deployment.apps/nginx-deployment image updated
[root@master01 k8s]# kubectl get rs -o wide
NAME                          DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES        SELECTOR
nginx-deployment-6dc77f4d96   2         2         2       36s   nginx        nginx:1.7.9   app=nginx,pod-template-hash=6dc77f4d96
nginx-deployment-d79d4c8c4    2         2         1       3s    nginx        nginx:1.9.1   app=nginx,pod-template-hash=d79d4c8c4
[root@master01 k8s]# kubectl get rs -o wide
NAME                          DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES        SELECTOR
nginx-deployment-6dc77f4d96   0         0         0       39s   nginx        nginx:1.7.9   app=nginx,pod-template-hash=6dc77f4d96
nginx-deployment-d79d4c8c4    3         3         3       6s    nginx        nginx:1.9.1   app=nginx,pod-template-hash=d79d4c8c4

回滚pod

# 创建时候记得添加参数--record
kubectl apply -f nginx_deployment.yaml --record# 查看pod情况
[root@master01 k8s]# kubectget pods
NAME                                READY   STATUS             RESTARTS   AGE
nginx-deployment-575f4c84f8-5hxzz   1/1     Running            0          2m16s
nginx-deployment-5dff796fc4-w69ps   0/1     ImagePullBackOff   0          2m10s
nginx-deployment-d79d4c8c4-cd6rt    1/1     Running            0          2m24s
nginx-deployment-d79d4c8c4-hqxhs    1/1     Running            0          2m22s
[root@master01 k8s]# kubectl rollout status deployment nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...# 查看资源信息
[root@master01 k8s]# kubectl get rs -owide
NAME                          DESIRED   CURRENT   READY   AGE    CONTAINERS   IMAGES         SELECTOR
nginx-deployment-575f4c84f8   3         3         3       2m1s   nginx        nginx:1.20.2   app=nginx,pod-template-hash=575f4c84f8
nginx-deployment-5dff796fc4   1         1         0       15s    nginx        nginx:1.91     app=nginx,pod-template-hash=5dff796fc4
nginx-deployment-6dc77f4d96   0         0         0       25m    nginx        nginx:1.7.9    app=nginx,pod-template-hash=6dc77f4d96
nginx-deployment-d79d4c8c4    0         0         0       24m    nginx        nginx:1.9.1    app=nginx,pod-template-hash=d79d4c8c4# 查看历史版本
[root@master01 k8s]# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=nginx_deployment.yaml --record=true
2         kubectl apply --filename=nginx_deployment.yaml --record=true
3         kubectl apply --filename=nginx_deployment.yaml --record=true
4         kubectl apply --filename=nginx_deployment.yaml --record=true# 查看指定版本详细信息
[root@master01 k8s]# kubectl rollout history deployment/nginx-deployment --revision=3
deployment.apps/nginx-deployment with revision #3
Pod Template:Labels:       app=nginxpod-template-hash=575f4c84f8Annotations:  kubernetes.io/change-cause: kubectl apply --filename=nginx_deployment.yaml --record=trueContainers:nginx:Image:      nginx:1.20.2Port:       80/TCPHost Port:  0/TCPEnvironment:        <none>Mounts:     <none>Volumes:      <none># 不指定版本则回滚上一个可执行版本
[root@master01 k8s]# kubectl rollout undo deployment/nginx-deployment
deployment.apps/nginx-deployment rolled back
# 历史版本已经被提到最新,原来位置的版本被删除
[root@master01 k8s]# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=nginx_deployment.yaml --record=true
2         kubectl apply --filename=nginx_deployment.yaml --record=true
4         kubectl apply --filename=nginx_deployment.yaml --record=true
5         kubectl apply --filename=nginx_deployment.yaml --record=true# 回滚指定版本,加上--to-revision=版本号
[root@master01 k8s]# kubectl rollout undo deployment nginx-deployment --to-revision=1
deployment.apps/nginx-deployment rolled back
[root@master01 k8s]# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
2         kubectl apply --filename=nginx_deployment.yaml --record=true
4         kubectl apply --filename=nginx_deployment.yaml --record=true
5         kubectl apply --filename=nginx_deployment.yaml --record=true
6         kubectl apply --filename=nginx_deployment.yaml --record=true
  相关解决方案