yaml基本语法 && 静态pod && pod生命周期 && pod健康检测
yaml 文件用法
https://yaml.org
hrjr.yaml
apiVersion: v1
kind: Pod
metadata:
name: hrjr-web
labels:
app: web
spec:
containers:
- name: front-end
image: nginx
ports:
- containerPort: 80
kubectl describe pod hrjr-web
19 静态 Pod
1)修改配置文件生成静态pod
2)通过url地址来生成静态pod
systemctl status kubelet
配置文件路径:
cd etc/systemd/system/kubelet.service.d
vi 10-kubeadm.conf
KUBELET_SYSTEM_PODS_ARGS=--pod-manifest-path=/etc/kubernetes/manifests --allow-privileged=true
生成静态pod的yaml文件放置的位置: 文件在pod在,文件移走pod消失;
cat <<EOF >/etc/kubernetes/manifest/static-web.yaml
apiVersion: v1
kind: Pod
metadata:
name: static-web
labels:
app: static
spec:
containers:
- name: web
image: nginx
ports:
- name: web
containerPort: 80
EOF
kubectl get pods
kubectl delete pod static-pod1-node01
kubectl delete pod haimaxy-pod
kubectl get pods
20 pod hook pod的生命周期
hook 钩子函数
PostStart
PreStop
vi hook-demo1.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: hook-demo1
labels:
app: hook
spec:
containers:
- name: hook-demo1
image: nginx
ports:
- name: webport
containerPort: 80
lifecycle:
postStart:
exec:
command: ["/bin/sh","-c","echo Hello from the postStart Handler > /usr/share/message"]
kubectl apply -f hook-demo1.yaml
kubectl describe pods hook-demo1
kubectl exec hook-demo1 -i -t /bin/bash
cd /usr/share
cat message
强制删除pod
kubectl delete pod hook-demo1 --grace-period=0 --force
preStop 优雅的退出pod
---
apiVersion: v1
kind: Pod
metadata:
name: hook-demo2
labels:
app: hook
spec:
containers:
- name: hook-demo2
image: nginx
ports:
- name: webport
containerPort: 80
volumeMounts:
- name: message
mountPath: /usr/share/
lifecycle:
preStop:
exec:
command: ['/bin/sh','-c','echo Hello from the preStop Handler > /usr/share/message']
volumes:
- name: message
hostPath:
path: /tmp
kubectl apply -f hook-demo2.yaml
kubectl get pods
kubectl describe pod hook-demo2
kubectl delete pod hook-demo2
kubectl delete -f hook-demo2.yaml
到node01 去/tmp下查看
message
21 Pod的健康检查
liveness probe(存活探针)
readiness probe(可读性探针)
配置方式:
exec 执行一段命令, 命令返回结果判定
http 检测某个 http 请求,请求结果返回判定
tcpSocket kubectl 尝试在指定的端口上打开容器的套接字,即 端口存活判定
1) exec方式
liveness-exec.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec
labels:
app: liveness
spec:
containers:
- name: liveness
image: busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy;sleep 30; rm -rf /tmp/healthy; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
kubectl apply -f liveness-exec.yaml
kubectl get pods
kubectl describe pod liveness-exec
强制删除pod
kubectl delete pod hook-demo1 --grace-period=0 --force
2)http 检测方式
---
apiVersion: v1
kind: Pod
metadata:
name: liveness-http
labels:
app: liveness
spec:
containers:
- name: liveness
image: cnych/liveness
args:
- /server
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
3) tcpSocket 检测方式
---
apiVersion: v1
kind: Pod
metadata:
name: liveness-readiness
labels:
app: liveness-readiness
spec:
containers:
- name: liveness-readiness
image: cnych/liveness
args:
- /server
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
readinessProbe:
tcpSocket:
port:8080
initialDelaySeconds: 3
perioidSeconds: 3
---
apiVersion: v1
kind: Pod
metadata:
name: liveness-readiness
labels:
app: liveness-readiness
spec:
containers:
- name: liveness-readiness
image: cnych/liveness
args:
- /server
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
readinessProbe:
tcpSocket:
port:8080
initialDelaySeconds: 3
perioidSeconds: 3