All guides

Sign in to Kubernetes with OIDC in place of shared kubeconfigs

With a client certificate, Kubernetes takes the username from the certificate's common name. With OpenID Connect, the API server reads the username and groups from a token that your identity provider issues to each person.

Hands typing on a laptop that shows code

How Kubernetes identifies a user

Kubernetes does not have objects that represent normal user accounts, and normal users cannot be added through an API call. Kubernetes treats a user as authenticated once they present a valid certificate that the cluster's certificate authority signed. Kubernetes reads the username from the certificate: it is the common name field of the subject.

Kubernetes also has native support for OpenID Connect (OIDC). kubectl sends the ID token from your identity provider to the API server.

How the API server checks a token

The ID token is a JSON web token. The Kubernetes documentation describes what the API server does with it:

  1. It makes sure the signature is valid. The public key is discovered from the issuer's endpoint through OIDC discovery.
  2. It checks that the token has not expired. The iss claim must match the configured issuer URL, and the aud claim must match a configured audience.
  3. It takes the username and the groups from the claims you configure.
  4. It makes sure the user is authorized.

Kubernetes does not need to contact the identity provider, because all the data needed to validate the user is in the ID token. The same page says the ID token cannot be revoked, so it should be short-lived, only a few minutes.

Option 1, the OIDC flags

kubelogin's setup guide gives the two flags to add to kube-apiserver:

Text

--oidc-issuer-url=ISSUER_URL
--oidc-client-id=YOUR_CLIENT_ID

These two are required, and the issuer URL must use the https:// scheme. The Kubernetes documentation lists optional flags, among them:

  • --oidc-username-claim: the claim to use as the user name. The default is sub, and other claims such as email can be chosen.
  • --oidc-username-prefix: prevents clashes with existing names such as system: users. With the value oidc:, usernames look like oidc:jane.doe.
  • --oidc-groups-claim: the claim to use as the user's groups. If present, it must be an array of strings.
  • --oidc-groups-prefix: with the value oidc:, group names look like oidc:engineering.

Multiple authenticators, and multiple audiences for one issuer, are available only with the configuration file.

Option 2, structured authentication configuration

The configuration file is passed with --authentication-config. It has been stable since Kubernetes v1.34 and was first available in v1.29. This is an example from the Kubernetes documentation, with a typo in the message corrected:

YAML

apiVersion: apiserver.config.k8s.io/v1
kind: AuthenticationConfiguration
jwt:
- issuer:
    url: https://example.com
    audiences:
    - my-app
  claimMappings:
    username:
      expression: 'claims.username + ":external-user"'
    groups:
      expression: 'claims.roles.split(",")'
    uid:
      expression: 'claims.sub'
    extra:
    - key: 'example.com/tenant'
      valueExpression: 'claims.tenant'
  userValidationRules:
  - expression: "!user.username.startsWith('system:')"
    message: 'username cannot use reserved system: prefix'

In the documentation, a token with the username foo and the roles user,admin becomes the user foo:external-user in the groups user and admin.

The file adds these capabilities:

  • multiple JWT authenticators, 64 at most
  • multiple audiences for one issuer
  • CEL expressions that map claims to user attributes and validate claims and user information
  • a reload when the file is modified, while the API server is running

The two options cannot be combined. If --authentication-config is specified along with any --oidc-* argument, the API server reports an error and immediately exits.

Sign in from kubectl

Kubernetes offers no web interface that starts the authentication process. kubelogin, from the int128/kubelogin repository, is a kubectl plugin also known as kubectl oidc-login. kubectl runs it ahead of each call to the Kubernetes API, the plugin opens a browser for the sign-in, and it hands the credentials back to kubectl.

Its README installs it with brew install kubelogin or kubectl krew install oidc-login. These two commands are from its setup guide. The first shows setup instructions and dumps the claims of an ID token from the provider. The second adds the user to the kubeconfig:

Shell

kubectl oidc-login setup \
  --oidc-issuer-url=ISSUER_URL \
  --oidc-client-id=YOUR_CLIENT_ID

kubectl config set-credentials oidc \
  --exec-interactive-mode=Never \
  --exec-api-version=client.authentication.k8s.io/v1 \
  --exec-command=kubectl \
  --exec-arg=oidc-login \
  --exec-arg=get-token \
  --exec-arg=--oidc-issuer-url=ISSUER_URL \
  --exec-arg=--oidc-client-id=YOUR_CLIENT_ID

The setup guide says you can share this kubeconfig with team members for onboarding. It also recommends adding --token-cache-storage=keyring, because kubelogin stores the token cache on the file system by default.

kubelogin refreshes an expired ID token with the refresh token, and performs the authentication again when the refresh token has expired.

Azure's kubelogin is a different plugin. It implements Microsoft Entra authentication for AKS.

Map groups to RBAC

RBAC subjects can be groups. This is the ClusterRoleBinding example from the Kubernetes documentation. Through a ClusterRole named secret-reader, it lets every user in the manager group read secrets in all namespaces:

YAML

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: read-secrets-global
subjects:
- kind: Group
  name: manager # Name is case sensitive
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: secret-reader
  apiGroup: rbac.authorization.k8s.io

When a groups prefix is set, bind the prefixed name, such as oidc:engineering. To see the username and groups the cluster maps to you, run kubectl auth whoami.

Managed clusters

Amazon EKS supports associating OIDC identity providers with a cluster. IAM authentication cannot be disabled, and clusters on Kubernetes 1.32 or later accept up to 10 providers, while earlier versions accept one. AKS has a Microsoft Entra integration that cannot be disabled once enabled, and Entra groups are included only by their object ID. For GKE, Google recommends Workforce Identity Federation. Identity Service for GKE is deprecated since 1 July 2026 and unsupported in GKE version 1.37 and later.

What to check before you switch

  1. The provider. Kubernetes requires three things of an identity provider: it supports OIDC discovery, it runs TLS with non-obsolete ciphers, and it has a CA-signed certificate.
  2. The claims. Read the claims that kubectl oidc-login setup prints. The token must contain the configured username claim, and a groups claim must be an array of strings.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides