k8s部署-35-k8s中的标签如何使用呢?一文带你了解

图片

什么是label,label的作用是什么,为什么在k8s中要有label这个概念呢?本文就简单的来介绍下label的作用是什么,以及在k8s集群中到底应该如何使用label。

图片

概念

Label,顾名思义就是标签,在k8s中标签就是一个键值对,key=value,几乎在所有的资源上都可以使用label,非常的灵活:Pod、Deployment、Service、Node上面都可以使用。

其次,同一个资源是可以拥有多个标签的,比如我们的Pod-A,既可以拥有app=tomcat,也可以具有web=nihao的标签,使用上更加方便。

命名规则

Label 必须以字母或数字开头,可以使用字母、数字、连字符、点和下划线,最长63个字符。

作用

我觉得标签的作用是为了统一管理,具有相同特点的pod,可以让其具有同一个label,这样子的话后面管理起来非常方便,也可以指定这个pod在哪个node节点上运行,如果你有特殊需要的话。

实操

首先我们新建一个yaml文件,里面配置上标签;

[root@node1 ~]# cd namespace/
[root@node1 namespace]# mkdir label
[root@node1 namespace]# cd label/
[root@node1 label]# vim label-web-demo.yaml 
#deploy
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-demo
spec:
  selector:
    matchLabels:
      app: tomcat-demo
  replicas: 1
  template:
    metadata:
      labels:
        app: tomcat-demo
    spec:
      containers:
      - name: tomcat-demo
        image: registry.cn-hangzhou.aliyuncs.com/liuyi01/tomcat:8.0.51-alpine
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: 100Mi
            cpu: 100m
          limits:
            memory: 100Mi
            cpu: 200m
---
#service
apiVersion: v1
kind: Service
metadata:
  name: tomcat-demo
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: tomcat-demo

---
#ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat-demo
spec:
  rules:
  - host: tomcat.yunweijia.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-demo
          servicePort: 80
[root@node1 label]# 
[root@node1 label]# kubectl apply -f label-web-demo.yaml -n dev
[root@node1 label]# kubectl get deploy -n dev | grep tomcat
tomcat-demo         1/1     1            1           33s
[root@node1 label]# 

上面可以看到运行成功,那么我们再修改下deployment的名字,name: tomcat-demo-yunweijia,别的配置都不修改。如下:

[root@node1 label]# vim label-deploy-web-demo.yaml
#deploy
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-demo-yunweijia
spec:
  selector:
    matchLabels:
      app: tomcat-demo
  replicas: 1
  template:
    metadata:
      labels:
        app: tomcat-demo
    spec:
      containers:
      - name: tomcat-demo
        image: registry.cn-hangzhou.aliyuncs.com/liuyi01/tomcat:8.0.51-alpine
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: 100Mi
            cpu: 100m
          limits:
            memory: 100Mi
            cpu: 200m
---
#service
apiVersion: v1
kind: Service
metadata:
  name: tomcat-demo
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: tomcat-demo

---
#ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat-demo
spec:
  rules:
  - host: tomcat.yunweijia.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-demo
          servicePort: 80
[root@node1 label]# 
[root@node1 label]# kubectl apply -f label-deploy-web-demo.yaml -n dev
[root@node1 label]# kubectl get deploy -n dev | grep tomcat
tomcat-demo             1/1     1            1           2m51s
tomcat-demo-yunweijia   1/1     1            1           9s
[root@node1 label]# 

可以看到,我们运行了两个pod,尽管他们的label是一致的,那是因为我们从最上层就给他分开了,所以互不影响。

实际上不推荐此类方法哈,只是验证下结果,我们给他删除掉;

[root@node1 label]# kubectl delete -f label-deploy-web-demo.yaml -n dev
[root@node1 label]# kubectl delete -f label-web-demo.yaml -n dev

标签的IN

什么叫标签的IN呢?其实就是标签的一种IN方法,{key: ceshi, operator: In, values: [dev, test]},可以判断某个pod是否具有某个标签,如果没有,就会执行失败,如下:

[root@node1 label]# vim label-web-demo.yaml
#deploy
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-demo
  namespace: dev
spec:
  selector:
    matchLabels:
      app: tomcat-demo
    matchExpressions:
      - {key: ceshi, operator: In, values: [dev, test]}
  replicas: 1
  template:
    metadata:
      labels:
        app: tomcat-demo
    spec:
      containers:
      - name: tomcat-demo
        image: registry.cn-hangzhou.aliyuncs.com/liuyi01/tomcat:8.0.51-alpine
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: 100Mi
            cpu: 100m
          limits:
            memory: 100Mi
            cpu: 200m
---
#service
apiVersion: v1
kind: Service
metadata:
  name: tomcat-demo
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: tomcat-demo

---
#ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat-demo
spec:
  rules:
  - host: tomcat.yunweijia.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-demo
          servicePort: 80
[root@node1 label]# 
[root@node1 label]# kubectl apply -f label-web-demo.yaml 
service/tomcat-demo created
Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
ingress.extensions/tomcat-demo created
The Deployment "tomcat-demo" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"tomcat-demo"}: `selector` does not match template `labels`
[root@node1 label]#

从上面看报错了,因为我们这个yaml文件中下面的资源没有标签是ceshi,提示只有一个标签是{"app":"tomcat-demo"},我们新建个label,ceshi: test,再次apply一下看看呢,修改成如下信息:

[root@node1 label]# vim label-web-demo.yaml 
#deploy
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-demo

spec:
  selector:
    matchLabels:
      app: tomcat-demo
    matchExpressions:
      - {key: ceshi, operator: In, values: [dev, test]}
  replicas: 1
  template:
    metadata:
      labels:
        app: tomcat-demo
        ceshi: test
    spec:
      containers:
      - name: tomcat-demo
        image: registry.cn-hangzhou.aliyuncs.com/liuyi01/tomcat:8.0.51-alpine
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: 100Mi
            cpu: 100m
          limits:
            memory: 100Mi
            cpu: 200m
---
#service
apiVersion: v1
kind: Service
metadata:
  name: tomcat-demo
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: tomcat-demo

---
#ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat-demo
spec:
  rules:
  - host: tomcat.yunweijia.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-demo
          servicePort: 80
[root@node1 label]# 
[root@node1 label]# kubectl apply -f label-web-demo.yaml 
deployment.apps/tomcat-demo created
service/tomcat-demo unchanged
ingress.extensions/tomcat-demo configured
[root@node1 label]#

为什么会有这样的配置呢?这样子是不是可以避免下面的pod乱掉,会强制规定他必须具有某个label呢。查询方式如下:

[root@node1 label]# kubectl get pod -l ceshi=test
NAME                           READY   STATUS    RESTARTS   AGE
tomcat-demo-5cb65b749b-sxrzz   1/1     Running   1          20s
[root@node1 label]# 
[root@node1 label]# kubectl get pod -l "ceshi in (dev, test)" -o wide
NAME                           READY   STATUS   RESTARTS   AGE     IP             NODE    NOMINATED NODE   READINESS GATES
tomcat-demo-5cb65b749b-sxrzz   1/1     Running   5          4m27s   10.200.104.3   node2   <none>           <none>
[root@node1 label]# 
[root@node1 label]# kubectl get pod -l "ceshi notin (dev, default)" -o wide
NAME                                 READY   STATUS             RESTARTS   AGE     IP              NODE    NOMINATED NODE   READINESS GATES
deploy-springboot-5dbfc55f78-sfqck   1/1     Running            0          90m     10.200.104.6    node2   <none>           <none>
nginx-ds-q2pjt                       1/1     Running            22         15d     10.200.135.32   node3   <none>           <none>
nginx-ds-zc5qt                       1/1     Running            27         21d     10.200.104.2    node2   <none>           <none>
tomcat-demo-5cb65b749b-sxrzz         1/1     Running            5          4m43s   10.200.104.3    node2   <none>           <none>
[root@node1 label]#

节点标签
剩余内容请转至VX公众号 “运维家” ,回复 “142” 查看。

posted @ 2022-04-18 10:37  郭-吉尔伽美什  阅读(4)  评论(0编辑  收藏  举报