TIPS之 Kubernetes Pod 均匀分布在各节点上

Kubernetes Pod 均匀分布在各节点上

Posted by 董江 on Friday, July 26, 2024

Kubernetes Pod 均匀分布在各节点上

背景

线上单个deployment副本数有100个, Node节点有十几个。问题是:有部分节点部署了30+个pod副本数,导致这个VM故障,有30%+的pod同时北驱逐

方案

pod 再现有的 node几点上分布不均匀。 可以添加对自己的反亲和。 由于Node数不够多,只能通过软反亲和.

  spec:
    affinity:
      podAntiAffinity:
        preferredDuringSchedulingIgnoredDuringExecution: #软反亲和
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                  - key: app
                    operator: In
                    values:
                      - <yourself-name>
              topologyKey: "kubernetes.io/hostname"

验证

5台集群,32个pod

部署应用

$ vi ng.yaml 

apiVersion: apps/v1
kind: Deployment
metadata:
 name: nginx
 namespace: ceph-nbd
spec:
 replicas: 32
 selector:
  matchLabels:
   app: nginx
 template:
  metadata:
   labels:
    app: nginx
  spec:
    affinity:
      podAntiAffinity:
        preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                  - key: app
                    operator: In
                    values:
                      - nginx
              topologyKey: "kubernetes.io/hostname"
    containers:
    - name: nginx
      image:  nginx:latest
      ports:
      - containerPort: 80
        name: web

部署:

$ kubectl apply -f ng.yaml 
deployment.apps/nginx created

$ kubectl get pod -o wide | grep "nginx"
nginx-587f6ccdd6-468dw                       1/1     Running     0              59s     100.77.200.172   master-172-30-214-17   <none>           <none>
nginx-587f6ccdd6-6msmx                       1/1     Running     0              59s     100.75.16.30     master-172-30-214-18   <none>           <none>
nginx-587f6ccdd6-7qdzt                       1/1     Running     0              59s     100.67.56.238    master-172-30-214-16   <none>           <none>
nginx-587f6ccdd6-8jlcl                       1/1     Running     0              59s     100.77.200.138   master-172-30-214-17   <none>           <none>
nginx-587f6ccdd6-8m6ss                       1/1     Running     0              59s     100.68.245.172   node-172-30-214-20     <none>           <none>
nginx-587f6ccdd6-9pkbt                       1/1     Running     0              59s     100.79.44.62     node-172-30-214-19     <none>           <none>
nginx-587f6ccdd6-c5xgw                       1/1     Running     0              59s     100.77.200.152   master-172-30-214-17   <none>           <none>
nginx-587f6ccdd6-dmstm                       1/1     Running     0              59s     100.68.245.131   node-172-30-214-20     <none>           <none>
nginx-587f6ccdd6-f2cjl                       1/1     Running     0              59s     100.75.16.27     master-172-30-214-18   <none>           <none>
nginx-587f6ccdd6-fm6jq                       1/1     Running     0              59s     100.67.56.206    master-172-30-214-16   <none>           <none>
nginx-587f6ccdd6-gpmzg                       1/1     Running     0              59s     100.75.16.29     master-172-30-214-18   <none>           <none>
nginx-587f6ccdd6-j4dsx                       1/1     Running     0              59s     100.77.200.186   master-172-30-214-17   <none>           <none>
nginx-587f6ccdd6-jrbvw                       1/1     Running     0              59s     100.79.44.13     node-172-30-214-19     <none>           <none>
nginx-587f6ccdd6-k8fdk                       1/1     Running     0              59s     100.68.245.145   node-172-30-214-20     <none>           <none>
nginx-587f6ccdd6-k9cxq                       1/1     Running     0              59s     100.79.44.46     node-172-30-214-19     <none>           <none>
nginx-587f6ccdd6-krlwq                       1/1     Running     0              59s     100.75.16.26     master-172-30-214-18   <none>           <none>
nginx-587f6ccdd6-kslhn                       1/1     Running     0              59s     100.67.56.229    master-172-30-214-16   <none>           <none>
nginx-587f6ccdd6-lxtbs                       1/1     Running     0              59s     100.75.16.28     master-172-30-214-18   <none>           <none>
nginx-587f6ccdd6-m5958                       1/1     Running     0              59s     100.68.245.158   node-172-30-214-20     <none>           <none>
nginx-587f6ccdd6-mqszb                       1/1     Running     0              59s     100.79.44.23     node-172-30-214-19     <none>           <none>
nginx-587f6ccdd6-mzg2h                       1/1     Running     0              59s     100.75.16.31     master-172-30-214-18   <none>           <none>
nginx-587f6ccdd6-n7t8m                       1/1     Running     0              59s     100.68.245.137   node-172-30-214-20     <none>           <none>
nginx-587f6ccdd6-n8s4g                       1/1     Running     0              59s     100.77.200.174   master-172-30-214-17   <none>           <none>
nginx-587f6ccdd6-p7cxl                       1/1     Running     0              59s     100.68.245.191   node-172-30-214-20     <none>           <none>
nginx-587f6ccdd6-qm8qh                       1/1     Running     0              59s     100.77.200.181   master-172-30-214-17   <none>           <none>
nginx-587f6ccdd6-rfvb5                       1/1     Running     0              59s     100.67.56.210    master-172-30-214-16   <none>           <none>
nginx-587f6ccdd6-s6tpp                       1/1     Running     0              59s     100.79.44.40     node-172-30-214-19     <none>           <none>
nginx-587f6ccdd6-szpgh                       1/1     Running     0              59s     100.75.16.25     master-172-30-214-18   <none>           <none>
nginx-587f6ccdd6-t558b                       1/1     Running     0              59s     100.67.56.240    master-172-30-214-16   <none>           <none>
nginx-587f6ccdd6-whcgc                       1/1     Running     0              59s     100.79.44.53     node-172-30-214-19     <none>           <none>
nginx-587f6ccdd6-wrncq                       1/1     Running     0              59s     100.67.56.231    master-172-30-214-16   <none>           <none>
nginx-587f6ccdd6-zb28v                       1/1     Running     0              59s     100.67.56.252    master-172-30-214-16   <none>           <none>

部署均衡验证:

$ kubectl get pod -o wide | grep "nginx" | awk -F ' ' '{print $7}' | sort | uniq -c
      7 master-172-30-214-16
      6 master-172-30-214-17
      7 master-172-30-214-18
      6 node-172-30-214-19
      6 node-172-30-214-20

「如果这篇文章对你有用,请随意打赏」

Kubeservice博客

如果这篇文章对你有用,请随意打赏

使用微信扫描二维码完成支付