不同namespace的pod反亲和配置

简介: 不同namespace的pod反亲和配置

业务需求 两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状态

目录
相关文章
|
4天前
|
Prometheus Kubernetes 监控
Kubernetes 的 NameSpace 无法删除应该怎么办?
Kubernetes 的 NameSpace 无法删除应该怎么办?
|
5月前
|
Kubernetes Nacos 数据中心
k8s(9)Namespace(命名空间)
Namespace(命名空间)
115 0
|
7月前
|
编译器 C语言 C++
【C++】命名空间 ( namespace )
【C++】命名空间 ( namespace )
|
4天前
|
编译器 数据安全/隐私保护 C语言
C++ 之命名空间namespace【详解】
C++ 之命名空间namespace【详解】
|
9月前
|
JSON Kubernetes 数据中心
k8s--命名空间 namespaces
k8s--命名空间 namespaces
|
5月前
|
存储 Kubernetes Docker
volume 、namespace
volume 、namespace
|
Kubernetes 容器 Perl
kubernetes强制删除pod、namespace等资源
kubernetes强制删除pod、namespace等资源
790 0
|
9月前
|
Kubernetes 负载均衡 数据中心
K8s学习笔记(3)——Namespace,Pod,Label,Deployment,Service
K8s学习笔记(3)——Namespace,Pod,Label,Deployment,Service
117 1
|
10月前
|
JSON Kubernetes 数据中心
05-Kubernetes-Namespace入门
05-Kubernetes-Namespace入门
05-Kubernetes-Namespace入门
|
10月前
|
编译器 程序员 C++
【C++】--- namespace命名空间
【C++】--- namespace命名空间
61 0