Skip to content
LF Cert Trainer

Lesson 69 · Section 7: Security

Lesson 69: Kubernetes security primitives: who gets in, and what they may do

The map of cluster security before the detailed lessons: securing the hosts, the two questions the API server asks of every request (who are you, what may you do), TLS between the components, and why Pods can all talk to each other until a NetworkPolicy says otherwise. · 16 min read · 5 min video

1/10Kubernetes security primitives
0:00 / 4:30

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

Why this lesson comes first

A cluster is a valuable target. It runs production applications, it stores passwords and keys as Secrets, and one command can delete everything in it. The security section of the course is long because the protection is built from several independent parts, and each part gets its own lesson.

This lesson is the map. It shows where each part sits and which problem it solves, so that when you reach TLS certificates, kubeconfig files, RBAC or network policies you already know why they exist. A primitive here just means a basic building block: Kubernetes gives you a handful of them and you combine them.

What has to be protected

The doors into a clusterHTTPSTLSSSH keys onlyTLS, client certrunsPeople and toolskubectl, CI, appsAPI serverport 6443etcdall data, all SecretsNode hostsLinux machineskubeletport 10250Podsopen network by default
The doors into a cluster. Each arrow is a way in, and each one needs its own lock. The API server is the main door, but the hosts, etcd and the kubelet are doors too.

Parts:

  • People and tools (kubectl, CI, apps)
  • API server (port 6443)
  • etcd (all data, all Secrets)
  • Node hosts (Linux machines)
  • kubelet (port 10250)
  • Pods (open network by default)

Connections:

  • People and tools to API server: HTTPS
  • API server to etcd: TLS
  • People and tools to Node hosts: SSH keys only
  • API server to kubelet: TLS, client cert
  • kubelet to Pods: runs

There is more than one way into a cluster, and an attacker only needs one.

DoorWhat is behind itThe lockCovered in
The node hostsWhoever is root on a node controls its containers, its kubelet and, on a control plane node, the cluster's keysHost hardening: SSH keys, no password loginsThis lesson
The API server (6443)Every object in the clusterAuthentication, authorization, admission controlAuthentication, Authorization, RBAC, Cluster Roles, Service Accounts
etcd (2379)All cluster data, including every SecretTLS with client certificates; only the API server gets oneTLS in Kubernetes
The kubelet (10250)Running containers on that node: exec, logs, the Pod listCertificates, no anonymous access, permissions checked with the API serverTLS in Kubernetes
Pod-to-Pod networkYour applications and their dataNetworkPolicyNetwork Policy
The containers themselvesThe node's kernel, shared by all containersTrusted images, security contextsImage Security, Security Contexts

The hosts come first

Kubernetes security assumes the machines underneath are safe. If someone can log in to a control plane node as root, nothing else in this section matters: they can read /etc/kubernetes/admin.conf and the private keys in /etc/kubernetes/pki, and from then on they are the cluster administrator.

So the first primitive is not a Kubernetes object at all:

  • Disable password logins over SSH and allow key-based authentication only.
  • Disable direct root login; administrators use their own account and sudo.
  • Keep the operating system patched and close ports that nothing needs.
  • Give node access to as few people as possible. Most people who work with a cluster never need a shell on a node.

How the API server controls access

Every action in a cluster is a request to the API server. kubectl, the scheduler, the kubelet and an application in a Pod all come in through the same door, and none of them can touch etcd directly. That is what makes the API server the right place to put the main lock.

Two questions for every requestarrivesidentityallowednobody accepts itno rule allows itRequestwith a credentialWho are you?AuthenticationWhat may you do?AuthorizationAdmissionthen stored in etcd401 Unauthorizedcredential not accepted403 Forbiddenknown, but not allowed
Two questions for every request. Authentication settles who is calling. Authorization settles what that caller may do. A request must pass both, in this order, before admission control and etcd.

Parts:

  • Request (with a credential)
  • Who are you? (Authentication)
  • What may you do? (Authorization)
  • Admission (then stored in etcd)
  • 401 Unauthorized (credential not accepted)
  • 403 Forbidden (known, but not allowed)

Connections:

  • Request to Who are you?: arrives
  • Who are you? to What may you do?: identity
  • What may you do? to Admission: allowed
  • Who are you? to 401 Unauthorized: nobody accepts it
  • What may you do? to 403 Forbidden: no rule allows it
  1. TLS connection
  2. Authentication: who?
  3. Authorization: allowed?
  4. Admission control
  5. Stored in etcd
Every request to the API server

Who can get in: authentication

Authentication turns a credential into an identity: a user name and a list of groups. The API server can accept several kinds of credential at once and tries each configured method until one recognises the request.

CredentialTypical userHow the API server checks it
X.509 client certificateAdministrators, the control plane components, kubeletsSigned by the CA in --client-ca-file. The certificate's common name is the user name, its organisations are the groups
Service account tokenApplications in Pods, automationA signed token the cluster issued itself. User name system:serviceaccount:<namespace>:<name>
OIDC token or authentication webhookEmployees signing in through a company identity providerThe API server verifies the token against the provider
Static token file (--token-auth-file)Labs onlyA CSV file of token, user, uid, groups read at startup
Bootstrap tokenA new node during kubeadm joinA short-lived token stored as a Secret in kube-system

Two facts surprise most newcomers.

Kubernetes has no User object. You cannot run kubectl create user, and kubectl get users does not exist. A human user is simply whatever name appears in a credential the API server trusts. To "create a user" you issue a certificate (or add the person to your identity provider), and to remove one you stop trusting that credential.

Service accounts are the exception. They are real namespaced objects, created with kubectl create serviceaccount, and they are the identity for software rather than people.

What they can do: authorization

Knowing who you are grants nothing by itself. The API server next passes the request (user, groups, verb, resource, namespace) to its authorizers, listed in --authorization-mode:

ModeWhat it decides
NodeA special authorizer for kubelets. Each kubelet may read only the Secrets, ConfigMaps and volumes needed by Pods on its own node
RBACRole-based access control. Permissions live in Roles and ClusterRoles and are handed out by RoleBindings and ClusterRoleBindings. This is the one you will configure
WebhookAsks an external service to decide
ABACAttribute-based rules in a file on the control plane node. Older and awkward: a change needs an API server restart
AlwaysAllow, AlwaysDenyFor tests only

A kubeadm cluster runs --authorization-mode=Node,RBAC. The modes are consulted in order. As soon as one says "allow", the request goes through; if none of them allows it, the answer is 403 Forbidden. RBAC has no deny rules, so permissions only ever add up, and the starting point for any new user is nothing.

Then admission control

A request that is authenticated and authorized still passes through admission controllers before it is stored. They look at the content of the object rather than at the caller. You met them in the Scheduling section; in the security picture they are the third gate, for rules such as "no privileged containers in this namespace".

The same rules apply to the components

The scheduler, the controller manager, the kubelets and kube-proxy get no special channel. Each is a client of the API server with its own identity and its own narrow permissions:

ComponentIdentity the API server sees
kube-schedulersystem:kube-scheduler
kube-controller-managersystem:kube-controller-manager
kubelet on node-1system:node:node-1, group system:nodes
kube-proxysystem:serviceaccount:kube-system:kube-proxy

A stolen kubelet certificate therefore does not unlock the cluster. It unlocks what one node is allowed to see.

TLS everywhere

Every connection is TLS, with certificates on both endsTLSTLSTLSTLS, both waysTLSkubectladmin certificateAPI serverserver certificateetcdaccepts only known clientsScheduler, controllersown client certificateskubeletsystem:node:node-1kube-proxyservice account token
Every connection is TLS, with certificates on both ends. The components do not trust the network. Each one proves its identity with a certificate, and the API server treats a component like any other client: it authenticates it and then checks its permissions.

Parts:

  • kubectl (admin certificate)
  • API server (server certificate)
  • etcd (accepts only known clients)
  • Scheduler, controllers (own client certificates)
  • kubelet (system:node:node-1)
  • kube-proxy (service account token)

Connections:

  • kubectl to API server: TLS
  • API server to etcd: TLS
  • Scheduler, controllers to API server: TLS
  • kubelet to API server: TLS, both ways
  • kube-proxy to API server: TLS

All of these checks would be worthless if someone on the network could read or change the traffic. So every connection between cluster components uses TLS, and in most cases both sides present a certificate: the server proves it is the real API server (or etcd, or kubelet), and the client proves who it is.

Two connections deserve attention because they bypass the API server's checks if left open:

  • etcd. Whoever can talk to etcd can read every Secret and rewrite any object, with no RBAC in the way. etcd is started with client certificate authentication, and on a kubeadm cluster the only client certificate handed out belongs to the API server (plus one used for health checks).
  • The kubelet API. Each kubelet serves an HTTPS API on port 10250 that can run commands in containers. kubeadm configures it to refuse anonymous requests and to ask the API server whether a caller is authorized.

The next lessons (TLS Basics through Certificates API) explain how these certificates work, where each file lives and how to create and inspect them.

Traffic between Pods

Everything above protects the control plane. Applications talking to each other are a different layer, and its default may surprise you.

Pod traffic: open until a policy closes itallowedallowedblocked by the policyselectsPod webfrontendPod apirole: apiPod dbport 5432Any other Podany namespaceNetworkPolicydb: only from role api
Pod traffic: open until a policy closes it. Without a NetworkPolicy any Pod can reach any other Pod. Once a policy selects the db Pod, only the traffic the policy lists gets through.

Parts:

  • Pod web (frontend)
  • Pod api (role: api)
  • Pod db (port 5432)
  • Any other Pod (any namespace)
  • NetworkPolicy (db: only from role api)

Connections:

  • Pod web to Pod api: allowed
  • Pod api to Pod db: allowed
  • Any other Pod to Pod db: blocked by the policy
  • NetworkPolicy to Pod db: selects

By default every Pod can reach every other Pod, across all namespaces. A namespace is a naming and permission boundary, not a network boundary. To restrict traffic you create a NetworkPolicy: it selects Pods by label and lists what may come in (ingress) and go out (egress). As soon as one policy selects a Pod for a direction, anything not listed for that direction is dropped.

Under the hood · Details that explain odd behaviour
  • A request with no credential is not rejected at the first gate. The API server labels it as the user system:anonymous in the group system:unauthenticated and lets authorization decide. That is why curl -k https://<server>:6443/api/v1/pods answers 403 and not 401, while /version and /livez answer normally: a built-in rule allows those few paths to everyone. A request with a wrong credential gets 401.
  • The group system:masters skips authorization entirely. A certificate with that organisation is allowed everything, and no RBAC change can take it away. Since Kubernetes 1.29, kubeadm's admin.conf uses the group kubeadm:cluster-admins instead, which is bound to the cluster-admin ClusterRole and can be revoked. The break-glass file super-admin.conf still carries system:masters; keep it off the nodes.
  • A certificate cannot be withdrawn one by one. Kubernetes does not check revocation lists. If an admin certificate leaks you remove its RBAC bindings, and for a system:masters certificate you must replace the CA. This is why certificates for people should be short-lived.
  • The kubelet's own defaults are open. Started with no configuration, a kubelet allows anonymous requests and authorizes everything. kubeadm writes anonymous.enabled: false and authorization.mode: Webhook into /var/lib/kubelet/config.yaml. On a hand-built cluster, check both.
  • Secrets are only base64-encoded in etcd unless you turn on encryption at rest. Protecting etcd and its backups is part of protecting Secrets.

Using it

Example · Find out who you are and what you may do

kubectl auth whoami shows the identity the API server derived from your credential:

kubectl auth whoami
ATTRIBUTE   VALUE
Username    kubernetes-admin
Groups      [kubeadm:cluster-admins system:authenticated]

kubectl auth can-i answers one authorization question. With --as you ask it for someone else, which is how you test a Role you have just created without switching kubeconfig:

kubectl auth can-i create deployments -n shop                       # yes
kubectl auth can-i delete pods -n shop --as jane                    # no
kubectl auth can-i get secrets -n shop --as system:serviceaccount:shop:web
kubectl auth can-i --list -n shop --as jane                         # everything jane may do in shop

Example · See the three answers of the API server

Talk to the API server without kubectl to watch authentication and authorization act separately. -k skips the server certificate check, which is acceptable for this experiment only.

# No credential, public path: allowed for everyone
curl -k https://controlplane:6443/version

# No credential, real resource: you are system:anonymous, and RBAC says no
curl -k https://controlplane:6443/api/v1/namespaces/default/pods
"message": "pods is forbidden: User \"system:anonymous\" cannot list resource \"pods\" in API group \"\" in the namespace \"default\"",
"code": 403
# A credential that nobody recognises: stopped at authentication
curl -k -H "Authorization: Bearer not-a-real-token" https://controlplane:6443/api/v1/namespaces/default/pods
"message": "Unauthorized",
"code": 401

A valid service account token gets past the first gate and is then judged by RBAC like everyone else:

TOKEN=$(kubectl create token default -n default)
curl -k -H "Authorization: Bearer $TOKEN" https://controlplane:6443/api/v1/namespaces/default/pods

The reply is 403 again, but now it names system:serviceaccount:default:default. The cluster knows the caller and has not given it permission to list Pods.

Example · Read a cluster's security settings in two minutes

On a kubeadm cluster the settings from this lesson are in a few files on the control plane node.

# How the API server authenticates and authorizes
grep -E "client-ca-file|authorization-mode|anonymous-auth|token-auth-file|enable-admission" \
  /etc/kubernetes/manifests/kube-apiserver.yaml
    - --authorization-mode=Node,RBAC
    - --client-ca-file=/etc/kubernetes/pki/ca.crt
    - --enable-admission-plugins=NodeRestriction
# Does etcd insist on client certificates?
grep -E "client-cert-auth|trusted-ca-file" /etc/kubernetes/manifests/etcd.yaml

# Is the kubelet API locked?
grep -A8 "^authentication:" /var/lib/kubelet/config.yaml
grep -A2 "^authorization:" /var/lib/kubelet/config.yaml
authentication:
  anonymous:
    enabled: false
  webhook:
    cacheTTL: 0s
    enabled: true
  x509:
    clientCAFile: /etc/kubernetes/pki/ca.crt
authorization:
  mode: Webhook

What you want to see: Node,RBAC (never AlwaysAllow), a client CA file, --client-cert-auth=true on etcd, and on the kubelet anonymous access disabled with authorization mode Webhook.

Example · Check whether Pod traffic is restricted

Before you debug a connection between two Pods, find out whether any policy is in play:

kubectl get networkpolicy -A
kubectl describe networkpolicy -n shop

No policies in a namespace means every Pod there accepts traffic from anywhere in the cluster. If a policy selects the target Pod, read its podSelector and its ingress rules: the source Pod's labels must match one of them.

Tricks

Common mistakes

  • Looking for a user object. There is no kubectl get users. Human users exist only as names inside certificates or tokens. Only ServiceAccounts are objects.
  • Mixing up authentication and authorization. A valid certificate proves who you are and grants nothing. A new user with a perfect certificate still gets 403 on everything until a binding exists.
  • Expecting an unauthenticated request to return 401. With anonymous access on (the default), a request without a credential becomes system:anonymous and is refused by authorization with 403.
  • Treating a namespace as a firewall. Namespaces separate names and permissions. Pods in different namespaces can still reach each other until a NetworkPolicy stops them.
  • Creating a NetworkPolicy on a cluster whose network plugin ignores it. The object is accepted and has no effect. Test the block, do not assume it.
  • Securing the API server and forgetting the rest. An open etcd port, a kubelet that allows anonymous requests or a shared root password on the nodes each bypass every RBAC rule you wrote.
  • Handing out admin.conf. It is the cluster administrator's identity. Give people their own credential and bind it to the smallest Role that does the job.

Recap

  • Secure the hosts first: key-based SSH, no root or password logins, few people with node access.
  • The API server asks two questions. Authentication: who are you? A failure is 401. Authorization: what may you do? A failure is 403. Admission control comes third.
  • Identities come from client certificates, service account tokens or an external provider. Kubernetes stores no human users; ServiceAccounts are the only identity objects.
  • kubeadm uses --authorization-mode=Node,RBAC. Nothing is allowed until a rule allows it.
  • All components talk over TLS with certificates, and each component is itself an authenticated, authorized client. etcd and the kubelet API must be locked as well.
  • Pods can all reach each other by default. NetworkPolicy restricts that, if the network plugin enforces it.
  • Check with kubectl auth whoami and kubectl auth can-i ... --as <user>.

Official docs

Quiz: check what you learned

5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.

Sign in to take the quiz and keep your score.

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.