KUBERNETES-1-4-控制器應用一
1.kubectl explain pods.spec.containers可以檢視容器可以定義的相關引數。image <string>用來定義所引用的映象。imagePullPolicy <string>用來定義映象拉去的策略,預設是always,即系統每次都會去抓取映象,但需要特變注意的是當我們的映象標籤為latest的時候,按照正常時肯定會抓取以確定是latest,有時候我們可能不想讓系統去抓取,可以選擇IfNotPresent。ports 主要用來選擇Pod 要暴露的埠。 args 用來指定向容器傳的引數。command 用來指定容器執行的程式,這裡的command可能更類似於dockerl裡面的ENTRYPOINT。附有一個dockerfile與kubernetes的對照說明。
[[email protected] ~]# kubectl explain pods.spec.containers
image <string>
Docker image name. More info:
https://kubernetes.io/docs/concepts/containers/images This field is
optional to allow higher level config management to default or override
container images in workload controllers like Deployments and StatefulSets.
imagePullPolicy <string>
Image pull policy. One of Always, Never, IfNotPresent. Defaults to Always
if :latest tag is specified, or IfNotPresent otherwise. Cannot be updated.
More info:
https://kubernetes.io/docs/concepts/containers/images#updating-images
ports <[]Object>
List of ports to expose from the container. Exposing a port here gives the
system additional information about the network connections a container
uses, but is primarily informational. Not specifying a port here DOES NOT
prevent that port from being exposed. Any port which is listening on the
default "0.0.0.0" address inside a container will be accessible from the
network. Cannot be updated.
args <[]string>
Arguments to the entrypoint. The docker image's CMD is used if this is not
provided. Variable references $(VAR_NAME) are expanded using the
container's environment. If a variable cannot be resolved, the reference in
the input string will be unchanged. The $(VAR_NAME) syntax can be escaped
with a double $$, ie: $$(VAR_NAME). Escaped references will never be
expanded, regardless of whether the variable exists or not. Cannot be
updated. More info:
https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
command <[]string>
Entrypoint array. Not executed within a shell. The docker image's
ENTRYPOINT is used if this is not provided. Variable references $(VAR_NAME)
are expanded using the container's environment. If a variable cannot be
resolved, the reference in the input string will be unchanged. The
$(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME).
Escaped references will never be expanded, regardless of whether the
variable exists or not. Cannot be updated. More info:
https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
2.我們可以通過kubectl get pods --show-labels來檢視pod的標籤資訊。kubectl get pods -l 可以檢視含有標籤值含有某欄位的相關資訊。kubectl get pods -L可以檢視含有標籤名含有某欄位的相關資訊。
[[email protected] ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 2d
myapp-848b5b879b-7h254 1/1 Running 2 2d
myapp-848b5b879b-d7rjs 1/1 Running 2 2d
myapp-848b5b879b-wv5cz 1/1 Running 2 2d
nginx-deploy-5b595999-tj8ms 1/1 Running 2 2d
[[email protected] ~]# cd manifests/
[[email protected] manifests]# vim pod-demo.yaml
[[email protected] 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
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
[[email protected] manifests]# kubectl create -f pod-demo.yaml
pod/pod-demo created
[[email protected] manifests]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client 0/1 Completed 0 2d run=client
myapp-848b5b879b-7h254 1/1 Running 2 2d pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-d7rjs 1/1 Running 2 2d pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-wv5cz 1/1 Running 2 2d pod-template-hash=4046164356,run=myapp
nginx-deploy-5b595999-tj8ms 1/1 Running 2 2d pod-template-hash=16151555,run=nginx-deploy
pod-demo 2/2 Running 0 51s app=myapp,tier=frontend
[[email protected] manifests]# kubectl get pods -l app --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo 2/2 Running 0 4m app=myapp,tier=frontend
[[email protected] manifests]# kubectl get pods -L app,run --show-labels
NAME READY STATUS RESTARTS AGE APP RUN LABELS
client 0/1 Completed 0 2d client run=client
myapp-848b5b879b-7h254 1/1 Running 2 2d myapp pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-d7rjs 1/1 Running 2 2d myapp pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-wv5cz 1/1 Running 2 2d myapp pod-template-hash=4046164356,run=myapp
nginx-deploy-5b595999-tj8ms 1/1 Running 2 2d nginx-deploy pod-template-hash=16151555,run=nginx-deploy
pod-demo 2/2 Running 0 4m myapp app=myapp,tier=frontend
3.kubectl label pods可以為pod增加標籤值。對同一個標籤再次賦值時會報錯,需要使用--overwrite引數。
[[email protected] manifests]# kubectl label pods pod-demo release=canary
pod/pod-demo labeled
[[email protected] manifests]# kubectl get pods -l app --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo 2/2 Running 0 6m app=myapp,release=canary,tier=frontend
[[email protected] manifests]# kubectl label pods pod-demo release=stable
error: 'release' already has a value (canary), and --overwrite is false
[[email protected] manifests]# kubectl label pods pod-demo release=stable --overwrite
pod/pod-demo labeled
[[email protected] manifests]# kubectl get pods -l app --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo 2/2 Running 0 11m app=myapp,release=stable,tier=frontend
4.標籤選擇器中等值關係選擇器的使用。kubectl get pods -l 後面附加等值篩選。或者後面加不等值篩選也可以實現。
[[email protected] manifests]# kubectl get pods -l release=stable --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo 2/2 Running 0 15m app=myapp,release=stable,tier=frontend
[[email protected] manifests]# kubectl get pods -l release!=stable --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client 0/1 Completed 0 2d run=client
myapp-848b5b879b-7h254 1/1 Running 2 2d pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-d7rjs 1/1 Running 2 2d pod-template-hash=4046164356,run=myapp
myapp-848b5b879b-wv5cz 1/1 Running 2 2d pod-template-hash=4046164356,run=myapp
nginx-deploy-5b595999-tj8ms 1/1 Running 2 2d pod-template-hash=16151555,run=nginx-deploy
5.標籤選擇器中集合關係選擇器的使用。kubectl get pods -l後面通過in來篩選標籤屬於某一集合。或者通過notin來篩選標籤屬於某一集合。對於字串中間可能存在的空格,可以使用雙引號。
[[email protected] manifests]# kubectl get pods -l "release in (stable,alpha,beta)"
NAME READY STATUS RESTARTS AGE
pod-demo 2/2 Running 0 25m
[[email protected] manifests]# kubectl get pods -l "release notin (stable,alpha,beta)"
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 2d
myapp-848b5b879b-7h254 1/1 Running 2 2d
myapp-848b5b879b-d7rjs 1/1 Running 2 2d
myapp-848b5b879b-wv5cz 1/1 Running 2 2d
nginx-deploy-5b595999-tj8ms 1/1 Running 2 2d
6.標籤選擇器除了可以給Pod打標籤,也可以用來給node等各種資源打標籤。
[[email protected] manifests]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master.example.com Ready master 3d v1.11.1
node1.example.com Ready <none> 3d v1.11.1
node2.example.com Ready <none> 2d v1.11.1
[[email protected] manifests]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master.example.com Ready master 3d v1.11.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master.example.com,node-role.kubernetes.io/master=
node1.example.com Ready <none> 3d v1.11.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node1.example.com
node2.example.com Ready <none> 2d v1.11.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node2.example.com
[[email protected] manifests]# kubectl label nodes node1.example.com disktype=ssd
node/node1.example.com labeled
[[email protected] manifests]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master.example.com Ready master 3d v1.11.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master.example.com,node-role.kubernetes.io/master=
node1.example.com Ready <none> 3d v1.11.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/hostname=node1.example.com
node2.example.com Ready <none> 2d v1.11.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node2.example.com
7.nodeSelector可以作為節點標籤選擇器,來對pod執行的節點進行篩選。例如在pod-demo.yaml中增加disktype: ssd的引數,則在建立資源的時候nodeSelector會自動篩選符合的節點。
[[email protected] manifests]# kubectl delete -f pod-demo.yaml
pod "pod-demo" deleted
[[email protected] manifests]# vim pod-demo.yaml
[[email protected] 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
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
nodeSelector:
disktype: ssd
[[email protected] manifests]# kubectl create -f pod-demo.yaml
pod/pod-demo created
[[email protected] manifests]# kubectl describe pods pod-demo
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 1m default-scheduler Successfully assigned default/pod-demo to node1.example.com
Normal Pulled 1m kubelet, node1.example.com Container image "ikubernetes/myapp:v1" already present on machine
Normal Created 1m kubelet, node1.example.com Created container
Normal Started 1m kubelet, node1.example.com Started container
Normal Pulled 1m kubelet, node1.example.com Container image "busybox:latest" already present on machine
Normal Created 1m kubelet, node1.example.com Created container
Normal Started 1m kubelet, node1.example.com Started container
8.資源註解。pod-demo.yaml指令碼中通過annotations:來定義註解。kubectl describe pods檢視innotation。
[[email protected] manifests]# kubectl delete -f pod-demo.yaml
pod "pod-demo" deleted
[[email protected] manifests]# vim pod-demo.yaml
[[email protected] manifests]# cat pod-demo.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
tier: frontend
annotations:
example.com/created-by: "cluster admin"
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
- name: https
containerPort: 443
- name: busybox
image: busybox:latest
imagePullPolicy: IfNotPresent
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
nodeSelector:
disktype: ssd
[[email protected] manifests]# kubectl create -f pod-demo.yaml
pod/pod-demo created
[[email protected] manifests]# kubectl describe pods pod-demo
Name: pod-demo
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: node1.example.com/172.20.0.129
Start Time: Mon, 10 Dec 2018 00:48:43 -0500
Labels: app=myapp
tier=frontend
Annotations: example.com/created-by=cluster admin