【问题标题】:How to execute shell script from POD A to another POD B如何从 POD A 执行 shell 脚本到另一个 POD B
【发布时间】:2021-02-19 20:18:16
【问题描述】:

我有两个在 kubernetes 集群中运行的 pod。豆荚如下

  1. mongodb pod 类型:StatefulSet
  2. 类似的脚本 pod:Job

script pod 我正在运行要在mongodb pod 上执行的 bash 脚本。

bash 脚本包含以下代码,这些代码执行到 mongodb pod 并执行以下命令。

kubectl exec mongo-0 -c mongo -- mongo --eval 'rs.initiate({_id: "rs0", version: 1, members: [ {_id: 0, host: "mongo-0.mongo.default.svc.cluster.local:27017"}, {_id: 1, host: "mongo-1.mongo.default.svc.cluster.local:27017"}, {_id: 2, host: "mongo-2.mongo.default.svc.cluster.local:27017"} ]});'

但是当我运行script pod 时,我得到以下错误

Error from server (Forbidden): pods "mongo-0" is forbidden: User "system:serviceaccount:default:default" cannot create resource "pods/exec" in API group "" in the namespace "default"

我应该怎么做才能为script pod提供在mongodb pod中运行上述命令的权限?


就像你说的那样,我创建了另一个 pod,它是 kind:job 并包含了 script.sh。

在 script.sh 文件中,我在主 pod 上运行“kubectl exec”以运行一些命令

脚本被执行,但我收到错误“无法在 API 组中创建资源“pods/exec”

所以我创建了一个带有资源的集群角色:["pods/exec"] 并使用 ClusterRoleBinding 将其绑定到默认服务帐户

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: pod-reader
rules:
- apiGroups: [""]
  resources: ["pods", "pods/log"]
  verbs: ["get", "list"]
- apiGroups: [""]
  resources: ["pods/exec"]
  verbs: ["create"]

--- 

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: service-account-role-binding
  namespace: default
subjects:
  - kind: ServiceAccount
    name: default
    namespace: default
roleRef:
  kind: ClusterRole
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

---

apiVersion: v1
kind: ServiceAccount
metadata:
  name: default
  namespace: default


In the pod which is of kind:job, I include the service account like shown below

restartPolicy: Never
serviceAccountName: default

but I still get the same error. What am I doing wrong here ?

Error from server (Forbidden): pods "mongo-0" is forbidden: User "system:serviceaccount:default:default" cannot create resource "pods/exec" in API group "" in the namespace "default"

【问题讨论】:

标签: kubernetes kubernetes-pod kubernetes-rbac


【解决方案1】:

如果需要定期运行以进行维护,请查看 Kubernetes 守护程序集对象。

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 2016-09-29
    • 2013-12-26
    相关资源
    最近更新 更多