Security
Authentication
We have 2 types of users:

Users
Kubernetes doesn't manage user accounts natively, it relies on external sources such as:

Service Accounts

Authentication Mechanisms
How does kube-apiserver authenticate?
Basic Authentication





TLS Basics
A certificate is used to guarantee trust between two parties during a transaction, for example:

Let's view what happens:

We can inspect the public key and see its certificate which includes information like:

How do we get our certificates to be signed by an authority?
That's where Certificate Authorities (CA) comes into play, some popular authorities includes:
The process of signing a certificate by an authority is:


Let's say we have sites hosted privately for our organization, what shall we do?
In summary:
Naming Conventions

server.crt = server public key etc...

server.key = server private key etc...
TLS in Kubernetes
So the 2 primary requirements are:

Server Certificates for servers
KubeAPI server

ETCD server

Kubelet server

Client Certificates for clients
Admins

Scheduler

Kube Controller Manager

Kube Proxy


This picture below summarizes everything regarding certificates for both client and server:

Certificate Generation
To generate certificates we must have at least one CA in our cluster, in fact we can have more than one:

Generate key:
Certificate Signing Request:
Sign Certificates:

Generate key:
Certificate Signing Request:
Sign Certificates:

Now, instead of authenticating with username and password/token, we can use the kube-admin certificate we generated and put it in the kubeconfig yaml to authenticate with it:
kubeconfig yaml to authenticate with it:
Or using curl (not recommended):

Peer Certificates


KubeAPI server
Some common names given to kube-apiserver are:


Kubelets


View Certificate Details
To check certificate details, lets start with kube-apiserver as an example:


When you run into issues, you want to start looking at the service logs:
Sometimes if the core components such as kubeapi-server/etcd-server is down, kubectl won't function, so you need to go one level down to docker:
Lab:

Most used command:


Certificates API
Instead of manually entering the master node and signing the certificates ourselves, we could:
Lets take an example:

Lab:
Also when encoding the csr, by default base64 prints it line by line, to make it work use this command:

Kubeconfig
To create a kubeconfig, it can be created using a pod definition yaml as shown below:

To view current context (The user and cluster you are using):
To view all the config:
To use another context:
You also can add a namespace in the context definition yaml, so you can switch to a new context with the namespace you added:

Lab:
The kubeconfig context definition in the yaml already have a name which specifies what cluster we must access and with what user, so if we want to change and use the research current context we should run:

API Groups
Core API Groups:

Named API groups:

Key Takeaways

Authorization
There are different authorization mechanisms in Kubernetes:
Node Authorization
ABAC
ABAC is to associate a user or a group of users a set of permissions by creating a permission file in a json format and passing it to the kube-apiserver:

RBAC

Webhook

Authorization Modes


Role Based Access Controls
We can create an RBAC using an object definition yaml:


Next step is to link user to that role, which we must create a Role Binding object definition:

To view all roles and role bindings:
To check our current access, we can run:
If you're an admin and want to check your users permissions, you can:
Lab:

To get all resources information which is so useful for RBAC:
Cluster Roles & Role Bindings
There are two types of resources in kubernetes:

To view all resources that are namespaced:
Cluster Roles

Lab:
Service Accounts
There are 2 types of accounts in kubernetes:
To create service accounts:
The token created by the service account is in fact a secret which can be viewed by running:
And to create a token as its not generated automatically:
Although post v1.24 you can still create a non-expiry token using a secret object definition, but this is not recommended at all and if you did it, make sure to create the service account first.

Lab:
I had to concentrate on how to include a serviceaccount in a deployment definition:

Also, I could have used:
Image Security
When specifying images in pod object definition yaml such as image: nginx it is in fact like this behind the scenes:
image: nginx it is in fact like this behind the scenes:
When an image is private and you want to pull it, you must authenticate to the registry first, this can be done in kubernetes as follows:

Lab (Review):

Security Context
By default, docker runs processes as root user, if you want to change the user from root to a normal user:


Docker limits some of these capabilities by default, although you can add or drop capabilities:
At a pod level:

At a container level:

Lab:
Useful commands that must be used:
Network Policy
Let's say we have:

To create a network policy:



Also If you want to include same pods ingress rule in different namespaces, you can add a namepsaceSelector:
namepsaceSelector:
If you want to allow certain IP addresses traffic, you could specify an ipBlock:
ipBlock:
Also, we could specify an agress rules as follows:


Lab:
I must change the matchLabels: role:db to the label in the pod that I want to write the policy for:
matchLabels: role:db to the label in the pod that I want to write the policy for:
Here is my final policy:

And here is how the k8s docs helped:

Kubectx & Kubens
Kubectx
Kubens
Slides
Last updated
