Label与Label Selector

标签

进入目录保存实验文件

并创建一个yaml使用多个标签

[root@master tz123]# cd /root/tz123/labfile/labelfile
[root@master labelfile]# vim labelpod.yaml
kind: Pod
apiVersion: v1
metadata:
  name: labelpod
  labels:
    app: busybox
    version: new
spec:
  containers:
    - name: labelpod
      image: busybox
      args:
      - /bin/sh
      - -c
      - sleep 30000

创建Pod,并查看pod的label

[root@master labelfile]# kubectl apply -f labelpod.yaml 
pod/labelpod created
[root@master labelfile]# kubectl get pod --show-labels
NAME       READY   STATUS    RESTARTS   AGE   LABELS
labelpod   1/1     Running   0          11s   app=busybox,version=new

为容器添加新标签

[root@master labelfile]# kubectl label pod labelpod time=2019
pod/labelpod labeled
[root@master labelfile]# kubectl get pod --show-labels
NAME       READY   STATUS    RESTARTS   AGE   LABELS
labelpod   1/1     Running   0          69s   app=busybox,time=2019,version=new

标签选择器

创建新的yaml

[root@master labelfile]# vim labelpod2.yaml
kind: Pod
apiVersion: v1
metadata:
  name: labelpod2
  labels:
    app: httpd
    version: new
spec:
  containers:
    - name: httpd
      image: httpd

创建并查看新创建的labelpod2

[root@master labelfile]# kubectl apply -f labelpod2.yaml
[root@master labelfile]# kubectl get pod --show-labels
NAME        READY   STATUS              RESTARTS   AGE   LABELS
labelpod    1/1     Running             0          12m   app=busybox,time=2019,version=new
labelpod2   0/1     ContainerCreating   0          23s   app=httpd,version=new

使用给予等值的标签选择器

[root@master labelfile]# kubectl get pod -l app=httpd
NAME        READY   STATUS    RESTARTS   AGE
labelpod2   1/1     Running   0          100s
或
[root@master labelfile]# kubectl get pod -l app==httpd
NAME        READY   STATUS    RESTARTS   AGE
labelpod2   1/1     Running   0          114s

使用基于不等值的标签选择器和查看pod针对某标签键的值

[root@master labelfile]# kubectl get pod -l app!=httpd
NAME       READY   STATUS    RESTARTS   AGE
labelpod   1/1     Running   0          14m
[root@master labelfile]# kubectl get pod -L app
NAME        READY   STATUS    RESTARTS   AGE    APP
labelpod    1/1     Running   0          15m    busybox
labelpod2   1/1     Running   0          3m5s   httpd

使用标签选择器实现调度

将节点1打上标签并查看

[root@master labelfile]# kubectl label node node env=test
node/node labeled
[root@master labelfile]# kubectl get node -L env
NAME     STATUS   ROLES                  AGE   VERSION   ENV
master   Ready    control-plane,master   91d   v1.20.6   
node     Ready    <none>                 91d   v1.20.6   test

使用nodeselector实现调度,创建新的yaml文件

[root@master labelfile]# vim nsdeploy.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx-dy
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
      nodeSelector:
        env: test

查看deployment中的pod位置

[root@master labelfile]# kubectl get pod -o wide
NAME                        READY   STATUS    RESTARTS   AGE     IP               NODE   NOMINATED NODE   READINESS GATES
labelpod                    1/1     Running   0          28m     10.244.167.145   node   <none>           <none>
labelpod2                   1/1     Running   0          15m     10.244.167.146   node   <none>           <none>
nginx-dy-6dd6c76bcb-667ss   1/1     Running   0          5m19s   10.244.167.148   node   <none>           <none>
nginx-dy-6dd6c76bcb-q8tqh   1/1     Running   0          5m19s   10.244.167.149   node   <none>           <none>
nginx-dy-6dd6c76bcb-xc9h7   1/1     Running   0          5m19s   10.244.167.147   node   <none>           <none>

使用 node affinity 调度,创建一个新的 yaml 文件 nadeploy2.yaml

[root@master labelfile]# vim nadeploy2.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
  name: httpd-dy
  labels:
    app: httpd
spec:
  replicas: 3
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
      - name: httpd
        image: httpd
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: env
                operator: In
                values:
                - test

创建deployment并查看deployment中的pod位置,三个pod都在node上

[root@master labelfile]# kubectl apply -f nadeploy2.yaml 
deployment.apps/httpd-dy created
[root@master labelfile]# kubectl get pod -o wide
NAME                        READY   STATUS              RESTARTS   AGE   IP               NODE   NOMINATED NODE   READINESS GATES
httpd-dy-5b4bb9646-g4jzb    1/1     Running             0          33s   10.244.167.150   node   <none>           <none>
httpd-dy-5b4bb9646-lb876    1/1     Running             0          33s   10.244.167.151   node   <none>           <none>
httpd-dy-5b4bb9646-q7zcm    0/1     ContainerCreating   0          33s   <none>           node   <none>           <none>
labelpod                    1/1     Running             0          38m   10.244.167.145   node   <none>           <none>
labelpod2                   1/1     Running             0          26m   10.244.167.146   node   <none>           <none>
nginx-dy-6dd6c76bcb-667ss   1/1     Running             0          15m   10.244.167.148   node   <none>           <none>
nginx-dy-6dd6c76bcb-q8tqh   1/1     Running             0          15m   10.244.167.149   node   <none>           <none>
nginx-dy-6dd6c76bcb-xc9h7   1/1     Running             0          15m   10.244.167.147   node   <none>           <none>

实训任务

创建一个deployment

使用镜像nginx,5个副本

deployment中的pod不能出现在node上

[root@master labelfile]# vim shixun01.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx-dy
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: env
                operator: Not In
                values:
                - node

寻找一种方式搜索出kubernetes系统上提供core-dns,kubeproxy以及dashboard服务的pod

先给core-dns,kubeproxy,dashboard打上标签

68422694647

[root@master labelfile]# kubectl label -n kube-system pod kube-proxy-kj8j5 app=kubeproxy
[root@master labelfile]# kubectl label -n kube-system pod coredns-7f89b7bc75-n224r app=coredns

查找关键词的pod

68422698315

搜索dashboard的pod

68422720762

使用标签和标签选择器,使用一条命令删除node2节点的nginx

68422750468

最后修改:2023 年 05 月 16 日
如果觉得我的文章对你有用,请随意赞赏