RC、RS与Deployment的创建

 RC主要作用就是用来确保容器应用副本始终保持在用户定义的数量,如果有容器异常退出,会自动创建新的 Pod来代理,异常多出来的容器会被系统回收  RS本质与RC没有什么不同,可以认为RS是RC的升级版,并且支持集合式的selector

RS实例

apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  name: frontend
spec:
  replicas: 2
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx:v1
        env:
        - name: GET_HOSTS_FROM
          value: dns
        ports:
        - containerPort: 80

RS与Deployment的关联

  Deployment为Pod和Rs提供申明式定义方法,用来代理以前的RC来方便管理应用。 下面使用Deployment部署一个简单的nginx应用

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80

//deployment使用apply创建,也可以使用create
[root@k8s-master yaml]# kubectl apply -f nginx-deploy.yaml
//查看生成的pod
[root@k8s-master yaml]# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-698c64b949-rsldr   1/1     Running   0          20h
nginx-deployment-698c64b949-s5xxk   1/1     Running   0          20h

[root@k8s-master yaml]# kubectl get deployment nginx-deployment -o wide
NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
nginx-deployment   2/2     2            2           20h   nginx        nginx:latest   app=nginx

Deployment的扩容

//是用scale命令对nignx-deployment的容器扩容
[root@k8s-master yaml]# kubectl scale deployment nginx-deployment --replicas=10
deployment.extensions/nginx-deployment scaled
//查看扩容结果
[root@k8s-master yaml]# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-698c64b949-7bb8c   1/1     Running   0          9s
nginx-deployment-698c64b949-7mlcc   1/1     Running   0          9s
nginx-deployment-698c64b949-8qn2w   1/1     Running   0          10s
nginx-deployment-698c64b949-m8x7k   1/1     Running   0          10s
nginx-deployment-698c64b949-mx674   1/1     Running   0          9s
nginx-deployment-698c64b949-mxw84   1/1     Running   0          9s
nginx-deployment-698c64b949-rsldr   1/1     Running   0          20h
nginx-deployment-698c64b949-s5xxk   1/1     Running   0          20h
nginx-deployment-698c64b949-t4h4l   1/1     Running   0          9s
nginx-deployment-698c64b949-vx78j   1/1     Running   0          10s

Deployment的镜像更新

//使用set image镜像更新,应用更新会新生成一个对应rs
//格式 kubectl set iamges deployment/deployment名称 容器名=容器版本
kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1

Deployment的回滚

kubectl rollout undo deployment/nginx-deployment

Deployment的回滚相关其他命令

  • kubectl rollout status 回滚状态信息
  • kubectl rollout history 回滚历史信息
posted @ 2020-08-31 19:31  純黑色  阅读(872)  评论(0编辑  收藏  举报