一起来学k8s 02.pod label
Pod LabelLabelLabel相当于熟悉的“标签”,给某个资源对象定义一个Label,就相当于给它打了一个标签,随后可以通过Label Selector(标签选择器)查询和筛选拥有某些Label的资源对象。Labels 介绍以Pod为例,Label定义在metadata中:apiVersion: v1kind: Podmetadata:name: myweblab...
·
Pod Label
环境
192.168.48.101 master01
192.168.48.201 node01
192.168.48.202 node02
Label
Label相当于熟悉的“标签”,给某个资源对象定义一个Label,就相当于给它打了一个标签,随后可以通过Label Selector(标签选择器)查询和筛选拥有某些Label的资源对象。
Labels 介绍
以Pod为例,Label定义在metadata中:
apiVersion: v1
kind: Pod
metadata:
name: myweb
labels:
app: myweb
RC和Service在spec中定义Selector与Pod进行关联
apiVersion: v1
kind: ReplicationController
metadata:
name: myweb
spec:
replicas: 1
selector:
app: myweb
template:
…………
Deployment、ReplicaSet、DaemonSet和Job则可以在Selector中使用基于集合的筛选条件
selector:
matchLabels:
app: myweb
matchExpressions:
- {key: tier, operator: In, values: [frontend]}
- {key: environment, operator: NotIn, values: [dev]}
运行pod例子
demo-pod
vim demo-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: demo-pod
namespace: default
labels:
app: myapp
type: pod
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
kubectl apply -f demo-pod.yaml
demo-deploy
vim demo-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deploy
namespace: default
labels:
app: myapp
type: deploy
spec:
replicas: 2
selector:
matchLabels:
app: myapp
type: deploy
template:
metadata:
name: myapp-pod
labels:
app: myapp
type: deploy
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
kubectl apply -f demo-deploy.yaml
demo-replicaset
vim demo-replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: demo-replicaset
namespace: default
labels:
app: myapp
type: replicaset
spec:
replicas: 2
selector:
matchLabels:
app: myapp
type: replicaset
template:
metadata:
name: myapp-pod
labels:
app: myapp
type: replicaset
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
kubectl apply -f demo-replicaset.yaml
Labels 查看
查看所有pods标签
[root@master01 ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
demo-deploy-8675c97685-vhncn 1/1 Running 1 31h app=myapp,pod-template-hash=8675c97685,type=deploy
demo-deploy-8675c97685-w7md2 1/1 Running 1 31h app=myapp,pod-template-hash=8675c97685,type=deploy
demo-pod 2/2 Running 4 31h app=myapp,type=pod
demo-replicaset-fpbs6 1/1 Running 0 6s app=myapp,type=replicaset
demo-replicaset-qk9t9 1/1 Running 0 6s app=myapp,type=replicaset
查看拥有app标签的pods
[root@master01 ~]# kubectl get pods -l app --show-labels
NAME READY STATUS RESTARTS AGE LABELS
demo-deploy-8675c97685-vhncn 1/1 Running 1 31h app=myapp,pod-template-hash=8675c97685,type=deploy
demo-deploy-8675c97685-w7md2 1/1 Running 1 31h app=myapp,pod-template-hash=8675c97685,type=deploy
demo-pod 2/2 Running 4 31h app=myapp,type=pod
demo-replicaset-fpbs6 1/1 Running 0 80s app=myapp,type=replicaset
demo-replicaset-qk9t9 1/1 Running 0 80s app=myapp,type=replicaset
打标签
[root@master01 ~]# kubectl label pod demo-pod release=canary
pod/demo-pod labeled
[root@master01 ~]# kubectl get pods -l release --show-labels
NAME READY STATUS RESTARTS AGE LABELS
demo-pod 2/2 Running 4 31h app=myapp,release=canary,type=pod
修改标签
[root@master01 ~]# kubectl label pod demo-pod release=stable --overwrite
pod/demo-pod labeled
[root@master01 ~]# kubectl get pods -l release --show-labels
NAME READY STATUS RESTARTS AGE LABELS
demo-pod 2/2 Running 4 31h app=myapp,release=stable,type=pod
可以利用in来查询标签
[root@master01~]# kubectl get pod -l "type in (pod,deploy)" --show-labels
NAME READY STATUS RESTARTS AGE LABELS
demo-deploy-8675c97685-vhncn 1/1 Running 1 31h app=myapp,pod-template-hash=8675c97685,type=deploy
demo-deploy-8675c97685-w7md2 1/1 Running 1 31h app=myapp,pod-template-hash=8675c97685,type=deploy
demo-pod 2/2 Running 4 31h app=myapp,release=stable,type=pod
[root@master01 ~]# kubectl get pod -l "type notin (pod,deploy)" --show-labels
NAME READY STATUS RESTARTS AGE LABELS
demo-replicaset-fpbs6 1/1 Running 0 9m21s app=myapp,type=replicaset
demo-replicaset-qk9t9 1/1 Running 0 9m21s app=myapp,type=replicaset
给node打标签
[root@master01 ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready master 41h v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master,kubernetes.io/os=linux,node-role.kubernetes.io/master=
node01 Ready <none> 41h v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux
node02 Ready <none> 41h v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node02,kubernetes.io/os=linux
[root@master01 ~]# kubectl label nodes node01 disk=ssd
node/node01 labeled
[root@master01 ~]# kubectl get nodes --show-labels -l disk
NAME STATUS ROLES AGE VERSION LABELS
node01 Ready <none> 41h v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disk=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux
定义node节点选择器
vim demo-pod-ssd.yaml
apiVersion: v1
kind: Pod
metadata:
name: demo-pod-ssd
namespace: default
labels:
app: myapp
type: pod-ssd
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
nodeSelector:
disk: ssd
创建并查看 pod在node01上
kubectl apply -f demo-pod-ssd.yaml
[root@master01 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
demo-deploy-8675c97685-vhncn 1/1 Running 1 31h 10.244.2.6 node02 <none> <none>
demo-deploy-8675c97685-w7md2 1/1 Running 1 31h 10.244.1.6 node01 <none> <none>
demo-pod 2/2 Running 4 31h 10.244.2.7 node02 <none> <none>
demo-pod-ssd 1/1 Running 0 47s 10.244.1.9 node01 <none> <none>
demo-replicaset-fpbs6 1/1 Running 0 17m 10.244.1.8 node01 <none> <none>
demo-replicaset-qk9t9 1/1 Running 0 17m 10.244.2.9 node02 <none> <none>
更多推荐
已为社区贡献16条内容
所有评论(0)