最小的单元
,它由一组、一个或多个容器组成apiVersion: v1 # 必选,API的版本号
kind: Pod # 必选,类型Pod
metadata: # 必选,元数据
name: nginx # 必选,符合RFC 1035规范的Pod名称
namespace: default # 可选,Pod所在的命名空间,不指定默认为default,可以使用-n 指定namespace
labels: # 可选,标签选择器,一般用于过滤和区分Pod
app: nginx
role: frontend # 可以写多个
annotations: # 可选,注释列表,可以写多个
app: nginx
spec: # 必选,用于定义容器的详细信息
initContainers: # 初始化容器,在容器启动之前执行的一些初始化操作
- command:
- sh
- -c
- echo "I am InitContainer for init some configuration"
image: busybox
imagePullPolicy: IfNotPresent
name: init-container
containers: # 必选,容器列表
- name: nginx # 必选,符合RFC 1035规范的容器名称
image: nginx:latest # 必选,容器所用的镜像的地址
imagePullPolicy: Always # 可选,镜像拉取策略
command: # 可选,容器启动执行的命令
- nginx
- -g
- "daemon off;"
workingDir: /usr/share/nginx/html # 可选,容器的工作目录
volumeMounts: # 可选,存储卷配置,可以配置多个
- name: webroot # 存储卷名称
mountPath: /usr/share/nginx/html # 挂载目录
readOnly: true # 只读
ports: # 可选,容器需要暴露的端口号列表
- name: http # 端口名称
containerPort: 80 # 端口号
protocol: TCP # 端口协议,默认TCP
env: # 可选,环境变量配置列表
- name: TZ # 变量名
value: Asia/Shanghai # 变量的值
- name: LANG
value: en_US.utf8
resources: # 可选,资源限制和资源请求限制
limits: # 最大限制设置
cpu: 1000m
memory: 1024Mi
requests: # 启动所需的资源
cpu: 100m
memory: 512Mi
# startupProbe: # 可选,检测容器内进程是否完成启动。注意三种检查方式同时只能使用一种。
# httpGet: # httpGet检测方式,生产环境建议使用httpGet实现接口级健康检查,健康检查由应用程序提供。
# path: /api/successStart # 检查路径
# port: 80
readinessProbe: # 可选,健康检查。注意三种检查方式同时只能使用一种。
httpGet: # httpGet检测方式,生产环境建议使用httpGet实现接口级健康检查,健康检查由应用程序提供。
path: / # 检查路径
port: 80 # 监控端口
livenessProbe: # 可选,健康检查
#exec: # 执行容器命令检测方式
#command:
#- cat
#- /health
#httpGet: # httpGet检测方式
# path: /_health # 检查路径
# port: 8080
# httpHeaders: # 检查的请求头
# - name: end-user
# value: Jason
tcpSocket: # 端口检测方式
port: 80
initialDelaySeconds: 60 # 初始化时间
timeoutSeconds: 2 # 超时时间
periodSeconds: 5 # 检测间隔
successThreshold: 1 # 检查成功为2次表示就绪
failureThreshold: 2 # 检测失败1次表示未就绪
lifecycle:
postStart: # 容器创建完成后执行的指令, 可以是exec httpGet TCPSocket
exec:
command:
- sh
- -c
- 'mkdir /data/ '
preStop:
httpGet:
path: /
port: 80
# exec:
# command:
# - sh
# - -c
# - sleep 9
restartPolicy: Always # 可选,默认为Always
#nodeSelector: # 可选,指定Node节点
# region: subnet7
imagePullSecrets: # 可选,拉取镜像使用的secret,可以配置多个
- name: default-dockercfg-86258
hostNetwork: false # 可选,是否为主机模式,如是,会占用主机端口
volumes: # 共享存储卷列表
- name: webroot # 名称,与上述对应
emptyDir: {} # 挂载目录
#hostPath: # 挂载本机目录
# path: /etc/hosts
vi pod.yaml #定义一个pod
apiVersion: v1 #必选,api版本号,通常是v1
kind: Pod #必选,类型为pod
metadata: #必选,元数据
name: nginx #必选,符合RFC 1035规范的pod名称
labels: #可选,标签选择器,一般用于过滤和区分pod,可以写多个注释,键值对类型
app: nginx
role: frontend
annotations: #可选,注释列表,可以写多个,键值对类型
app: nginx
spec: #必选,定义容器的详细信息
containers: #必选容器列表
- name: nginx #必选符合RFC 1035规范的容器名称
image: nginx:1.15.2 #必选,容器使用的镜像的名称
imagePullPolicy: IfNotPresent #可选,镜像拉取策略,ifNotPresent为当主机有这个镜像就不拉取了,没有则拉取
command: #可选,容器启动时命令,不指定则使用镜像打包时的启动命令
- nginx
- -g
- "daemon off;"
workingDir: /usr/share/nginx/html #可选,容器的工作目录,不指定则使用镜像打包时的默认目录
ports: #可选,容器需要暴露的端口号列表
- name: http #端口名称
containerPort: 80 #端口号
protocol: TCP #端口协议
env: #可选,配置环境变量,可写多个
- name: TZ #变量名称
value: Asia/Shanghai #变量值
- name: LANG
value: en_US.utf8
restartPolicy: Always #可选,容器重启策略,always为容器不管什么原因终止,都进行重启
[root@master1 yn-wxb-app]# kubectl create -f test.yaml
pod/nginx created
[root@master1 yn-wxb-app]# kubectl get pods -A #查看所有命名空间的pod
NAMESPACE NAME READY STATUS RESTARTS AGE
default nginx 0/1 ContainerCreating 0 59s
kube-system calico-kube-controllers-57546b46d6-qx4vm 1/1 Running 1 6d
kube-system calico-node-gr2hd 1/1 Running 1 6d
kube-system calico-node-rdrwb 1/1 Running 0 5d3h
kube-system coredns-7ff77c879f-hl7mf 1/1 Running 1 6d
kube-system coredns-7ff77c879f-tv8tx 1/1 Running 1 6d
kube-system etcd-master1 1/1 Running 1 6d
kube-system kube-apiserver-master1 1/1 Running 1 6d
kube-system kube-controller-manager-master1 1/1 Running 1 6d
kube-system kube-proxy-drmds 1/1 Running 1 6d
kube-system kube-proxy-z9nd7 1/1 Running 0 5d3h
kube-system kube-scheduler-master1 1/1 Running 2 6d
[root@master1 yn-wxb-app]# kubectl get pods #默认查看defaults命名空间的pod,因为yaml文件没有指定,所以创建的pod默认就是defaults命名空间的
NAME READY STATUS RESTARTS AGE
nginx 0/1 ContainerCreating 0 63s
[root@master1 yn-wxb-app]# kubectl get pod --show-labels #查看当前defaults命名空间所有pod标签
NAME READY STATUS RESTARTS AGE LABELS
nginx 0/1 ImagePullBackOff 0 2m23s app=nginx,role=frontend
[root@master1 yn-wxb-app]# kubectl get pod --show-labels -A #查看所有命名空间pod的标签
NAMESPACE NAME READY STATUS RESTARTS AGE LABELS
default nginx 1/1 Running 0 3m50s app=nginx,role=frontend
kube-system calico-kube-controllers-57546b46d6-qx4vm 1/1 Running 1 6d k8s-app=calico-kube-controllers,pod-template-hash=57546b46d6
kube-system calico-node-gr2hd 1/1 Running 1 6d controller-revision-hash=64fbccd6f7,k8s-app=calico-node,pod-template-generation=1
kube-system calico-node-rdrwb 1/1 Running 0 5d3h controller-revision-hash=64fbccd6f7,k8s-app=calico-node,pod-template-generation=1
kube-system coredns-7ff77c879f-hl7mf 1/1 Running 1 6d k8s-app=kube-dns,pod-template-hash=7ff77c879f
kube-system coredns-7ff77c879f-tv8tx 1/1 Running 1 6d k8s-app=kube-dns,pod-template-hash=7ff77c879f
kube-system etcd-master1 1/1 Running 1 6d component=etcd,tier=control-plane
kube-system kube-apiserver-master1 1/1 Running 1 6d component=kube-apiserver,tier=control-plane
kube-system kube-controller-manager-master1 1/1 Running 1 6d component=kube-controller-manager,tier=control-plane
kube-system kube-proxy-drmds 1/1 Running 1 6d controller-revision-hash=9875f5fb,k8s-app=kube-proxy,pod-template-generation=1
kube-system kube-proxy-z9nd7 1/1 Running 0 5d3h controller-revision-hash=9875f5fb,k8s-app=kube-proxy,pod-template-generation=1
kube-system kube-scheduler-master1 1/1 Running 2 6d component=kube-scheduler,tier=control-plane
其实就是筛选拥有指定标签的pod
[root@master1 yn-wxb-app]# kubectl get pod -l app=nginx #查看标签
NAME READY STATUS RESTARTS AGE
nginx 0/1 ImagePullBackOff 0 3m15s
[root@master1 yn-wxb-app]# kubectl get pod -l component=etcd -A #加-A查看所有命名空间的pod
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system etcd-master1 1/1 Running 1 6d
[root@master1 yn-wxb-app]# kubectl get pod -l component=etcd #这里看不到就是因为默认是defaults命名空间
No resources found in default namespace.
注意:默认创建的pod在命名空间default中,也可以将pod创建到其它命名空间
[root@master01 ~]# kubectl delete pod nginx -n dgf #-n 指定命名空间
删除pod的流程:
- Pod状态会变成Terminating状态(这种状态停留时间默认30秒)
- 在30秒期间,会从Endpoint中删除该Pod的IP地址
- 最后执行yaml文件中PreStop中的指令
[root@master1 yn-wxb-app]# kubectl get ns
NAME STATUS AGE
default Active 6d
kube-node-lease Active 6d
kube-public Active 6d
kube-system Active 6d
[root@master1 yn-wxb-app]# kubectl get ns -A
NAME STATUS AGE
default Active 6d
kube-node-lease Active 6d
kube-public Active 6d
kube-system Active 6d
[root@master1 yn-wxb-app]# kubectl create ns test
namespace/test created
[root@master1 yn-wxb-app]# kubectl create -f test.yaml -n test #使用-n或者在yaml文件中指定,都可以指定pod的命名空间
pod/nginx created
[root@master1 yn-wxb-app]# kubectl get pod -n test #-n指定查看的命名空间的pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 13s
[root@master1 yn-wxb-app]# kubectl get pod -A #-A查看所有命名空间的pod
NAMESPACE NAME READY STATUS RESTARTS AGE
default nginx 1/1 Running 0 14m
kube-system calico-kube-controllers-57546b46d6-qx4vm 1/1 Running 1 6d
kube-system calico-node-gr2hd 1/1 Running 1 6d
kube-system calico-node-rdrwb 1/1 Running 0 5d3h
kube-system coredns-7ff77c879f-hl7mf 1/1 Running 1 6d
kube-system coredns-7ff77c879f-tv8tx 1/1 Running 1 6d
kube-system etcd-master1 1/1 Running 1 6d
kube-system kube-apiserver-master1 1/1 Running 1 6d
kube-system kube-controller-manager-master1 1/1 Running 1 6d
kube-system kube-proxy-drmds 1/1 Running 1 6d
kube-system kube-proxy-z9nd7 1/1 Running 0 5d3h
kube-system kube-scheduler-master1 1/1 Running 2 6d
test nginx 1/1 Running 0 18s
[root@master1 yn-wxb-app]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 50s
[root@master1 yn-wxb-app]# kubectl delete pod nginx -n test
pod "nginx" deleted
[root@master1 yn-wxb-app]# kubectl get pod -n test
No resources found in test namespace.
[root@master1 yn-wxb-app]# kubectl delete ns test #删除指定命名空间
namespace "test" deleted
[root@master1 yn-wxb-app]# kubectl get ns
NAME STATUS AGE
default Active 6d
kube-node-lease Active 6d
kube-public Active 6d
kube-system Active 6d
[root@master1 yn-wxb-app]# kubectl get ns -A
NAME STATUS AGE
default Active 6d
kube-node-lease Active 6d
kube-public Active 6d
kube-system Active 6d
健康检查又称为Pod探针(Probe),探针是由kubelet对容器执行的定期诊断
StartupProbe
K8S 1.16版本后新加的探测方式,
主要针对不能确定具体启动时间的pod
,配置了StartupProbe探测方式后,在StartupProbe探测状态为Success成功
之前,其他的所有探测方式都处于无效状态
,只有StartupProbe探测方式状态为Success成功,其他探测方式才会生效
,注意:如果StartupProbe状态为失败,kubelet将会杀死容器,容器会按照
restartPolicy重启策略进行重启
如果没有配置StartupProbe探测方式,那么状态默认就是Success成功
LivenessProbe(存活探针)
此探针用于判断容器是否正在运行,即是否需要重启
,如果探测失败,kubelet会杀死容器并根据restartPolicy重启策略进行相应处理
,如果没有配置该探针,那么状态默认就是Success
ReadinessProbe(可读性探针、就绪性探针)
此探针判断容器是否准备好接收请求,也就是判断pod是否能够提供正常服务
,一般用于探测容器内的程序是否健康,它的返回值如果为success,那么就代表这个容器已经完成启动,并且程序已经是可以接受流量的状态,如果没有配置,则状态默认为success
注意:上面的探针可以同时定义,在readinessProbe探针检测成功之前,Pod的running状态是不会变成 ready 状态的
ExecAction
在容器内执行一个命令,如果返回值为0,则认为容器健康
TCPSocketAction
通过TCP连接,检查容器内的端口通信是否是正常,正常则认为容器是健康的
HTTPGetAction
利用HTTP GET请求
,通过应用程序暴露的API地址来检查程序是否是正常的,如果状态码为200~399之间,则认为容器健康。注意:检测的是路径上的文件是否存在
上面的探测方式在每次探测后都会获取下面三种状态之一:
成功
:容器通过了检测失败
:容器没有通过检测未知
:检测失败,不会采取任何操作
apiVersion: v1 #必选,api版本号,通常是v1
kind: Pod #必选,类型为pod
metadata: #必选,元数据
name: nginx #必选,符合RFC 1035规范的pod名称
labels: #可选,标签选择器,一般用于过滤和区分pod,可以写多个注释,键值对类型
app: nginx
role: frontend
annotations: #可选,注释列表,可以写多个,键值对类型
app: nginx
spec: #必选,定义容器的详细信息
containers: #必选容器列表
- name: nginx #必选符合RFC 1035规范的容器名称
image: nginx:1.15.2 #必选,容器使用的镜像的名称
imagePullPolicy: IfNotPresent #可选,镜像拉取策略,ifNotPresent为当主机有这个镜像就不拉取了,没有则拉取
command: #可选,容器启动时命令,不指定则使用镜像打包时的启动命令
- nginx
- -g
- "daemon off;"
workingDir: /usr/share/nginx/html #可选,容器的工作目录,不指定则使用镜像打包时的默认目录
ports: #可选,容器需要暴露的端口号列表
- name: http #端口名称
containerPort: 80 #端口号
protocol: TCP #端口协议
env: #可选,配置环境变量,可写多个
- name: TZ #变量名称
value: Asia/Shanghai #变量值
- name: LANG
value: en_US.utf8
restartPolicy: Always #可选,容器重启策略,always为容器不管什么原因终止,都进行重启
[root@master1 ~]# cat test.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
role: frontend
annotations:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.2
imagePullPolicy: IfNotPresent
command:
- nginx
- -g
- "daemon off;"
workingDir: /usr/share/nginx/html
ports:
- name: http
containerPort: 80
protocol: TCP
env:
- name: TZ
value: Asia/Shanghai
- name: LANG
value: en_US.utf8
livenessProves:
exec: #命令检测
command:
- curl 127.0.0.1 #访问本地,nginx是否成功启动
failureThreshold: 1
initialDelaySeconds: 5
periodSeconds: 5
restartPolicy: Always
#注释
存活探针'livenessProves'写在与'containers'同一级,下面来看存活探针中包含的配置:
(1)'initialDelaySeconds':容器启动,探针延后工作,默认是0秒,即kubelet在执行第一次探测前,会延迟等待n秒后开始探测,这里配置的意思是第一次探测在容器启动5秒之后才会开始执行,默认是0秒,最小值为0
(2)'periodSeconds':探测检测周期,用于指定kubelet执行存活探针的周期时间,默认为10秒,最小值为1秒,这里配置为5秒探测一次
(3)'failureThreshold':当探针检测为失败时,k8s在放弃之前重试的次数,默认为3,最小值为1,'存活探测的放弃是指重启容器,而就绪探测的放弃是指pod会打上未就绪的标签'
(4)'timeoutSeconds':指定探针检测超时后等待多少秒,默认值为1,最小值为1,'需要注意:在Kubernetes 1.20版本之前,exec 探针会忽略timeoutSeconds探针会无限期地持续运行,甚至可能超过所配置的限期,直到返回结果为止'
#创建pod
[root@master1 ~]# kubectl create -f test.yaml
pod/nginx created
[root@master1 ~]# kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
default nginx 1/1 Running 0 2s
kube-system calico-kube-controllers-57546b46d6-qx4vm 1/1 Running 1 8d
kube-system calico-node-gr2hd 1/1 Running 1 8d
kube-system calico-node-rdrwb 1/1 Running 0 7d6h
kube-system coredns-7ff77c879f-hl7mf 1/1 Running 1 8d
kube-system coredns-7ff77c879f-tv8tx 1/1 Running 1 8d
kube-system etcd-master1 1/1 Running 1 8d
kube-system kube-apiserver-master1 1/1 Running 1 8d
kube-system kube-controller-manager-master1 1/1 Running 1 8d
kube-system kube-proxy-drmds 1/1 Running 1 8d
kube-system kube-proxy-z9nd7 1/1 Running 0 7d6h
kube-system kube-scheduler-master1 1/1 Running 2 8d
[root@master1 ~]#vim test.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
role: frontend
annotations:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.2
imagePullPolicy: IfNotPresent
command:
- nginx
- -g
- "daemon off;"
workingDir: /usr/share/nginx/html
ports:
- name: http
containerPort: 80
protocol: TCP
env:
- name: TZ
value: Asia/Shanghai
- name: LANG
value: en_US.utf8
livenessProbe:
httpGet: #http检测
path: /index.html
port: http
failureThreshold: 1
initialDelaySeconds: 5
periodSeconds: 5
restartPolicy: Always
[root@master1 ~]# kubectl create -f test.yaml
pod/nginx created
[root@master1 ~]# kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
default nginx 1/1 Running 0 2s
kube-system calico-kube-controllers-57546b46d6-qx4vm 1/1 Running 1 8d
kube-system calico-node-gr2hd 1/1 Running 1 8d
kube-system calico-node-rdrwb 1/1 Running 0 7d6h
kube-system coredns-7ff77c879f-hl7mf 1/1 Running 1 8d
kube-system coredns-7ff77c879f-tv8tx 1/1 Running 1 8d
kube-system etcd-master1 1/1 Running 1 8d
kube-system kube-apiserver-master1 1/1 Running 1 8d
kube-system kube-controller-manager-master1 1/1 Running 1 8d
kube-system kube-proxy-drmds 1/1 Running 1 8d
kube-system kube-proxy-z9nd7 1/1 Running 0 7d6h
kube-system kube-scheduler-master1 1/1 Running 2 8d
[root@master1 yn-wxb-app]# cat test.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
role: frontend
annotations:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.2
imagePullPolicy: IfNotPresent
workingDir: /usr/share/nginx/html
ports:
- name: http
containerPort: 80
protocol: TCP
env:
- name: TZ
value: Asia/Shanghai
- name: LANG
value: en_US.utf8
livenessProbe:
tcpSocket: #tcp检测
port: http
failureThreshold: 1
initialDelaySeconds: 5
periodSeconds: 5
restartPolicy: Always
[root@master1 yn-wxb-app]# kubectl create -f test.yaml
pod/nginx created
[root@master1 yn-wxb-app]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 1 8s
[root@master1 yn-wxb-app]# cat test.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
role: frontend
annotations:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.2
imagePullPolicy: IfNotPresent
workingDir: /usr/share/nginx/html
ports:
- name: http
containerPort: 80
protocol: TCP
env:
- name: TZ
value: Asia/Shanghai
- name: LANG
value: en_US.utf8
readinessProbe: #就绪探针
httpGet:
port: http
path: /index.html
failureThreshold: 1
initialDelaySeconds: 5
periodSeconds: 5
restartPolicy: Always
[root@master1 yn-wxb-app]# kubectl create -f test.yaml
pod/nginx created
[root@master1 yn-wxb-app]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 Running 1 2s