RHCSA Rapid Track
Course update
An updated version of this course is available that uses a newer version of Red Hat Enterprise Linux in the lab environment. Therefore, the RHEL 9.0 version of the lab environment will retire on December 31, 2024. Please complete any work in this lab environment before it is removed on December 31, 2024. For the most up-to-date version of this course, we recommend moving to the RHEL 9.3 version.
In this lab, you configure on your server a container that provides a MariaDB database service, stores its database on persistent storage, and starts automatically with the server.
Outcomes
Create detached containers.
Configure port redirection and persistent storage.
Configure
systemdfor containers to start when the host machine starts.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares your environment and ensures that all required resources are available.
[student@workstation ~]$ lab start containers-review
Instructions
On
serverb, install the container tools package.Log in to
serverbas thestudentuser.[student@workstation ~]$
ssh student@serverb...output omitted... [student@serverb ~]$Install the
container-toolspackage.[student@serverb ~]$
sudo dnf install container-tools[sudo] password for student:student...output omitted... Is this ok [y/N]:y...output omitted... Complete!
The container image registry at
registry.lab.example.comstores therhel8/mariadb-103image with several tags. Use thepodsvcuser to list the available tags and note the tag with the lowest version number. Use theadminuser andredhat321password to authenticate to the registry. Use the/tmp/registries.conffile as a template for the registry configuration.Return to the
workstationmachine as thestudentuser.[student@serverb ~]$
exitlogout Connection to serverb closed. [student@workstation ~]$Log in to
serverbas thepodsvcuser.[student@workstation ~]$
ssh podsvc@serverb...output omitted... [podsvc@serverb ~]$Configure access to the
registry.lab.example.comclassroom registry in your home directory. Use the/tmp/registries.conffile as a template.[podsvc@serverb ~]$
mkdir -p ~/.config/containers/[podsvc@serverb ~]$cp /tmp/registries.conf \ ~/.config/containers/Log in to the container registry with the
podman logincommand.[podsvc@serverb ~]$
podman login registry.lab.example.comUsername:adminPassword:redhat321Login Succeeded!Note
The repository that contains the
mariadbcontainer image is not a public repository, and so thepodman search mariadbcommand returns no results. Review the note in thepodman-search(1) man page about the unreliability of usingpodman-searchto determine the existence of an image.View information about the
registry.lab.example.com/rhel8/mariadb-103image.[podsvc@serverb ~]$
skopeo inspect \docker://registry.lab.example.com/rhel8/mariadb-103{ "Name": "registry.lab.example.com/rhel8/mariadb-103", "Digest": "sha256:a95b...4816", "RepoTags": ["1-86", "1-102", "latest" ], ...output omitted...The lowest version tag is the
1-86version.
Create the
/home/podsvc/db_datadirectory, and configure the directory so that containers have read/write access. Then, create theinventorydbdetached container. Use therhel8/mariadb-103image from theregistry.lab.example.comregistry, and specify the tag with the lowest version number on that image, which you found in a preceding step. Map port 3306 in the container to port 13306 on the host. Mount the/home/podsvc/db_datadirectory on the host as/var/lib/mysql/datain the container. Declare the following variable values for the container:Variable Value MYSQL_USERoperator1MYSQL_PASSWORDredhatMYSQL_DATABASEinventoryMYSQL_ROOT_PASSWORDredhatYou can copy and paste these parameters from the
/home/podsvc/containers-review/variablesfile onserverb. Execute the/home/podsvc/containers-review/testdb.shscript to confirm that the MariaDB database is running.Start the
db_01detached container to obtain themysqlUID and GID.[podsvc@serverb ~]$
podman run -d --name db_01 -p 13306:3306 \-e MYSQL_USER=operator1 \-e MYSQL_PASSWORD=redhat \-e MYSQL_DATABASE=inventory \-e MYSQL_ROOT_PASSWORD=redhat \registry.lab.example.com/rhel8/mariadb-103:1-86...output omitted... c33f85d177dc8c51a303e231e6be63c1f251b9d426b4ccb56498603ab72d4219Create the
/home/podsvc/db_datadirectory.[podsvc@serverb ~]$
mkdir /home/podsvc/db_dataObtain the
mysqlUID and GID from thedb_01container, and then remove thedb01container.[podsvc@serverb ~]$
podman exec -it db_01 grep mysql /etc/passwdmysql:x:27:27:MySQL Server:/var/lib/mysql:/sbin/nologin [podsvc@serverb ~]$podman stop db_01db_01 [podsvc@serverb ~]$podman rm db_01c33f85d177dc8c51a303e231e6be63c1f251b9d426b4ccb56498603ab72d4219Use the
podman unsharecommand to set the user namespace UID and GID of27as the owner of the directory.[podsvc@serverb ~]$
podman unshare chown 27:27 /home/podsvc/db_dataCreate the container.
[podsvc@serverb ~]$
podman run -d --name inventorydb -p 13306:3306 \-e MYSQL_USER=operator1 \-e MYSQL_PASSWORD=redhat \-e MYSQL_DATABASE=inventory \-e MYSQL_ROOT_PASSWORD=redhat \-v /home/podsvc/db_data:/var/lib/mysql/data:Z \registry.lab.example.com/rhel8/mariadb-103:1-86...output omitted...Confirm that the database is running.
[podsvc@serverb ~]$
~/containers-review/testdb.shTesting the access to the database...SUCCESS
Configure the
systemddaemon so that theinventorydbcontainer starts automatically when the system boots.If you used
sudoorsuto log in as thepodsvcuser, then exitserverband use thesshcommand to log in directly toserverbas thepodsvcuser. Remember, thesystemddaemon requires the user to open a direct session from the console or through SSH. Omit this step if you already logged in to theserverbmachine as thepodsvcuser by using SSH.[student@workstation ~]$
ssh podsvc@serverb...output omitted... [podsvc@serverb ~]$Create the
~/.config/systemd/user/directory.[podsvc@serverb ~]$
mkdir -p ~/.config/systemd/user/Create the
systemdunit file from the running container.[podsvc@serverb ~]$
cd ~/.config/systemd/user/[podsvc@serverb user]$podman generate systemd --name inventorydb --files --new/home/podsvc/.config/systemd/user/container-inventorydb.serviceStop and then delete the
inventorydbcontainer.[podsvc@serverb user]$
podman stop inventorydbinventorydb [podsvc@serverb user]$podman rm inventorydb0d28f0e0a4118ff019691e34afe09b4d28ee526079b58d19f03b324bd04fd545Instruct the
systemddaemon to reload its configuration, and then enable and start thecontainer-inventorydbservice.[podsvc@serverb user]$
systemctl --user daemon-reload[podsvc@serverb user]$systemctl --user enable --now container-inventorydb.serviceCreated symlink /home/podsvc/.config/systemd/user/default.target.wants/container-inventorydb.service → /home/podsvc/.config/systemd/user/container-inventorydb.service.Confirm that the container is running.
[podsvc@serverb user]$
~/containers-review/testdb.shTesting the access to the database... SUCCESS [podsvc@serverb user]$podman psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3ab24e7f000d registry.lab.example.com/rhel8/mariadb-103:1-86 run-mysqld 47 seconds ago Up 46 seconds ago 0.0.0.0:13306->3306/tcp inventorydbRun the
loginctl enable-lingercommand for the user services to start automatically when the server starts.[podsvc@serverb ~]$
loginctl enable-lingerReturn to the
workstationmachine as thestudentuser.[podsvc@serverb ~]$
exitlogout Connection to serverb closed. [student@workstation ~]$
This concludes the section.