Let’s set up role-based access control (RBAC) suitable for running the cluster in production. We will cover roles for using Calico. General RBAC for a production Kubernetes cluster is beyond the scope of this lab.
Using calicoctl
In order for the calicoctl tool to perform version mismatch verification (to make sure the versions for both the cluster and calicoctl are the same), whoever is using it needs to have get access to clusterinformations at the cluster level, i.e., not in a namespace. The network admin role below already has such access, but we will see that we will need to add it to the service owner user we will create turther on.
kubectl apply -f - <<EOFkind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1metadata: name: calicoctl-userrules: - apiGroups: ["crd.projectcalico.org"] resources: - clusterinformations verbs: - getEOFNetwork admin
A network admin is a person responsible for configuring and operating the Calico network as a whole. As such, they will need access to all Calico custom resources, as well as some associated Kubernetes resources.
Create the role
kubectl apply -f - <<EOFkind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1metadata: name: network-adminrules: - apiGroups: [""] resources: - pods - nodes verbs: - get - watch - list - update - apiGroups: [""] resources: - namespaces - serviceaccounts verbs: - get - watch - list - apiGroups: ["networking.k8s.io"] resources: - networkpolicies verbs: ["*"] - apiGroups: ["crd.projectcalico.org"] resources: - felixconfigurations - ipamblocks - blockaffinities - ipamhandles - ipamconfigs - bgppeers - bgpconfigurations - ippools - hostendpoints - clusterinformations - globalnetworkpolicies - globalnetworksets - networkpolicies - networksets verbs: ["*"]EOFTo test out the network admin role, we’ll create the key and certificate signing request. Note that we include /O=network-admins in the subject. This places Nik in the network-admins group.
openssl req -newkey rsa:4096 \ -keyout nik.key \ -nodes \ -out nik.csr \ -subj "/O=network-admins/CN=nik"We will sign this certificate using the main Kubernetes CA.
sudo openssl x509 -req -in nik.csr \ -CA /etc/kubernetes/pki/ca.crt \ -CAkey /etc/kubernetes/pki/ca.key \ -CAcreateserial \ -out nik.crt \ -days 365sudo chown $(id -u):$(id -g) nik.crtNext, we create a kubeconfig file for Nik.
APISERVER=$(kubectl config view -o jsonpath='{.clusters[0].cluster.server}')kubectl config set-cluster kubernetes \ --certificate-authority=/etc/kubernetes/pki/ca.crt \ --embed-certs=true \ --server=$APISERVER \ --kubeconfig=nik.kubeconfig
kubectl config set-credentials nik \ --client-certificate=nik.crt \ --client-key=nik.key \ --embed-certs=true \ --kubeconfig=nik.kubeconfig
kubectl config set-context default \ --cluster=kubernetes \ --user=nik \ --kubeconfig=nik.kubeconfig
kubectl config use-context default --kubeconfig=nik.kubeconfigBind the role to the group network-admins.
kubectl create clusterrolebinding network-admins --clusterrole=network-admin --group=network-adminsTest Nik’s access by creating a global network set
KUBECONFIG=./nik.kubeconfig calicoctl apply -f - <<EOFapiVersion: projectcalico.org/v3kind: GlobalNetworkSetmetadata: name: niks-setspec: nets: - 110.120.130.0/24 - 210.220.230.0/24EOFVerify the global network set exists
KUBECONFIG=./nik.kubeconfig calicoctl get globalnetworkset -o wideService owner
A service owner is a person responsible for operating one or more services in kubernetes. They should be able to define network policy for their service, but don’t need to view or mofidy any global configuratoin related to Calico.
Define the role
kubectl apply -f - <<EOFkind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1metadata: name: network-service-ownerrules: - apiGroups: ["networking.k8s.io"] resources: - networkpolicies verbs: ["*"] - apiGroups: ["crd.projectcalico.org"] resources: - networkpolicies - networksets verbs: ["*"]EOFTo test out the service owner role, we’ll create a user named Sam and grant then the role.
On the Kubernetes control plane node, create the key and certificate signing request.
openssl req -newkey rsa:4096 \ -keyout sam.key \ -nodes \ -out sam.csr \ -subj "/CN=sam"We will sign this certificate using the main Kubernetes CA.
sudo openssl x509 -req -in sam.csr \ -CA /etc/kubernetes/pki/ca.crt \ -CAkey /etc/kubernetes/pki/ca.key \ -CAcreateserial \ -out sam.crt \ -days 365sudo chown $(id -u):$(id -g) sam.crtNext, we create a kubeconfig file for Sam.
APISERVER=$(kubectl config view -o jsonpath='{.clusters[0].cluster.server}')kubectl config set-cluster kubernetes \ --certificate-authority=/etc/kubernetes/pki/ca.crt \ --embed-certs=true \ --server=$APISERVER \ --kubeconfig=sam.kubeconfig
kubectl config set-credentials sam \ --client-certificate=sam.crt \ --client-key=sam.key \ --embed-certs=true \ --kubeconfig=sam.kubeconfig
kubectl config set-context default \ --cluster=kubernetes \ --user=sam \ --kubeconfig=sam.kubeconfig
kubectl config use-context default --kubeconfig=sam.kubeconfigWe will limit Sam’s access to a single namespace. Create namespace.
kubectl create namespace samBind the role to Sam in the namespace
kubectl create rolebinding -n sam network-service-owner-sam --clusterrole=network-service-owner --user=samAlso bind the calicoctl-user role to sam at the cluster level so that they can use calicoctl properly.
kubectl create clusterrolebinding calicoctl-user-sam --clusterrole=calicoctl-user --user=samSam cannot create global network set resources (like Nik can as network admin)
KUBECONFIG=./sam.kubeconfig calicoctl get globalnetworkset -o wideResult
connection is unauthorized: globalnetworksets.crd.projectcalico.org is forbidden: User "sam" cannot list resource "globalnetworksets" in API group "crd.projectcalico.org" at the cluster scopeHowever, Sam can create resources in their own namespace
KUBECONFIG=./sam.kubeconfig calicoctl apply -f - <<EOFapiVersion: projectcalico.org/v3kind: NetworkSetmetadata: name: sams-set namespace: samspec: nets: - 110.120.130.0/24 - 210.220.230.0/24EOFVerify the resource exists
KUBECONFIG=./xam.kubeconfig calicoctl get networksets -n samResult
NAMESPACE NAMEsam sams-setDelete the NetworkSet
KUBECONFIG=./sam.kubeconfig calicoctl delete networkset sams-set -n samreference