Pod对象是一组容器的集合,这些容器共享Network、UTS及IPC名称空间,因此具有相同的域名、主机名和网络接口,并可通过IPC直接通信
为一个Pod对象中的各容器提供网络名称空间等共享机制的是底层基础容器pause
,一个Pod对象中的多个容器必须运行于同一工作节点之上
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-ds-7gmf9 1/1 Running 31 39d
nginx-ds-hnv6g 1/1 Running 4 2d1h
wordpress-7f95769f89-rzmcm 1/1 Running 4 46h
docker ps |grep wordpress-7f95769f89-rzmcm
db29c2b3c4b8 df73d804e139 "/wordpress_entrypoi…" 10 minutes ago Up 10 minutes k8s_wordpress_wordpress-7f95769f89-rzmcm_default_081b4b0c-bc3d-4db0-9638-4020fe4c5b04_4
adafaeadec13 harbor.od.com/public/pause:latest "/pause" 10 minutes ago Up 10 minutes k8s_POD_wordpress-7f95769f89-rzmcm_default_081b4b0c-bc3d-4db0-9638-4020fe4c5b04_4
一级字段:
pod-daemon.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
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
explain
kubectl explain deployments.spec.template.spec.containers.image
kubectl explain deployments.spec.template.spec.containers.imagePullPolicy
spec.containers <[]object>
- name >
image: >
imagePullPolicy: , Never, IfNotPresent)>
修改镜像中的默认应用:
command, args
https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/
Docker field name | Kubernetes field name |
---|---|
Entrypoint | command |
Cmd | args |
Image Entrypoint | Image Cmd | Container command | Container args | Command run |
---|---|---|---|---|
[/ep-1] | [foo bar] | [ep-1 foo bar] | ||
[/ep-1] | [foo bar] | [/ep-2] | [ep-2] | |
[/ep-1] | [foo bar] | [zoo boo] | [ep-1 zoo boo] | |
[/ep-1] | [foo bar] | [/ep-2] | [zoo boo] | [ep-2 zoo boo] |
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
protocol: TCP
注意:
hostPort与NodePort类型的Service对象暴露端口的方式不同,
NodePort是通过所有节点暴露容器服务,
而hostPort则是经由Pod对象所在节点的IP地址来进行
资源与标签对应关系 : 多对对
key=value
key: 字母 数字 _ - . (字母或数字开头)
value: 可以为空, 只能字母或数字开头及结尾
查看标签
kubectl get pods -l app --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 5 39d app=web
pod-demo 2/2 Running 0 4m12s app=myapp,tier=frontend
kubectl get pods -l release,app --show-labels
kubectl get pods -L app,run
NAME READY STATUS RESTARTS AGE APP RUN
busybox 1/1 Running 624 38d
client 1/1 Running 0 4d16h client
myapp-9b4987d5-5trrs 1/1 Running 0 4d15h myapp
myapp-9b4987d5-6shwd 1/1 Running 0 4d15h myapp
myapp-9b4987d5-9xstm 1/1 Running 0 4d15h myapp
myapp-9b4987d5-h5k2d 1/1 Running 0 4d15h myapp
myapp-9b4987d5-jw55t 1/1 Running 0 4d15h myapp
nginx 1/1 Running 5 39d web
nginx-deploy-84cbfc56b6-j5wbr 1/1 Running 0 4d16h nginx-deploy
nginx-deploy-84cbfc56b6-tjmzz 1/1 Running 0 4d16h nginx-deploy
pod-demo 2/2 Running 0 6m32s myapp
打标签
kubectl label pods pod-demo release=canary
kubectl label pods nginx-deploy-84cbfc56b6-j5wbr release=canary
已有标签覆盖
kubectl label pods pod-demo release=stable --overwrite
=
==
!=
kubectl get pods -l release=stable --show-labels
kubectl get pods -l release=stable,app=myapp --show-labels
KEY in (VALUE1,VALUE2,...)
KEY notin (VALUE1,VALUE2,...)
!KEY
kubectl get pods -l "release in (canary,beta,alpha)"
kubectl get pods -l "release notin (canary,beta,alpha)"
matchLables: 直接给定键值
matchExpressions: 基于给定的表达式来定义使用标签选择器 {key:“KEY”, operator:“OPERATOR”, values:[VAL1,VAL2,…]}
操作符:
In NotIn : values字段值必须为非空列表
Exists NotExists : values字段的值必须为空列表
nodeSelector
节点打标签
kubectl label nodes 10.0.0.12 disktype=ssd
nodeSelector:
disktype: ssd
nodeName
annotations: 与label不同的地方在于它不能用于挑选资源对象,仅用于为对象提供"元数据"
annotations:
wuxingge.org/created-by: "cluster admin"
Pending 已经创建,但没有适合运行的节点(调度没有完成)
Running
Failed
Succeded
Unknown
pod生命周期中的重要行为:
cat liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
namespace: default
spec:
containers:
- name: liveness-exec-container
image: busybox:latest
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","touch /tmp/healthy; sleep 60; rm -f /tmp/healthy;sleep 3600"]
livenessProbe:
exec:
command: ["test","-e","/tmp/healthy"]
initialDelaySeconds: 1
periodSeconds: 3
cat liveness-httpGet.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget-pod
namespace: default
spec:
containers:
- name: liveness-httpget-container
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
进入pod容器
kubectl exec -it liveness-httpget-pod -- /bin/sh
cat readiness-httpGet.yaml
apiVersion: v1
kind: Pod
metadata:
name: readiness-httpget-pod
namespace: default
spec:
containers:
- name: readiness-httpget-container
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
kubectl explain pods.spec.containers.lifecycle
cat poststart-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: poststart-pod
namespace: default
spec:
containers:
- name: busybox-httpd
image: busybox:latest
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: ["mkdir","-p","/data/web/html"]
command: ["/bin/sh","-c","sleep 3600"]
restartPolicy
Always, OnFailure,Never Default to Always
apiVersion , kind , metadata , spec , status(只读)
spec:
containers:
name
image
imagePullPolicy: Always , Never , IfNotPresent
port:
name
containerPort
livenessProbe
readinessProbe
lifecycle
nodeSelecter
nodeName
restartPolicy:
Always , Never , OnFailure