Feb 19, 2025
Keycloak, Helm & Kubernetes: Setup & Authentication Tutorial
In this tutorial, we'll deploy Keycloak on a local Kubernetes cluster (e.g., Docker Desktop or Minikube) using Helm. We will:
Spin up Keycloak (and its PostgreSQL database) via a Helm chart.
Create a Realm in Keycloak to isolate authentication data.
Create a Client to define valid authentication requests.
Add a User and assign a Role.
Use Postman to get an access token, refresh it, and log out from Keycloak.
Note: While our focus is on a local cluster (NodePort service type), you can easily adapt this for a production environment with a LoadBalancer or Ingress Controller.
Prerequisites
Kubernetes cluster running locally (e.g., Docker Desktop, Minikube) or on the cloud.
Helm installed locally. For installation instructions, see Install Helm on Mac and Windows.
Basic knowledge of kubectl commands.
(Optional) Postman for sending authentication requests.
Cloning the Starter Code
A GitHub repository contains all necessary YAML files and Postman collections:
Inside, you’ll see:
Helm
folder orhelm-values.yaml
(the values file for the Keycloak chart).A
Postman
collection file, which you can import into Postman.Other resource links (like how to set up Docker Desktop for Kubernetes, etc.).
Helm Chart and Values File
The values.yaml
Explained
Below is the core content of helm-values.yaml
(already included in the repository). It configures both Keycloak and PostgreSQL under one chart:
Key Points:
PostgreSQL is enabled and set to store Keycloak data (
bn_keycloak
user,bn_keycloak
password,bitnami_keycloak
DB).We use NodePort (
30080
) so Keycloak is accessible viahttp://localhost:30080
on a single-node cluster.The Keycloak admin user/password is
admin
/admin
by default.Additional environment variables override certain defaults (HTTP enabled, hostname strict disabled, etc.).
Production Considerations: For a real environment, you might switch service.type to LoadBalancer or manage Keycloak behind an Ingress Controller with TLS.
Installing Keycloak on Kubernetes
Adding the Bitnami Helm Repo
Bitnami hosts the Keycloak Helm chart:
Deploying Keycloak with Helm
Use the provided helm-values.yaml
to install Keycloak:
Notes:
-version 24.4.2
ensures we use a specific version for consistency.
-namespace keycloak --create-namespace
creates a new namespace namedkeycloak
and deploys Keycloak there.
Check pods and services:
You should see one or more Keycloak pods and a PostgreSQL pod, plus a Service (type NodePort
) mapping Keycloak’s port 8080
to 30080
on your host.
Accessing the Keycloak Admin Console
Once Keycloak finishes starting (it may take a few minutes and pod restarts initially), open a browser and go to:
Click “Administration Console.”
Log in with:
Username:
admin
Password:
admin
You’ll enter the Keycloak Admin Console, where you can configure realms, clients, roles, and users.
Creating a Realm
A Realm is an isolated authentication domain. Each realm has its own users, roles, and clients.
In the top-left dropdown (where it says Master), click “Create Realm.”
Name your realm, e.g.,
test-realm
.Click “Create.”
Everything we do next (users, clients, roles) is scoped to this test-realm
.
Creating a Client
Clients represent applications or services that request authentication from Keycloak.
Under “Clients” (left menu), click “Create Client.”
For Client ID, enter
test-client
.Enable Client Authentication, so Keycloak issues a client secret.
Add some valid Redirect URIs (e.g.,
http://localhost/*
) even if you don’t fully use them for direct grant type.Click “Save.”
Under “Credentials” for this client, you’ll find the generated client secret, needed for token requests.
Defining a User and Realm Role
Create a Role
Under “Realm Roles,” click “Create Role.”
Call it
api-access
. (This role could represent permission to call certain APIs.)
Create a User
Navigate to “Users” → “Add User.”
For Username, type
test-user
, and click “Save.”(Optional) Add an email or first name.
Under “Credentials,” set a password (e.g.,
test-password
).Uncheck “Temporary” if you don’t want the user to reset next login.
Click “Role Mappings.”
Assign the
api-access
role totest-user
.
Now, test-user
has credentials and a role granting them access to potential protected endpoints.
Testing Authentication with Postman
We can confirm the authentication flow using the Keycloak OpenID Connect endpoints. Import the Postman collection provided in the repo:
In Postman, click “Import.”
Select the
Keycloak_Auth_Flow - Kubernetes.json
or similar file from the repository.You’ll see four requests:
Get Access Token (Password Grant)
Refresh Token
User Info
Logout
Obtaining an Access Token
Open “1. Get Access Token (Password Grant)”:
client_id
:test-client
client_secret
: (from Keycloak →test-client
→ “Credentials”)grant_type
:password
username
:test-user
password
:test-password
URL:
http://localhost:30080/realms/test-realm/protocol/openid-connect/token
Click “Send.” If successful, you’ll see:
Accessing a Protected Resource
Keycloak offers a built-in protected endpoint: userinfo
.
Copy the
"access_token"
.Open “3. User Info.”
Under Headers, set
Authorization: Bearer <access_token>
.Send.
If authentication succeeds, you’ll get back user details (username, email, roles, etc.).
Refreshing the Token
Refresh tokens allow you to obtain a new access_token
without re-entering user credentials:
Open “2. Refresh Token.”
Supply the same
client_id
,client_secret
, but setgrant_type
torefresh_token
.Provide
"refresh_token"
from your previous step.Click “Send.”
You’ll receive a fresh access token in the response.
Logging Out
To invalidate the refresh token and session:
Open “4. Logout.”
Provide
client_id
,client_secret
, and the currentrefresh_token
.Click “Send.”
If successful, Keycloak returns 204 No Content
, ending the user session.
Cleaning Up
When finished, you can remove all Keycloak resources from your cluster:
Optionally remove the namespace:
Also, if you want to free up space, remove any unused images:
Video Chapters
00:00:00 – 00:01:40 What to Expect: Keycloak Auth on Kubernetes w/ Helm
00:01:40 – 00:04:50 Download Helm Starter Code
00:04:50 – 00:06:18 Keycloak Bitnami Helm Chart
00:06:18 – 00:08:51 Install Keycloak Helm Chart on Kubernetes
00:08:51 – 00:09:40 Keycloak Admin Console
00:09:40 – 00:10:24 Create Keycloak Realm
00:10:24 – 00:13:24 Create Keycloak Client
00:13:24 – 00:15:22 Create Realm Role for Keycloak User
00:15:22 – 00:16:20 Import Postman Collection
00:16:20 – 00:18:08 Keycloak Access Token
00:18:08 – 00:19:04 Access Protected Resource
00:19:04 – 00:20:04 Keycloak Refresh Token
00:20:04 – end Keycloak Logout & Cleaning Up
Additional Resources
GitHub Repository (Tutorial Code):Keycloak Authentication Tutorial (Helm Kubernetes)
Setting Up a Kubernetes Cluster with Docker Desktop:Kubernetes Cluster Setup
Install Helm (Mac/Windows):Install Helm
Postman Download:Download Postman
Kubernetes Training Course:Kubernetes Full Course: https://kubernetestraining.io/
Conclusion
Congratulations! You have successfully:
Deployed Keycloak on Kubernetes using the Bitnami Helm Chart.
Created a Realm, Client, and a User with a Role.
Demonstrated the token-based authentication flow via Postman—access tokens, refresh tokens, and logout functionality.
This setup is perfect for local testing or proof-of-concept work. With a few tweaks (switching to a LoadBalancer or adding TLS), you can move the same approach to production clusters. If you found this helpful, consider exploring more DevOps and Kubernetes content and be sure to check out the full YouTube tutorial for a hands-on walkthrough.
Kubernetes Training
If you find these guides helpful, check out our Kubernetes Training course