目录

kubectl-标签管理

  • 查看标签

    kubectl get [资源类型] --show-labels

      [root@k8s-master k8s-ymls]# kubectl get pods --show-labels
      NAME                                READY   STATUS    RESTARTS   AGE     LABELS
      nginx-deployment-6f55ccc6df-7cb7z   1/1     Running   0          5m43s   app=nginx,pod-template-hash=6f55ccc6df
      nginx-deployment-6f55ccc6df-bqgwd   1/1     Running   0          5m43s   app=nginx,pod-template-hash=6f55ccc6df
      nginx-deployment-6f55ccc6df-vkfhg   1/1     Running   0          5m43s   app=nginx,pod-template-hash=6f55ccc6df
    
  • 设置标签

    kubectl label [资源类型] [资源名称] [标签key]=[标签value]

      [root@k8s-master k8s-ymls]# kubectl label pod nginx-deployment-6f55ccc6df-7cb7z key=value
      pod/nginx-deployment-6f55ccc6df-7cb7z labeled
      [root@k8s-master k8s-ymls]# kubectl get pods --show-labels
      NAME                                READY   STATUS    RESTARTS   AGE   LABELS
      nginx-deployment-6f55ccc6df-7cb7z   1/1     Running   0          13m   app=nginx,key=value,pod-template-hash=6f55ccc6df
      nginx-deployment-6f55ccc6df-bqgwd   1/1     Running   0          13m   app=nginx,pod-template-hash=6f55ccc6df
      nginx-deployment-6f55ccc6df-vkfhg   1/1     Running   0          13m   app=nginx,pod-template-hash=6f55ccc6df
    
  • 删除标签

    kubectl label [资源类型] [资源名称] [标签key]-

      [root@k8s-master k8s-ymls]# kubectl label pod nginx-deployment-6f55ccc6df-7cb7z key-
      pod/nginx-deployment-6f55ccc6df-7cb7z unlabeled
      [root@k8s-master k8s-ymls]# kubectl get pods --show-labels
      NAME                                READY   STATUS    RESTARTS   AGE   LABELS
      nginx-deployment-6f55ccc6df-7cb7z   1/1     Running   0          20m   app=nginx,pod-template-hash=6f55ccc6df
      nginx-deployment-6f55ccc6df-bqgwd   1/1     Running   0          20m   app=nginx,pod-template-hash=6f55ccc6df
      nginx-deployment-6f55ccc6df-vkfhg   1/1     Running   0          20m   app=nginx,pod-template-hash=6f55ccc6df
    

例:给k8s-node1设置disk=ssd标签,设置nginx资源配置在有disk=ssd标签的node节点上

[root@k8s-master k8s-ymls]# kubectl get nodes --show-labels
NAME         STATUS   ROLES           AGE     VERSION   LABELS
k8s-master   Ready    control-plane   4d23h   v1.34.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node1    Ready    <none>          4d23h   v1.34.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux
k8s-node2    Ready    <none>          4d23h   v1.34.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux
[root@k8s-master k8s-ymls]# kubectl label nodes k8s-node1 disk=ssd
node/k8s-node1 labeled
[root@k8s-master k8s-ymls]# kubectl get nodes --show-labels
NAME         STATUS   ROLES           AGE     VERSION   LABELS
k8s-master   Ready    control-plane   4d23h   v1.34.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node1    Ready    <none>          4d23h   v1.34.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disk=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux
k8s-node2    Ready    <none>          4d23h   v1.34.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux
[root@k8s-master k8s-ymls]# cat kube-nginx.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  annotations:
    kubernetes.io/change-cause: nginx.v1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      nodeSelector:				#设置标签选择器
        disk: ssd				#选择指定标签
      containers:
      - name: nginx
        image: hub.godone.top/pub/nginx:latest
        ports:
        - containerPort: 80

推荐文章

kubectl-job
kubectl-deployment
kubectl-daemonset