本文需求,实际场景中有多个节点,但为了区分不同环境运行在不同的node节点上区分环境及环境运行的影响。把不同环境的pods分发到指定的node节点上。

1、查看现有节点运行环境已有标签

~]# kubectl get nodes --show-labels
NAME      STATUS   ROLES                  AGE     VERSION   LABELS
master1   Ready    control-plane,master   6d3h    v1.21.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master1,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
node2     Ready    <none>                 3h35m   v1.21.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master2,kubernetes.io/os=linux
node1     Ready    <none>                 5d23h   v1.21.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node1,kubernetes.io/os=linux
以上是默认现有的一些标签

2、添加标签

]# kubectl label node node1 env-role=uat
node/node1 labeled

]# kubectl label node node2 env-role=prod
node/node2 labeled

]# kubectl get node --show-labels  (查看标签)
NAME      STATUS   ROLES                  AGE     VERSION   LABELS
master1   Ready    control-plane,master   6d4h    v1.21.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master1,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
node2     Ready    <none>                 4h7m    v1.21.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,env-role=prod,kubernetes.io/arch=amd64,kubernetes.io/hostname=master2,kubernetes.io/os=linux
node1     Ready    <none>                 5d23h   v1.21.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,env-role=uat,kubernetes.io/arch=amd64,kubernetes.io/hostname=node1,kubernetes.io/os=linux

3、指定标签分发pod

~]# cat nginx_node1.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-node1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      nodeSelector:
        env-role: uat      #指定标签
      containers:
      - name: nginx-node1
        image: nginx:latest
        ports:
        - containerPort: 80

4、查看分发pod在哪个node上

~]# kubectl get pods -o wide
NAME                             READY   STATUS    RESTARTS   AGE   IP            NODE      NOMINATED NODE   READINESS GATES
nginx-node1-5f4b6d4d5b-ldk9b     1/1     Running   0          59m   10.244.2.36   node1     <none>           <none>
nginx-node1-5f4b6d4d5b-pltjv     1/1     Running   0          59m   10.244.2.35   node1     <none>           <none>

6、 删除标签

]# kubectl label node node1 env-role-
node/node1 labeled

~]# kubectl label node node2 env-role-
node/node2 labeled
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐