一、拓扑域划分示例

k8s跨区域部署架构 k8s跨机房部署_机柜

二、使用Topology实现多地多机房部署

### --- 使用Topology实现多机柜部署
~~~     模拟一下5个节点是属于同一个机房不同机柜的,为其打标签

[root@k8s-master01 ~]# kubectl label node k8s-master01 k8s-node01 jigui=1
node/k8s-master01 labeled
node/k8s-node01 labeled
[root@k8s-master01 ~]# kubectl label node k8s-master02 k8s-node02 jigui=2
node/k8s-master02 labeled
node/k8s-node02 labeled
[root@k8s-master01 ~]# kubectl label node k8s-master03 jigui=3
node/k8s-master03 labeled
### --- 改成机柜的来划分拓扑域
~~~     是按照机柜来划分的拓扑域,在这个机柜里面,只能部署一个k8s-app=demo-nginx这个应用。

[root@k8s-master01 ~]# kubectl edit deploy demo-nginx
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - demo-nginx
            topologyKey: jigui
### --- replace容器,查看pod部署位置
~~~     之前的2个删除掉之后,pending状态的才可以被部署掉。
~~~     因为根据刚才打的标签,k8s-master01和k8s-node01;
~~~     k8s-master02和k8s-node02,k8s-master03分别都是一个拓扑域,
~~~     现在每个拓扑域上都有一个pod了,所以说这个Pending状态的demo-nginx就没有地方去部署了,
~~~     因为每个拓扑域已经部署了一个
~~~     在Terminating状态删除掉之后,它就会部署在其它的节点。
~~~     但是不能是k8s-master02节点,因为k8s-master02已经占据了一个拓扑域,
~~~     它只能部署在k8s-master02所在拓扑域之外的拓扑域。
[root@k8s-master01 ~]# kubectl get po -owide
NAME                          READY   STATUS        RESTARTS   AGE   IP              NODE           NOMINATED NODE   READINESS GATES
busybox                       1/1     Running       3          13h   172.25.92.90    k8s-master02   <none>           <none>
demo-nginx-5cb4fd6867-2c6d8   0/2     Pending       0          24s   <none>          <none>         <none>           <none>
demo-nginx-5cb4fd6867-wg4hc   2/2     Running       0          25s   172.25.92.93    k8s-master02   <none>           <none>
demo-nginx-7c464c79f7-hg4rm   0/2     Terminating   0          12h   <none>          k8s-master03   <none>           <none>
demo-nginx-7c464c79f7-v7pmw   2/2     Terminating   0          12h   172.17.125.35   k8s-node01     <none>           <none>
### --- 查看pod部署位置
~~~     部署在了k8s-master02和k8s-master03节点上

[root@k8s-master01 ~]# kubectl get po -owide
NAME                          READY   STATUS    RESTARTS   AGE     IP              NODE           NOMINATED NODE   READINESS GATES
busybox                       1/1     Running   3          13h     172.25.92.90    k8s-master02   <none>           <none>
demo-nginx-5cb4fd6867-2c6d8   2/2     Running   0          5m14s   172.18.195.43   k8s-master03   <none>           <none>
demo-nginx-5cb4fd6867-wg4hc   2/2     Running   0          5m15s   172.25.92.93    k8s-master02   <none>           <none>

三、把容器扩充到4个节点,验证部署结果

### --- 为容器扩容至4个节点,因为我们只有3个拓扑域,

[root@k8s-master01 ~]# kubectl scale deploy demo-nginx --replicas=4
deployment.apps/demo-nginx scaled
### --- 查看效果
~~~     每个机柜只能跑一个,根据机柜互斥的原则,只能跑一个,所以第四个pending状态了

[root@k8s-master01 ~]# kubectl get po -owide
NAME                          READY   STATUS    RESTARTS   AGE   IP               NODE           NOMINATED NODE   READINESS GATES
busybox                       1/1     Running   4          13h   172.25.92.90     k8s-master02   <none>           <none>
demo-nginx-5cb4fd6867-2c6d8   2/2     Running   0          13m   172.18.195.43    k8s-master03   <none>           <none>
demo-nginx-5cb4fd6867-wg4hc   2/2     Running   0          13m   172.25.92.93     k8s-master02   <none>           <none>
demo-nginx-5cb4fd6867-wjjzz   0/2     Pending   0          15s   <none>           <none>         <none>           <none>
demo-nginx-5cb4fd6867-zbw7w   2/2     Running   0          15s   172.25.244.198   k8s-master01   <none>           <none>
### --- 把互斥改成软互斥,查看状态
~~~     因为配置的是软亲和力,不是强一致性的,
~~~     所以第四个显示pending状态的容器会漂移到其它的节点上面。
~~~     改写为软亲和力,也就是说一个拓扑域是可以部署2个容器的

[root@k8s-master01 ~]# kubectl edit deploy demo-nginx
     spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:   // 将会 required改成preferred软互斥
          - weight: 1
            podAffinityTerm:                                 // 添加软亲和力参数
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - demo-nginx
              topologyKey: jigui
### --- 查看状态 

[root@k8s-master01 ~]# kubectl get po -owide
NAME                          READY   STATUS    RESTARTS   AGE     IP               NODE           NOMINATED NODE   READINESS GATES
busybox                       1/1     Running   4          13h     172.25.92.90     k8s-master02   <none>           <none>
demo-nginx-5cb4fd6867-2c6d8   2/2     Running   0          34m     172.18.195.43    k8s-master03   <none>           <none>
demo-nginx-5cb4fd6867-wg4hc   2/2     Running   0          34m     172.25.92.93     k8s-master02   <none>           <none>
demo-nginx-5cb4fd6867-zbw7w   2/2     Running   0          20m     172.25.244.198   k8s-master01   <none>           <none>
demo-nginx-6bdcdcc585-d56r8   0/2     Pending   0          3m51s   <none>           <none>         <none>           <none>
demo-nginx-6bdcdcc585-mhfhl   0/2     Pending   0          63s     <none>           <none>         <none>           <none>
### --- 因为有更新环节,所以有2个Pod显示是pending状态,把节点数量设置为0,重新触发容器

[root@k8s-master01 ~]# kubectl scale deploy demo-nginx --replicas=0
deployment.apps/demo-nginx scaled
### --- 重新设置为4个pod,验证部署结果

[root@k8s-master01 ~]# kubectl scale deploy demo-nginx --replicas=4
deployment.apps/demo-nginx scaled
~~~     # 可以看到k8s-master01部署了2个pod,
~~~     它尽量的不部署在一块,但是若是条件满足不了,但是还是会部署在一块的。

[root@k8s-master01 ~]# kubectl get po -owide
NAME                          READY   STATUS    RESTARTS   AGE   IP               NODE           NOMINATED NODE   READINESS GATES
busybox                       1/1     Running   4          13h   172.25.92.90     k8s-master02   <none>           <none>
demo-nginx-6bdcdcc585-5wrx9   2/2     Running   0          46s   172.25.92.94     k8s-master02   <none>           <none>
demo-nginx-6bdcdcc585-cjcm2   2/2     Running   0          46s   172.18.195.44    k8s-master03   <none>           <none>
demo-nginx-6bdcdcc585-mpvfs   2/2     Running   0          45s   172.25.244.201   k8s-master01   <none>           <none>
demo-nginx-6bdcdcc585-zc4px   2/2     Running   0          46s   172.25.244.199   k8s-master01   <none>           <none>

 

 

 

 

 

 

 

 

 


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart

                                                                                                                                                   ——W.S.Landor