I want to create a role to service account with context.
My goal is to be able to run kubectl get pods
with the context of the service account.
To do it I need:
- Create service account
- Create role
- Create bind role
- Create context
I created a service account:
kubectl create serviceaccount myservice
Role.yaml:
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
namespace: development
name: my-role
rules:
- apiGroups: ["", "extensions", "apps"]
resources: ["pods"]
verbs: ["get"]
BindRole.yaml:
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: my-role-binding
namespace: development
subjects:
- kind: ServiceAccount
name: myservice
namespace: development
apiGroup: ""
roleRef:
kind: Role
name: my-role
apiGroup: ""
I want to be able to run kubectl get pods
in the context of the service account myservice
.
To create context I need something like that:
kubectl config set-context myservice-context --cluster=kubernetes --user=???
But I can't use --user
for the service account.
So how can I do it ?
I thought to use kubectl config set-credentials
but it just creates a user and I already have the service account.
EDIT:
Here is my try to create a user with the token of the service account and then use it with kubectl --context=myservice-context get pods
but it failed: