用于 kubernetes 的 Pod 和 Job 的 helm hook 不运行所有 yaml
我正在将 Kubernetes 与 Helm 3 结合使用。
它在 CentOS Linux 7(核心)上运行。
K8S(通过运行检查:kubectl版本):
git版本(kubernetes):v1.21.6,go版本:go1.16.9。
helm版本:v3.3.4
helm版本(git)go1.14.9。
我需要创建一个在创建 Pod 后运行的作业。
pod yaml:
apiVersion: v1
kind: Pod
metadata:
name: {{ include "test.fullname" . }}-mysql
labels:
app: {{ include "test.fullname" . }}-mysql
annotations:
"helm.sh/hook": post-install
"helm.sh/hook-weight": "-20"
"helm.sh/delete-policy": before-hook-creation
spec:
containers:
- name: {{ include "test.fullname" . }}-mysql
image: {{ .Values.mysql.image }}
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ROOT_PASSWORD
value: "12345"
- name: MYSQL_DATABASE
value: test
作业:
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "test.fullname" . }}-migration-job
labels:
app: {{ include "test.fullname" . }}-migration-job
annotations:
"helm.sh/hook": post-install
"helm.sh/hook-weight": "-10"
"helm.sh/hook-delete-policy": hook-succeeded, hook-failed
spec:
parallelism: 1
completions: 1
backoffLimit: 1
template: #PodTemplateSpec (Core/V1)
spec: #PodSpec (core/v1)
initContainers: # regular
- name: wait-mysql
image: bitnami/kubectl
imagePullPolicy: IfNotPresent
args:
- wait
- pod/{{ include "test.fullname" . }}-mysql
- --namespace={{ .Release.Namespace }}
- --for=condition=ready
- --timeout=120s
containers:
- name: {{ include "test.fullname" . }}
image: {{ .Values.myMigration.image }}
imagePullPolicy: IfNotPresent
command: {{- toYaml .Values.image.entrypoint | nindent 12 }}
args: {{- toYaml .Values.image.cmd | nindent 12}}
MySQL 是 MySQL 5.6 映像。
当我编写上述内容时,还运行 helm install test ./test --namespace test --create-namespace
即使我更改了预安装的钩子(对于 Pod 和 Job),工作是从不运行。
在这两种情况下,我都会收到消息(并且需要按 - 退出 - 我也不希望出现这种行为:
Pod test-mysql 待处理 Pod test-mysql 待处理 Pod
test-mysql 待定 Pod test-mysql 运行 Pod
test-mysql 运行 Pod test-mysql 运行 Pod
test-mysql正在运行...
在此示例中,当我在作业中放置“错误”时,例如: containersx
而不是 container
,我没有得到任何结果通知我的语法错误。
也许因为MySQL正在运行(并且未完成),我可以强制转到hook声明的下一个yaml吗? (即使我声明了 Pod 和 Job 的正确顺序。Pod 应该在 Job 之前运行)。
出了什么问题,如何确保在作业之前创建 pod?当 Pod 开始运行时,我的作业就会运行吗?
谢谢。
I am using Kubernetes with Helm 3.
It is ran on CentOS Linux 7 (Core).
K8S (check by running: kubectl version):
git version (kubernetes): v1.21.6, go version: go1.16.9.
helm version: v3.3.4
helm version (git) go1.14.9.
I need to create a Job that is running after a Pod is created.
The pod yaml:
apiVersion: v1
kind: Pod
metadata:
name: {{ include "test.fullname" . }}-mysql
labels:
app: {{ include "test.fullname" . }}-mysql
annotations:
"helm.sh/hook": post-install
"helm.sh/hook-weight": "-20"
"helm.sh/delete-policy": before-hook-creation
spec:
containers:
- name: {{ include "test.fullname" . }}-mysql
image: {{ .Values.mysql.image }}
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ROOT_PASSWORD
value: "12345"
- name: MYSQL_DATABASE
value: test
The Job:
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "test.fullname" . }}-migration-job
labels:
app: {{ include "test.fullname" . }}-migration-job
annotations:
"helm.sh/hook": post-install
"helm.sh/hook-weight": "-10"
"helm.sh/hook-delete-policy": hook-succeeded, hook-failed
spec:
parallelism: 1
completions: 1
backoffLimit: 1
template: #PodTemplateSpec (Core/V1)
spec: #PodSpec (core/v1)
initContainers: # regular
- name: wait-mysql
image: bitnami/kubectl
imagePullPolicy: IfNotPresent
args:
- wait
- pod/{{ include "test.fullname" . }}-mysql
- --namespace={{ .Release.Namespace }}
- --for=condition=ready
- --timeout=120s
containers:
- name: {{ include "test.fullname" . }}
image: {{ .Values.myMigration.image }}
imagePullPolicy: IfNotPresent
command: {{- toYaml .Values.image.entrypoint | nindent 12 }}
args: {{- toYaml .Values.image.cmd | nindent 12}}
MySQL is MySQL 5.6 image.
When I write the above, also run helm install test ./test --namespace test --create-namespace
Even though I changed the hook for pre-install (for Pod and Job), the job is never running.
In both situations, I get messages (and need to press - to exit - I don't want this behavior either:
Pod test-mysql pending Pod test-mysql pending Pod
test-mysql pending Pod test-mysql running Pod
test-mysql running Pod test-mysql running Pod
test-mysql running ...
In this example, when I put a 'bug' in the Job, for example: containersx
instead of container
, I don't get any notification that I have a wrong syntax.
Maybe because MySQL is running (and not completed), can I force to go to the next yaml declared by hook? (Even I declare the proper order for Pod and Job. Pod should run before Job).
What is wrong, and how can I ensure the pod is created before the job? And when the pod starts running, my job will run after that?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的配置,您似乎需要设置
post-install
hook 正是 Job,因为它应该在所有资源加载到 Kubernetes 后执行。在 Pod 和 Job 上执行pre-install
挂钩时,它会在加载图表的其余部分之前运行,这似乎会阻止 Job 启动。As per your configuration, it looks like you need to set
post-install
hook precisely for Job as it should execute after all resources are loaded into Kubernetes. On executingpre-install
hook both on Pod and Job, it is run before the rest of the chart is loaded, which seems to prevent Job from starting.