Pod控制器应用进阶
标签是K8s极具特色之一 一个资源可以拥有多个标签 同一个标签可以贴到不同的对象上资源配置清单: 自主式Pod资源 资源的清单格式: 一级字段:apiVersion(group/version),kind,metadata(name,namespace,labels,annotations,...),spec,status(只读) ...
·
标签是K8s极具特色之一
一个资源可以拥有多个标签
同一个标签可以贴到不同的对象上
资源配置清单:
自主式Pod资源
资源的清单格式:
一级字段:apiVersion(group/version),kind,metadata(name,namespace,labels,annotations,...),spec,status(只读)
Pod资源
spec.containers <[]object>
- name <string>
image <string>
imagePullPolicy <string>
Always,Never,IfNotPresent 分别对应:总是去下载,总是不去下载,如果本地不存在就下载
示例:在yaml文件中:
spec:
containers:
imagePullPolicy: IfNotPresent
查看端口的一些信息
kubectl explain pods.spec.containers.ports
例如:
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
- name:https
containerPort: 443
标签:
key=value
key:字符,数字 _ - .只能以字母数字开头
value:可以为空,只能以字母数字开头和结尾
标签选择器:
等值关系:=,==,!=
集合关系:KEY notin(VALUE1,VALUE2)
KEY notin (VALUE1,VALUE2)
KEY
!KEY
许多资源支持内嵌字段使用标签选择器:
matchLabels:直接给定健值
matchExpressions:基于给定的表达式来使用标签选择器{key:"KEY",operator:"OPERATOR",values:[VAL1,VAL2,...]}
操作符:
In, NotIn:value字段的值必须为非空列表
Exists,NotExists:values字段的值必须为空列表
nodeSelector <map[string]string>
节点选择器标签
nodeName <string> 指定节点选择器
annotations:
和label不同的地方在于,它不能用于挑选资源对象,仅用于对象提供"元数据"
Pod的生命周期:
状态: Pending 挂起 没有能满足的,调度没有完成
Running 运行
Failed 失败
Succeeded 成功
Unknown 位置状态
创建Pod:
Pod生命周期中的重要行为:
初始化容器
容器探测:
liveness
readiness
reatartPolicy(容器的重启策略)
Always,OnFailure,Neber,Default to Always
必须重启,状态错误重启,不充钱,默认
查看所有的标签:
kubectl get pods --show-labels
[root@master manifests]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client 1/1 Running 0 15h run=client
myapp-848b5b879b-4xpl7 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-7gtn4 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-xtcdd 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
nginx-deploy-5b595999-fsxlp 1/1 Running 0 15h pod-template-hash=16151555,run=nginx-deploy
pod-demo 2/2 Running 0 4s app=myapp,tier=frontend
指定标签过滤器:
kubectl get pods -L app
[root@master manifests]# kubectl get pods -L app
NAME READY STATUS RESTARTS AGE APP
client 1/1 Running 0 15h
myapp-848b5b879b-4xpl7 1/1 Running 0 14h
myapp-848b5b879b-7gtn4 1/1 Running 0 14h
myapp-848b5b879b-xtcdd 1/1 Running 0 14h
nginx-deploy-5b595999-fsxlp 1/1 Running 0 15h
pod-demo 2/2 Running 0 1m myapp
[root@master manifests]# kubectl get pods -l app
NAME READY STATUS RESTARTS AGE
pod-demo 2/2 Running 0 2m
[root@master manifests]# kubectl get pods -l app --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo 2/2 Running 0 3m app=myapp,tier=frontend
显示每一个pod的标签值
[root@master manifests]# kubectl get pods -L app --show-labels
NAME READY STATUS RESTARTS AGE APP LABELS
client 1/1 Running 0 15h run=client
myapp-848b5b879b-4xpl7 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-7gtn4 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-xtcdd 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
nginx-deploy-5b595999-fsxlp 1/1 Running 0 15h pod-template-hash=16151555,run=nginx-deploy
pod-demo 2/2 Running 0 3m myapp app=myapp,tier=frontend
给特定的pod增加一个标签:
kubectl label pods pod-demo release=canary
打标签之前:
[root@master manifests]# kubectl get pods -L app --show-labels
NAME READY STATUS RESTARTS AGE APP LABELS
client 1/1 Running 0 15h run=client
myapp-848b5b879b-4xpl7 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-7gtn4 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-xtcdd 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
nginx-deploy-5b595999-fsxlp 1/1 Running 0 15h pod-template-hash=16151555,run=nginx-deploy
pod-demo 2/2 Running 0 3m myapp app=myapp,tier=frontend
[root@master manifests]# kubectl label pods pod-demo release=canary #开始打标签
pod/pod-demo labeled
打标签之后:
[root@master manifests]# kubectl get pods -L app --show-labels
NAME READY STATUS RESTARTS AGE APP LABELS
client 1/1 Running 0 15h run=client
myapp-848b5b879b-4xpl7 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-7gtn4 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-xtcdd 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
nginx-deploy-5b595999-fsxlp 1/1 Running 0 15h pod-template-hash=16151555,run=nginx-deploy
pod-demo 2/2 Running 0 6m myapp app=myapp,release=canary,tier=frontend
如果已经有这个标签,再次打标签会报错的
[root@master manifests]# kubectl label pods pod-demo release=stable
error: 'release' already has a value (canary), and --overwrite is false
#已经有这个标签,如果想打上这个标签,使用--overwrite is false覆盖标签
[root@master manifests]# kubectl label pods pod-demo release=stable
error: 'release' already has a value (canary), and --overwrite is false
[root@master manifests]# kubectl label pods pod-demo release=stable --overwrite
pod/pod-demo labeled
[root@master manifests]# kubectl get pods -L app --show-labels
NAME READY STATUS RESTARTS AGE APP LABELS
client 1/1 Running 0 15h run=client
myapp-848b5b879b-4xpl7 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-7gtn4 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-xtcdd 1/1 Running 0 14h pod-template-hash=4046164356,run=myapp
nginx-deploy-5b595999-fsxlp 1/1 Running 0 15h pod-template-hash=16151555,run=nginx-deploy
pod-demo 2/2 Running 0 11m myapp app=myapp,release=stable,tier=frontend
筛选出来:
[root@master manifests]# kubectl get pods -l app --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo 2/2 Running 0 12m app=myapp,release=stable,tier=frontend
筛选有release标签的
[root@master manifests]# kubectl get pods -l release
NAME READY STATUS RESTARTS AGE
pod-demo 2/2 Running 0 13m
筛选有release和app标签的
[root@master manifests]# kubectl get pods -l release,app
NAME READY STATUS RESTARTS AGE
pod-demo 2/2 Running 0 13m
筛选出release=stable的pod
kubectl get pods -l release=stable --show-labels
[root@master manifests]# kubectl get pods -l release=stable --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo 2/2 Running 0 17m app=myapp,release=stable,tier=frontend
我们在给别的pod打个标签
[root@master manifests]# kubectl label pods nginx-deploy-5b595999-fsxlp release=canary
pod/nginx-deploy-5b595999-fsxlp labeled
[root@master manifests]# kubectl get pods -l release --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-deploy-5b595999-fsxlp 1/1 Running 0 15h pod-template-hash=16151555,release=canary,run=nginx-deploy
pod-demo 2/2 Running 0 19m app=myapp,release=stable,tier=frontend
[root@master manifests]# kubectl get pods -l release=canary --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-deploy-5b595999-fsxlp 1/1 Running 0 15h pod-template-hash=16151555,release=canary,run=nginx-deploy
多标签筛选:
[root@master manifests]# kubectl get pods -l release,app --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo 2/2 Running 0 22m app=myapp,release=stable,tier=frontend
排除某一个标签:
[root@master manifests]# kubectl get pods -l release!=canary
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 15h
myapp-848b5b879b-4xpl7 1/1 Running 0 15h
myapp-848b5b879b-7gtn4 1/1 Running 0 15h
myapp-848b5b879b-xtcdd 1/1 Running 0 15h
pod-demo 2/2 Running 0 30m
包含和不包含
[root@master manifests]# kubectl get pods -l "release in (canary,beta,alpha)"
NAME READY STATUS RESTARTS AGE
nginx-deploy-5b595999-fsxlp 1/1 Running 0 15h
[root@master manifests]# kubectl get pods -l "release notin (canary,beta,alpha)"
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 16h
myapp-848b5b879b-4xpl7 1/1 Running 0 15h
myapp-848b5b879b-7gtn4 1/1 Running 0 15h
myapp-848b5b879b-xtcdd 1/1 Running 0 15h
pod-demo 2/2 Running 0 34m
通过此方法也可以获得node标签
[root@master manifests]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready master 17h v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master,node-role.kubernetes.io/master=
node1 Ready <none> 17h v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node1
node2 Ready <none> 17h v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node2
给node1上打一个ssh的标签
[root@master manifests]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready master 17h v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master,node-role.kubernetes.io/master=
node1 Ready <none> 17h v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssh,kubernetes.io/hostname=node1
node2 Ready <none> 17h v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node2
在yaml文件中定义node标签,创建时生成:
kubectl delete -f pod-node.yaml
kubectl create -f pod-node.yaml
[root@master manifests]# cat pod-demo.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
tier: frontend
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
- name: https
containerPort: 443
- name: busybox
image: busybox:latest
imagePullPolicy: IfNotPresent #Always,Never,IfNotPresent 分别对应:总是去下载,总是不去下载,如果本地不存在就下载
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
nodeSelector:
disktype: ssd
[root@master manifests]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready master 17h v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master,node-role.kubernetes.io/master=
node1 Ready <none> 17h v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssh,kubernetes.io/hostname=node1
node2 Ready <none> 17h v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node2
资源注解:
通过:
kubectl describe pods pod-demo
yaml中添加:
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
tier: frontend
annotations:
node1/create-by: "cluster admin"
显示:
Annotations: node1/create-by=cluster admin
更多推荐
已为社区贡献5条内容
所有评论(0)