Este contenido no está disponible en el idioma seleccionado.
Chapter 11. Red Hat Single Sign-On Operator
The Red Hat Single Sign-On Operator automates Red Hat Single Sign-On administration in Openshift. You use this Operator to create custom resources (CRs), which automate administrative tasks. For example, instead of creating a client or a user in the Red Hat Single Sign-On admin console, you can create custom resources to perform those tasks. A custom resource is a YAML file that defines the parameters for the administrative task.
You can create custom resources to perform the following tasks:
After you create custom resources for realms, clients, and users, you can manage them by using the Red Hat Single Sign-On admin console or as custom resources using the oc
command. However, you cannot use both methods, because the Operator performs a one way sync for custom resources that you modify. For example, if you modify a realm custom resource, the changes show up in the admin console. However, if you modify the realm using the admin console, those changes have no effect on the custom resource.
Begin using the Operator by Installing the Red Hat Single Sign-On Operator on a cluster.
11.1. Installing the Red Hat Single Sign-On Operator on a cluster Copiar enlaceEnlace copiado en el portapapeles!
To install the Red Hat Single Sign-On Operator, you can use:
11.1.1. Installing using the Operator Lifecycle Manager Copiar enlaceEnlace copiado en el portapapeles!
Prerequisites
- You have cluster-admin permission or an equivalent level of permissions granted by an administrator.
Procedure
Perform this procedure on an OpenShift cluster.
- Open the OpenShift Container Platform web console.
-
In the left column, click
Operators, OperatorHub
. Search for Red Hat Single Sign-On Operator.
OperatorHub tab in OpenShift
Click the Red Hat Single Sign-On Operator icon.
An Install page opens.
Operator Install page on OpenShift
-
Click
Install
. Select a namespace and click Subscribe.
Namespace selection in OpenShift
The Operator starts installing.
Additional resources
- When the Operator installation completes, you are ready to create your first custom resource. See Red Hat Single Sign-On installation using a custom resource.
- For more information on OpenShift Operators, see the OpenShift Operators guide.
11.2. Operator compatibility with different RH-SSO versions Copiar enlaceEnlace copiado en el portapapeles!
The RH-SSO Operator 7.6 is fully compatible with RH-SSO 7.5. The 7.6 Operator by default deploys RH-SSO 7.6. To use the RH-SSO 7.5 image instead, override the image coordinates by setting the RELATED_IMAGE_RHSSO
environment variable in the Operator pod. To make that change, update your OLM Subscription
for the RH-SSO Operator inside your OpenShift cluster, for example:
The usage of RH-SSO Operator 7.6 with RH-SSO 7.5 is fully supported.
Additional resources
- When the Operator installation completes, you are ready to create your first custom resource. See Red Hat Single Sign-On installation using a custom resource.
- For more information on OpenShift Operators, see the OpenShift Operators guide.
11.3. Using the Red Hat Single Sign-On Operator in production environment Copiar enlaceEnlace copiado en el portapapeles!
- The usage of embedded DB is not supported in a production environment.
- Backup CRD is deprecated and not supported in a production environment.
-
We fully support using the rest of the CRDs in production, despite the
v1alpha1
version. We do not plan to make any breaking changes in this CRDs version.
11.4. Installing Red Hat Single Sign-On using a custom resource Copiar enlaceEnlace copiado en el portapapeles!
You can use the Operator to automate the installation of Red Hat Single Sign-On by creating a Keycloak custom resource. When you use a custom resource to install Red Hat Single Sign-On, you create the components and services that are described here and illustrated in the graphic that follows.
-
keycloak-db-secret
- Stores properties such as the database username, password, and external address (if you connect to an external database) -
credentials-<CR-Name>
- Admin username and password to log into the Red Hat Single Sign-On admin console (the<CR-Name>
is based on theKeycloak
custom resource name) -
keycloak
- Keycloak deployment specification that is implemented as a StatefulSet with high availability support -
keycloak-postgresql
- Starts a PostgreSQL database installation -
keycloak-discovery
Service - PerformsJDBC_PING
discovery -
keycloak
Service - Connects to Red Hat Single Sign-On through HTTPS (HTTP is not supported) -
keycloak-postgresql
Service - Connects an internal and external, if used, database instance -
keycloak
Route - The URL for accessing the Red Hat Single Sign-On admin console from OpenShift
How Operator components and services interact
11.4.1. The Keycloak custom resource Copiar enlaceEnlace copiado en el portapapeles!
The Keycloak custom resource is a YAML file that defines the parameters for installation. This file contains three properties.
-
instances
- controls the number of instances running in high availability mode. -
externalAccess
- if theenabled
isTrue
, the Operator creates a route for OpenShift for the Red Hat Single Sign-On cluster. -
externalDatabase
- in order to connect to an externally hosted database. That topic is covered in the external database section of this guide. Setting it to false should be used only for testing purposes and will install an embedded PostgreSQL database. Be aware that externalDatabase:false is NOT supported in production environments.
Example YAML file for a Keycloak custom resource
You can update the YAML file and the changes appear in the Red Hat Single Sign-On admin console, however changes to the admin console do not update the custom resource.
11.4.2. Creating a Keycloak custom resource on OpenShift Copiar enlaceEnlace copiado en el portapapeles!
On OpenShift, you use the custom resource to create a route, which is the URL of the admin console, and find the secret, which holds the username and password for the admin console.
Prerequisites
- You have a YAML file for this custom resource.
- You have cluster-admin permission or an equivalent level of permissions granted by an administrator.
Procedure
Create a route using your YAML file:
oc create -f <filename>.yaml -n <namespace>
. For example:oc create -f sso.yaml -n sso
$ oc create -f sso.yaml -n sso keycloak.keycloak.org/example-sso created
Copy to Clipboard Copied! Toggle word wrap Toggle overflow A route is created in OpenShift.
- Log into the OpenShift web console.
Select
Networking
,Routes
and search for Keycloak.Routes screen in OpenShift web console
On the screen with the Keycloak route, click the URL under
Location
.The Red Hat Single Sign-On admin console login screen appears.
Admin console login screen
Locate the username and password for the admin console in the OpenShift web console; under
Workloads
, clickSecrets
and search for Keycloak.Secrets screen in OpenShift web console
Enter the username and password into the admin console login screen.
Admin console login screen
You are now logged into an instance of Red Hat Single Sign-On that was installed by a Keycloak custom resource. You are ready to create custom resources for realms, clients, and users.
Red Hat Single Sign-On master realm
Check the status of the custom resource:
oc describe keycloak <CR-name>
$ oc describe keycloak <CR-name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Results
After the Operator processes the custom resource, view the status with this command:
oc describe keycloak <CR-name>
$ oc describe keycloak <CR-name>
Keycloak custom resource Status
Additional resources
- Once the installation of Red Hat Single Sign-On completes, you are ready to create a realm custom resource.
- An external database is the supported option and needs to be enabled in the Keycloak custom resource. You can disable this option only for testing and enable it when you switch to a production environment. See Connecting to an external database.
11.5. Creating a realm custom resource Copiar enlaceEnlace copiado en el portapapeles!
You can use the Operator to create realms in Red Hat Single Sign-On as defined by a custom resource. You define the properties of the realm custom resource in a YAML file.
You can update the YAML file and changes appear in the Red Hat Single Sign-On admin console, however changes to the admin console do not update the custom resource.
Example YAML file for a Realm
custom resource
Prerequisites
- You have a YAML file for this custom resource.
-
In the YAML file, the
app
underinstanceSelector
matches the label of a Keycloak custom resource. Matching these values ensures that you create the realm in the right instance of Red Hat Single Sign-On. - You have cluster-admin permission or an equivalent level of permissions granted by an administrator.
Procedure
Use this command on the YAML file that you created:
oc create -f <realm-name>.yaml
. For example:oc create -f initial_realm.yaml
$ oc create -f initial_realm.yaml keycloak.keycloak.org/test created
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Log into the admin console for the related instance of Red Hat Single Sign-On.
Click Select Realm and locate the realm that you created.
The new realm opens.
Admin console master realm
Results
After the Operator processes the custom resource, view the status with this command:
oc describe keycloak <CR-name>
$ oc describe keycloak <CR-name>
Realm custom resource status
Additional resources
- When the realm creation completes, you are ready to create a client custom resource.
11.6. Creating a client custom resource Copiar enlaceEnlace copiado en el portapapeles!
You can use the Operator to create clients in Red Hat Single Sign-On as defined by a custom resource. You define the properties of the realm in a YAML file.
You can update the YAML file and changes appear in the Red Hat Single Sign-On admin console, however changes to the admin console do not update the custom resource.
Example YAML file for a Client custom resource
Prerequisites
- You have a YAML file for this custom resource.
- You have cluster-admin permission or an equivalent level of permissions granted by an administrator.
Procedure
Use this command on the YAML file that you created:
oc create -f <client-name>.yaml
. For example:oc create -f initial_client.yaml
$ oc create -f initial_client.yaml keycloak.keycloak.org/example-client created
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Log into the Red Hat Single Sign-On admin console for the related instance of Red Hat Single Sign-On.
Click Clients.
The new client appears in the list of clients.
Results
After a client is created, the Operator creates a Secret containing the Client ID
and the client’s secret using the following naming pattern: keycloak-client-secret-<custom resource name>
. For example:
Client’s Secret
apiVersion: v1 data: CLIENT_ID: <base64 encoded Client ID> CLIENT_SECRET: <base64 encoded Client Secret> kind: Secret
apiVersion: v1
data:
CLIENT_ID: <base64 encoded Client ID>
CLIENT_SECRET: <base64 encoded Client Secret>
kind: Secret
After the Operator processes the custom resource, view the status with this command:
oc describe keycloak <CR-name>
$ oc describe keycloak <CR-name>
Client custom resource Status
Additional resources
- When the client creation completes, you are ready to create a user custom resource.
11.7. Creating a user custom resource Copiar enlaceEnlace copiado en el portapapeles!
You can use the Operator to create users in Red Hat Single Sign-On as defined by a custom resource. You define the properties of the user custom resource in a YAML file.
You can update properties, except for the password, in the YAML file and changes appear in the Red Hat Single Sign-On admin console, however changes to the admin console do not update the custom resource.
Example YAML file for a user custom resource
Prerequisites
- You have a YAML file for this custom resource.
-
The
realmSelector
matches the labels of an existing realm custom resource. - You have cluster-admin permission or an equivalent level of permissions granted by an administrator.
Procedure
Use this command on the YAML file that you created:
oc create -f <user_cr>.yaml
. For example:oc create -f initial_user.yaml
$ oc create -f initial_user.yaml keycloak.keycloak.org/example-user created
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Log into the admin console for the related instance of Red Hat Single Sign-On.
- Click Users.
Search for the user that you defined in the YAML file.
You may need to switch to a different realm to find the user.
Results
After a user is created, the Operator creates a Secret using the following naming pattern: credential-<realm name>-<username>-<namespace>
, containing the username and, if it has been specified in the CR credentials
attribute, the password.
Here’s an example:
KeycloakUser
Secret
Once the Operator processes the custom resource, view the status with this command:
oc describe keycloak <CR-name>
$ oc describe keycloak <CR-name>
User custom resource Status
Additional resources
- If you have an external database, you can modify the Keycloak custom resource to support it. See Connecting to an external database.
- To back up your database using custom resources, see schedule database backups.
11.8. Connecting to an external database Copiar enlaceEnlace copiado en el portapapeles!
You can use the Operator to connect to an external PostgreSQL database by creating a keycloak-db-secret
YAML file and setting Keycloak CR externalDatabase property to enabled. Note that values are Base64 encoded.
Example YAML file for keycloak-db-secret
The following properties set the hostname or IP address and port of the database.
-
POSTGRES_EXTERNAL_ADDRESS
- an IP address or a hostname of the external database. -
POSTGRES_EXTERNAL_PORT
- (Optional) A database port.
The other properties work in the same way for a hosted or external database. Set them as follows:
-
POSTGRES_DATABASE
- Database name to be used. -
POSTGRES_USERNAME
- Database username -
POSTGRES_PASSWORD
- Database password -
POSTGRES_SUPERUSER
- Indicates whether backups should run as super user. Typicallytrue
. -
SSL_MODE
- Indicates whether to use TLS on the connection to the external PostgreSQL database. Check the possible values
When SSL_MODE
is enabled, the operator searches for a secret called keycloak-db-ssl-cert-secret
containing the root.crt
that has been used by the PostgreSQL database. Creating the secret is optional and the secret is used only when you want to verify the Database’s certificate (for example SSLMODE: verify-ca
). Here is an example :
Example YAML file for TLS Secret
to be used by the operator.
The Operator will create a Service named keycloak-postgresql
. This Service is configured by the Operator to expose the external database based on the content of POSTGRES_EXTERNAL_ADDRESS
. Red Hat Single Sign-On uses this Service to connect to the Database, which means it does not connect to the Database directly but rather through this Service.
The Operator will create a Service named keycloak-postgresql
. This Service is configured by the Operator to expose the external database based on the content of POSTGRES_EXTERNAL_ADDRESS
. Red Hat Single Sign-On uses this Service to connect to the Database, which means it does not connect to the Database directly but rather through this Service.
The Keycloak custom resource requires updates to enable external database support.
Example YAML file for Keycloak
custom resource that supports an external database
Prerequisites
-
You have a YAML file for
keycloak-db-secret
. -
You have modified the Keycloak custom resource to set
externalDatabase
totrue
. - You have cluster-admin permission or an equivalent level of permissions granted by an administrator.
Procedure
Locate the secret for your PostgreSQL database:
oc get secret <secret_for_db> -o yaml
. For example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
POSTGRES_EXTERNAL_ADDRESS
is in Base64 format.Decode the value for the secret:
echo "<encoded_secret>" | base64 -decode
. For example:echo "MTcyLjE3LjAuMw==" | base64 -decode
$ echo "MTcyLjE3LjAuMw==" | base64 -decode 192.0.2.3
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Confirm that the decoded value matches the IP address for your database:
oc get pods -o wide
$ oc get pods -o wide NAME READY STATUS RESTARTS AGE IP keycloak-0 1/1 Running 0 13m 192.0.2.0 keycloak-postgresql-c8vv27m 1/1 Running 0 24m 192.0.2.3
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Confirm that
keycloak-postgresql
appears in a list of running services:oc get svc
$ oc get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE keycloak ClusterIP 203.0.113.0 <none> 8443/TCP 27m keycloak-discovery ClusterIP None <none> 8080/TCP 27m keycloak-postgresql ClusterIP 203.0.113.1 <none> 5432/TCP 27m
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
keycloak-postgresql
service sends requests to a set of IP addresses in the backend. These IP addresses are called endpoints.View the endpoints used by the
keycloak-postgresql
service to confirm that they use the IP addresses for your database:oc get endpoints keycloak-postgresql
$ oc get endpoints keycloak-postgresql NAME ENDPOINTS AGE keycloak-postgresql 192.0.2.3.5432 27m
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Confirm that Red Hat Single Sign-On is running with the external database. This example shows that everything is running:
oc get pods
$ oc get pods NAME READY STATUS RESTARTS AGE IP keycloak-0 1/1 Running 0 26m 192.0.2.0 keycloak-postgresql-c8vv27m 1/1 Running 0 36m 192.0.2.3
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.9. Scheduling database backups Copiar enlaceEnlace copiado en el portapapeles!
Backup CR is deprecated and could be removed in future releases.
You can use the Operator to schedule automatic backups of the database as defined by custom resources. The custom resource triggers a backup job and reports back its status.
You can use Operator to create a backup job that performs a one-time backup to a local Persistent Volume.
Example YAML file for a Backup custom resource
apiVersion: keycloak.org/v1alpha1 kind: KeycloakBackup metadata: name: test-backup
apiVersion: keycloak.org/v1alpha1
kind: KeycloakBackup
metadata:
name: test-backup
Prerequisites
- You have a YAML file for this custom resource.
-
You have a
PersistentVolume
with aclaimRef
to reserve it only for aPersistentVolumeClaim
created by the Red Hat Single Sign-On Operator.
Procedure
Create a backup job:
oc create -f <backup_crname>
. For example:oc create -f one-time-backup.yaml
$ oc create -f one-time-backup.yaml keycloak.keycloak.org/test-backup
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The Operator creates a
PersistentVolumeClaim
with the following naming scheme:Keycloak-backup-<CR-name>
.View a list of volumes:
oc get pvc
$ oc get pvc NAME STATUS VOLUME keycloak-backup-test-backup Bound pvc-e242-ew022d5-093q-3134n-41-adff keycloak-postresql-claim Bound pvc-e242-vs29202-9bcd7-093q-31-zadj
Copy to Clipboard Copied! Toggle word wrap Toggle overflow View a list of backup jobs:
oc get jobs
$ oc get jobs NAME COMPLETIONS DURATION AGE test-backup 0/1 6s 6s
Copy to Clipboard Copied! Toggle word wrap Toggle overflow View the list of executed backup jobs:
oc get pods
$ oc get pods NAME READY STATUS RESTARTS AGE test-backup-5b4rf 0/1 Completed 0 24s keycloak-0 1/1 Running 0 52m keycloak-postgresql-c824c6-vv27m 1/1 Running 0 71m
Copy to Clipboard Copied! Toggle word wrap Toggle overflow View the log of your completed backup job:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Additional resources
- For more details on persistent volumes, see Understanding persistent storage.
11.10. Installing extensions and themes Copiar enlaceEnlace copiado en el portapapeles!
You can use the operator to install extensions and themes that you need for your company or organization. The extension or theme can be anything that Red Hat Single Sign-On can consume. For example, you can add a metrics extension. You add the extension or theme to the Keycloak custom resource.
Example YAML file for a Keycloak custom resource
You can package and deploy themes in the same way as any other extensions. See Deploying Themes manual entry for more information.
Prerequisites
- You have a YAML file for the Keycloak custom resource.
- You have cluster-admin permission or an equivalent level of permissions granted by an administrator.
Procedure
-
Edit the YAML file for the Keycloak custom resource:
oc edit <CR-name>
-
Add a line called
extensions:
after theinstances
line. - Add a URL to a JAR file for your custom extension or theme.
- Save the file.
The Operator downloads the extension or theme and installs it.
11.11. Command options for managing custom resources Copiar enlaceEnlace copiado en el portapapeles!
After you create a custom request, you can edit it or delete using the oc
command.
-
To edit a custom request, use this command:
oc edit <CR-name>
-
To delete a custom request, use this command:
oc delete <CR-name>
For example, to edit a realm custom request named test-realm
, use this command:
oc edit test-realm
$ oc edit test-realm
A window opens where you can make changes.
You can update the YAML file and changes appear in the Red Hat Single Sign-On admin console, however changes to the admin console do not update the custom resource.
11.12. Upgrade strategy Copiar enlaceEnlace copiado en el portapapeles!
You can configure how the operator performs Red Hat Single Sign-On upgrades. You can choose from the following upgrade strategies.
-
recreate
: This is the default strategy. The operator removes all Red Hat Single Sign-On replicas, optionally creates a backup and then creates the replicas based on a newer Red Hat Single Sign-On image. This strategy is suitable for major upgrades as a single Red Hat Single Sign-On version is accessing the underlying database. The downside is Red Hat Single Sign-On needs to be shut down during the upgrade. -
rolling
: The operator removes one replica at a time and creates it again based on a newer Red Hat Single Sign-On image. This ensures a zero-downtime upgrade but is more suitable for minor version upgrades that do not require database migration since the database is accessed by multiple Red Hat Single Sign-On versions concurrently. Automatic backups are not supported with this strategy.
Example YAML file for a Keycloak custom resource