Keycloak (Identity Provider / SSO)

OIDC authentication for kubectl and Single Sign-On for Grafana

Keycloak provides OIDC authentication for kubectl and Single Sign-On (SSO) for Grafana. It runs inside the cluster with a PostgreSQL backend and is published at https://keycloak.lab.internal (TLS through its own Istio Gateway).

The Keycloak Service is ClusterIP: all access goes through the gateway. Realm, client and user configuration is applied in-cluster with kcadm.sh via kubectl exec, so no external port is needed.

Admin credentials

URL:      https://keycloak.lab.internal/admin
Username: admin
Password: Vault → secret/k8s-provisioner/keycloak/admin → password
vagrant ssh controlplane -c \
  'sudo k8s-provisioner vault get k8s-provisioner/keycloak/admin password -c /etc/k8s-provisioner/config.yaml'

Every Keycloak login has its own Vault path under secret/k8s-provisioner/keycloak/:

Vault pathKeysDescription
keycloak/adminusername, passwordAdmin console (admin)
keycloak/postgresusername, passwordPostgreSQL (keycloak)
keycloak/users/k8sadminusername, passwordRealm user k8sadmin
keycloak/users/developerusername, passwordRealm user developer
keycloak/clients/grafanaclient_id, client_secretGrafana OIDC client

To choose the test users’ passwords yourself, set them after Vault is initialized and before Keycloak is installed (or recreate the cluster):

vault kv patch secret/k8s-provisioner/keycloak/users/k8sadmin password="MyStr0ng@Pass"
vault kv patch secret/k8s-provisioner/keycloak/users/developer password="An0ther@Pass123"

Pre-configured realm: k8s

ResourceDetails
Realmk8s
kubectl clientkubectl (public, PKCE)
Grafana clientgrafana (confidential)
Admin groupk8s-admins → cluster-admin RBAC, Grafana Admin
Developer groupk8s-developers → view RBAC, Grafana Viewer
Test user (admin)k8sadmin (Vault: keycloak/users/k8sadmin)
Test user (dev)developer (Vault: keycloak/users/developer)

kubectl access

Admin kubeconfig

# Already points at https://192.168.56.10:6443
vagrant ssh controlplane -c 'sudo cat /etc/kubernetes/admin.conf' > ~/.kube/config-lab
export KUBECONFIG=~/.kube/config-lab
kubectl get nodes

This uses cluster-admin client certificates. Fine for day-to-day lab use.

OIDC login via Keycloak (kubelogin)

Each user logs in with their own Keycloak credentials and gets the permissions of their group. The kubeconfig-oidc holds no credentials, so it is safe to hand out; it is stored in Vault during provisioning and verifies the lab CA (no insecure-skip-tls-verify).

How authentication works

kubectl → kubelogin → browser → Keycloak login (PKCE)
                ↓
         id_token (groups claim)
                ↓
         kube-apiserver validates it via --authentication-config
                ↓
         groups prefixed "oidc:" → ClusterRoleBinding → RBAC

The API server reads /etc/kubernetes/pki/auth-config.yaml:

apiVersion: apiserver.config.k8s.io/v1
kind: AuthenticationConfiguration
jwt:
- issuer:
    url: https://keycloak.lab.internal/realms/k8s
    certificateAuthority: |
      <lab CA from cert-manager>
    audiences:
    - kubectl
    audienceMatchPolicy: MatchAny
  claimMappings:
    username:
      claim: preferred_username
      prefix: "oidc:"
    groups:
      claim: groups
      prefix: "oidc:"

The API server runs on the host network and resolves keycloak.lab.internal through the lab DNS (the nodes use the storage node as nameserver). The oidc-k8s-admins (→ cluster-admin) and oidc-k8s-developers (→ view) ClusterRoleBindings are applied during provisioning.

First-time setup (once per machine)

# 1. Install kubelogin
brew install int128/kubelogin/kubelogin   # macOS; or: kubectl krew install oidc-login

# 2. Host setup: resolve lab.internal and trust the lab CA
#    (see Getting Started → Host Setup)

# 3. Fetch the OIDC kubeconfig from Vault (from the vagrant/ directory)
vagrant ssh controlplane -c \
  'sudo k8s-provisioner vault get k8s-provisioner/kubeconfig-oidc config -c /etc/k8s-provisioner/config.yaml' \
  > ~/.kube/config-oidc
export KUBECONFIG=~/.kube/config-oidc

# 4. First login opens the browser at Keycloak
kubectl get nodes

The token is cached locally; the browser opens again only when it expires (24h). Check who you are:

kubectl auth whoami
# ATTRIBUTE   VALUE
# Username    oidc:alice
# Groups      [oidc:k8s-developers system:authenticated]

After vagrant destroy + vagrant up, fetch the OIDC kubeconfig again: it embeds the lab CA, which is new. See Host Setup → After recreating the lab.

Adding a user

Access is group-based: create the user in Keycloak, add them to a group, and send them the kubeconfig-oidc.

Keycloak groupKubernetes accessGrafana role
k8s-adminscluster-adminAdmin
k8s-developersview (read-only)Viewer

Option A — Admin console:

  1. Open https://keycloak.lab.internal/admin → realm k8s → Users → Add user
  2. Set username and email, enable the user, save
  3. Credentials tab → set the password
  4. Groups tab → join k8s-admins or k8s-developers

Option B — CLI (from the controlplane):

KCADM="kubectl exec -n keycloak deploy/keycloak -- /opt/keycloak/bin/kcadm.sh"
ADMIN_PW=$(sudo k8s-provisioner vault get k8s-provisioner/keycloak/admin password)
$KCADM config credentials --server http://localhost:8080 --realm master --user admin --password "$ADMIN_PW"

NEW_UID=$($KCADM create users -r k8s -s username=alice -s email=alice@example.com \
  -s firstName=Alice -s lastName=Smith -s enabled=true -i)
$KCADM set-password -r k8s --username alice --new-password 'Alice@K8s123'

GID=$($KCADM get groups -r k8s | grep -A1 k8s-developers | grep id | cut -d'"' -f4)
$KCADM update users/$NEW_UID/groups/$GID -r k8s -s realm=k8s -s userId=$NEW_UID -s groupId=$GID -n

Grafana SSO

Click “Sign in with Keycloak” on the Grafana login page.

UserPasswordGrafana role
k8sadminVault keycloak/users/k8sadminAdmin
developerVault keycloak/users/developerViewer

Users in k8s-admins get the Admin role, everyone else Viewer. The local admin login still works (password in Vault grafana/admin).

Registering a new application

To protect an app with Keycloak SSO, create a client in the k8s realm.

Admin console: Clients → Create client → type OpenID Connect, client ID myapp → enable Client authentication for a confidential (server-side) client → Valid redirect URIs https://myapp.lab.internal/*, Web origins https://myapp.lab.internal → save. The secret is on the Credentials tab.

CLI:

# Confidential client (server-side apps)
$KCADM create clients -r k8s -s clientId=myapp -s publicClient=false \
  -s 'redirectUris=["https://myapp.lab.internal/*"]' -s enabled=true

# Public client with PKCE (SPAs / CLIs)
$KCADM create clients -r k8s -s clientId=myapp-spa -s publicClient=true \
  -s 'redirectUris=["https://myapp.lab.internal/*"]' \
  -s 'attributes={"pkce.code.challenge.method":"S256"}' -s enabled=true

To put the user’s groups in the token, add the realm’s groups client scope to the client (Client scopes tab → Add client scope → groups). The token then carries "groups": ["k8s-admins"], which your app can use for role-based access. Publish the app itself as described in Lab DNS.