业务需求 两pod互斥,增加系统健壮性
pod1:
-name:prometheus-dep -namespace:prometheus
pod2:
-name:prometheus-server -namespace:prometheus-server-dep
测试结论
查看两个pod的label 备用
kubectl -n <ns> describe pods <pods-name> |grep Labels
pod1.yaml
配置模块均配置对方pod的信息
apiVersion: apps/v1 kind: Deployment metadata: name: mynginx-1 namespace: dev-1 spec: replicas: 2 selector: matchLabels: app: mynginx-1 template: metadata: labels: app: mynginx-1 containers: - name: mycontainer-1 image: nginx imagePullPolicy: IfNotPresent ports: - name: nginx containerPort: 80 volumeMounts: - mountPath: "/usr/share/nginx/html/" name: nginx-vol affinity: ##调度策略配置模块 podAntiAffinity: ##类型pod反亲和 requiredDuringSchedulingIgnoredDuringExecution: ##硬需求 - labelSelector: ##标签匹配 matchExpressions: - key: app ##刚刚查到另一个pod的标签 key值 operator: In ##匹配规则 values: [mynginx-test] ##对应的value值 topologyKey: kubernetes.io/hostname ##固定字段控制域 namespaces: [dev-2] ##互斥pod的名存空间
pod2.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: mynginx-test namespace: dev-2 spec: replicas: 2 selector: matchLabels: app: mynginx-test template: metadata: labels: app: mynginx-test spec: containers: - name: mycontainer-2 image: nginx imagePullPolicy: IfNotPresent ports: - name: nginx containerPort: 80 volumeMounts: - mountPath: "/usr/share/nginx/html/" name: nginx-vol affinity: podAntiAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: app ##对应标签的key operator: In values: [mynginx-1] ##对应标签的value topologyKey: kubernetes.io/hostname namespaces: [dev-1] ##对方pod的名称空间 volumes: - name: nginx-vol hostPath: path: /data/svc/web1-html
这样两个pod都配置互斥的好处是:不会因为启动顺序而失效,无论谁先启动都会检测 分配到合适的node
注意:唯一缺陷
当dep启动的pod 副本数大于或等于 集群 node数量时,可能会出现每个pod分布在每个node节点上的情况,导致另一个pod 不能分配工作节点,一直处于pending状态