Kubernetes实战(十六)-k8s节点打标签

pod可以根据调度策略让pod调度到想要的节点上运行,或者不在某节点运行。

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

$ kubectl get nodes --show-labels
NAME           STATUS   ROLES                  AGE   VERSION   LABELS
ops-master-1   Ready    control-plane,master   26d   v1.21.9   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-master-1,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
ops-master-2   Ready    control-plane,master   26d   v1.21.9   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-master-2,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
ops-master-3   Ready    control-plane,master   26d   v1.21.9   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-master-3,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
ops-worker-1   Ready                     26d   v1.21.9   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-worker-1,kubernetes.io/os=linux
ops-worker-2   Ready                     26d   v1.21.9   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-worker-2,kubernetes.io/os=linux

2 添加标签


#给节点ops-worker-1打上标签env=uat
$ kubectl label node ops-worker-1 env=uat
 
#给节点ops-worker-2打上标签env=prod
$ kubectl label node ops-worker-2 env=prod
 
#查看生成的标签
$ kubectl get node --show-labels | grep env 
ops-worker-1   Ready                     26d   v1.21.9   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,env=uat,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-worker-1,kubernetes.io/os=linux
ops-worker-2   Ready                     26d   v1.21.9   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,env=prod,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-worker-2,kubernetes.io/os=linux

3 指定标签分发pod

3.1 创建pod时候指定调度节点

3.1.1 生成deployment文件

$ cat nginx.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-test
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      nodeSelector:        #添加此行  
        env: uat      #指定标签
      containers:
      - name: nginx-test
        image: nginx:latest
        ports:
        - containerPort: 80

3.1.2 执行生成pod 

$ kubectl apply -f nginx.yaml 
deployment.apps/nginx-test created

3.1.3 查看结果 

$ kubectl get pod  -o wide 
NAME                           READY   STATUS    RESTARTS   AGE   IP             NODE           NOMINATED NODE   READINESS GATES
nginx-test-6895cc9759-989h8    1/1     Running   0          7s      172.25.78.78   ops-worker-1              
nginx-test-6895cc9759-slzcm    1/1     Running   0          7s      172.25.78.79   ops-worker-1              

3.2 运行状态pod迁移到指定节点

迁移过程相当于删除当前节点pod,再在新node上生成pod,生产环境操作时需要谨慎。

3.2.1 调整pod运行标签

$ kubectl edit deploy nginx-test 
修改
      nodeSelector:
        env: uat
为
      nodeSelector:
        env: prod

deployment.apps/nginx-test edited

3.2.2 查看结果 

$ kubectl get pod  -o wide 
NAME                           READY   STATUS    RESTARTS   AGE     IP              NODE           NOMINATED NODE   READINESS GATES
nginx-test-6b7c99bbb-8sfh7     1/1     Running   0          49s     172.25.50.140   ops-worker-2              
nginx-test-6b7c99bbb-hp62s     1/1     Running   0          67s     172.25.50.139   ops-worker-2              

 pod已经切换至ops-worker-2节点上,且pod创建时间不足1分钟。

4 删除标签

$ kubectl label node ops-worker-1 env-
node/ops-worker-1 labeled

 查看节点上pod状态。

$ kubectl get pod  -o wide 
NAME                           READY   STATUS    RESTARTS   AGE     IP              NODE           NOMINATED NODE   READINESS GATES
nginx-node1-5f9555db6b-db2cq   1/1     Running   0          12m     172.25.78.76    ops-worker-1              
nginx-node1-5f9555db6b-rq2pn   1/1     Running   0          12m     172.25.78.77    ops-worker-1              

删除节点标签,正在运行的pod不受影响 。

 查看节点标签。

$ kubectl get nodes --show-labels | grep ops-worker-1 
ops-worker-1   Ready                     26d   v1.21.9   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-worker-1,kubernetes.io/os=linux

删除节点上的pod,验证pod重新调度是否正常。

$ kubectl delete pod nginx-node1-5f9555db6b-db2cq 
pod "nginx-node1-5f9555db6b-db2cq" deleted
$ kubectl get pod -o wide 
NAME                           READY   STATUS    RESTARTS   AGE     IP              NODE           NOMINATED NODE   READINESS GATES
nginx-node1-5f9555db6b-ctbb9   0/1     Pending   0          59s                                   
nginx-node1-5f9555db6b-rq2pn   1/1     Running   0          16m     172.25.78.77    ops-worker-1              

 pod无法调度运行,因为调度策略中的nodeSelector调度策略还存在,因此生产环境在清理了node标签后,如无其他符合条件的可用节点,一定要将运行在节点上的pod中配置的标签清理掉,否则pod重启后将无法调度。

5 多标签

同一个node同类型只能设置一个, 重复设置会报错:

$ kubectl get nodes --show-labels | grep env
ops-worker-2   Ready                     27d   v1.21.9   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,env=prod,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-worker-2,kubernetes.io/os=linux

 看到ops-worker-2节点上已经有env相关的标签了。

$ kubectl label node ops-worker-2 env=uat
error: 'env' already has a value (prod), and --overwrite is false

再次在 ops-worker-2节点上设置env相关的标签会提示标签已存在,无法重复设置同类型的标签。

你可能感兴趣的:(#,Kubernetes系列,kubernetes,容器,云原生)