[kubernetes]-k8s通过initcontainer确保容器依次启动

导语:k8s通过initcontainer确保容器依次启动,避免启动业务容器前mysql还没有用启动,mysql加入了readiness。

apiVersion: apps/v1
kind: Deployment
metadata:
# deployment名字 和svc 和ingress绑定没关系
  name: aiserverconfig-dp
  namespace: default
spec:
  replicas: 1

  # 在定义模板的时候必须定义labels,因为Deployment.spec.selector是必须字段,而他又必须和template.labels对应
  selector:
    matchLabels:
      app: aiserverconfig
      env: default
  # template里面定义的内容会应用到下面所有的副本集里面(例如depolyment下的pod),在template.spec.containers里面不能定义labels标签。可以kubectl get pods --show-labels查看
  template:
    metadata:
      labels:
        app: aiserverconfig
        env: default
    spec:
      hostNetwork: true
      # 使用主机网络
      dnsPolicy: ClusterFirstWithHostNet
#      hostAliases:
#      - ip: 192.168.20.147
#        hostnames:
#        - "datanode1"
#      - ip: 192.168.200.80
#        hostnames:
#        - "bi-web.ihaozhuo.com"
      initContainers:
        - command: ['sh', '-c', "until nslookup mysql-headless.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]
        #- command: ['sh', '-c', 'until ping mysql-headless.default.svc ; do echo waiting for mysql; sleep 2; done;']
          image: busybox:1.28
          #image: yauritux/busybox-curl:latest
          name: wait-for-mysql
        - command: ['sh', '-c', "until nslookup zk-headless.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]
          image: yauritux/busybox-curl:latest
          name: wait-for-zk
      restartPolicy: Always

      containers:


      # containers名字 和svc 和ingress绑定没关系
      - name: aiserverconfig
        image: aiserverconfig:cta_ge_930_2021122_040101
        readinessProbe:
          tcpSocket:
            port: 11383
          initialDelaySeconds: 30
          periodSeconds: 10

        livenessProbe:
          tcpSocket:
            port: 11383
          initialDelaySeconds: 30
          periodSeconds: 10


        volumeMounts:
        - name: log-aiserverconfig       # 指定挂在目录到logdata 同一个pod内的两个应用共享目录logdata, 一个写一个读
          mountPath: /root/aiserverconfig/log
        - name: sign-conf
          mountPath: /etc/sign.conf
          subPath: sign.conf
      volumes:
      - name: log-aiserverconfig         #定义logdata为EmptyDir类型挂载目录
        hostPath:
          path: /data1/container-root/aiserverconfig
          type: DirectoryOrCreate
      - name: sign-conf         #定义logdata为EmptyDir类型挂载目录
        configMap:
          name: sign-conf

# 20200822
#        volumeMounts:
#       # 预留一个/etc/sign.cnf的configmap
#        - name: aiserverconfigd-cnf
#          mountPath: /etc/aiserverconfig/aiserverconfig.conf.d/aiserverconfigd.cnf
#          subPath: aiserverconfigd.cnf
#      volumes:
#      - name: aiserverconfig-cnf         #定义logdata为EmptyDir类型挂载目录
#        configMap:
#          name: aiserverconfig-cnf

#      imagePullSecrets:
#      - name: aliyun-sc-default

官方文档

https://kubernetes.io/zh/docs/concepts/workloads/pods/init-containers/

之前使用mysql-headless.default.svc 一直没通过检测

 command: ['sh', '-c', "until nslookup mysql-headless.default.svc; do echo waiting for myservice; sleep 2; done"]

还试过ping ,ping的通还是初始化状态。