gke setting up kubernetes rbac for a user

To configure RBAC capabilities for a gke cluster, we need to enable this feature. This allow the user or principal to grant permission what he/she can do or cannot do in a cluster. 



gcloud projects add-iam-policy-binding project-id \
  --member="user:kepung@gmail.com" \
  --role="roles/viewer"


And you can see this in the IAM portal :-




Next, let's connect to our cluster and create our namespace. The we will create role and rolebinding using the following script:

Creating namespace

kubectl create namespace team-a

Creating role.yaml 

# role-team-a-dev.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: team-a-developer
  namespace: team-a
rules:
  - apiGroups: [""]
    resources: ["pods", "pods/log"]
    verbs: ["get", "list", "watch", "create", "update", "delete"]
  - apiGroups: [""]
    resources: ["services"]
    verbs: ["get", "list", "watch"]
  - apiGroups: ["networking.istio.io"]
    resources: ["virtualservices", "destinationrules"]
    verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]

Creating rolebinding 

# rolebinding-team-a-dev.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: team-a-developer-binding
  namespace: team-a
subjects:
  - kind: User
    name: alice@example.com
    apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: team-a-developer
  apiGroup: rbac.authorization.k8s.io


Apply this 2 files:- 

kubectl apply -f role-team-a-dev.yaml

kubectl apply -f rolebinding-team-a-dev.yaml


# Should be allowed 

kubectl auth can-i create pods -n team-a --as=alice@example.com 

kubectl auth can-i create virtualservices.networking.istio.io -n team-a --as=alice@example.com (only if you have enabled service mesh in your gke cluster)


# Should be denied (different namespace, or no binding) 

kubectl auth can-i create pods -n team-b --as=alice@example.com

kubectl auth can-i delete namespaces --as=alice@example.com





Comments

Popular posts from this blog

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

llama cpp running it in google colab