Start Minikube
minikube deleteminikube start --cni=cilium --memory=4096In case Minikube has errors starting try
minikube delete --all --purgerm -rf ~/.minikube/and then the start command from above again.
kubectl create namespace rbac-examplekubectl create serviceaccount -n rbac-example myuserkubectl create rolebinding -n rbac-example myuser-view --clusterrole=view --serviceaccount=rbac-example:myuseralias kubectl-user='kubectl --as=system:serviceaccount:rbac-example:myuser'kubectl-user get pod -n rbac-examplekubectl-user get podkubectl get podkubectl-user auth can-i get pods -n defaultkubectl create rolebinding -n default myuser-default-view --clusterrole=view --serviceaccount=rbac-example:myuserkubectl-user auth can-i get pods -n defaultkubectl-user get podkubectl-user auth can-i get pods --all-namespacesAdmin access to a specific namespace:
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: development-admin
namespace: development
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
- kind: Group
name: dev-admin
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: admin
apiGroup: rbac.authorization.k8s.ioRead access to the whole cluster:
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: cluster-viewer
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
- kind: Group
name: cluster-view
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: view
apiGroup: rbac.authorization.k8s.iocat prometheus.yamlkubectl create -f prometheus.yamlkubectl -n kube-system get podskubectl -n kube-system logs prometheus-0cat prometheus-rbac.yamlkubectl create -f prometheus-rbac.yamlkubectl -n kube-system delete pod prometheus-0kubectl -n kube-system get podskubectl -n kube-system logs prometheus-0kubectl delete -f prometheus.yamlkubectl create ns restrictedkubectl run -n restricted --image=nginx nginx-app --port=80kubectl -n restricted get pod -o widekubectl run utils \
--restart Never \
--image webwurst/curl-utils \
--command sleep 3000kubectl exec utils -- curl IPOFNGINX:80Deny all (ingress) traffic to pods in that namespace
cat default-deny.yamlkubectl create -n restricted -f default-deny.yamlkubectl exec utils -- curl IPOFNGINX:80Allow traffic from busybox to nginx
kubectl label ns default name=defaultcat allow-nginx.yamlkubectl create -n restricted -f allow-nginx.yamlkubectl exec utils -- curl IPOFNGINX:80kubectl -n restricted run bla \
--restart Never \
--image webwurst/curl-utils \
--command sleep 3000kubectl -n restricted exec bla -- curl IPOFNGINX:80Allow all traffic within namespace
kubectl label ns restricted name=restrictedcat allow-within-ns.yamlkubectl create -f allow-within-ns.yamlkubectl -n restricted exec bla -- curl IPOFNGINX:80Egress to pods within a cluster
kubectl -n restricted exec bla -- nslookup google.deDeny all egress in namespace
cat default-deny-egress.yamlkubectl -n restricted create -f default-deny-egress.yamlkubectl -n restricted exec bla -- nslookup google.deAllow DNS lookups
kubectl label ns kube-system name=kube-systemcat allow-dns.yamlkubectl -n restricted create -f allow-dns.yamlkubectl -n restricted exec bla -- nslookup google.deEgress to IPs outside the cluster
kubectl -n restricted exec bla -- ping 9.9.9.9Allow
cat allow-external.yamlkubectl -n restricted create -f allow-external.yamlkubectl -n restricted exec bla -- ping 9.9.9.9Note, as of this writing, PSPs are deprecated and will be replaced in the near future. Thus, the next steps might be useful if you already have the needs or want to learn more about pod security contexts, but not necessary.
Running minikube with PSP is not trivial, you can start it by running
minikube start \
--extra-config=apiserver.enable-admission-plugins="NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,PodSecurityPolicy,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota"This is will take a lot of time as minikube wants to verify it is working. It will finally result in a failed start, but minikube should actually be running. Just the Kubernetes components (besides API server) won't be up. To get them running you can apply following manifest that contain default PSPs and bindings for the main components.
kubectl create -f minikube-psp.yamlOnce you have a working minikube with PSP enabled you should check out https://kubernetes.io/docs/concepts/policy/pod-security-policy/#example and https://docs.giantswarm.io/guides/securing-with-rbac-and-psp/#running-applications-that-need-privileged-access.