Skip to content

Cluster Access

This guide covers how to configure kubectl access to the Kubernetes cluster using OIDC authentication via Keycloak.

ToolInstallation
kubectlbrew install kubectl
kubeloginbrew install kubelogin
yqbrew install yq

Cross-platform: Linux/Windows users can download binaries from kubectl releases and kubelogin releases.

Create the kubeconfig file at ~/.kube/configs/fzymgc-house-oidc.yml:

apiVersion: v1
kind: Config
clusters:
- name: fzymgc-house
cluster:
server: https://192.168.20.140:6443
certificate-authority-data: <base64-encoded-ca>
contexts:
- name: fzymgc-house-oidc
context:
cluster: fzymgc-house
user: oidc
users:
- name: oidc
user:
exec:
apiVersion: client.authentication.k8s.io/v1beta1
command: kubectl
args:
- oidc-login
- get-token
- --oidc-issuer-url=https://id.fzymgc.house/realms/fzymgc
- --oidc-client-id=kubernetes
- --oidc-extra-scope=offline_access
- --oidc-extra-scope=profile
- --oidc-extra-scope=email
interactiveMode: IfAvailable
current-context: fzymgc-house-oidc

Extract from any control plane node:

Terminal window
ssh tpi-alpha-1 'sudo cat /etc/rancher/k3s/k3s.yaml' | yq '.clusters[0].cluster.certificate-authority-data'
Terminal window
export KUBECONFIG=~/.kube/configs/fzymgc-house-oidc.yml

Add to your shell profile for persistence.

Run any kubectl command to trigger browser authentication:

Terminal window
kubectl get nodes
# Browser opens → Login to Keycloak → Return to terminal
ContextDescription
fzymgc-house-oidcStandard user access via OIDC (default)
fzymgc-house-adminAdmin access (break-glass, static token)
docker-desktopLocal Docker Desktop cluster

Access is determined by Keycloak group membership:

Keycloak GroupKubernetes RolePermissions
k8s-adminscluster-adminFull cluster access
k8s-developerseditCreate/modify workloads (no RBAC)
k8s-viewersviewRead-only access
TokenLifetimeBehavior
Access15 minAuto-refreshes silently
Refresh8 hoursBrowser re-auth when expired
EndpointAddress
API Server VIP192.168.20.140:6443
Load balanced viakube-vip

When Keycloak is unavailable, use the static admin kubeconfig:

Terminal window
export KUBECONFIG=~/.kube/configs/fzymgc-house-admin.yml
kubectl --context fzymgc-house get nodes

Warning: Admin kubeconfig has full cluster-admin privileges. Use only for emergencies.

Service Account Read-Only Access (Automation)

Section titled “Service Account Read-Only Access (Automation)”

The sysadm/crew-homelab-ro read-only access model is managed by Ansible.

Apply or reconcile it with:

Terminal window
ansible-playbook -i ansible/inventory/hosts.yml ansible/k3s-playbook.yml \
--tags k3s-readonly-access

Validate the effective permissions for the service account:

Terminal window
kubectl auth can-i --as=system:serviceaccount:sysadm:crew-homelab-ro list nodes
kubectl auth can-i --as=system:serviceaccount:sysadm:crew-homelab-ro list secrets -A
kubectl auth can-i --as=system:serviceaccount:sysadm:crew-homelab-ro get secrets -A

Expected:

  • list nodes -> yes
  • list secrets -A -> no
  • get secrets -A -> no

Try manual browser flow:

Terminal window
kubectl oidc-login get-token \
--oidc-issuer-url=https://id.fzymgc.house/realms/fzymgc \
--oidc-client-id=kubernetes

OIDC access tokens expire after 15 minutes. With offline_access scope configured, tokens refresh automatically. For very long streaming operations like kubectl logs -f, you may still need the admin kubeconfig if the stream outlasts the 8-hour refresh token.

Clear token cache and re-authenticate:

Terminal window
rm -rf ~/.kube/cache/oidc-login
kubectl get nodes # Re-authenticates

Ensure the cluster CA is trusted. If testing, use --insecure-skip-tls-verify temporarily.

  1. Verify Keycloak is accessible: https://id.fzymgc.house
  2. Check browser for authentication errors
  3. Review kubeconfig OIDC settings