在连接被拒绝的情况下,继续在 Kubernetes Cron Job 上获取错误状态?

问题描述

我正在尝试编写一个 cron 作业,该作业命中它正在拉取映像的应用程序的其余端点。 下面是示例代码

---
apiVersion: batch/v1beta1
kind: CronJob
Metadata:
  name: {{ .Chart.Name }}-cronjob
  labels:
    app: {{ .Release.Name }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    release: {{ .Release.Name }}
spec:
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 2
  FailedJobsHistoryLimit: 2
  startingDeadlineseconds: 1800
  jobTemplate:
    spec:
      template:
        Metadata:
          name: {{ .Chart.Name }}-cronjob
          labels:
            app: {{ .Chart.Name }}
        spec:
          restartPolicy: OnFailure
          containers:
            - name: demo
              image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
              command: ["/bin/sh","-c","curl http://localhost:8080/hello"]
              readinessProbe:
                httpGet:
                  path: "/healthcheck"
                  port: 8081
                initialDelaySeconds: 300
                periodSeconds: 60
                timeoutSeconds: 30
                failureThreshold: 3
              livenessProbe:
                httpGet:
                  path: "/healthcheck"
                  port: 8081
                initialDelaySeconds: 300
                periodSeconds: 60
                timeoutSeconds: 30
                failureThreshold: 3
              resources:
                requests:
                  cpu: 200m
                  memory: 2Gi
                limits:
                  cpu: 1
                  memory: 6Gi
  schedule: "*/5 * * * *"

但我一直遇到*curl: (7) Failed to connect to localhost port 8080: Connection refused*。 我可以从事件中看到它创建了容器并立即抛出:Back-off restarting Failed container。 我已经有运行演示应用程序的 pod,它工作正常,只是当我试图指向这个现有应用程序并点击一个休息端点时,我开始遇到连接被拒绝的错误

查看日志时的确切输出

 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to localhost port 8080: Connection refused

事件日志:

Container image "wayfair/demo:728ac13-as_test_cron_job" already present on machine
9m49s       normal    Created                pod/demo-cronjob-1619108100-ndrnx   Created container demo
6m17s       Warning   BackOff                pod/demo-cronjob-1619108100-ndrnx   Back-off restarting Failed container
5m38s       normal    SuccessfulDelete       job/demo-cronjob-1619108100         Deleted pod: demo-cronjob-1619108100-ndrnx
5m38s       Warning   BackoffLimitExceeded   job/demo-cronjob-1619108100         Job has reached the specified backoff limit

刚接触 K8,任何指点都有帮助!

解决方法

您正在尝试使用 curl 连接到 localhost:8080,根据我对您的 CronJob 定义的理解,这没有意义。

来自文档(在 https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#define-a-command-and-arguments-when-you-create-a-pod 处)

您在配置文件中定义的命令参数 覆盖容器提供的默认命令和参数 图像。如果定义了 args,但没有定义命令,则默认 命令与您的新参数一起使用。

注意:command 字段对应某个容器中的入口点 运行时。请参阅下面的注释。

如果你为镜像定义了一个命令,即使镜像会在本地主机的 8080 端口上启动一个休息应用程序,它的默认入口点(或命令,取决于你使用的容器类型),该命令会覆盖入口点并没有应用程序启动。

如果您需要启动应用程序然后执行其他操作,例如 curls 等,我建议使用 .sh 脚本或类似的东西,具体取决于工作目标是什么。