Online Talk: RBAC in Kubernetes
Online Talk: RBAC in Kubernetes
Online Talk: RBAC in Kubernetes
https://github.com/javsalgar/rbac-online-talk
I - Creating users
Question
● When starting with K8s, we tend to use full administrator
credentials. Examples: minikube, k8s sandbox...
3
User management in Kubernetes
● Certificate-based authentication
● Token-based authentication
● Basic authentication
● OAuth2
4
Certificate-based authentication
● Kubernetes is configured with a Certificate Authority (CA)
/etc/kubernetes/pki/ca.crt /etc/kubernetes/pki/ca.key
Public certificate Private key
● Every SSL certificate signed with this CA will be accepted by the Kubernetes API
● Common Name (CN): Kubernetes will interpret this value as the user
5
Creating user certificate: steps
● Create private key (if it does not exist)
Developer openssl req -new -key juan.key -out juan.csr -subj "/CN=juan/O=devs"
user group
● Send the CSR to the administrator
Administrator
openssl x509 -req -in juan.csr -CA CA_LOCATION/ca.crt -CAkey
CA_LOCATION/ca.key -CAcreateserial -out juan.crt -days 500
6
Next step: Create kubectl configuration
● To add in your local machine the new configuration:
7
Finally: Test your new configuration
● Change to the newly created context
● What happened?
Error from server (Forbidden): pods is forbidden: User "juan" cannot list
pods in the namespace "default"
8
II - Role Based Access Control (RBAC)
RBAC in Kubernetes
● Three important groups
ConfigMaps
Pod
e AutoS
Servic caler
list get
Developer Developer Deploym Secrets
ent
create watch
PV
ReplicaSe
ts Ingress
Administrator Administrator Namespace
delete patch
DaemonSet
Job
Nodes
OS Process Process in CronJob
Pod PVC
10
RBAC in Kubernetes: Roles
● Establish a set of allowed operations (rules) over a set of resources in a namespace
role pod-access
ConfigMaps
Pod list get
AutoS
caler
e
Servic
Secrets create delete
Deploym
ent
watch
ts Ingress role ns-admin patch
ReplicaSe
Job
DaemonSet
CronJob
namespace “test”
11
RBAC in Kubernetes: Roles
● Need to specify:
kind: Role ○ Api group
○ Name
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata: Find it in the API reference, examples
namespace: test
name: pod-access
rules:
- apiGroups: [""] WHICH RESOURCES
resources: ["pods"]
verbs: ["get", "list"] WHICH OPERATIONS
12
RBAC in Kubernetes: Roles
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
namespace: test
name: ns-admin
rules:
- apiGroups: ["*"] ● Wildcards are allowed
resources: ["*"]
verbs: ["*"]
13
RBAC in Kubernetes: RoleBindings
● Connects a role to a subject or set of subjects
user: jsalmeron
group: tech-lead, dev
Developer
role pod-access
user: dgalvez
group: dev
Developer
role ns-access
user: juan
group: dev
Developer
namespace “test”
14
RBAC in Kubernetes: RoleBinding
● Examples
kind: RoleBinding
○ User
apiVersion: rbac.authorization.k8s.io/v1 ○ Group
metadata: ○ ...
name: devs-read-pods
namespace: test Later we will see another one
subjects:
- kind: Group
name: devs WHICH SUBJECTS
apiGroup: rbac.authorization.k8s.io
roleRef: Used to specify which api group the kind
belongs to
kind: Role
name: pod-access WHICH ROLE (ONLY ONE PER BINDING)
apiGroup: rbac.authorization.k8s.io
15
RBAC in Kubernetes: RoleBinding
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: salme-ns-admin
namespace: test
subjects:
- kind: User Mini-exercise: Another way of doing this?
name: jsalmeron # Name is case sensitive
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: ns-admin
apiGroup: rbac.authorization.k8s.io
16
RBAC in Kubernetes: ClusterRoles
● Establish a set of allowed operations over a set of resources in the whole cluster
role all-pods-access
ConfigMaps
Pod list get
AutoS
caler
e
Servic
create
PV delete
Deploym Secrets
ent role pv-admin watch
ts Ingress patch
ReplicaSe
Nodes
Job
DaemonSet
CronJob PVC
17
RBAC in Kubernetes: ClusterRoles
● Roles and ClusterRoles have very similar yaml
18
RBAC in Kubernetes: ClusterRoleBinding
● Connects a role to a subject or set of subjects
user: jsalmeron
group: tech-lead, dev
Developer
role all-pod-access
user: dbarranco
group: sre, dev, devops
Administrator
role pv-admin
user: jbianquetti
group: sre, dev
Administrator
Whole cluster
19
RBAC in Kubernetes: ClusterRoleBinding
● Just like the previous case, very similar YAML
20
Default ClusterRoleBindings
● Kubernetes includes some ClusterRoleBindings. For example:
21
list get
More about the possible actions (verbs) create watch
● TRIVIA: Example operations and their requirements delete
patch
kubectl run --image=bitnami/mongodb my-mongodb deployments: create
pods: get
kubectl exec -ti mypod bash pods/exec: create
22
Questions
● Find the necessary RBAC rules so the user can contact Helm’s Tiller pod
● We know that this command should work with the previously created RBAC rules (salme-ns-admin)
23
Helm under the hood
A server called tiller is in charge of rendering and deploying charts
helm install my-wordpress/ Your cluster
kind: Deployment kind: Deployment
metadata: metadata:
Process in Pod
name: {{ template "fullname" . }} name: pilfering-anaconda
spec: spec:
replicas: {{ .Values.replicaCount }} Tiller replicas: 1
template: template:
spec: spec:
containers: containers:
- name: wp - name: wp
image: {{ .Values.image }} image: bitnami/wordpress:4.8.3
Kubernetes API
... ...
● While regular users are not handled by Kubernetes, processes inside pods do have an API object
ServiceAccount
Developer Administrator OS Process Process in
Pod
● Also used for other operations like storing image pull secrets
25
RBAC in Kubernetes (again): ServiceAccount
apiVersion: v1
kind: ServiceAccount ● An API token will be automatically
created and stored in the cluster
metadata:
name: my-service-account
26
Deploying Tiller
● Create a Tiller ServiceAccount kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
apiVersion: v1 metadata:
kind: ServiceAccount name: tiller-rolebinding
metadata:
name: tiller-sa subjects:
namespace: kube-system - kind: ServiceAccount
name: tiller-sa # Name is case sensitive
● Set up RBAC for Tiller
apiGroup: rbac.authorization.k8s.io
namespace: kube-system
● Which operations requires Tiller?
roleRef:
● In principle, it can deploy kind: ClusterRole
ANYTHING in ANY NAMESPACE name: cluster-admin
apiGroup: rbac.authorization.k8s.io
27
Deploying Tiller
helm ls
28
Next steps in Kubernetes Cluster Administration
● NetworkPolicies
● PodSecurityPolicies
https://docs.bitnami.com/kubernetes/how-to/
29
Thank
You
For more
information,
visit bitnami.com