【问题标题】:K8S groundnuty/k8s-wait-for image failing to start as init container (with helm)K8S groundnuty/k8s-wait-for 映像无法作为 init 容器启动(带 helm)
【发布时间】:2021-09-15 08:35:33
【问题描述】:

我遇到了图片groundnuty/k8s-wait-for的问题。在github 进行项目,在dockerhub 进行回购。

我很确定错误出现在命令参数中,因为 init 容器因 Init:CrashLoopBackOff 而失败。

关于图片: 此镜像用于需要推迟 pod 部署的 init 容器。镜像中的脚本等待 pod 或作业完成,完成后它让主容器和所有副本开始部署。

在我的示例中,它应该等待名为 {{ .Release.Name }}-os-server-migration-{{ .Release.Revision }} 的作业完成,并且在检测到它完成后,它应该让主容器启动。使用 Helm 模板。

据我了解,作业名称为{{ .Release.Name }}-os-server-migration-{{ .Release.Revision }},并且 deployment.yml 中 init 容器的第二个命令参数需要相同,以便 init 容器可以依赖于命名作业。对这种方法还有其他意见或经验吗?

附有模板。

DEPLOYMENT.YML:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}-os-{{ .Release.Revision }}
  namespace: {{ .Values.namespace }}
  labels:
    app: {{ .Values.fullname }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ .Values.fullname }}
  template:
    metadata:
      labels:
        app: {{ .Values.fullname }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 8080
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      initContainers:
        - name: "{{ .Chart.Name }}-init"
          image: "groundnuty/k8s-wait-for:v1.3"
          imagePullPolicy: "{{ .Values.init.pullPolicy }}"
          args:
            - "job"
            - "{{ .Release.Name }}-os-server-migration-{{ .Release.Revision }}"

JOB.YML:

apiVersion: batch/v1
kind: Job
metadata:
  name: {{ .Release.Name }}-os-server-migration-{{ .Release.Revision }}
  namespace: {{ .Values.migration.namespace }}
spec:
  backoffLimit: {{ .Values.migration.backoffLimit }}
  template:
    spec:
      {{- with .Values.migration.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: {{ .Values.migration.fullname }}
          image: "{{ .Values.migration.image.repository }}:{{ .Values.migration.image.tag }}"
          imagePullPolicy: {{ .Values.migration.image.pullPolicy }}
          command:
            - sh
            - /app/migration-entrypoint.sh
      restartPolicy: {{ .Values.migration.restartPolicy }}

日志:

  Normal   Scheduled  46s                default-scheduler  Successfully assigned development/octopus-dev-release-os-1-68cb9549c8-7jggh to minikube
  Normal   Pulled     41s                kubelet            Successfully pulled image "groundnuty/k8s-wait-for:v1.3" in 4.277517553s
  Normal   Pulled     36s                kubelet            Successfully pulled image "groundnuty/k8s-wait-for:v1.3" in 3.083126925s
  Normal   Pulling    20s (x3 over 45s)  kubelet            Pulling image "groundnuty/k8s-wait-for:v1.3"
  Normal   Created    18s (x3 over 41s)  kubelet            Created container os-init
  Normal   Started    18s (x3 over 40s)  kubelet            Started container os-init
  Normal   Pulled     18s                kubelet            Successfully pulled image "groundnuty/k8s-wait-for:v1.3" in 1.827195139s
  Warning  BackOff    4s (x4 over 33s)   kubelet            Back-off restarting failed container

kubectl get all -n development

NAME                                                        READY   STATUS                  RESTARTS   AGE
pod/octopus-dev-release-os-1-68cb9549c8-7jggh   0/1     Init:CrashLoopBackOff   2          44s
pod/octopus-dev-release-os-1-68cb9549c8-9qbdv   0/1     Init:CrashLoopBackOff   2          44s
pod/octopus-dev-release-os-1-68cb9549c8-c8h5k   0/1     Init:Error              2          44s
pod/octopus-dev-release-os-migration-1-9wq76    0/1     Completed               0          44s
......
......
NAME                                                       COMPLETIONS   DURATION   AGE
job.batch/octopus-dev-release-os-migration-1   1/1           26s        44s

【问题讨论】:

    标签: kubernetes devops kubernetes-helm


    【解决方案1】:

    对于遇到相同问题的任何人,我将解释我的解决方法。

    问题是 deployment.yaml 中的容器没有使用 Kube API 的权限。因此,groundnuty/k8s-wait-for:v1.3 容器无法检查是否有作业 {{ .Release.Name }}-os-server-migration-{{ .Release.Revision }}完成与否。这就是为什么 init 容器会立即因 CrashLoopError 而失败。

    添加服务帐户、角色和角色绑定后一切正常,groundnuty/k8s-wait-for:v1.3 成功等待作业(迁移)完成,以便让主容器运行。

    以下是解决问题的服务帐户、角色和角色绑定的代码示例。

    sa.yaml

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: sa-migration
      namespace: development
    

    role.yaml

    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: migration-reader
    rules:
      - apiGroups: ["batch","extensions"]
        resources: ["jobs"]
        verbs: ["get","watch","list"]
    

    role-binding.yaml

    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: migration-reader
    subjects:
    - kind: ServiceAccount
      name: sa-migration
    roleRef:
      kind: Role
      name: migration-reader
      apiGroup: rbac.authorization.k8s.io
    

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      • 2019-03-24
      • 1970-01-01
      • 2020-01-04
      • 2018-09-21
      • 2021-05-20
      相关资源
      最近更新 更多