K8s初始化容器重新启动

时间:2020-05-31 15:00:39

标签: kubernetes

我有一个包含一个初始化容器和一个应用容器的容器, 它们之间有一个带有共享文件夹的卷。

我的问题是,init容器每天运行一次甚至更多,因此会自动运行,因此会从卷中删除节点模块,然后主应用程序会因为缺少模块而崩溃。 该应用程序容器不会进行任何重新启动,只会进行初始化容器。

有人在k8s中熟悉此问题吗?为什么这些重启仅发生在init容器中?

谢谢:)

编辑: 部署yaml文件-

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "25"
  creationTimestamp: "2020-05-19T06:48:18Z"
  generation: 25
  labels:
    apps: ******
    commit: ******
  name: *******
  namespace: fleet
  resourceVersion: "24059934"
  selfLink: *******
  uid: *******
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: *******
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: *******
        commit: *******
        revision: *******
    spec:
      containers:
        image: XXXXXXXXXXXX
        imagePullPolicy: IfNotPresent
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /ping
            port: http
            scheme: HTTP
          initialDelaySeconds: 120
          periodSeconds: 30
          successThreshold: 1
          timeoutSeconds: 30
        name: *******
        ports:
        - containerPort: 1880
          name: http
          protocol: TCP
        readinessProbe:
          failureThreshold: 20
          httpGet:
            path: /ping
            port: http
            scheme: HTTP
          initialDelaySeconds: 20
          periodSeconds: 5
          successThreshold: 1
          timeoutSeconds: 30
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /opt/breeze
          name: workdir
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: flowregistrykey
      initContainers:
        image: XXXXXXXXXXXX
        imagePullPolicy: IfNotPresent
        name: get-flow-json
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /work-dir
          name: workdir
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - emptyDir: {}
        name: workdir
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2020-06-01T12:30:10Z"
    lastUpdateTime: "2020-06-01T12:30:10Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2020-05-19T06:48:18Z"
    lastUpdateTime: "2020-06-01T12:45:05Z"
    message: ReplicaSet "collection-associator.sandbox.services.collection-8784dcb9d"
      has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 25
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

,此图片说明了问题- 该pod是在7天前创建的,但是今天已经创建了其中的文件,并且没有node_modules文件夹-因为只有init容器再次运行,而app容器没有运行,因此没有mpm install

enter image description here

1 个答案:

答案 0 :(得分:1)

init容器自身运行,因此它从卷中删除了节点模块

initContainer仅在Pod重新启动时运行。不要将其视为服务或应用程序。它应该是一个脚本,仅是在安装应用程序之前进行设置的工作。

然后由于缺少模块导致主应用程序崩溃。

node_modules不是动态加载。当您npm start

加载时

您可能想尝试livenessProbe

许多长时间运行的应用程序最终会转换为损坏的状态,除非重新启动,否则无法恢复。 Kubernetes提供了活动性探针来检测和纠正这种情况。

initContainers:
  - name: set-delete-time
    command: ["/bin/sh","-c"]
    args: 
      - |
        # Let's set 3600
        echo 3600 > deleteTime

...
containers:
  - name: node-app
    livenessProbe:
      exec:
        command:
        # If deleteTime > 0 exit 0
        # Else /tmp/deleteTime - $periodSeconds > /tmp/deleteTime
        - cat
        - /tmp/deleteTime
        - ...
      initialDelaySeconds: 5
      periodSeconds: 5