This space is dedicated to Keycloak, which is an open source OpenID Connect provider. Here you will find extensive information on self hosting Keycloak on the Edge.
This guide walks you step by step until you get Keycloak published as a service on the Edge that can be accessed on the Internet.
Make sure that all of the mandatory prerequisites have been met before progressing further.
| Name | Source | Description |
|---|---|---|
| <CONTAINER_NAME> | User input | The name of the Linux container where the app runs. |
| <DB_PASSWORD> | User input | The password for logging in with the keycloak PostgreSQL user. |
| <KEYCLOAK_ADMIN_PASSWORD> | User input | The initial password of the Keycloak user admin that is created when the Keycloak server boots up for the first time. |
| <ROOT_PWD__CONTAINER> | User input | The password of the root user in the container. |
| <IP_ADDRESS__CONTAINER> | Generated | The IPv4 address of the contaier run by the server on the Edge. |
| <IP_ADDRESS_INET_GW> | Linux VPS Admin Panel | |
| <KEYCLOAK_DOMAIN> | User input | The host for the A record created with your domain registrar, e.g. auth.myowndomain.net. |
Run the following commands on your server on the Edge.
incus launch images:debian/bookworm/cloud <CONTAINER_NAME>
incus exec <CONTAINER_NAME> bash
You may need to change the default MTU for HTTPS connections to function. Run
ip link set dev eth0 mtu 1000if you findapt updateorapt upgradestuck for long time. This setting is non persistent and it is lost when the container reboots.
apt update && apt upgrade && apt install fish curl wget
If you are not already within the container, run the following command on your server on the Edge to log in as
root.
incus exec <CONTAINER_NAME> fish
passwd
Set a password for the root user and make a (mental) note of it. It will be denoted with a variable <ROOT_PWD__CONTAINER>.
From here on the root user will use fish as default shell.
chsh -s /usr/bin/fish
Let's create the keycloak user and set a system password for it.
useradd -m -d /var/www/keycloak -s /usr/bin/fish -p keycloak keycloak
passwd keycloak
The ability to establish SSH connections from the server on the Edge to the container is key for the upcoming Ansible automation. There is an Ansible connection driver for Linux containers with limited capabilities. The default Ansible connection driver through SSH is much more robust and powerful.
You can perform this step now or in future, when you make up your mind to give the automated solutions for system and application maintenance a try.
In order to complete this stage you need to perform the following commands in the container run by the server on The Edge.
apt update && apt install openssh-server
vi /etc/ssh/sshd_config
Add the following line to the file.
PermitRootLogin yes
systemctl enable ssh
systemctl start ssh
systemctl restart ssh
Run the following commands on your server on the Edge, i.e. outside of the container.
Run ssh-keygen only if you haven't generated SSH private and public keys on the server on the Edge. Otherwise use the already existing pair.
ssh-keygen
incus ls | grep <CONTAINER_NAME>
Make a note of the IPV4 value <IP_ADDRESS__CONTAINER>.
ssh-copy-id root@<IP_ADDRESS__CONTAINER>
Pass the password for the root user in the container as prompted.
From here on SSH connection as the
rootuser will be possible from the server on the Edge to the container. Containers are not visible and accessible to any other network devices.
Run the following commands on your server on the Edge.
incus exec <CONTAINER_NAME> fish
Let's harden the setup of the ssh server running in the container by disabling the password for logging in with root.
vi /etc/ssh/sshd_config
Delete the line PermitRootLogin yes.
Append the line PermitRootLogin prohibit-password.
systemctl restart ssh
You should have a fully functional Linux container named
<CONTAINER_NAME>that has also a non-administrative userkeycloak. In addition, the server on the Edge listens for SSH connections with therootuser. You know all login credentials.
In order to complete this stage you need to perform the following commands in the container run by the server on the Edge.
If you are not already within the container, run the following command on your server on the Edge to log in as
root.
incus exec <CONTAINER_NAME> fish
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
apt update && apt install postgresql-15
In order to enable data checksums we need to delete the existing cluster and initialize it again.
rm -rf /var/lib/postgresql/15/main
mkdir /var/lib/postgresql/15/main
chown -R postgres:postgres /var/lib/postgresql/15/main
chmod 700 /var/lib/postgresql/15/main
sudo -u postgres /usr/lib/postgresql/15/bin/initdb \
-D /var/lib/postgresql/15/main/ \
--locale=C.UTF-8 --encoding=UTF8 --data-checksums
systemctl enable postgresql@15-main.service
systemctl start postgresql@15-main.service
Verify that the stage has been completed successfully by running the following command in the container. Its output should indicate that the service is running.
systemctl status postgresql@15-main.service
In order to complete this stage you need to perform the following commands in the container run by the server on the Edge.
If you are not already within the container, run the following command on your server on the Edge to log in as
root.
incus exec <CONTAINER_NAME> fish
Take a not of the password for the database user. It will be denoted with a variable <DB_PASSWORD>.
sudo -u postgres createuser keycloak --interactive --pwprompt
Take a note of the password <DB_PASSWORD>.
sudo -u postgres createdb keycloak -O keycloak --encoding='utf-8'
In order to complete this stage you need to perform the following commands in the container run by the server on the Edge.
If you are not already within the container, run the following command on your server on the Edge to log in as
root.
incus exec <CONTAINER_NAME> fish
sudo su - keycloak
cd ~
mkdir tls
openssl req -x509 -nodes -newkey rsa:4096 -days 365 \
-keyout tls/key.pem \
-out tls/cert.pem
If you are not already within the container, run the following command on your server on the Edge to log in as
root.
incus exec <CONTAINER_NAME> fish
apt update && apt install openjdk-17-jdk
sudo su - keycloak
cd ~
mkdir -p versions config
Download the latest version of the Keycloak server.
cd versions
Do not forget to substitute the version of the Keycloak below with the one matching your downloaded archive.
wget https://github.com/keycloak/keycloak/releases/download/23.0.3/keycloak-23.0.3.tar.gz
tar -xvf keycloak-23.0.3.tar.gz
rm keycloak-23.0.3.tar.gz
cd ~
ln -s versions/keycloak-23.0.3 current
If you are not already within the container, run the following command on your server on the Edge to log in as
root.
incus exec <CONTAINER_NAME> fish
sudo -u keycloak
cd ~
cp current/conf/keycloak.conf config/
Edit the config/keycloak.conf file.
vi config/keycloak.conf
Set the respective key-value pairs as specified below
Make sure to substitute the variables enclosed in <...> with the real values matching your setup.
db=postgres
db-username=keycloak
db-password=<DB_PASSWORD>
https-certificate-file=/var/www/keycloak/tls/cert.pem
https-certificate-key-file=/var/www/keycloak/tls/key.pem
proxy=reencrypt
hostname=<KEYCLOAK_DOMAIN>
If you are not already within the container, run the following command on your server on the Edge to log in as
root.
incus exec <CONTAINER_NAME> fish
vi /etc/systemd/system/keycloak.service
Make sure the file contains the systemd service definition below.
Make sure to substitute the variables enclosed in <...> with the real values matching your setup.
[Unit]
Description=The Keycloak Server
After=syslog.target network.target
Before=httpd.service
[Service]
Environment=LAUNCH_JBOSS_IN_BACKGROUND=1
Environment="KEYCLOAK_ADMIN=admin"
Environment="KEYCLOAK_ADMIN_PASSWORD=<KEYCLOAK_ADMIN_PASSWORD>"
User=keycloak
Group=keycloak
LimitNOFILE=102642
PIDFile=/var/run/keycloak/keycloak.pid
ExecStart=/var/www/keycloak/current/bin/kc.sh -cf /var/www/keycloak/config/keycloak.conf start
[Install]
WantedBy=multi-user.target
When the Keycloak server is started for the first time, it creates an administrative user
adminwith a password <KEYCLOAK_ADMIN_PASSWORD>. You can use it later to log in with the Keycloak Administration Console. One could also reset the password of theadminuser.
systemctl enable keycloak.service
systemctl start keycloak.service
systemctl status keycloak.service
You should be able to run a successful HTTP request against the Keycloak server running within the container.
curl -k https://localhost:8443
In a terminal on on the server on the Edge run the following command to forward incoming HTTPS connections on port 16443 of the server to port 8443 on the container.
incus config device add <CONTAINER_NAME> \
https-16443-8443 proxy listen=tcp:0.0.0.0:16443 \
connect=tcp:127.0.0.1:8443
From here on the Keycloak server instance should be accessible from the Server on the Edge and any other device in the same network.
curl -k https://localhost:16443
Make sure that you have created an A domain record for <KEYCLOACK_DOMAIN> with your domain registrar and you have waited for this record to become active.
If you use a domain name and that's not picked up by the domain name servers yet, Caddy will not be able to serve HTTPS requests because of Let's Encrypt (An SSL certificate authority) failing to verify the validity (ownership over) of the domain.
ssh mycelium@<IP_ADDRESS__INET_GW>
sudo su -
Add the following to /etc/caddy/Caddyfile.
Do not forget to substitute the variables enclosed in
<...>with the values matching your setup.
<KEYCLOAK_DOMAIN> {
reverse_proxy https://<IP_ADDRESS__EDGE_SERVER_VPN>:16443 {
header_up Host {host}
header_up X-Real-IP {remote}
header_up X-Forwarded-For {host}
header_up X-Forwarded-Host {host}
header_up X-Forwarded-Server {host}
header_up X-Forwarded-Proto {scheme}
transport http {
tls_insecure_skip_verify
}
}
}
systemctl reload caddy
If everything is configured well, you should be able to access the Keycloak server through its domain name. In a web browser navigate to
https://<KEYCLOAK_DOMAIN>.
You should be able to log in with the Keycloak administration console and proceed with administering Keycloak.