有没有办法为 helm 安装后挂钩启用 shareProcessNamespace?

问题描述 投票:0回答:1

我正在运行一个包含 3 个容器(telegraf、fluidd 和一个内部代理)的 pod,它使用

shareProcessNamespace: true

我编写了一个 python 脚本来从中央控制器 API 端点获取 telegraf 和 fluidd 的初始配置。由于这是一次性操作,我计划使用 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 脚本及其自己的启动脚本。它有效,但很笨拙。我想将其从代理容器中移出。

kubernetes kubernetes-helm kubernetes-jobs
1个回答
0
投票

共享进程命名空间

该标志最重要的部分是它仅在一个 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 hook也可以被认为用在某些逻辑所在的地方。它将在创建容器后运行。由于主应用程序需要时间来启动并且已经有逻辑在等待它,所以这不是问题:

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

© www.soinside.com 2019 - 2024. All rights reserved.