首页 > 解决方案 > 有没有办法为 helm post-install hook 启用 shareProcessNamespace?

问题描述

我正在运行一个带有 3 个容器(telegraf、fluentd 和一个内部代理)的 pod,它使用shareProcessNamespace: true.

我编写了一个 python 脚本来从中央控制器 API 端点获取电报的初始配置和流利的。由于这是一次性操作,我计划使用 helm post-install hook。

apiVersion: batch/v1
kind: Job
metadata:
  name: agent-postinstall
  annotations:
    "helm.sh/hook-weight": "3"
    "helm.sh/hook": "post-install"
spec:
  template:
    spec:
      containers:
      - name: agent-postinstall
        image: "{{ .Values.image.agent.repository }}:{{ .Values.image.agent.tag | default .Chart.AppVersion }}"
        imagePullPolicy: IfNotPresent
        command: ['python3', 'getBaseCfg.py']
        volumeMounts:
          - name: config-agent-volume
            mountPath: /etc/config
      volumes:
        - name: config-agent-volume
          configMap:
            name: agent-cm
      restartPolicy: Never
  backoffLimit: 1

在获取配置之前,python 脚本需要检查 telegraf/fluentd/agent 进程是否已启动。我打算等待(超时)直到pgrep <telegraf/fluentd/agent>返回 true,然后触发 API。有没有办法启用shareProcessNamespace安装后挂钩?谢谢。

PS:目前,代理调用python脚本及其自己的启动脚本。它有效,但它很笨拙。我想将其移出代理容器。

标签: kuberneteskubernetes-helmkubernetes-podkubernetes-jobshelm3

解决方案


共享进程命名空间

这个标志最重要的部分是它只在一个 pod 中工作,一个 pod 中的所有容器将在彼此之间共享进程。

job应该使用所描述的方法。Job 创建了一个单独的pod,因此它不会以这种方式工作。容器应该是“主” pod 的一部分,所有其他容器都可以访问该 pod 的运行进程。

有关进程共享的更多详细信息

可能的解决方法

可以使用kubectl命令直接从容器中获取进程。

以下是如何使用pgrep命令检查进程状态的示例。pgrepContainer容器需要已经安装了命令pgrep

job.yaml:

apiVersion: batch/v1
kind: Job
metadata:
  name: "{{ .Release.Name }}-postinstall-hook"
  annotations: "helm.sh/hook": post-install
spec:
  template:
    spec:
      serviceAccountName: config-user # service account with appropriate permissions is required using this approach
      volumes:
      - name: check-script
        configMap:
          name: check-script
      restartPolicy: Never
      containers:
      - name: post-install-job
        image: "bitnami/kubectl" # using this image with kubectl so we can connect to the cluster
        command: ["bash", "/mnt/script/checkScript.sh"]
        volumeMounts:
        - name: check-script
          mountPath: /mnt/script

其中configmap.yaml包含脚本和逻辑,用于检查循环中的三个进程,每个进程每 10 秒进行 60 次迭代:

apiVersion: v1
kind: ConfigMap
metadata:
  name: check-script
data:
  checkScript.sh: | 
    #!/bin/bash
     podName=test
     pgrepContainer=app-1
     process1=sleep
     process2=pause
     process3=postgres
     attempts=0
    
   until [ $attempts -eq 60 ]; do 
     kubectl exec ${podName} -c ${pgrepContainer} -- pgrep ${process1} 1>/dev/null 2>&1 \
     && kubectl exec ${podName} -c ${pgrepContainer} -- pgrep ${process2} 1>/dev/null 2>&1 \
     && kubectl exec ${podName} -c ${pgrepContainer} -- pgrep ${process3} 1>/dev/null 2>&1 
   
     if [ $? -eq 0 ]; then
       break
     fi
   
     attempts=$((attempts + 1))
     sleep 10
     echo "Waiting for all containers to be ready...$[ ${attempts}*10 ] s"
   done
 
   if [ $attempts -eq 60 ]; then
     echo "ERROR: Timeout"
     exit 1
   fi
 
   echo "All containers are ready !"
   echo "Configuring telegraf and fluentd services"

最终结果将如下所示:

$ kubectl get pods
NAME                        READY   STATUS     RESTARTS  AGE
test                        2/2     Running    0         20m
test-postinstall-hook-dgrc9 0/1     Completed  0         20m

$ kubectl logs test-postinstall-hook-dgrc9
Waiting for all containers to be ready...10 s
All containers are ready !
Configuring telegraf and fluentd services

以上是另一种方法,您可以使用它的逻辑作为基础来实现您的最终目标。

启动后

也可以考虑在某些逻辑所在的位置使用postStart 钩子。它将在容器创建后运行。由于主应用程序需要时间来启动并且已经有等待它的逻辑,所以这不是问题:

不能保证钩子会在容器 ENTRYPOINT 之前执行


推荐阅读