Kubernetes之(七)Pod控制器應用進階
Pod控制器下spec常用字段
#containers
[root@master ~]# 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的所有標簽
[root@master ~]# 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
[root@master ~]# kubectl get pods -l app
NAME READY STATUS RESTARTS AGE
pod-demo 2/2 Running 2 17h
顯示所有pod標簽為app的值,沒有則顯示為空
[root@master ~]# 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]
[root@master manifests]# kubectl label pod pod-demo release=canary
pod/pod-demo labeled
[root@master 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
[root@master 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不同的地方:它不能用於挑選資源對象,僅用於為對象提供"元數據",沒有鍵值長度限制。
節點標簽選擇器
在添加資源時,可以讓資源對節點有傾向性。
[root@master ~]# kubectl explain pods.spec.
nodeName <string> #指定node名稱
nodeSelector <map[string]string> #使用標簽選擇器
給node01打標簽disktype=ssd,然后運行pod-demo使用節點標簽選擇器,使之運行在node01上
#給node01打標簽
[root@master manifests]# kubectl label node node01 disktype=ssd
node/node01 labeled
[root@master 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
[root@master 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
[root@master manifests]# kubectl delete -f pod-demo.yaml
pod "pod-demo" deleted
[root@master manifests]# kubectl apply -f pod-demo_1.yaml
pod/pod-demo created
#驗證結果
[root@master 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查看
[root@master 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來查看才看的到
[root@master 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
