kubernetes中的Label(标签)是给用户可灵活定义的对象属性,可以对于正在运行的资源对象,随时可以通过kubectl label命令进行增加、修改、删除等操作。方便区分和选择容器,关联到对象上。以下为实验环境运行~
给pod打标签
命令:kubectl label pod nginx -n dev verison=1.0
查看标签
[root@master ~]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 25m run=nginx,verison=1.0
重新打标签
[root@master ~]# kubectl label pod nginx -n dev verison=2.0 --overwrite
pod/nginx labeled
查看
[root@master ~]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 28m run=nginx,verison=2.0
通过标签选择挑出version=1.0
[root@master ~]# kubectl get pods -l "version=1.0" -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-test 1/1 Running 0 4m19s version=1.0
去除nginx-test 标签test=0001
[root@master ~]# kubectl get pods -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 46m run=nginx,verison=2.0
nginx-test 1/1 Running 0 8m24s test=0001,version=1.0
去除命令
[root@master ~]# kubectl label pod nginx-test -n dev test-
pod/nginx-test labeled
查看结果
[root@master ~]# kubectl get pods -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 49m run=nginx,verison=2.0
nginx-test 1/1 Running 0 10m version=1.0