kubernetes每個節點創建一個服務的Pod

1. 問題場景

希望一個worker節點上僅部署同樣的服務一個. 比如: kubernets有三個worker節點,三個節點部署N個副本的api服務, 爲了提高服務效率希望加入緩存,需要爲三個節點個部署一個redis服務, 這個時候我們定義這個redis服務副本爲3, 但是如何設置一個節點僅部署一個redis服務呢?

2. 相關知識

  • 親和性

    k8s設計了親和性來配置pod和node, pod和pod的關係, 即:

  • 思路

    pod和node關係解決的pod選擇node的場景, 這裏不是解決本問題的關鍵, pod和pod的關係纔是解決的關鍵, 只要讓服務副本設置爲3, 且每個node上redis的pod是互斥或者排他的選擇即可, 如何設置同一個node上的pod互斥呢? 通過Inter-pod affinity and anti-affinity 可以知道需要指定: topologyKey

  • 具體配置結果

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: redis
    spec:
      nodeSelector:
        matchLabels:
          service: mirage
      selector:
        matchLabels:
          service: redis
      replicas: 3
      template:
        metadata:
          labels:
            service: redis
        spec:
          affinity:
            podAntiAffinity:
              preferredDuringSchedulingIgnoredDuringExecution:
              - weight: 100
                podAffinityTerm:
                  labelSelector:
                    matchExpressions:
                    - key: service
                      operator: In
                      values:
                        - redis
                  topologyKey: kubernetes.io/hostname
          containers:
            - name: redis
              image: redis:latest
          ports:
            - containerPort: 3306
    

    解讀上面的配置: 在Pod進行選擇則node創建Pod的時候首先排除那些在該node上有redis pod的節點. 有點繞但是就是這樣的邏輯.

3. 參考文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章