什么是label

Label以key/value键值对的形式附加到任何对象上,如Pod,Service,Node, RC(ReplicationController)/RS(ReplicaSet)等。
一个资源如果需要找到其它关联的资源比如rc匹配pod、deployment匹配rs都是通过selector选择对应的lable达到关联其它对象的目的。

k8s对象的label在资源的metadata定义,例如之前学习过的deplyment定义文件

标签的增删改查

label查询

#查询某一类资源或者指定某个资源查询其label信息
[root@k8s-01 ~]# kubectl get deployment --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-d 3/3 3 3 2d21h app=nginx
wordpress 1/1 1 1 49d app=wordpress,tier=frontend
wordpress-mysql 1/1 1 1 49d app=wordpress,tier=mysql
You have new mail in /var/spool/mail/root
[root@k8s-01 ~]# kubectl get deployment nginx-d --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-d 3/3 3 3 2d21h app=nginx [root@k8s-01 ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-d-659bf7c684-22nc8 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684
nginx-d-659bf7c684-9l9jk 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684
nginx-d-659bf7c684-tmtkl 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684
wordpress-69c467c658-r7c45 1/1 Running 11 49d app=wordpress,pod-template-hash=69c467c658,tier=frontend-pod
wordpress-mysql-6c696cddc9-r6dvx 1/1 Running 15 42d app=wordpress,pod-template-hash=6c696cddc9,tier=mysql-pod
[root@k8s-01 ~]# kubectl get pods nginx-d-659bf7c684-22nc8 --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-d-659bf7c684-22nc8 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684

label的增加  删除  修改

#指定对象label的添加和删除,添加使用key=value,删除keu-,修改key=value --overwrite
[root@k8s-01 ~]# kubectl get pods nginx-d-659bf7c684-22nc8 --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-d-659bf7c684-22nc8 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684
[root@k8s-01 ~]#
[root@k8s-01 ~]# kubectl label pods nginx-d-659bf7c684-22nc8 test=label
pod/nginx-d-659bf7c684-22nc8 labeled
[root@k8s-01 ~]# kubectl get pods nginx-d-659bf7c684-22nc8 --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-d-659bf7c684-22nc8 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684,test=label
[root@k8s-01 ~]# kubectl label pods nginx-d-659bf7c684-22nc8 test-
pod/nginx-d-659bf7c684-22nc8 labeled
[root@k8s-01 ~]# kubectl get pods nginx-d-659bf7c684-22nc8 --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-d-659bf7c684-22nc8 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684
[root@k8s-01 ~]# kubectl label pods nginx-d-659bf7c684-22nc8 test=label1
pod/nginx-d-659bf7c684-22nc8 labeled
[root@k8s-01 ~]# kubectl get pods nginx-d-659bf7c684-22nc8 --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-d-659bf7c684-22nc8 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684,test=label1
[root@k8s-01 ~]# kubectl label pods nginx-d-659bf7c684-22nc8 test=label2
error: 'test' already has a value (label1), and --overwrite is false
[root@k8s-01 ~]# kubectl label pods nginx-d-659bf7c684-22nc8 test=label2 --overwrite
pod/nginx-d-659bf7c684-22nc8 labeled
[root@k8s-01 ~]# kubectl get pods nginx-d-659bf7c684-22nc8 --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-d-659bf7c684-22nc8 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684,test=label2

一般建议k8s对象的label在定义文件中进行定义和修改

nodeselector

nodeselector是pod的专有的选择器,在资源定义文件中可以指定pod在哪些node节点上创建。不配置该参数的话k8s会根据自身算法选择相应的node来部署pod

不配置nodeselector情况下pod的分布,如果有多个node则pod一定分布在多个pod上。

[root@k8s-01 ~]# kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-d 3/3 3 3 2d21h nginx nginx:1.18 app=nginx
[root@k8s-01 ~]# kubectl get pod -o wide|grep nginx-d
nginx-d-659bf7c684-22nc8 1/1 Running 1 2d21h 10.244.2.52 k8s-03 <none> <none>
nginx-d-659bf7c684-9l9jk 1/1 Running 1 2d21h 10.244.2.53 k8s-03 <none> <none>
nginx-d-659bf7c684-tmtkl 1/1 Running 1 2d21h 10.244.1.71 k8s-02 <none> <none>

查询环境node信息,给node打上label

[root@k8s-01 ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-01 Ready master 61d v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-01,kubernetes.io/os=linux,node-role.kubernetes.io/master=
k8s-02 Ready <none> 61d v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-02,kubernetes.io/os=linux
k8s-03 Ready <none> 61d v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-03,kubernetes.io/os=linux
[root@k8s-01 ~]# kubectl label nodes k8s-02 selectortest=k8s-02
node/k8s-02 labeled
[root@k8s-01 ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-01 Ready master 61d v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-01,kubernetes.io/os=linux,node-role.kubernetes.io/master=
k8s-02 Ready <none> 61d v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-02,kubernetes.io/os=linux,selectortest=k8s-02
k8s-03 Ready <none> 61d v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-03,kubernetes.io/os=linux

deployment定义文件中pod部分使用nodeselector指定node创建Pod

#pod对象定义中指定nodeSelector后pod调度的效果
[root@k8s-01 ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
wordpress 1/1 1 1 49d
wordpress-mysql 1/1 1 1 49d
[root@k8s-01 ~]# kubectl get deployment|grep nginx
[root@k8s-01 ~]# kubectl create -f deployment_demo.yml
deployment.apps/nginx-d created
[root@k8s-01 ~]# kubectl get deployment|grep nginx
nginx-d 0/3 3 0 3s
[root@k8s-01 ~]# kubectl get pods -o wide|grep nginx-d
nginx-d-8668fb97b9-7tw25 1/1 Running 0 43s 10.244.1.75 k8s-02 <none> <none>
nginx-d-8668fb97b9-9hfmb 1/1 Running 0 43s 10.244.1.74 k8s-02 <none> <none>
nginx-d-8668fb97b9-vlzgt 0/1 ContainerCreating 0 43s <none> k8s-02 <none> <none>

k8s入门_label标签、nodeSelector的更多相关文章

  1. K8s 入门

    中文文档:https://www.kubernetes.org.cn/kubernetes%E8%AE%BE%E8%AE%A1%E6%9E%B6%E6%9E%84 小结大白话 Portainer 挺好 ...

  2. 反手来个K8S入门到跑路

    layout: post title: 反手来个K8S入门到跑路 category: linux date: 2019-06-09 tags: linux k8s 反手来个K8S入门到跑路 前言 放假 ...

  3. k8s入门你至少需要会哪些

    body { margin: 0; overflow: auto; font: normal 14px Verdana; background: rgba(255, 255, 255, 1); pad ...

  4. k8s入门系列之guestbook快速部署

    k8s集群以及一些扩展插件已经安装完毕,本篇文章介绍一下如何在k8s集群上快速部署guestbook应用. •实验环境为集群:master(1)+node(4),详细内容参考<k8s入门系列之集 ...

  5. k8s入门系列之扩展组件(一)DNS安装篇

    DNS (domain name system),提供域名解析服务,解决了难于记忆的IP地址问题,以更人性可读可记忆可标识的方式映射对应IP地址. Cluster DNS扩展插件用于支持k8s集群系统 ...

  6. k8s入门系列之集群安装篇

    关于kubernetes组件的详解介绍,请阅读上一篇文章<k8s入门系列之介绍篇> Kubernetes集群安装部署 •Kubernetes集群组件: - etcd 一个高可用的K/V键值 ...

  7. k8s 入门系列之集群安装篇

    关于kubernetes组件的详解介绍,请阅读上一篇文章<k8s入门系列之介绍篇> Kubernetes集群安装部署 •Kubernetes集群组件: - etcd 一个高可用的K/V键值 ...

  8. k8s入门之集群搭建(二)

    一.准备三台节点 从上篇文章 k8s入门之基础环境准备(一)安装的Ubuntu虚拟机克隆出三台虚拟机,如图所示 启动这三台虚拟机节点,分别做如下配置 虚拟机名称 IP HostName k8sMast ...

  9. 前端入门2-HTML标签

    本篇文章已授权微信公众号 dasu_Android(大苏)独家发布 声明 本系列文章内容全部梳理自以下四个来源: <HTML5权威指南> <JavaScript权威指南> MD ...

  10. K8S调度之标签选择器

    Kubernetes 调度简介 除了让 kubernetes 集群调度器自动为 pod 资源选择某个节点(默认调度考虑的是资源足够,并且 load 尽量平均),有些情况我们希望能更多地控制 pod 应 ...

随机推荐

  1. springboot加入cloud,并注册到nacos

    pom.xml下新增 <dependency> <groupId>org.springframework.cloud</groupId> <artifactI ...

  2. redhat6.5_server_install

    CentOS repo: wget http://mirrors.163.com/.help/CentOS6-Base-163.repo EPEL wget http://download.fedor ...

  3. JavaSE总结(1)

    Java发展历史.HelloWorld.常量.变量类型转换.运算符.方法(函数)1.jdk版本:    jdk1.2---J2EE/J2SE/J2ME    jdk1.5---JavaSE/JavaE ...

  4. antd切换主题

    { "name": "my-react-app", "version": "0.1.0", "private& ...

  5. Java面向对象之static关键字详解

    static关键字详解 package OOP.Demo10; public class Person { //2:赋初值 { System.out.println("匿名代码块" ...

  6. 使用Mockito 对方法中的私有void方法

    这里用一段代码举例 public void ruleJudgement(Long tenantId, Long productId, Long equipId, List<ModelAttrib ...

  7. 蓝牙mesh组网实践(环境监测传感器应用)

    目录 蓝牙mesh组网中的低功耗节点,在应用于低频率上传数据的传感器网络时有着得天独厚的功耗优势,在1min唤醒上传一包的情况下ch582的平均功耗仅有12uA,若每小时甚至每天采样一次数据并上传,平 ...

  8. Mysql压缩包版本安装

    之前一直使用的是安装包,今天懒得去下载了,直接用压缩包安装,记录一下. 1.先去官网下载压缩包.https://dev.mysql.com/downloads/mysql/8.0.html 2.将文件 ...

  9. 069_VisualForce Ajax 更多用法解读02

    一.同步调用的异常处理:AJAX Toolkit  try catch... <html> <head> <script src="/soap/ajax/44. ...

  10. vscode plugin - jenkins jack使用方法

    Jenkins jack用于使vscode可直连jenkins,能推送代码至对应jenkins job进行代码测试 一.vscode安装jenkins jack 二.连接jenkins 根据提示输入j ...