K8S label 操作

在部署完成 node 节点集群之后,为了更灵活的操控 node 节点,有时候需要对 node 节点进行对各个 node 节点进行 lable 标签标记。

  1. 查看各个节点的信息
    [root@porxy02-suqian-ts ~]# kubectl get nodes 
    NAME            LABELS                                                       STATUS     RESOURCES                                                          AGE10.16.115.104   common=true                                                  Ready      [pod(52%):21/40 cpu(65%):20990m/32 memory(49%): 64000/128748(M)]   230d
    10.16.115.105   common=true                                                  Ready      [pod(55%):22/40 cpu(75%):23470m/32 memory(54%): 70656/128916(M)]   230d
    10.16.115.99    common=true,kubernetes.io/hostname=10.16.115.99   Ready      [pod(32%):13/40 cpu(40%):12990m/32 memory(27%): 35072/128748(M)]   3d

    我们可以看到有三个 node 节点,104 和 105 都打了标签 common=true ,我们现在希望将 99 打上  test=false  标签。

  2. 查看 label 帮助信息
    [root@porxy02-suqian-ts ~]# kubectl label -h

    通过这个命令可以获取到很详细的帮助信息,并且还有相关的实例展示。

  3. 为 99 打上 test=true 标签
    [root@porxy02-suqian-ts ~]# kubectl label nodes 10.16.115.99 test=false
    node "10.16.115.99" labeled
    [root@porxy02-suqian-ts ~]# kubectl get nodes 
    NAME            LABELS                                                       STATUS     RESOURCES                                                          AGE 10.16.115.104   common=true                                                  Ready      [pod(52%):21/40 cpu(65%):20990m/32 memory(49%): 64000/128748(M)]   230d
    10.16.115.105   common=true                                                  Ready      [pod(55%):22/40 cpu(75%):23470m/32 memory(54%): 70656/128916(M)]   230d
    10.16.115.99    common=true,kubernetes.io/hostname=10.16.115.99,test=false   Ready      [pod(32%):13/40 cpu(40%):12990m/32 memory(27%): 35072/128748(M)]   3d

    可以看到现在 99 机器已经打上了一个标签,且这个标签为增量式的,并以逗号隔开。

  4. 强制刷新覆盖已有的标签健对值
    如果标签已经存在,那么我们重复创建的话会怎么样呢?
    [root@porxy02-suqian-ts ~]# kubectl label nodes 10.16.115.99 test=false
    'test' already has a value (false), and --overwrite is false

    提示 KEY= 'test' 健对值已经存在了,要想修改健对值,就需要设置 --overwrite 值为true,即在执行命令的时候带上  --overwrite 就可以了。

    [root@porxy02-suqian-ts ~]# kubectl label --overwrite nodes 10.16.115.99 test=true
    node "10.16.115.99" labeled
    [root@porxy02-suqian-ts ~]# kubectl get nodes 10.16.115.99
    NAME           LABELS                                                      STATUS    RESOURCES                                                          AGE
    10.16.115.99   common=true,kubernetes.io/hostname=10.16.115.99,test=true   Ready     [pod(32%):13/40 cpu(40%):12990m/32 memory(27%): 35072/128748(M)]   3d

    我们可以看到 test 的值变成了我们修改的 true.

  5. 删除 label 
    [root@porxy02-suqian-ts ~]# kubectl label nodes 10.16.115.99 test-
    node "10.16.115.99" labeled
    [root@porxy02-suqian-ts ~]# kubectl get nodes 10.16.115.99
    NAME           LABELS                                            STATUS    RESOURCES                                                          AGE
    10.16.115.99   common=true,kubernetes.io/hostname=10.16.115.99   Ready     [pod(32%):13/40 cpu(40%):12990m/32 memory(27%): 35072/128748(M)]   3d

    删除就是在 KEY 的后面加上一个减号"-"就可以了。

以上就是label的基本操作,当然可以直接针对 pods 进行操作,这里就先不赘述了。

原文地址:https://www.cnblogs.com/cxbhakim/p/8533861.html