skip to content

User Access & Kubeconfig

Kubernetes has no User object, so humans arrive as a client certificate signed through the CSR API or as an OIDC token from an external issuer, picked by a kubeconfig context. Revoking that access is the part people get wrong.

part ofKubernetesoverview, primer and where to startread it →
on this pageshow

questions

5

What are the clusters, users and contexts sections of a Kubernetes kubeconfig file, and how does kubectl decide which ones to use?

level: juniorimportance: must knowfreq 76%

answer

  1. three lists and one pointer
  2. context = cluster + user + namespace
  3. flag, then env list, then home file
  4. first file defining a name wins
  5. user entry is a local label

basics

~10 s

A kubeconfig lists clusters (API server URL and CA), users (credentials) and contexts that pair one cluster with one user and a default namespace. kubectl uses current-context unless the --context flag names another.

solid answer

~40 s

A kubeconfig (`kind: Config`) has three named lists. `clusters` hold the API server `server` URL and the CA data used to trust it; `users` each hold a credential (typically a client certificate and key, a bearer token, or an `exec` credential plugin); `contexts` tie one cluster to one user plus an optional default `namespace`. `current-context` names the context `kubectl` uses by default, and `--context` overrides it per command. The file comes from `--kubeconfig` if set, else the merged `KUBECONFIG` list (the first file to define a name wins), else `~/.kube/config`. The "user" name is only a local label: the identity the API server records comes from the credential itself, which `kubectl auth whoami` shows.

code

yaml · 19 lines
yaml
apiVersion: v1
kind: Config
clusters:
- name: spot-mix-38
  cluster:
    server: https://10.40.0.12:6443
    certificate-authority-data: LS0tLS1CRUdJTi...
users:
- name: fraud-dev
  user:
    client-certificate-data: LS0tLS1CRUdJTi...
    client-key-data: LS0tLS1CRUdJTi...
contexts:
- name: fraud-rules-prod
  context:
    cluster: spot-mix-38
    user: fraud-dev
    namespace: fraud-rules
current-context: fraud-rules-prod

go deeper

for a junior

Name the three lists and current-context, and show use-context and set-context --current --namespace from memory.

for a middle

Explain the load order (--kubeconfig, then KUBECONFIG merge, then the home file) and the first-file-wins merge rule, and why the user label is not an identity.

for a senior

Talk about kubeconfig as a credential: per-person identities, no shared admin files, explicit --context in scripts, and using kubectl auth whoami to verify what a context really is.

for a principal

Frame kubeconfig distribution as part of the access design: files that carry only cluster data plus a credential plugin, so issuance and revocation live in the identity provider rather than in copied files.

## What a kubeconfig is A **kubeconfig** is the YAML file that tells `kubectl` (and any client built on client-go) **where** the Kubernetes API server is and **who** to be when talking to it. Its top level is `apiVersion: v1` and `kind: Config`. It is not a Kubernetes object stored in the cluster: it lives on the client machine, by default at `~/.kube/config`. Because it usually carries a credential, it should be handled like a password. ## The three lists and the pointer A kubeconfig holds three named lists plus one pointer: | Section | Each entry holds | Example name | |---|---|---| | `clusters` | `server` (the API server URL) and the CA that signed the API server's serving certificate (`certificate-authority` or `certificate-authority-data`) | `spot-mix-38` | | `users` | a credential, typically `client-certificate-data` + `client-key-data`, a bearer `token`, or an `exec` block that runs a credential plugin | `fraud-dev` | | `contexts` | a reference to one cluster, one user and, optionally, a default `namespace` | `fraud-rules-prod` | | `current-context` | the name of the context used when nothing else is specified | `fraud-rules-prod` | Key points about the `users` list: - A "user" here is only a **local label for a credential**. Kubernetes has no User object; the username the API server records comes from the credential (a certificate's subject, a token's claims), not from this label. - The same credential can be reused by several contexts, for example one context per namespace. - A context does not grant anything. Permissions still come from RBAC bindings on the cluster side. ## How kubectl picks a file and a context `kubectl` resolves its configuration in this order: 1. If the `--kubeconfig` flag is given, that single file is used and nothing is merged. 2. Otherwise, if the `KUBECONFIG` environment variable is set, every file in its list (colon-separated on Linux and macOS, semicolon-separated on Windows) is loaded and **merged**. 3. Otherwise, `~/.kube/config` is used. Merging follows a **first-file-wins** rule: the first file that defines a given cluster, user or context name wins, and that entry is taken whole from that file; a later file's fields are never blended into it. `current-context` is likewise taken from the first file that sets it. Once the merged config exists, the context is chosen: `--context` if given, otherwise `current-context`. The flags `--cluster`, `--user` and `--namespace` override individual parts of that context for one command. ## Everyday commands ```bash kubectl config get-contexts kubectl config use-context fraud-rules-prod kubectl config set-context --current --namespace=fraud-rules kubectl config view --minify kubectl auth whoami ``` - `get-contexts` lists contexts and marks the current one with `*`. - `use-context` rewrites `current-context` in the file (it changes state for every later command). - `set-context --current --namespace` changes only the default namespace of the active context. - `view --minify` shows only what the current context uses; `--raw` would print the secret data too, and `--flatten` inlines referenced files so the result is portable. - `kubectl auth whoami` asks the API server, through a SelfSubjectReview, which username and groups it actually derived from your credential; this is the fastest way to confirm what the context really authenticates as. ## What a users entry can hold The `user` block of an entry normally carries one way of proving identity (client-go rejects a bearer token combined with basic auth), and the choice decides how access is issued and revoked: - **`client-certificate-data` and `client-key-data`**: an X.509 certificate signed by a CA the API server trusts. Simple and needs nothing else, but long-lived and not revocable. - **`token`**: a static bearer token pasted into the file. It is common for ServiceAccount tokens used by automation and a poor fit for people, because anyone who copies the file becomes you. - **`exec`**: a command that `kubectl` runs to fetch a short-lived credential, typically from an OIDC identity provider. The file then contains no secret at all, only instructions for getting one. Paths such as `client-certificate` and `client-key` can replace the `-data` fields when the material lives in separate files. ## Common failure modes - **Wrong cluster, right name.** Two teams' files both define a context called `prod`; with `KUBECONFIG` merging, whichever file is listed first silently wins. - **Shared admin file.** Copying one kubeconfig to many people makes every action look like one identity and makes revocation impossible without breaking everyone. - **Relative paths.** `client-certificate` and `certificate-authority` paths are resolved relative to the kubeconfig file itself, so moving the file breaks them; the `-data` variants, or `--flatten`, avoid that. - **Accidental production commands.** Because `use-context` is sticky, many teams prefer per-command `--context` in scripts, or a separate `KUBECONFIG` per shell.

  • Two files in KUBECONFIG both define a user named fraud-dev with different certificates. Which one does kubectl use?
    The entry from the file listed first, taken whole. client-go merges kubeconfig files so that the first file to set a given map key wins and its value is never changed by later files, so no fields are blended. `current-context` follows the same rule. Run `kubectl config view --minify` to see the effective result, and avoid reusing names across files.
  • How would you hand a colleague a kubeconfig safely, and what should never be in it?
    Give them a file that contains only the cluster entry and the CA, and let them add their own credential, ideally an `exec` plugin that fetches a short-lived token for their own identity. Never pass on your client key, token or an admin file: every action would be attributed to you. `kubectl config view --minify --flatten` produces a self-contained file, but it includes secrets, so strip the user entry first.

A kubeconfig is like a phone's saved contacts: clusters are the numbers, users are the SIM you call from, and a context is a speed-dial button pairing the two.

saying these in an interview costs you the question

  • A kubeconfig user entry creates a user account inside the cluster.
  • Switching context also switches your RBAC permissions on the same cluster by itself.
  • When KUBECONFIG lists several files, the last file overrides earlier ones.
  • Setting a context's namespace restricts you to that namespace.
  • Sharing one admin kubeconfig across the team is fine because it is inside the VPN.
open as a page

A contractor leaving the fraud-rules engine team still holds a Kubernetes client certificate valid for 290 more days. How do you actually cut off their cluster access?

level: seniorimportance: must knowfreq 58%

basics

~20 s

Kubernetes checks no CRL for client certificates, so the certificate keeps authenticating. Remove every RBAC binding matching its CN, re-key any group in its O field, rotate the CA if it holds system:masters, and hunt for credentials they minted.

open as a page

How do you issue a Kubernetes client certificate through the CertificateSigningRequest API, and how does the API server derive the username and groups?

level: middleimportance: should knowfreq 56%

basics

~20 s

Submit their PKCS#10 request as a CertificateSigningRequest with signer kubernetes.io/kube-apiserver-client, approve it with kubectl certificate approve, and read status.certificate. The API server takes the certificate's CN as the username and each O as a group.

open as a page

When a Kubernetes API server trusts an OIDC identity provider, what does the API server verify and what does a kubeconfig exec credential plugin do?

level: middleimportance: should knowfreq 52%

basics

~20 s

The API server only verifies ID tokens from a configured issuer and maps claims to a username and groups. A kubeconfig exec plugin performs the login, caches the token and gives it to kubectl, which sends it as a bearer token.

open as a page

You own human access to a 38-node Kubernetes cluster shared by several teams. How do you choose between client certificates and OIDC, and what break-glass path do you keep?

level: principalimportance: should knowfreq 38%

basics

~20 s

Use an OIDC identity provider for daily access, with prefixed groups bound by RBAC and short-lived tokens, because leavers and movers are handled centrally. Keep an independent, short-lived or sealed client certificate as an alerted break-glass path.

open as a page