Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 4Naam in de cursus: Role-based access control

Control access with RBAC and service accounts

Every request to the API server is authenticated (who are you?) and authorised (may you do this?). RBAC answers the second question with roles (what) and bindings (who). Pods use service accounts as their identity.

  • 6 Lessen
  • 3 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

Vaardigheden in dit hoofdstuk

  • RBAC beheren

4.1

Know the four RBAC objects

A Role lists allowed actions; a Binding gives the Role to users, groups or service accounts.

ObjectScope
RoleRules in one namespace
ClusterRoleRules for the whole cluster (or reused in namespaces)
RoleBindingGives a Role or ClusterRole inside one namespace
ClusterRoleBindingGives a ClusterRole in all namespaces

A rule = apiGroups + resources + verbs (get, list, watch, create, update, patch, delete).

4.2

Create a role and a binding

Use the imperative commands — fast and correct.

kubectl create namespace shop
kubectl create role pod-reader --verb=get,list,watch --resource=pods,pods/log -n shop
kubectl create rolebinding mia-pod-reader --role=pod-reader --user=mia -n shop
kubectl create clusterrole node-reader --verb=get,list --resource=nodes
kubectl create clusterrolebinding ops-node-reader --clusterrole=node-reader --group=ops
kubectl get role,rolebinding -n shop

4.3

Test permissions

kubectl auth can-i asks the API server — also for other users and service accounts.

kubectl auth can-i list pods -n shop --as mia
yes
kubectl auth can-i delete pods -n shop --as mia
no
kubectl auth can-i --list -n shop --as system:serviceaccount:shop:deployer
kubectl auth can-i '*' '*' --as admin

Examentip

Always prove an RBAC task with auth can-i … --as … — yes for what is allowed, no for something that is not.

4.4

Use service accounts

A service account is the identity of pods. Give it a role, and set it in the pod spec.

kubectl create serviceaccount deployer -n shop
kubectl create rolebinding deployer-edit --clusterrole=edit --serviceaccount=shop:deployer -n shop
kubectl create token deployer -n shop --duration=1h
pod-sa.yaml
apiVersion: v1
kind: Pod
metadata:
  name: tools
  namespace: shop
spec:
  serviceAccountName: deployer
  automountServiceAccountToken: true
  containers:
    - name: kubectl
      image: bitnami/kubectl:1.33
      command: ["sleep", "3600"]

Onthoud

Pods that do not need the API should have automountServiceAccountToken: false — fewer tokens to steal.

4.5

Add a user with a certificate

Kubernetes has no user database. A user is a client certificate signed by the cluster CA (or an identity from OIDC). The CertificateSigningRequest API signs it.

openssl genrsa -out mia.key 2048
openssl req -new -key mia.key -subj "/CN=mia/O=developers" -out mia.csr
cat <<EOF | kubectl apply -f -
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
  name: mia
spec:
  request: $(base64 -w0 mia.csr)
  signerName: kubernetes.io/kube-apiserver-client
  usages: ["client auth"]
EOF
kubectl certificate approve mia
kubectl get csr mia -o jsonpath='{.status.certificate}' | base64 -d > mia.crt
kubectl config set-credentials mia --client-key=mia.key --client-certificate=mia.crt --embed-certs=true
kubectl config set-context mia@lab --cluster=lab --user=mia --namespace=shop

The CN is the user name, every O is a group — used in bindings.

4.6

Use the built-in cluster roles

Some ClusterRoles exist already; bind them instead of writing your own.

ClusterRoleGives
cluster-adminEverything (use very rarely)
adminEverything in a namespace, also RBAC
editCreate and change most objects, no RBAC
viewRead most objects (not Secrets)

Alle commando's in dit hoofdstuk