k8s的label和selector
在Kubernetes中,label和selector是两个重要的概念,它们一起用于实现资源对象的关联和调度。
label
- 创建label
有两种方式创建label:
- 在配置文件中指定label标签
- 使用kubectl label命令
- 临时创建label
kubectl label po 资源名字 app=hello//定义的label -n kube-public //对应的命名空间- 修改已经存在的label
kubectl label po 资源名字 app=hello2 kube-public --overwrite[root@master python]# kubectl get po --show-labels NAME READY STATUS RESTARTS AGE > LABELS my-pod 1/1 Running 0 4h21m > app=my-app nginx-demo 1/1 Running 1 (4h42m ago) 8h > test=1.0.0,type=app nginx-po 1/1 Running 1 (4h42m ago) 8h > test=1.0.0,type=app [root@master python]# kubectl label po nginx-po author=hmm pod/nginx-po labeled [root@master python]# kubectl get po --show-labels NAME READY STATUS RESTARTS AGE > LABELS my-pod 1/1 Running 0 4h22m > app=my-app nginx-demo 1/1 Running 1 (4h44m ago) 8h > test=1.0.0,type=app nginx-po 1/1 Running 1 (4h44m ago) 8h > author=hmm,test=1.0.0,type=app [root@master python]# kubectl label po nginx-po > author=mmmm --overwrite pod/nginx-po labeled [root@master python]# kubectl get po nginx-po --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-po 1/1 Running 1 (4h45m ago) 8h > author=mmmm,test=1.0.0,type=app
deployment的label,service的label,namespace的label,如果是临时label的话,更新模版的时候,会被删除掉。
selectora
selector是label的筛选条件,通过selector,可以筛选出符合label条件的资源对象。
[root@master python]# kubectl get po -A -l type=app
NAMESPACE NAME READY STATUS RESTARTS AGE
default nginx-demo 1/1 Running 1 (4h50m ago) 8h
default nginx-po 1/1 Running 1 (4h50m ago) 8h
[root@master python]# kubectl get po -A -l type=app --show-labels
NAMESPACE NAME READY STATUS RESTARTS AGE LABELS
default nginx-demo 1/1 Running 1 (4h51m ago) 8h test=1.0.0,type=app
default nginx-po 1/1 Running 1 (4h51m ago) 8h author=mmmm,test=1.0.0,type=app
[root@master python]#
标签:kubectl,selector,app,label,nginx,Running,pod,k8s,po
From: https://www.cnblogs.com/humlogs/p/18196314