ReplicationController 与 ReplicaSet 与 Deployment 关联 – 记忆角落

ReplicationController 与 ReplicaSet 与 Deployment 关联

/ 0评 / 0

ReplicationController 与 ReplicaSet 与 Deployment 关联

RC (ReplicationController )主要的作用就是用来确保容器应用的副本数始终保持在用户定义的副本数 。即如果有容器异常退出,会自动创建新的Pod来替代;而如果异常多出来的容器也会自动回收

Kubernetes 官方建议使用 RS(ReplicaSet ) 替代 RC (ReplicationController ) 进行部署,RS 跟 RC 没有本质的不同,只是名字不一样,并且 RS 支持集合式的 selector

RC 控制器

apiVersion: v1
kind: ReplicationController
metadata:
  name: rc-demo
  labels:
    app: rc
spec:
  replicas: 3
  selector:
    app: rc
  template:
    metadata:
      labels:
        app: rc
    spec:
      containers:
        - name: nginx-demo
          image: nginx
          env:
            - name: GET_HOSTS_FROM
              value: dns
            - name: username
              value: "glj"
          ports:
            - containerPort: 80

RS 控制器

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-demo
spec:
  replicas: 3
  selector:
    matchLabels:
      name: rs-demo
  template:
    metadata:
      name: rs-demo
      labels:
        name: rs-demo
    spec:
      containers:
        - name: nginx-demo
          image: nginx
          env:
            - name: GET_HOSTS_FROM
              value: dns
            - name: username
              value: "glj"
          ports:
            - containerPort: 80

selector.matchExpressions

rs 在标签选择器上,除了可以定义键值对的选择形式,还支持 matchExpressions 字段,可以提供多种选择。
目前支持的操作包括:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-demo
spec:
  replicas: 3
  selector:
    matchExpressions:
      - key: name
        operator: Exists
  template:
    metadata:
      name: rs-demo
      labels:
        name: rs-demo
    spec:
      containers:
        - name: nginx-demo
          image: nginx
          env:
            - name: GET_HOSTS_FROM
              value: dns
            - name: username
              value: "glj"
          ports:
            - containerPort: 80


---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-demo
spec:
  selector:
    matchExpressions:
      - key: name
        operator: In
        values:
        - glj
        - guoliangjun
  template:
    metadata:
      labels:
        name: glj
    spec:
      containers:
        - name: rs-c1
          image: nginx
          ports:
            - containerPort: 80

RS 与 Deployment 的关联

image-20230117234802769

Deployment

Deployment 为 Pod 和 ReplicaSet 提供了一个声明式定义(declarative)方法,用来替代以前的ReplicationController 来方便的管理应用。典型的应用场景包括:

临时入门

1.部署一个简单的 Nginx 应用

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: labels-nginx-deployment
spec:
  replicas: 5
  selector:
    matchLabels:
      app: labels-nginx-deployment
  template:
    metadata:
      labels:
        app: labels-nginx-deployment
    spec:
      containers:
        - name: nginx
          image: daocloud.io/library/nginx:1.13.0-alpine
          imagePullPolicy: IfNotPresent
          ports:
              - containerPort: 80
                protocol: TCP
kubectl apply -f nginx-deployment.yaml --record
## --record参数可以记录命令,我们可以很方便的查看每次 revision 的变化

2.测试扩容命令

kubectl scale deployment nginx-deployment --replicas 10

3.测试更新命令

kubectl set image deployment/nginx-deployment nginx=nginx:1.21.1

4.测试简单的回滚

kubectl rollout undo deployment/nginx-deployment

更新 Deployment

注意: Deployment 的 rollout 当且仅当 Deployment 的 pod template(例如.spec.template)中的label更新或者镜像更改时被触发。其他更新,例如扩容Deployment不会触发 rollout

代替原来的镜像

[root@k8s-m1 deploy]# kubectl set image deployment.apps/nginx-deployment nginx=nginx:1.22.1
deployment.apps/nginx-deployment image updated

可以使用edit命令来编辑 Deployment

[root@k8s-m1 deploy]# kubectl edit deployment/nginx-deployment
deployment "nginx-deployment" edited

查看 rollout 的状态

[root@k8s-m1 deploy]# kubectl rollout status deployment/nginx-deployment
deployment "nginx-deployment" successfully rolled out

查看历史 RS

[root@k8s-m1 deploy]# kubectl get rs
NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deployment-7f4cc97958   0         0         0       99s
replicaset.apps/nginx-deployment-d4c88946c    5         5         5       47s

Deployment 更新策略

Deployment 可以保证在升级时只有一定数量的 Pod 是 down 的。

Deployment 同时也可以确保只创建出超过期望数量的一定数量的 Pod。

暂时默认是:25% ,使用命令查询:kubectl describe deployments

image-20230117235633536

更新策略声明

kubectl explain deploy.spec.strategy.type

测试使用:kubectl patch deployment nginx-deployment -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}'

金丝雀部署

设置一部分pod升级到nginx:1.23.3,然后暂停升级
[root@k8s-m1 deploy]# kubectl set image deploy  nginx-deployment nginx=nginx:1.23.3 && kubectl rollout pause deploy  nginx-deployment
deployment.apps/nginx-deployment image updated
deployment.apps/nginx-deployment paused

image-20230118000302878

没问题后,resume重新执行升级
[root@k8s-m1 deploy]# kubectl rollout resume deploy  nginx-deployment
deployment.apps/nginx-deployment resumed

image-20230118000455303

回退 Deployment

kubectl set image deployment/nginx-deployment nginx=nginx:1.21.1
kubectl rollout status deployments nginx-deployment
kubectl get pods
kubectl rollout history deployment/nginx-deployment
kubectl rollout undo deployment/nginx-deployment
kubectl rollout undo deployment/nginx-deployment --to-revision=2   ## 可以使用 --revision参数指定某个历史版本
kubectl rollout pause deployment/nginx-deployment    ## 暂停 deployment 的更新

清理 Policy

您可以通过设置.spec.revisonHistoryLimit项来指定 deployment 最多保留多少 revision 历史记录。默认的会保留所有的 revision;如果将该项设置为0,Deployment 就不允许回退了

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: labels-nginx-deployment
spec:
  revisionHistoryLimit: 0
  replicas: 5
  selector:
    matchLabels:
      app: labels-nginx-deployment
  template:
    metadata:
      labels:
        app: labels-nginx-deployment
    spec:
      containers:
        - name: nginx
          image: daocloud.io/library/nginx:1.13.0-alpine
          imagePullPolicy: IfNotPresent
          ports:
              - containerPort: 80
                protocol: TCP

发表评论

您的电子邮箱地址不会被公开。