目录
- Kubernetes之Pod控制器应用进阶
- Pod控制器下spec常用字段
- 标签(Labels)和标签选择器(LabelSelector)
- 标签
- 标签选择器
Kubernetes之Pod控制器应用进阶
Pod控制器下spec常用字段
#containers
[[email protected] ~]# kubectl explain pods.spec.containers.
name <string> -required- #容器名,必选字段
image <string> #镜像,默认是dockerhub仓库,可以客户自己定义仓库
imagePullPolicy <string> #镜像获取策略,Always(总是下载),IfNotPresent(如果本地不存在就下载),Never(从不下载,使用本地镜像),默认IfNotPresent,如果镜像标签是latest,默认是Always
ports <[]Object> # 对象列表,可以暴漏多个端口,也可以对每个端口的属性进行单独定义,此处暴漏端口只是提供额外信息,不能限制系统是否真的暴漏
- containerPort <integer>#容器端口
hostIP <string> #主机IP(一般不需要)
hostPort <integer> #节点端口
name <string> #名称
protocol <string> #使用协议,默认是TCP
args <[]string> #为command传递参数,相当于docker的CMD
command <[]string> #运行的命令 相当于docker中的ENTRYPOINT
Description | docker field name | kubernetes field name |
---|---|---|
the command run by container | entrypoint | command |
the arguments passed to the command | Cmd | args |
说明:
1、如果pod不提供command和args则默认使用docker镜像中的entrypoint和Cmd;
2、如果pod只提供command但是不提供args,则只使用command,docker镜像中的entrypoint和cmd都会被忽略;
3、如果pod不提供command但是只提供args,则使用docker镜像中默认的entrypoint,并且把pod中定义的args当作参数传给entrypoint,docker镜像中的cmd被忽略;
4、如果pod既提供command又提供args,那么就直接运行command和args,docker镜像中的entrypoint和cmd都被忽略。
标签(Labels)和标签选择器(LabelSelector)
标签
- 使用标签把资源分类分组,可以方便管理资源
- 所谓标签就是资源附加的键值对,key:只能使用,字母,数字 _ - . (只能以字母数字开头,不能超过63给字符),value: 可以为空,只能使用,字母,数字开头
- 每一个标签都可以被标签选择器进行匹配度检查,从而完成资源挑选
- 一个资源对象可有多个标签,同一个标签也可以对应多个资源
- 可以在创建资源的时候配置标签,也可以使用kubectl label进行后期打标
显示所有pod的所有标签
[[email protected] ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client-f5cdb799f-pklmc 1/1 Running 0 19h pod-template-hash=f5cdb799f,run=client
myapp-9b4987d5-47sjj 1/1 Running 0 19h pod-template-hash=9b4987d5,run=myapp
myapp-9b4987d5-684q9 1/1 Running 0 19h pod-template-hash=9b4987d5,run=myapp
myapp-9b4987d5-djdr9 1/1 Running 0 19h pod-template-hash=9b4987d5,run=myapp
nginx-deploy-84cbfc56b6-tcssz 1/1 Running 0 23h pod-template-hash=84cbfc56b6,run=nginx-deploy
pod-demo 2/2 Running 1 16h app=myapp,tier=frontend
筛选包含app标签的pod
[[email protected] ~]# kubectl get pods -l app
NAME READY STATUS RESTARTS AGE
pod-demo 2/2 Running 2 17h
显示所有pod标签为app的值,没有则显示为空
[[email protected] ~]# kubectl get pods -L app
NAME READY STATUS RESTARTS AGE APP
client-f5cdb799f-pklmc 1/1 Running 0 20h
myapp-9b4987d5-47sjj 1/1 Running 0 20h
myapp-9b4987d5-684q9 1/1 Running 0 20h
myapp-9b4987d5-djdr9 1/1 Running 0 20h
nginx-deploy-84cbfc56b6-tcssz 1/1 Running 0 24h
pod-demo 2/2 Running 2 17h myapp
使用kubectl label给资源打标签
#语法
kubectl label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N
[--resource-version=version] [options]
[[email protected] manifests]# kubectl label pod pod-demo release=canary
pod/pod-demo labeled
[[email protected] manifests]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client-f5cdb799f-pklmc 1/1 Running 0 20h pod-template-hash=f5cdb799f,run=client
myapp-9b4987d5-47sjj 1/1 Running 0 20h pod-template-hash=9b4987d5,run=myapp
myapp-9b4987d5-684q9 1/1 Running 0 20h pod-template-hash=9b4987d5,run=myapp
myapp-9b4987d5-djdr9 1/1 Running 0 20h pod-template-hash=9b4987d5,run=myapp
nginx-deploy-84cbfc56b6-tcssz 1/1 Running 0 24h pod-template-hash=84cbfc56b6,run=nginx-deploy
pod-demo 2/2 Running 2 17h app=myapp,release=canary,tier=frontends
筛选同时包含多个标签的pod
[[email protected] manifests]# kubectl get pods -l release,app
NAME READY STATUS RESTARTS AGE
pod-demo 2/2 Running 2 17h
标签选择器
- 等值关系:=,==,!=
- 集合关系:
- KEY in (VALUE1,VALUE2...)
- KEY notin (VALUE1,VALUE2...)
- KEY (表示存在KEY)
- !KEY (表示不存在KEY)
许多资源支持内嵌字段定义其使用的标签选择器:
- matchLabels: 直接给定键值 类似等值关系
- matchExpressions: 基于给定的表达式来定义使用标签选择器,{key: "KEY", opeartor: "OPERATOR", values: [VAL1,VAL2...]}
意思是给定的key键对values的值做基于operator表达式的比较,是否满足条件。其中 operator常用的有 In NotIn Exits NotExits,- In NotIn: value必须是非空列表,
- Exits NotExits: values必须是空列表
- annotations:与label不同的地方:它不能用于挑选资源对象,仅用于为对象提供"元数据",没有键值长度限制。
节点标签选择器
在添加资源时,可以让资源对节点有倾向性。
[[email protected] ~]# kubectl explain pods.spec.
nodeName <string> #指定node名称
nodeSelector <map[string]string> #使用标签选择器
给node01打标签disktype=ssd,然后运行pod-demo使用节点标签选择器,使之运行在node01上
#给node01打标签
[[email protected] manifests]# kubectl label node node01 disktype=ssd
node/node01 labeled
[[email protected] manifests]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready master 43h v1.13.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master,node-role.kubernetes.io/master=
node01 Ready <none> 42h v1.13.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/hostname=node01
node02 Ready <none> 42h v1.13.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node02
#编辑pod-demo.yaml
[[email protected] manifests]# vim pod-demo_1.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
#labels: {"app": "myapp","tier": "frontend"} 和下面效果一样,建议使用下面格式
labels:
app: myapp
tier: frontend
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
- name: busybox
image: busybox
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
nodeSelector:
disktype: ssd
#重新部署pod-demo,需提前删除原pod-demo
[[email protected] manifests]# kubectl delete -f pod-demo.yaml
pod "pod-demo" deleted
[[email protected] manifests]# kubectl apply -f pod-demo_1.yaml
pod/pod-demo created
#验证结果
[[email protected] manifests]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
client-f5cdb799f-pklmc 1/1 Running 0 20h 10.244.2.7 node02 <none> <none>
myapp-9b4987d5-47sjj 1/1 Running 0 20h 10.244.1.13 node01 <none> <none>
myapp-9b4987d5-684q9 1/1 Running 0 20h 10.244.1.12 node01 <none> <none>
myapp-9b4987d5-djdr9 1/1 Running 0 20h 10.244.2.13 node02 <none> <none>
nginx-deploy-84cbfc56b6-tcssz 1/1 Running 0 24h 10.244.2.4 node02 <none> <none>
pod-demo 2/2 Running 0 10s 10.244.1.14 node01 <none> <none>
增加annotations注解,使用kubectl describe查看
[[email protected] manifests]# cat pod-demo_1.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
tier: frontend
annotations:
white.io/created-by: "cluster white"
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
- name: busybox
image: busybox
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
nodeSelector:
disktype: ssd
#需要使用kubectl describe pods pod-demo来查看才看的到
[[email protected] manifests]# kubectl describe pods pod-demo
Name: pod-demo
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: node01/10.0.0.11
Start Time: Fri, 29 Mar 2019 11:30:51 +0800
Labels: app=myapp
tier=frontend
Annotations: white.io/created-by: cluster white
Status: Running
IP: 10.244.1.15
... ...
参考资料
https://www.cnblogs.com/linuxk
马永亮. Kubernetes进阶实战 (云计算与虚拟化技术丛书)
Kubernetes-handbook-jimmysong-20181218
原文地址:https://www.cnblogs.com/wlbl/p/10652884.html
时间: 2024-11-05 22:36:13