jeril
jeril

Reputation: 1253

How to execute shell script from POD A to another POD B


So like you said I created a another pod which is of kind:job and included the script.sh.

In the script.sh file, I run "kubectl exec" to the main pod to run few commands

The script gets executed, but I get the error "cannot create resource "pods/exec in API group"

So I created a clusterrole with resources: ["pods/exec"] and bind it to the default service account using 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"

Upvotes: 2

Views: 2373

Answers (1)

John Peterson
John Peterson

Reputation: 389

If this is something that needs to be regularly run for maintenance look into Kubernetes daemon set object.

Upvotes: 1

Related Questions