关于kubernetes:02kubernetes笔记-Pod控制器一-ReplicaSet

34次阅读

共计 18190 个字符,预计需要花费 46 分钟才能阅读完成。

控制器简介

Kubernetes 中内建了很多 controller(控制器),这些相当于一个状态机,用来管制 Pod 的具体状态和行为

  • 负责利用编排的控制器类型有如下几种:
  • ReplicationController: 最晚期的 Pod 控制器;
  • RelicaSet: 正本集, 负责管理一个利用(Pod)的多个正本;
  • Deployment: 部署,它不间接治理 Pod,而是借助于 ReplicaSet 来治理 Pod; 最罕用的无状态利用控制器;
  • DaemonSet: 守护过程集,用于确保在每个节点仅运行某个利用的一个 Pod 正本;
  • StatefulSet: 性能相似于 Deployment,但 StatefulSet 专用于编排有状态利用;
  • Job: 有终止期限的一次性作业式工作,而非始终处于运行状态的服务过程;
  • CronJob: 有终止期限的周期性作业式工作;

ReplicaSet 简介

  • ReplicationController 和 ReplicaSet
    ReplicationController(RC)用来确保容器利用的正本数始终保持在用户定义的正本数,即如果有容器异样退
    出,会主动创立新的 Pod 来代替;而如果异样多进去的容器也会主动回收;
    在新版本的 Kubernetes 中倡议应用 ReplicaSet 来取代 ReplicationController。ReplicaSet 跟
    ReplicationController 没有实质的不同,只是名字不一样,并且 ReplicaSet 反对汇合式的 selector;
  • Pod 控制器定义因素:
  • 标签选择器;
  • 冀望的正本数;
  • Pod 模板;
  • ReplicaSet 的更新机制:
    删除式更新
    比方:- set image: 更新利用版本,但对于 replicaset 来说,仅能更新 API Server 中的定义 理论运行 Pod 只能删除后重新启动才失效

Replicaset 字段阐明

apiversion: apps/v1
kind: Replicaset
metadata:
  name: ...
  namespace: …
spec:
  minReadySeconds <integer>  #Pod 就绪后多少秒内任一容器无 crash 方可视为“就绪”replicas <integer> #冀望的 Pod 正本数, 默认为 1
  selectorl:  #标签选择器,必须匹配 template 字段中 Pod 模板中的标签;
    matchExpressions_<[ ]Object> #标签选择器表达式列表,多个列表项之间为“与 " 关系 
    matchLabels <map[string]string> #map 格局的标签选择器; 和之前 SVC 的标签选择器为 selector: 相似
  template: #Pod 模板对象
    metadata: #Pod 对象元数据
      labels: #由模板创立出的 Pod 对象所领有的标签,必须要可能匹配后面定义的标签选择器
      spec:# Pod 标准, 格局同自主式 Pod
       ......

示例 1:创 ReplicaSet 控制器

[root@k8s-master PodControl]# cat replicaset.demo.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replicaset-demo
spec:
  minReadySeconds: 3
  replicas: 2
  selector:
    matchLabels:
      app: demoapp
      release: stable
      version: v1.0
  template:
    metadata:
      labels:   #这里的标签要能覆盖住下面的标签, 能够比下面多但不能比下面少, 因为实践上控制器如果匹配不到 Pod 控制器会有限创立 Pod, 理论是建创时会报错
        app: demoapp
        release: stable
        version: v1.0
        test: test22
    spec:
      containers:
      - name: demoapp
        image: ikubernetes/demoapp:v1.0
        ports:
        - name: http
          containerPort: 80
        livenessProbe:
          httpGet:
            path: '/livez'
            port: 80
          initialDelaySeconds : 10
        readinessProbe:
          httpGet:
            path: '/readyz'
            port: 80
          initialDelaySeconds: 15


[root@k8s-master PodControl]# kubectl get replicaset -o wide
NAME                    DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                     SELECTOR
replicaset-demo         2         2         2       30m   demoapp      ikubernetes/demoapp:v1.0   app=demoapp,release=stable,version=v1.0

[root@k8s-master PodControl]# kubectl get pod -o wide
NAME                          READY   STATUS    RESTARTS   AGE   IP             NODE        NOMINATED NODE   READINESS GATES
my-grafana-7d788c5479-4ztb7   1/1     Running   0          44m   10.244.1.208   k8s-node1   <none>           <none>
replicaset-demo-b4hx9         1/1     Running   0          34m   10.244.1.210   k8s-node1   <none>           <none>
replicaset-demo-zb7m5         1/1     Running   1          34m   10.244.2.203   k8s-node2   <none>           <none>

#查看形容信息
[root@k8s-master PodControl]# kubectl describe replicasets/replicaset-demo
Name:         replicaset-demo
Namespace:    default
Selector:     app=demoapp,release=stable,version=v1.0
Labels:       <none>
Annotations:  <none>
Replicas:     2 current / 2 desired  #当初运行 /  冀望运行  
Pods Status:  2 Running / 0 Waiting / 0 Succeeded / 0 Failed  #Pod 状态
Pod Template:
  Labels:  app=demoapp
           release=stable
           test=test22
           version=v1.0
  Containers:
   demoapp:
    Image:        ikubernetes/demoapp:v1.0
    Port:         80/TCP
    Host Port:    0/TCP
    Liveness:     http-get http://:80/livez delay=10s timeout=1s period=10s #success=1 #failure=3
    Readiness:    http-get http://:80/readyz delay=15s timeout=1s period=10s #success=1 #failure=3
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                   Message
  ----    ------            ----   ----                   -------
  Normal  SuccessfulCreate  8m28s  replicaset-controller  Created pod: replicaset-demo-b4hx9
  Normal  SuccessfulCreate  8m28s  replicaset-controller  Created pod: replicaset-demo-zb7m5


[root@k8s-master PodControl]# kubectl get pod replicaset-demo-zb7m5 -o yaml
...
ownerReferences:     #查看 Pod 受哪个管理器治理
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: replicaset-demo
    uid: d6525139-a5a5-4886-9c2b-d7cfe2db6d35
  resourceVersion: "8790580"
  selfLink: /api/v1/namespaces/default/pods/replicaset-demo-zb7m5
...
  • ReplicaSet 更新 可应用 kubectl set 命令更新
[root@k8s-master PodControl]# kubectl set image --help   #查看 set 更新的相干案例
Update existing container image(s) of resources.

 Possible resources include (case insensitive):

  pod (po), replicationcontroller (rc), deployment (deploy), daemonset (ds), replicaset (rs)

Examples:
  # Set a deployment's nginx container image to'nginx:1.9.1', and its busybox container image to'busybox'.
  kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1
  
  # Update all deployments'and rc's nginx container's image to'nginx:1.9.1'
  kubectl set image deployments,rc nginx=nginx:1.9.1 --all
  
  # Update image of all containers of daemonset abc to 'nginx:1.9.1'
  kubectl set image daemonset abc *=nginx:1.9.1
  
  # Print result (in yaml format) of updating nginx container image from local file, without hitting
the server
  • 更新 image 镜像
[root@k8s-master PodControl]# kubectl set image replicasets/replicaset-demo demoapp=ikubernetes/demoapp:v1.1   #更新 replicasets 中镜像版本为 v1.1
replicaset.apps/replicaset-demo image updat

[root@k8s-master PodControl]# kubectl get pod -o wide
NAME                          READY   STATUS    RESTARTS   AGE   IP             NODE        NOMINATED NODE   READINESS GATES
my-grafana-7d788c5479-4ztb7   1/1     Running   0          44m   10.244.1.208   k8s-node1   <none>           <none>
replicaset-demo-b4hx9         1/1     Running   0          34m   10.244.1.210   k8s-node1   <none>           <none>
replicaset-demo-zb7m5         1/1     Running   1          34m   10.244.2.203   k8s-node2   <none>           <none>

[root@k8s-master PodControl]# kubectl get replicaset/replicaset-demo -o yaml|grep image
      ...
      - image: ikubernetes/demoapp:v1.1
        imagePullPolicy: IfNotPresent

#能够看到 replicaset 中的镜像信息曾经更新胜利
[root@k8s-master PodControl]# kubectl get pods replicaset-demo-zb7m5 -o yaml  

containerStatuses:
  - containerID: docker://4c4e576d7f5accb89c4f73a0aef18bd99cac37ee81b34a07a363d2afc35d5189
    image: ikubernetes/demoapp:v1.0
    imageID: docker-pullable://ikubernetes/demoapp@sha256:6698b205eb18fb0171398927f3a35fe27676c6bf5757ef57a35a4b055badf2c3

#但 Pod 中的 image 并没有更新
[root@k8s-master PodControl]# curl 10.244.1.210  
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: replicaset-demo-b4hx9, ServerIP: 10.244.1.210!
[root@k8s-master PodControl]# curl 10.244.1.210
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: replicaset-demo-b4hx9, ServerIP: 10.244.1.210!

#理论拜访 Pod 也并没有更新胜利 这是因为 ReplicaSet 为删除式更新 Pod 删除重启后才会失效
[root@k8s-master PodControl]# kubectl delete pod -l app=demoapp,release=stable
pod "replicaset-demo-b4hx9" deleted
pod "replicaset-demo-zb7m5" deleted

[root@k8s-master PodControl]# kubectl get pod -o wide
NAME                          READY   STATUS    RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
my-grafana-7d788c5479-4ztb7   1/1     Running   0          57m     10.244.1.208   k8s-node1   <none>           <none>
replicaset-demo-686fl         0/1     Running   1          2m28s   10.244.1.212   k8s-node1   <none>           <none>
replicaset-demo-k4pq9         0/1     Running   0          2m28s   10.244.3.49    k8s-node3   <none>           <none>

[root@k8s-master PodControl]# curl 10.244.3.49   #更新胜利
iKubernetes demoapp v1.1 !! ClientIP: 10.244.0.0, ServerName: replicaset-demo-k4pq9, ServerIP: 10.244.3.49!
[root@k8s-master PodControl]# curl 10.244.3.49  
iKubernetes demoapp v1.1 !! ClientIP: 10.244.0.0, ServerName: replicaset-demo-k4pq9, ServerIP: 10.244.3.49

服务部署 蓝绿公布、滚动更新

Pod 部署类型:
蓝绿公布: 在旧版本的根底上 在新建一套新版本

     长处: 回滚不便 能够把新旧版本齐全切换 
     毛病: 因为须要从新创立一套服务, 更新中须要会占用更多的系统资源, 切换途中流量可能会中断

滚动更新: 新增一个新版 Pod 在删除一个旧版 Pod; 或删除一个旧版 Pod 在新增一个新版 Pod

     长处: 降级过程对系统资源须要更少; 能够保障流量不间断
     毛病: 新旧版本同时存在, 回滚麻烦

示例 2: 滚动公布

[root@k8s-master PodControl]# cat replicaset.demo.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replicaset-demo
spec:
  minReadySeconds: 3
  replicas: 2
  selector:
    matchLabels:
      app: demoapp
      release: stable
      version: v1.0
  template:
    metadata:
      labels:   
        app: demoapp
        release: stable
        version: v1.0
        test: test22
    spec:
      containers:
      - name: demoapp
        image: ikubernetes/demoapp:v1.0
        ports:
        - name: http
          containerPort: 80

[root@k8s-master PodControl]# cat service-for-replicaset-demo.yaml   #创立对应的 svc 留神 SVC 标签须要能同时匹配新旧版本
apiVersion: v1
kind: Service
metadata:
  name: demoapp
  namespace: default
spec:
  type: ClusterIP
  clusterIP: 10.97.26.1
  selector:    #滚动降级 SVC 应用的标签须要能同时匹配新旧版本
    app: demoapp
    release: stable
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80

[root@k8s-master PodControl]# kubectl apply -f replicaset.demo.yaml  -f service-for-replicaset-demo.yaml

[root@k8s-master PodControl]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
pod-18865               1/1     Running   0          16m
replicaset-demo-d8jm9   1/1     Running   0          2m14s
replicaset-demo-xtz2n   1/1     Running   0          2m14s


iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: replicaset-demo-d8jm9, ServerIP: 10.244.1.218!
[root@k8s-master PodControl]# curl 10.97.26.1
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: replicaset-demo-xtz2n, ServerIP: 10.244.2.204!
[root@k8s-master PodControl]# curl 10.97.26.1
  • 别起一个终端创立 Pod 用作测试
[root@k8s-master ~]# kubectl run pod-$RANDOM --image=ikubernetes/admin-box:latest -it --rm --command -- /bin/sh   #创立一个 Pod 用作测试
root@pod-18865 # while true; do curl --connect-timeout 1 demoapp.default.svc; sleep .2;done      #拜访测试 调度策略为轮询 1.0 版本
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-xtz2n, ServerIP: 10.244.2.204!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-d8jm9, ServerIP: 10.244.1.218!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-xtz2n, ServerIP: 10.244.2.204!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-d8jm9, ServerIP: 10.244.1.218!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-xtz2n, ServerIP: 10.244.2.204!
  • 新增 ReplicaSet 应用 ikubernetes/demoapp:v1.1 版本
[root@k8s-master PodControl]# cat replicaset.demo-v1.1.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replicaset-demo-v1.1
spec:
  minReadySeconds: 3
  replicas: 1
  selector:
    matchLabels:
      app: demoapp
      release: stable
      version: v1.1
  template:
    metadata:
      labels:
        app: demoapp
        release: stable
        version: v1.1
        test: test22
    spec:
      containers:
      - name: demoapp
        image: ikubernetes/demoapp:v1.1
        ports:
        - name: http
          containerPort: 80

          
[root@k8s-master PodControl]# kubectl apply -f replicaset.demo-v1.1.yaml 
replicaset.apps/replicaset-demo-v1.1 created

[root@k8s-master PodControl]# kubectl get pod
NAME                         READY   STATUS    RESTARTS   AGE
pod-18865                    1/1     Running   0          26m
replicaset-demo-d8jm9        1/1     Running   1          12m
replicaset-demo-v1.1-dhmlq   1/1     Running   0          21s
replicaset-demo-xtz2n        1/1     Running   0          12m

  • 查看测试后果 SVC 曾经绑定到 1.1 版本 新旧版本并存
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-v1.1-dhmlq, ServerIP: 10.244.1.219!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-d8jm9, ServerIP: 10.244.1.218!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-xtz2n, ServerIP: 10.244.2.204!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-v1.1-dhmlq, ServerIP: 10.244.1.219!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-d8jm9, ServerIP: 10.244.1.218!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-xtz2n, ServerIP: 10.244.2.204!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-v1.1-dhmlq, ServerIP: 10.244.1.219!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-d8jm9, ServerIP: 10.244.1.218!
  • 批改老版本正本数
[root@k8s-master PodControl]# kubectl edit rs replicaset-demo
spec:
  minReadySeconds: 3
  replicas: 1
  selector:

[root@k8s-master PodControl]# kubectl get pod
NAME                         READY   STATUS    RESTARTS   AGE
pod-18865                    1/1     Running   0          30m
replicaset-demo-v1.1-dhmlq   1/1     Running   0          6m22s
replicaset-demo-xtz2n        1/1     Running   0          16m

  • 查看测试后果 新旧版本并存 访问量各占一半
root@pod-18865 # while true; do curl --connect-timeout 1 demoapp.default.svc; sleep .2;done
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-xtz2n, ServerIP: 10.244.2.204!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-v1.1-dhmlq, ServerIP: 10.244.1.219!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-xtz2n, ServerIP: 10.244.2.204!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-v1.1-dhmlq, ServerIP: 10.244.1.219!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-xtz2n, ServerIP: 10.244.2.204!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-v1.1-dhmlq, ServerIP: 10.244.1.219!
  • 持续更新 新版本新增一个 Pod 老版本删一个 Pod
[root@k8s-master PodControl]# kubectl edit rs replicaset-demo-v1.1  #新版本正本数批改为 2
replicaset.apps/replicaset-demo-v1.1 edited

[root@k8s-master PodControl]# kubectl edit rs replicaset-demo  #老版本正本数为 0
replicaset.apps/replicaset-demo edited

[root@k8s-master PodControl]# kubectl get pod
NAME                         READY   STATUS    RESTARTS   AGE
pod-18865                    1/1     Running   0          34m
replicaset-demo-v1.1-bnck8   1/1     Running   0          80s
replicaset-demo-v1.1-dhmlq   1/1     Running   0          10m
  • 查看测试后果 更新版本实现
Kubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-v1.1-bnck8, ServerIP: 10.244.3.50!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-v1.1-dhmlq, ServerIP: 10.244.1.219!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-v1.1-bnck8, ServerIP: 10.244.3.50!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: replicaset-demo-v1.1-dhmlq, ServerIP: 10.244.1.219!

示例 3: 蓝绿公布

envsubst 应用环境变量替换文件内容 模板复用
  • 应用 envsubst 命令 envsubst 替换文件内容, 默认 kubernetes、YAML 都是不反对变量的
[root@k8s-master PodControl]# cat replicaset-blue-gree.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-${DEPLOY} #为了使模板能复用 应用变量
spec:
  minReadySeconds: 3
  replicas: 2
  selector:
    matchLabels:
      app: demoapp
      ctr: rs-${DEPLOY}
      version: ${VERSION}
  template:
    metadata:
      labels:
        app: demoapp
        ctr: rs-${DEPLOY}
        version: ${VERSION}
    spec:
      containers:
      - name: demoapp
        image: ikubernetes/demoapp:${VERSION}
        ports:
        - name: http
          containerPort: 80

[root@k8s-master PodControl]# DEPLOY=blue VERSION=v1.0 envsubst < replicaset-blue-gree.yaml #替换变量
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-blue #利用变量值
spec:
  minReadySeconds: 3
  replicas: 2
  selector:
    matchLabels:
      app: demoapp
      ctr: rs-blue
      version: v1.0
  template:
    metadata:
      labels:
        app: demoapp
        ctr: rs-blue
        version: v1.0
    spec:
      containers:
      - name: demoapp
        image: ikubernetes/demoapp:v1.0
        ports:
        - name: http
          containerPort: 80

[root@k8s-master PodControl]# DEPLOY=blue VERSION=v1.0 envsubst < replicaset-blue-gree.yaml |kubectl apply -f -   #创立 SVC

[root@k8s-master PodControl]# cat replicaset-blue-gree.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-${DEPLOY} #为了使模板能复用 应用变量
spec:
  minReadySeconds: 3
  replicas: 2
  selector:
    matchLabels:
      app: demoapp
      ctr: rs-${DEPLOY}
      version: ${VERSION}
  template:
    metadata:
      labels:
        app: demoapp
        ctr: rs-${DEPLOY}
        version: ${VERSION}
    spec:
      containers:
      - name: demoapp
        image: ikubernetes/demoapp:${VERSION}
        ports:
        - name: http
          containerPort: 80

[root@k8s-master PodControl]# DEPLOY=blue VERSION=v1.0 envsubst < service-blue-green.yaml |kubectl apply -f -
service/demoapp-svc created

[root@k8s-master PodControl]# kubectl get svc
NAME          TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
demoapp       ClusterIP   10.97.26.1     <none>        80/TCP    113m
demoapp-svc   ClusterIP   10.99.170.77   <none>        80/TCP    20s
kubernetes    ClusterIP   10.96.0.1      <none>        443/TCP   11
  • 测试
root@pod-18865 # while true; do curl --connect-timeout 1 demoapp-svc.default.svc; sleep .2;done
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-gwnw7, ServerIP: 10.244.2.205!   #能够看到全副来自 rs-blue
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-28lgc, ServerIP: 10.244.1.220!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-gwnw7, ServerIP: 10.244.2.205!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-28lgc, ServerIP: 10.244.1.220!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-gwnw7, ServerIP: 10.244.2.205!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-28lgc, ServerIP: 10.244.1.220!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-gwnw7, ServerIP: 10.244.2.205!
  • 新增 rs 用于更新公布
[root@k8s-master PodControl]# DEPLOY=green  VERSION=v1.1 envsubst < replicaset-blue-gree.yaml |kubectl apply -f -
replicaset.apps/rs-green created

[root@k8s-master PodControl]# kubectl get rs
NAME                   DESIRED   CURRENT   READY   AGE
replicaset-demo        0         0         0       92m
replicaset-demo-v1.1   2         2         2       81m
rs-blue                2         2         2       13m
rs-green               2         2         0       8s


[root@k8s-master PodControl]# kubectl get pod  #等 Pod 全副准备就绪
NAME                         READY   STATUS    RESTARTS   AGE
pod-18865                    1/1     Running   0          106m
replicaset-demo-v1.1-bnck8   1/1     Running   0          73m
replicaset-demo-v1.1-dhmlq   1/1     Running   0          82m
rs-blue-28lgc                1/1     Running   0          14m
rs-blue-gwnw7                1/1     Running   0          14m
rs-green-d4t66               1/1     Running   0          40s
rs-green-l75j7               1/1     Running   0          40s

[root@k8s-master PodControl]# DEPLOY=green  VERSION=v1.1 envsubst < service-blue-green.yaml |kubectl apply -f -
service/demoapp-svc configured
  • 查看测试后果
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-28lgc, ServerIP: 10.244.1.220!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-gwnw7, ServerIP: 10.244.2.205!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-28lgc, ServerIP: 10.244.1.220!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-gwnw7, ServerIP: 10.244.2.205!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-28lgc, ServerIP: 10.244.1.220!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-gwnw7, ServerIP: 10.244.2.205!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-28lgc, ServerIP: 10.244.1.220!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-gwnw7, ServerIP: 10.244.2.205!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-l75j7, ServerIP: 10.244.3.52!  #实现版本更新
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-d4t66, ServerIP: 10.244.1.222!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-l75j7, ServerIP: 10.244.3.52!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-d4t66, ServerIP: 10.244.1.222!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-l75j7, ServerIP: 10.244.3.52!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-d4t66, ServerIP: 10.244.1.222!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-l75j7, ServerIP: 10.244.3.52!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-d4t66, ServerIP: 10.244.1.222!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-l75j7, ServerIP: 10.244.3.52!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-d4t66, ServerIP: 10.244.1.222!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-l75j7, ServerIP: 10.244.3.52!
  • 批改变量 版本回退
[root@k8s-master PodControl]# DEPLOY=blue  VERSION=v1.0  envsubst < service-blue-green.yaml |kubectl apply -f -
service/demoapp-svc configured
#查看测试后果
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-l75j7, ServerIP: 10.244.3.52!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-d4t66, ServerIP: 10.244.1.222!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-l75j7, ServerIP: 10.244.3.52!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-d4t66, ServerIP: 10.244.1.222!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-l75j7, ServerIP: 10.244.3.52!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-d4t66, ServerIP: 10.244.1.222!
iKubernetes demoapp v1.1 !! ClientIP: 10.244.1.217, ServerName: rs-green-l75j7, ServerIP: 10.244.3.52!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-gwnw7, ServerIP: 10.244.2.205!  #实现版本回退
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-28lgc, ServerIP: 10.244.1.220!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-gwnw7, ServerIP: 10.244.2.205!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-28lgc, ServerIP: 10.244.1.220!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-gwnw7, ServerIP: 10.244.2.205!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-28lgc, ServerIP: 10.244.1.220!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.217, ServerName: rs-blue-gwnw7, ServerIP: 10.244.2.205!

正文完
 0