You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
RBAC - Create user for kubeconfig with restricted permissions (microk8s) - bis Kubernetes 1.24
Enable RBAC in microk8s
# This is important, if not enable every user on the system is allowed to do everything
microk8s enable rbac
Schritt 1: Nutzer-Account auf Server anlegen / in Client
cd
mkdir -p manifests/rbac
cd manifests/rbac
Mini-Schritt 1: Definition für Nutzer
# vi service-account.yml
apiVersion: v1
kind: ServiceAccount
metadata:
name: training
namespace: default
kubectl apply -f service-account.yml
Mini-Schritt 2: ClusterRolle festlegen - Dies gilt für alle namespaces, muss aber noch zugewiesen werden
## Bevor sie zugewiesen ist, funktioniert sie nicht - da sie keinem Nutzer zugewiesen ist
# vi pods-clusterrole.yml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: pods-clusterrole
rules:
- apiGroups: [""] # "" indicates the core API group
resources: ["pods"]
verbs: ["get", "watch", "list"]
kubectl apply -f pods-clusterrole.yml
Mini-Schritt 3: Die ClusterRolle den entsprechenden Nutzern über RoleBinding zu ordnen
kubectl auth can-i get pods -n default --as system:serviceaccount:default:training
Schritt 2: Context anlegen / Credentials auslesen und in kubeconfig hinterlegen (bis Version 1.25.)
Mini-Schritt 1: kubeconfig setzen
kubectl config set-context training-ctx --cluster microk8s-cluster --user training
# extract name of the token from here
TOKEN=`kubectl get secret trainingtoken -o jsonpath='{.data.token}' | base64 --decode`
echo $TOKEN
kubectl config set-credentials training --token=$TOKEN
kubectl config use-context training-ctx
# Hier reichen die Rechte nicht aus
kubectl get deploy
# Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:kube-system:training" cannot list # resource "pods" in API group "" in the namespace "default"
Mini-Schritt 2:
kubectl config use-context training-ctx
kubectl get pods