Kubernetes 分派 Pod 到指定節點

學習利用 Kubernetes nodeSelector 來限制 pod 的開啟節點

ta-ching chen

1 minute read

 文章目錄

前言 

目前 Kubernetes 支援 nodeSelectoraffinityanti-affinity 來限制每個 pod 能夠在哪些節點上開啟,本文會介紹如何利用 nodeSelector 來達到此功能

動手做 

查詢現有標籤 (labels) 

$ kubectl get nodes --show-labels
NAME            STATUS    AGE       LABELS
192.168.1.1    Ready     4d        beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.1.1
192.168.1.2    Ready     4d        beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.1.2
192.168.1.3    Ready     4d        beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.1.3

新增標籤 

$ kubectl label node <node name> <label>=<value>
$ kubectl label node 192.168.1.1 networkSpeed=high
$ kubectl label node 192.168.1.2 networkSpeed=high
$ kubectl label node 192.168.1.2 networkSpeed=low

確認 labels 是否有更新上去

$ kubectl get nodes --show-labels
NAME            STATUS    AGE       LABELS
192.168.1.1    Ready     4d        beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.1.1,networkSpeed=high
192.168.1.2    Ready     4d        beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.1.2,networkSpeed=high
192.168.1.3    Ready     4d        beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.1.3,networkSpeed=low

移除標籤 

You can remove incorrect label with <label>-

$ kubectl label node 192.168.1.1 networkSpeed-

建立測試用 Deployment 

Dont forget to quote the label value.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-test
spec:
  replicas: 3
  template:
    metadata:
      name: nginx
      namespace: default
      labels:
        env: beta
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
      nodeSelector:
        networkSpeed: "high"

確認 Pods 在哪些節點開啟 

現在我們可以看到 Pod 都跑在有 networkSpeed=high 標籤的節點上了。

$ kubectl get pod -o wide
NAME                              READY     STATUS    RESTARTS   AGE       IP             NODE
nginx-test-3190407156-4g8k5       1/1       Running   0          17s       172.16.88.2    192.168.1.2
nginx-test-3190407156-837ad       1/1       Running   0          17s       172.16.88.3    192.168.1.2
nginx-test-3190407156-o95l8       1/1       Running   0          17s       172.16.84.2    192.168.1.1

Troubleshooting 

建立 Deployment 失敗 

unable to decode "nginx.yaml": [pos 1742]: json: expect char '"' but got char 't'

相關文章

comments powered by Disqus