欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

K8S中若要掛載其他命名空間中的 Secret操作方法

 更新時(shí)間:2025年03月22日 10:58:52   作者:網(wǎng)絡(luò)飛鷗  
在Kubernetes中,通過創(chuàng)建ServiceAccount和RoleBinding,可以實(shí)現(xiàn)一個(gè)命名空間中的Pod掛載另一個(gè)命名空間中的Secret,以下是具體步驟和示例代碼,包括創(chuàng)建ServiceAccount、Role和RoleBinding,以及在Pod中使用這些資源掛載Secret,感興趣的朋友一起看看吧

在Kubernetes(k8s)里,若要掛載其他命名空間中的Secret,你可以通過創(chuàng)建一個(gè) SecretServiceAccountRoleBinding 來實(shí)現(xiàn)對(duì)其他命名空間 Secret 的訪問,接著在 Pod 中掛載這個(gè) Secret。

下面是詳細(xì)的步驟和示例代碼:

步驟

  • 創(chuàng)建 ServiceAccount:在要掛載 Secret 的命名空間里創(chuàng)建一個(gè) ServiceAccount
  • 創(chuàng)建 RoleRoleBinding:在包含 Secret 的命名空間創(chuàng)建一個(gè) Role 以及 RoleBinding,以此賦予 ServiceAccount 訪問 Secret 的權(quán)限。
  • Pod 中使用 ServiceAccount 并掛載 Secret:在 Pod 定義里運(yùn)用 ServiceAccount,并且掛載 Secret。

示例代碼

下面是一系列的 YAML 文件,用來實(shí)現(xiàn)上述步驟。

1. 創(chuàng)建 ServiceAccount

在要掛載 Secret 的命名空間(假設(shè)為 target-namespace)創(chuàng)建 ServiceAccount

apiVersion: v1
kind: ServiceAccount
metadata:
  name: secret-reader
  namespace: target-namespace

2. 創(chuàng)建 RoleRoleBinding

在包含 Secret 的命名空間(假設(shè)為 source-namespace)創(chuàng)建 RoleRoleBinding

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: secret-reader-role
  namespace: source-namespace
rules:
- apiGroups: [""]
  resources: ["secrets"]
  verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: secret-reader-rolebinding
  namespace: source-namespace
subjects:
- kind: ServiceAccount
  name: secret-reader
  namespace: target-namespace
roleRef:
  kind: Role
  name: secret-reader-role
  apiGroup: rbac.authorization.k8s.io

3. 在 Pod 中使用 ServiceAccount 并掛載 Secret

target-namespace 里創(chuàng)建一個(gè) Pod,使用 ServiceAccount 并掛載 Secret

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  namespace: target-namespace
spec:
  serviceAccountName: secret-reader
  containers:
  - name: my-container
    image: nginx
    volumeMounts:
    - name: secret-volume
      mountPath: "/etc/secret"
      readOnly: true
  volumes:
  - name: secret-volume
    secret:
      secretName: my-secret
      namespace: source-namespace

解釋

  • ServiceAccount:在 target-namespace 創(chuàng)建的 secret-reader ServiceAccount 用于給 Pod 授予訪問權(quán)限。
  • RoleRoleBinding:在 source-namespace 創(chuàng)建的 RoleRoleBinding 賦予 secret-reader ServiceAccount 訪問 Secret 的權(quán)限。
  • Pod:在 target-namespace 創(chuàng)建的 Pod 使用 secret-reader ServiceAccount,并且掛載 source-namespace 中的 my-secret Secret。

操作步驟

  • 把上述 YAML 文件保存為不同的文件,例如 serviceaccount.yaml、role.yamlpod.yaml
  • 依次執(zhí)行以下命令:
kubectl apply -f serviceaccount.yaml
kubectl apply -f role.yaml
kubectl apply -f pod.yaml

這樣,Pod 就能成功掛載其他命名空間中的 Secret 了。

到此這篇關(guān)于K8S中若要掛載其他命名空間中的 Secret的文章就介紹到這了,更多相關(guān)K8S命名空間 Secret內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論