initC特殊说明
查看编辑pod 的配置文件
kubectl edit pod myapp-pod
编辑read.yaml文件
vim read.yaml
apiVersion: v1
kind: Pod
metadata:
name: readiness-httpget-pod
namespace: default
spec:
containers:
- name: readiness-httpget-container
image: hub.atguigu.com/cdzbc/myapp:v1
imagePullPolicy: IfNotPresent
readinessProbe:
httpGet:
port: 80
path: /index1.html
initialDelaySeconds: 1
periodSeconds: 3
执行read.yaml文件
kubectl apply -f read.yaml
kubectl get pod
查看readiness-httpget-pod 运行错误的具体情况
kubectl describe pod readiness-httpget-pod
进入readiness-httpget-pod窗口
kubectl get pod
kubectl exec readiness-httpget-pod -it -- /bin/sh
在readiness-httpget-pod窗口内创建index1.html文件 并写入123字符串
ls
cd /usr/share/nginx/
ls
cd html
ls
echo "123" >> index1.html
ls
exit
查看readiness-httpget-pod的状态
kubectl get pod
编辑live-exec.yaml文件
vim live-exec.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
namespace: default
spec:
containers:
- name: liveness-exec-container
image: busybox
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","touch /tmp/live ; sleep 60; rm -rf /tmp/live; sleep 3600"]
livenessProbe:
exec:
command: ["test","-e","/tmp/live"]
initialDelaySeconds: 1
periodSeconds: 3
执行live-exec.yaml文件之前清除无关pod svc
kubectl get pod
kubectl delete pod --all
kubectl get svc
kubectl delete svc mydb myservice
kubectl apply -f live-exec.yaml
清除无关pod
kubectl get pod -w
kubectl delete pod --all
编辑live-http.yaml文件
vim live-http.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget-pod
namespace: default
spec:
containers:
- name: liveness-httpget-container
image: hub.atguigu.com/cdzbc/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 10
动态获取pod状态
kubectl get pod -o wide
curl 10.244.1.14
kubectl exec liveness-httpget-pod -it -- /bin/sh
rm -rf /usr/share/nginx/html/index.html
exit
curl 10.244.1.14/index.html
kubectl get pod -o wide
进入readiness-httpget-pod窗口 动态获取pod状态
kubectl exec liveness-httpget-pod -it -- rm -rf /usr/share/nginx/html/index.html
kubectl get pod -w
编辑live-tcp.yaml文件
vim live-tcp.yaml
apiVersion: v1
kind: Pod
metadata:
name: probe-tcp
spec:
containers:
- name: nginx
image: hub.atguigu.com/cdzbc/myapp:v1
livenessProbe:
initialDelaySeconds: 5
timeoutSeconds: 1
tcpSocket:
port: 8080
periodSeconds: 3
删除无关pod 执行live-tcp.yaml文件 动态获取pod状态
kubectl delete pod --all
kubectl apply -f live-tcp.yaml
kubectl get pod -w
编辑live-http.yaml文件 整合就绪检测和存活检测关键代码
vim live-http.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget-pod
namespace: default
spec:
containers:
- name: liveness-httpget-container
image: hub.atguigu.com/cdzbc/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
port: 80
path: /index1.html
initialDelaySeconds: 1
periodSeconds: 3
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 10
删除无关pod 执行live-http.yaml文件
kubectl get pod
kubectl delete pod --all
kubectl apply -f live-http.yaml
kubectl get pod
进入readiness-httpget-pod窗口 创建index1.html文件 并写入123字符串
kubectl get pod
kubectl exec liveness-httpget-pod -it -- /bin/sh
cd /usr/share/nginx/html/
ls
echo "123" >> index1.html
exit
kubectl get pod
删除index1.html文件 动态获取pod状态
kubectl exec liveness-httpget-pod -it -- rm -rf /usr/share/nginx/html/index1.html
kubectl get pod -w
编辑post.yaml文件
vim post.yaml
apiVersion: v1
kind: Pod
metadata:
name: lifecycle-demo
spec:
containers:
- name: lifecycle-demo-container
image: hub.atguigu.com/cdzbc/myapp:v1
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
preStop:
exec:
command: ["/bin/sh", "-c", "echo Hello from the poststop handler > /usr/share/message"]
执行post.yaml文件 查看pod日志 进入pod窗口 查看设置信息
kubectl apply -f post.yaml
kubectl get pod
kubectl log lifecycle-demo
kubectl exec lifecycle-demo -it -- /bin/sh
cat /usr/share/message
学习笔记来源。1
尚硅谷kubernetes教程 ↩︎