Kubernetes 如何将shell脚本从一个POD A执行到另一个POD B

Kubernetes 如何将shell脚本从一个POD A执行到另一个POD B,kubernetes,kubernetes-pod,kubernetes-rbac,Kubernetes,Kubernetes Pod,Kubernetes Rbac,我有两个吊舱,在库伯内特斯星系团中运行。豆荚如下 mongodb pod类型:StatefulSet 脚本pod是一种:Job 在scriptpod中,我正在运行一个bash脚本,该脚本将在mongodbpod上执行 bash脚本包含以下代码,执行mongodb pod并执行以下命令 kubectl exec mongo-0 -c mongo -- mongo --eval 'rs.initiate({_id: "rs0", version: 1, members: [ {_

我有两个吊舱,在库伯内特斯星系团中运行。豆荚如下

  • mongodb pod类型:StatefulSet
  • 脚本pod是一种:Job
  • scriptpod
    中,我正在运行一个bash脚本,该脚本将在
    mongodbpod
    上执行

    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"} ]});'
    
    但是当我运行
    scriptpod
    时,我得到以下错误

    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"
    
    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"
    
    我应该如何为
    脚本pod
    提供在mongodb pod中运行上述命令的权限


    就像你说的,我创建了另一个类似于job的pod,包括script.sh

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

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

    因此,我使用以下资源创建了一个clusterrole:[“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 ?
    
    

    如果这是需要定期运行以进行维护的内容,请查看Kubernetes daemon set对象。

    这是否回答了您的问题?如您所说,添加了群集角色和绑定。我已编辑了上述问题,但我收到了相同的错误。您不应使用
    kubectl exec
    或类似命令连接到数据库或other常规任务;保留供人调试的任务。使用普通的MongoDB客户端库(可能需要比shell脚本更强大的语言,或者在作业映像中安装
    mongo
    客户端)。这篇文章帮助我@DavidMaze-OK将尝试重新考虑我的方法。。