在 Kubernetes 中,一个 Namespace 中的 Secret 默认是不可见的,只能在同一个 Namespace 中被访问和使用。如果你想将一个 Namespace 中的 Secret 应用于多个 Namespace,可以通过以下几种方式实现:

  1. 使用 Secret 的名称和 Namespace 的名称来引用它:你可以在目标 Namespace 中创建一个新的 Secret 对象,然后通过在 Pod 的配置文件中引用源 Namespace 中的 Secret 名称和 Namespace 来访问它。例如:
apiVersion: v1
kind: Secret
metadata:
  name: 'my-secret'
  namespace: 'source-namespace'
apiVersion: v1
kind: Pod
metadata:
  name: 'my-pod'
  namespace: 'target-namespace'
spec:
  containers:
    - name: 'my-container'
      image: 'my-image'
      env:
        - name: 'MY_SECRET'
          valueFrom:
            secretKeyRef:
              name: 'my-secret'
              namespace: 'source-namespace'
              key: 'secret-key'
  1. 使用 Secret 的类型为kubernetes.io/service-account-token:这种类型的 Secret 在所有 Namespace 中都是可见的,并且可以用来访问 API 服务器。你可以在源 Namespace 中创建一个 ServiceAccount,并将其授权给目标 Namespace,然后在目标 Namespace 中使用该 ServiceAccount 来访问 Secret。例如:
apiVersion: v1
kind: ServiceAccount
metadata:
  name: 'my-service-account'
  namespace: 'source-namespace'
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: 'my-role-binding'
  namespace: 'target-namespace'
roleRef:
  kind: ClusterRole
  name: 'view'
  apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
  name: 'my-service-account'
  namespace: 'source-namespace'
apiVersion: v1
kind: Pod
metadata:
  name: 'my-pod'
  namespace: 'target-namespace'
spec:
  containers:
    - name: 'my-container'
      image: 'my-image'
      env:
        - name: 'MY_SECRET'
          valueFrom:
            secretKeyRef:
              name: 'my-secret'
              namespace: 'source-namespace'
              key: 'secret-key'

这样,目标 Namespace 中的 Pod 就可以使用MY_SECRET环境变量来访问源 Namespace 中的 Secret 了。

注意:在将 Secret 应用于多个 Namespace 时,请确保有足够的权限和访问控制来保护敏感信息的安全性。

Kubernetes 中如何跨 Namespace 共享 Secret - 详细步骤与示例

原文地址: https://www.cveoy.top/t/topic/ptAq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录