尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

Infisical 如何用 Agent Injector Helm Chart 为 Kubernetes 应用自动注入密钥?

Infisical 如何用 Agent Injector Helm Chart 为 Kubernetes 应用自动注入密钥? Infisical 如何用 Agent Injector Helm Chart 为 Kubernetes 应用自动注入密钥【免费下载链接】infisicalInfisical is the open-source platform for secrets, certificates, and privileged access management.项目地址: https://gitcode.com/GitHub_Trending/in/infisical如果你的应用运行在 Kubernetes 上又不想把 Infisical 的密钥硬编码进镜像或 Secret 资源里可以部署 Infisical Agent Injector。它是一个基于 Mutating Admission Webhook 的组件监听集群中 pod 的CREATE和UPDATE事件把带org.infisical.com/inject: true注解的 pod 改写为包含 Infisical Agent 容器agent 通过 pod 自身的 service account 向 Infisical 认证再把密钥渲染到 pod 内的共享卷/文件中供应用直接读取。整个过程不要求修改应用逻辑。适用前提一个可操作的 Kubernetes 集群、已安装 Helm 与 kubectl、一个可访问的 Infisical 实例Cloud 或自托管并已按文档配置好 Kubernetes Auth 机器身份和要读取的密钥。准备条件已在 Infisical 中创建好应用要读取的静态密钥并记下项目 ID、环境 slug如dev和密钥路径。官方 Quick Start 以名为API_KEY的密钥为例。一个配置了 Kubernetes Auth 认证方式的机器身份创建方法见下文。Injector 使用 pod 的 service account token 通过该身份向 Infisical 认证。应用 pod 绑定的 service account 必须在机器身份的Allowed Service Account Names/Allowed Namespaces允许列表内。集群中有权限执行kubectl apply管理 RBAC 资源的管理员Kubernetes Auth 的 token reviewer 方案需要绑定system:auth-delegatorClusterRole这是集群级 RBAC 变更。安装 Agent Injector 并验证 webhook 就绪Infisical 官方 Helm chart 发布在 Cloudsmith 仓库上helm repo add infisical-helm-charts https://dl.cloudsmith.io/public/infisical/helm-charts/helm/charts/ helm repo update helm install infisical-agent-injector infisical-helm-charts/infisical-agent-injector \ --namespace infisical \ --create-namespace安装后检查 pod 与日志kubectl get pods -n infisical kubectl logs -n infisical deployment/infisical-agent-injector文档示例的启动日志如下输出为文档示例具体时间和地址以你的环境为准2025/05/19 14:20:05 Starting infisical-agent-injector... 2025/05/19 14:20:05 Generating self-signed certificate... 2025/05/19 14:20:06 Writing cert to: /tmp/tls/tls.crt 2025/05/19 14:20:06 Starting HTTPS server on port 8585... 2025/05/19 14:20:06 Attempting to update webhook config (attempt 1)... 2025/05/19 14:20:06 Successfully updated webhook configuration with CA bundle日志中出现Successfully updated webhook configuration with CA bundle说明 injector 的 webhook 配置已注册完成可以开始改写 pod 了。chart 的关键可配置项默认值来自 Agent Injector Helm 文档Parameter默认值说明replicaCount1injector 副本数image.tagv0.1.12镜像版本failurePolicyIgnorewebhook 不可用时的策略Ignore表示 pod 仍可正常部署resources.limits.cpu/memory200m/256Mi资源上限如果你希望 injector 故障时阻断 pod 创建即 fail-closed可以通过--set failurePolicyFail覆盖但请注意这会让 webhook 宕机时所有 pod 无法创建。配置 Kubernetes Auth 机器身份Injector 用 pod 的 service account 做 Kubernetes Auth因此需要先在 Infisical 里创建并配置该身份。流程依据 Kubernetes Auth 文档第 1 步为 Infisical 准备 token reviewer JWT可选方案 1集中管理。创建一个仅供 Infisical 校验 TokenReview 用的 service account并绑定system:auth-delegator# infisical-service-account.yaml apiVersion: v1 kind: ServiceAccount metadata: name: infisical-auth namespace: default# cluster-role-binding.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: role-tokenreview-binding namespace: default roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: system:auth-delegator subjects: - kind: ServiceAccount name: infisical-auth namespace: defaultkubectl apply -f infisical-service-account.yaml kubectl apply -f cluster-role-binding.yaml再为它生成长期有效的 JWT 并取出# service-account-token.yaml apiVersion: v1 kind: Secret type: kubernetes.io/service-account-token metadata: name: infisical-auth-token annotations: kubernetes.io/service-account.name: infisical-authkubectl apply -f service-account-token.yaml kubectl patch serviceaccount infisical-auth -p {secrets: [{name: infisical-auth-token}]} -n default kubectl get secret infisical-auth-token -n default -ojsonpath{.data.token} | base64 --decode备选路径不想维护长期 token 时可以给每个应用 service account 绑定system:auth-delegator文档 Option 2配置身份时Token Reviewer JWT留空或者使用付费的 Gateway 作为 reviewerOption 3。本文主路径使用上面的 Option 1。第 2 步在 Infisical 创建机器身份。进入组织的Access ControlMachine IdentitiesCreate创建一个身份并选择其要 assume 的组织角色。随后编辑该身份的Authentication区域移除默认的 Universal Auth 配置添加 Kubernetes Auth各字段按文档填写Kubernetes HostKubernetes API server 地址通常可用kubectl cluster-info获取Token Reviewer JWT第 1 步取出的 JWTAllowed Service Account Names允许认证的 service account 名逗号分隔Allowed Namespaces允许的命名空间逗号分隔其余字段如 Access Token TTL 等按文档默认值即可。第 3 步把身份加入项目。打开目标项目进入Access ControlMachine IdentitiesAdd Machine Identity to Project选择Assign Existing选中刚创建的身份并分配项目角色。记下该身份的 ID——下一步的 ConfigMap 要用到。用 ConfigMap 配置 agent 与密钥模板在目标 pod 所在的命名空间示例为test-namespace创建 ConfigMap全部内容以字符串形式放在config.yaml键下# config-map.yaml apiVersion: v1 kind: ConfigMap metadata: name: nginx-infisical-config-map namespace: test-namespace data: config.yaml: | infisical: address: https://app.infisical.com auth: type: kubernetes config: identity-id: your-infisical-machine-identity-id templates: - destination-path: /infisical/secrets template-content: | {{- with secret your-project-id your-environment-slug / }} {{- range . }} {{ .Key }}{{ .Value }} {{- end }} {{- end }}需要替换的占位符均出现在方括号/尖括号中your-infisical-machine-identity-id上一步创建的机器身份 IDyour-project-id、your-environment-slug你在 Infisical 中创建密钥的项目 ID 与环境 sluginfisical.address默认为https://app.infisical.comInfisical Cloud自托管实例请改为你的实例地址destination-path是密钥在 pod 内落盘的路径未指定时默认为/shared/infisical-secrets多个模板未指定路径时依次默认为/shared/infisical-secrets-1、/shared/infisical-secrets-2等template-content按 Go Template 渲染模板函数与 Infisical Agent 相同listSecrets、listSecretsByProjectSlug、getSecretByName、dynamicSecret见 Infisical Agent 文档。kubectl apply -f config-map.yaml给应用 Pod 加注解触发自动注入给目标 pod 加上三个注解即可。以官方示例中的 nginx 应用为例# nginx.yaml --- apiVersion: v1 kind: Pod metadata: name: nginx-pod namespace: test-namespace labels: app: nginx annotations: org.infisical.com/inject: true org.infisical.com/inject-mode: init org.infisical.com/agent-config-map: nginx-infisical-config-map spec: containers: - name: simple-app-demo image: nginx:alpine command: [/bin/sh, -c] args: - | export $(cat /infisical/secrets | xargs) echo API_KEY is set to: $API_KEY nginx -g daemon off;org.infisical.com/inject: true开启注入pod 在创建或更新时会被 patch 进 agent 容器org.infisical.com/inject-mode注入模式示例用init见下节选择org.infisical.com/agent-config-map指向同命名空间里的 ConfigMap 名称ConfigMap 必须与 pod 在同一命名空间。kubectl apply -f nginx.yaml选择注入模式org.infisical.com/inject-mode支持三种取值init注入一个 init 容器先于 pod 内其他所有容器包括其他 init 容器运行把密钥渲染到共享卷适合一次性获取即可的场景sidecaragent 以 sidecar 形式与主容器并行运行渲染的密钥会持续与 Infisical 保持同步sidecar-init同时创建上述两个容器——init 容器先取到密钥sidecar 在其后整个生命周期内维持同步。需要跨 agent 重启持久化动态密钥租约时尤其sidecar-init模式可加注解org.infisical.com/agent-cache-enabled: true需要 agent 在收到SIGTERM时吊销动态密钥租约时可加org.infisical.com/agent-revoke-on-shutdown: true注意断电等灾难场景不会发送SIGTERM凭据不会被吊销。验证密钥已注入pod 就绪并注入密钥可能需要几分钟。先用kubectl get pods -n test-namespace确认状态然后检查落盘文件与容器日志kubectl exec -it pod/nginx-pod -n test-namespace -- cat /infisical/secrets文档示例输出其中sk_api_...是文档示例值实际应为你在 Infisical 创建的密钥值Defaulted container simple-app-demo out of: simple-app-demo, infisical-agent-init (init) API_KEYsk_api_...注意输出中列出了infisical-agent-init (init)容器——这就是 injector 自动 patch 进去的 agent 容器。再确认应用侧读到了密钥kubectl logs pod/nginx-pod -n test-namespace文档示例日志Defaulted container simple-app-demo out of: simple-app-demo, infisical-agent-init (init) API_KEY is set to: sk_api_...文件里出现预期的KEYVALUE行、且应用日志能打印出对应值即表示注入链路webhook patch → agent 认证 → 密钥渲染全部打通。Pod 卡在 Init 状态时如何排查如果 pod 一直停留在Init状态说明 agent init 容器启动失败或卡在重启循环常见原因包括机器身份权限不足、或从不存在的项目/环境拉取密钥。查看 init 容器日志# 针对 deployment kubectl logs deployment/your-deployment-name -c infisical-agent-init -n namespace # 针对 pod kubectl logs pod/your-pod-name -c infisical-agent-init -n namespacenamespace替换为 pod 所在命名空间。文档给出的一类典型错误示例示例地址与 ID 均来自文档11:10AM ERR unable to process template because template: literalTemplate:1:9: ... error calling secret: ... [status-code404] [response{...message:Project with ID 3c0d3ff6-165c-4dc9-b52c-ff3ffaedfce311111 not found during bot lookup. Are you sure you are using the correct project ID?,...}] exit 1 Agent failed with exit code 1该示例中错误原因是 ConfigMap 里的 project ID 无效。对照检查identity-id是否为真实存在的机器身份、secret模板中的 project ID / 环境 slug 是否正确、pod 的 service account 是否在 Allowed Service Account Names / Allowed Namespaces 范围内。卸载确认不再需要注入时helm uninstall infisical-agent-injector --namespace infisical限制与边界Injector 对命名空间无感监听所有命名空间的 pod但只 patch 带org.infisical.com/inject: true注解的 podConfigMap 中支持的认证类型为kubernetes、ldap-auth、aws-iam三种本文使用kubernetesinfisical.revoke-credentials-on-shutdown的 ConfigMap 配置仅在 Linux pod 上生效Windows pod 上建议使用注解org.infisical.com/agent-revoke-on-shutdownWindows pod 的注入从 injectorv0.1.4开始支持仅 Windows Server 2019/2022注入 Windows pod 无需额外配置相关文档Agent Injector Helm Chart、Kubernetes Agent Injector 完整注解参考、Helm Charts 总览。【免费下载链接】infisicalInfisical is the open-source platform for secrets, certificates, and privileged access management.项目地址: https://gitcode.com/GitHub_Trending/in/infisical创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表