Kubernetes Init c探测

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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维生涯记录

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值