Init模板
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox
command: ['sh','-c','echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox
command: ['sh','-c','until nslookup myservice; do echo waiting for myservice; sleep 2;done;']
- name: init-mydb
image: busybox
command: ['sh','-c','until nslookup mydb; do echo waiting for mydb; sleep 2; done;']
kind: Service
apiVersion: v1
metadata:
name: myservice
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 9376
---
kind: Service
apiVersion: v1
metadata:
name: mydb
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 9377
检测探针-就绪检测
readinessProbe-httpget
apiVersion: apps/v1
kind: Deployment
metadata:
name: readiness-httpget-pod
namespace: lzulms
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: readiness-httpget-container
image: reg.chinaedu.net/hetaotao/myapp:v1
imagePullPolicy: IfNotPresent # 镜像下载策略
readinessProbe: # 就绪检测
httpGet: # 检测http
port: 80
path: /index1.html
initialDelaySeconds: 1 # 初始化检测延时(容器启动多少秒以后开始检测)
periodSeconds: 3 # 多少秒检测一次
successThreshold: 1 # 陈功,success=1,表示一次响应说明服务正常
timeoutSeconds: 1 # 超时,timeout=1s,表示容器必须在1s内进行响应,否则这次探测记作失败
failureThreshold: 3 # 失败,failure=3,连续3次失败后重启容器
检测探针 - 存活检测
livenessProbe-exec
apiVersion: apps/v1
kind: Deployment
metadata:
name: liveness-exec-pod
namespace: lzulms
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: liveness-exec-container
image: busybox:1.28.4
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","touch /tmp/live ; sleep 20; rm -rf /tmp/live; sleep3600"]
livenessProbe:
exec:
command: ["test","-e","/tmp/live"]
initialDelaySeconds: 1
periodSeconds: 3
livenessProbe-httpget
apiVersion: apps/v1
kind: Deployment
metadata:
name: liveness-httpget-pod
namespace: lzulms
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: liveness-httpget-container
image: reg.chinaedu.net/hetaotao/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 10
livenessProbe-tcp
apiVersion: apps/v1
kind: Deployment
metadata:
name: probe-tcp
namespace: lzulms
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: nginx
image: reg.chinaedu.net/hetaotao/myapp:v1
livenessProbe:
initialDelaySeconds: 5
timeoutSeconds: 1
tcpSocket:
port: 80
启动、退出动作
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: lifecycle-demo
namespace: lzulms
spec:
serviceName: "mongodb"
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: reg.chinaedu.net/public/mongo4.2.2:latest
volumeMounts:
- mountPath: /data/backup
name: volume-test
env:
- name: MONGO_INITDB_ROOT_USERNAME
value: 'root'
- name: MONGO_INITDB_ROOT_PASSWORD
value: 'edu@china'
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo `date` Hello from the poststart handler >> /data/backup/test.txt"]
preStop:
exec:
command: ["/bin/sh", "-c", "mongodump -u root -p edu@china --authenticationDatabase admin -o /data/backup/`date '+%F
-%T'`"]
volumes:
- hostPath:
path: /nfs/yaml/hetaotao/peixun-k8s/init-c/mongodb-backup
name: volume-test
imagePullSecrets:
- name: lzulms-harbor