这篇文章以具体的例子来介绍如何在Pod实例上添加Label。
比如如下在一个名为dev-pod的Pod上添加一个Label,Label的内容是:environment: dev
[root@host131 Pod]# cat pod-with-labels.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: dev-pod
namespace: default
labels:
environment: dev
spec:
containers:
- name: dev-pod-host
image: liumiaocn/tornado:latest
command: ["python"]
args: ["/usr/local/bin/daemon.py", "in dev environment"]
ports:
- containerPort: 80
...
[root@host131 Pod]#
Pod生成并确认
[root@host131 Pod]# kubectl create -f pod-with-labels.yaml
pod/dev-pod created
[root@host131 Pod]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dev-pod 1/1 Running 0 6s 10.254.152.5 192.168.163.131 <none> <none>
[root@host131 Pod]#
给多个Pod都添加上environment的标签
[root@host131 Pod]# cat pod-with-labels.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: dev-pod
namespace: default
labels:
environment: dev
spec:
containers:
- name: dev-pod-host
image: liumiaocn/tornado:latest
command: ["python"]
args: ["/usr/local/bin/daemon.py", "in dev environment"]
ports:
- containerPort: 80
...
---
apiVersion: v1
kind: Pod
metadata:
name: test-pod
namespace: default
labels:
environment: test
spec:
containers:
- name: test-pod-host
image: liumiaocn/tornado:latest
command: ["python"]
args: ["/usr/local/bin/daemon.py", "in test environment"]
ports:
- containerPort: 80
...
---
apiVersion: v1
kind: Pod
metadata:
name: prod-pod
namespace: default
labels:
environment: prod
spec:
containers:
- name: prod-pod-host
image: liumiaocn/tornado:latest
command: ["python"]
args: ["/usr/local/bin/daemon.py", "in prod environment"]
ports:
- containerPort: 80
...
[root@host131 Pod]#
Pod生成与结果确认
[root@host131 Pod]# kubectl create -f pod-with-labels.yaml
pod/dev-pod created
pod/test-pod created
pod/prod-pod created
[root@host131 Pod]#
[root@host131 Pod]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dev-pod 1/1 Running 0 7s 10.254.152.5 192.168.163.131 <none> <none>
prod-pod 1/1 Running 0 7s 10.254.152.7 192.168.163.131 <none> <none>
test-pod 1/1 Running 0 7s 10.254.152.6 192.168.163.131 <none> <none>
[root@host131 Pod]#
使用-l或者--selector即可进行查询了
[root@host131 Pod]# kubectl get pods -l environment=dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dev-pod 1/1 Running 0 4m43s 10.254.152.5 192.168.163.131 <none> <none>
[root@host131 Pod]#
[root@host131 Pod]# kubectl get pods --selector environment=prod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
prod-pod 1/1 Running 0 5m14s 10.254.152.7 192.168.163.131 <none> <none>
[root@host131 Pod]#
[root@host131 Pod]# kubectl get pods -l environment!=prod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dev-pod 1/1 Running 0 6m9s 10.254.152.5 192.168.163.131 <none> <none>
test-pod 1/1 Running 0 6m9s 10.254.152.6 192.168.163.131 <none> <none>
[root@host131 Pod]#
比如事先又如下Pod
[root@host131 ReplicaSet]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-rs-7c27t 1/1 Running 0 6m55s 10.254.152.5 192.168.163.131 <none> <none>
[root@host131 ReplicaSet]#
此Pod的Label信息如下所示:
[root@host131 ReplicaSet]# kubectl describe pod test-rs-7c27t |grep Labels:
Labels: app=busybox-prod
[root@host131 ReplicaSet]#
修改命令:kubectl label pod Pod名称 修改后的Label内容 --overwrite
执行示例和结果确认如下所示:
[root@host131 ReplicaSet]# kubectl label pod test-rs-7c27t app=busybox-dev --overwrite
pod/test-rs-7c27t labeled
[root@host131 ReplicaSet]#
[root@host131 ReplicaSet]# kubectl describe pod test-rs-7c27t |grep Labels:
Labels: app=busybox-dev
[root@host131 ReplicaSet]#
相同的Label可以添加在不同的资源上,同一资源可以设定多个Label,在使用时可以根据Lable进行资源选择,可以根据需要灵活使用。