三、k8s入门系列----annotations、namespace、node label

  上一节讲到了label标签,label标签属于metadata元数据对象的一个属性,这节讲下另外两个属性 annotations 和 namespace 以及 label 在 node 节点的使用。

  Annotations 是注解的意思,也使用key/value 键值对进行定义,Annotations 的作用类似代码的注释功能,可以为资源添加说明,可以是镜像的相关信息,日志记录,负责人信息等等。

  不同于 Labels 用于标志和选择对象,Annotations 用来记录一些附加信息可以用来辅助应用部署、安全策略以及调度策略等。比如 deployment 使用 annotations 来记录 rolling update 的状态。

  编辑deployment 资源配置文件,添加 Annotations 注释 imageregistry: "https://index.docker.io/v1/":

[root@ylserver10686071 ~]# cat deployment.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app003
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      annotations:
        imageregistry: "https://index.docker.io/v1/"
      labels:
        app: web
    spec:
      containers:
      - name: tomcat
        image: tomcat:8.0

  创建deployment,查看 pod annotations 信息,可以看到添加的 imageregistry ,另外2个 key/value 则为自动写入信息,记录 pod的IP地址:

[root@ylserver10686071 ~]# kubectl describe pod app003|grep -5 Annotations
Priority:     0
Node:         ylserver10686072/10.68.60.72
Start Time:   Sun, 18 Jul 2021 21:47:18 +0800
Labels:       app=web
              pod-template-hash=569979b4c7
Annotations:  cni.projectcalico.org/podIP: 10.233.67.26/32
              cni.projectcalico.org/podIPs: 10.233.67.26/32
              imageregistry: https://index.docker.io/v1/
Status:       Running
IP:           10.233.67.26
IPs:
[root@ylserver10686071 ~]# 

  在metadata元数据对象里还有一个很重要的属性,就是namespace 命名空间,namespace 是 k8s 支持多个虚拟集群,底层依赖于同一个物理集群,当多团队或多项目的场景可以用到命名空间,命名空间也可以进行资源配额。

  查看系统已经创建的namespace,其中default为默认namespace,资源不指定namespace时,默认使用该namespace,kube-system则为系统创建对象所使用的namespace:

[root@ylserver10686071 ~]# kubectl get namespace
NAME              STATUS   AGE
default           Active   6d8h
ingress-nginx     Active   6d7h
kube-node-lease   Active   6d8h
kube-public       Active   6d8h
kube-system       Active   6d8h
[root@ylserver10686071 ~]# 

  创建一个名为 prod 的namespace:

[root@ylserver10686071 ~]# kubectl create namespace prod
namespace/prod created
[root@ylserver10686071 ~]# 

  创建 deployment 资源,namespace 为 prod:

[root@ylserver10686071 ~]# cat deployment.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app004
  namespace: prod
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      annotations:
        imageregistry: "https://index.docker.io/v1/"
      labels:
        app: web
    spec:
      containers:
      - name: tomcat
        image: tomcat:8.0

  应用配置文件并查看,参数 -n 指定命名空间:

[root@ylserver10686071 ~]# kubectl apply -f deployment.yml 
deployment.apps/app004 created
[root@ylserver10686071 ~]# kubectl get deployments -n prod
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
app004   1/1     1            1           15s

  查看属于namespace 管理类型的资源(这里只截取前面几个):

[root@ylserver10686071 ~]# kubectl api-resources --namespaced=true
NAME                        SHORTNAMES   APIGROUP                    NAMESPACED   KIND
bindings                                                             true         Binding
configmaps                  cm                                       true         ConfigMap
endpoints                   ep                                       true         Endpoints
events                      ev                                       true         Event
limitranges                 limits                                   true         LimitRange
persistentvolumeclaims      pvc                                      true         PersistentVolumeClaim
pods                        po                                       true         Pod

  查看不属于namespace 管理类型的资源(这里只截取前面几个):

[root@ylserver10686071 ~]# kubectl api-resources --namespaced=false
NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND
componentstatuses                 cs                                          false        ComponentStatus
namespaces                        ns                                          false        Namespace
nodes                             no                                          false        Node
persistentvolumes                 pv                                          false        PersistentVolume

  metadata元数据对象的一些属性也可以注释到pod 的env对象的属性中,最终在Container的环境变量中可以直接调用,编写deployment 资源配置文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app005
  namespace: prod
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      annotations:
        imageregistry: "https://index.docker.io/v1/"
      labels:
        app: web
    spec:
      containers:
      - name: tomcat
        image: tomcat:8.0
        env:
        - name: "NODENAME"
          valueFrom:
            fieldRef:
              fieldPath: metadata.name

  创建deployment资源,并进入 pod的 tomcat 容器中:

[root@ylserver10686071 ~]# kubectl apply -f deployment.yml 
deployment.apps/app005 created
[root@ylserver10686071 ~]# kubectl exec -it app005-569979b4c7-gfrr2 -c tomcat    -n prod -- env|grep -2 NODENAME
HOSTNAME=app005-569979b4c7-gfrr2
TERM=xterm
NODENAME=app005-569979b4c7-gfrr2
KUBERNETES_PORT_443_TCP=tcp://10.233.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
[root@ylserver10686071 ~]# 

  允许注入pod env的对象有: "metadata.name", "metadata.namespace", "metadata.uid", "spec.nodeName", "spec.serviceAccountName", "status.hostIP", "status.podIP", "status.podIPs" 

  label标签还可以应用到 node 节点上,node 节点就是宿主机,查看之前创建的pod 在哪个node上创建:

[root@ylserver10686071 ~]# kubectl get pods -n prod -o wide
NAME                      READY   STATUS    RESTARTS   AGE     IP             NODE               NOMINATED NODE   READINESS GATES
app004-569979b4c7-h4zxx   1/1     Running   0          10m     10.233.67.28   ylserver10686072   <none>           <none>
app005-569979b4c7-gfrr2   1/1     Running   0          3m51s   10.233.75.61   ylserver10686071   <none>           <none>
[root@ylserver10686071 ~]# 

  如果pod在创建的时候没有指定某个node,则会根据k8s 内置的调度算法在某个node上启动,我们也可以给某个node 做 label标记,然后使用标签选择器指定该label,从而达到pod在指定的node上启动。

  查看集群所有node节点:

[root@ylserver10686071 ~]# kubectl get nodes
NAME               STATUS   ROLES    AGE    VERSION
ylserver10686071   Ready    master   6d8h   v1.19.10
ylserver10686072   Ready    master   6d8h   v1.19.10
ylserver10686073   Ready    master   6d8h   v1.19.10
[root@ylserver10686071 ~]# 

  给node节点 ylserver10686073 打上标签 disk=ssd :

[root@ylserver10686071 ~]# kubectl label node ylserver10686073 disk=ssd
node/ylserver10686073 labeled
[root@ylserver10686071 ~]# 

  查看 node 节点 ylserver10686073  所有label,可以看到已经打上 disk=ssd 标签:

[root@ylserver10686071 ~]# kubectl get node ylserver10686073 --show-labels
NAME               STATUS   ROLES    AGE    VERSION    LABELS
ylserver10686073   Ready    master   6d8h   v1.19.10   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disk=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=ylserver10686073,kubernetes.io/os=linux,node-role.kubernetes.io/master=
[root@ylserver10686071 ~]# 

  编辑deployment配置文件,指定在label 包含 disk=ssd 的node上启动:

[root@ylserver10686071 ~]# cat deployment.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app006
  namespace: prod
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      annotations:
        imageregistry: "https://index.docker.io/v1/"
      labels:
        app: web
    spec:
      nodeSelector:
        "disk": 'ssd'
      containers:
      - name: tomcat
        image: tomcat:8.0
        env:
        - name: "NODENAME"
          valueFrom:
            fieldRef:
              fieldPath: metadata.name

  创建deployment app006,并查看 pod:

[root@ylserver10686071 ~]# kubectl apply -f deployment.yml 
deployment.apps/app006 created
[root@ylserver10686071 ~]# kubectl get pods -n prod -o wide
NAME                      READY   STATUS    RESTARTS   AGE     IP             NODE               NOMINATED NODE   READINESS GATES
app004-569979b4c7-h4zxx   1/1     Running   0          28m     10.233.67.28   ylserver10686072   <none>           <none>
app005-569979b4c7-gfrr2   1/1     Running   0          22m     10.233.75.61   ylserver10686071   <none>           <none>
app006-748746dccf-pjj9d   1/1     Running   0          3m43s   10.233.72.37   ylserver10686073   <none>           <none>
[root@ylserver10686071 ~]# 

  node label也可以删除,只需要在key后面加 - ,删除disk=ssd 标签如下:

[root@ylserver10686071 ~]# kubectl label node ylserver10686073 disk-
node/ylserver10686073 labeled
[root@ylserver10686071 ~]# kubectl get node ylserver10686073|grep disk
[root@ylserver10686071 ~]# 

  删除node label,已经创建的pod 不会受到影响:

[root@ylserver10686071 ~]# kubectl get pods -n prod -o wide
NAME                      READY   STATUS    RESTARTS   AGE     IP             NODE               NOMINATED NODE   READINESS GATES
app004-569979b4c7-h4zxx   1/1     Running   0          30m     10.233.67.28   ylserver10686072   <none>           <none>
app005-569979b4c7-gfrr2   1/1     Running   0          24m     10.233.75.61   ylserver10686071   <none>           <none>
app006-748746dccf-pjj9d   1/1     Running   0          5m51s   10.233.72.37   ylserver10686073   <none>           <none>
[root@ylserver10686071 ~]# 

  总结一下:

  1. metadata元数据对象属性中 Annotations  和 Label 都用 key/value 键值对 定义,Annotations 为用户添加的额外注解,Label为上层控制器对下层控制器的匹配定位;
  2. metadata元数据的一些对象属性以及spec、status的一些对象属性可以注入到pod 的env环境变量中;
  3. Label标签也可以应用到node节点上,pod可以根据node节点包含的label 选择在哪些node上启动。
posted @ 2021-07-18 22:48  梦君子  阅读(4003)  评论(0编辑  收藏  举报