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 exercise, you pass environment variables to a container during creation, mount persistent storage to a container, create and connect multiple container networks, and expose container ports from the host machine.
Outcomes
Create container networks and connect them to containers.
Troubleshoot failed containers.
Pass environment variables to containers during creation.
Create and mount persistent storage to containers.
Map host ports to ports inside containers.
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-resources
Instructions
Log in to the
serveramachine as thestudentuser.[student@workstation ~]$
ssh student@servera...output omitted... [student@servera ~]$Create the
frontendcontainer network. Create thedb_clientanddb_01containers and connect them to thefrontendnetwork.Use the
podman network createcommand--subnetand--gatewayoptions to create thefrontendnetwork with the10.89.1.0/24subnet and the10.89.1.1gateway.[student@servera ~]$
podman network create --subnet 10.89.1.0/24 \--gateway 10.89.1.1 frontendfrontendLog in to the
registry.lab.example.comregistry.[student@servera ~]$
podman login registry.lab.example.comUsername:adminPassword:redhat321Login Succeeded!Start a container named
db_clientin the background, and connect it to thefrontendnetwork. To be able to install packages in thedb_clientcontainer, mount the/etc/yum.repos.dDNF repositories directory at the/etc/yum.repos.dcontainer path. Run thesleep infinitycommand in thedb_clientcontainer to prevent the container from exiting. Use theregistry.lab.example.com/ubi9-beta/ubiimage.[student@servera ~]$
podman run -d --name db_client \--network frontend \-v /etc/yum.repos.d:/etc/yum.repos.d \registry.lab.example.com/ubi9-beta/ubi \sleep infinitye20dfed7e392abe4b7bea3c25e9cb17ef95d16af9cedd50d68f997a663ba6c15Start in the background a container named
db_01that is connected to thefrontendnetwork. Use theregistry.lab.example.com/rhel8/mariadb-105image.[student@servera ~]$
podman run -d --name db_01 --network frontend \registry.lab.example.com/rhel8/mariadb-1053e767ae6eea4578152a216beb5ae98c8ef03a2d66098debe2736b8b458bab405View all containers.
[student@servera ~]$
podman ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e20dfed7e392 registry.lab.example.com/ubi8/ubi:latest sleep infinity 56 seconds ago Up 56 seconds ago db_client 3e767ae6eea4 registry.lab.example.com/rhel8/mariadb-105:latest run-mysqld 1 second ago Exited (1) 1 second ago db_01
Troubleshoot the
db_01container and determine why it is not running. Re-create thedb_01container by using the required environment variables.View the container logs and determine why the container exited.
[student@servera ~]$
podman container logs db_01...output omitted... You must either specify the following environment variables: MYSQL_USER (regex: '^[a-zA-Z0-9_]+$') MYSQL_PASSWORD (regex: '^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:|]+$') MYSQL_DATABASE (regex: '^[a-zA-Z0-9_]+$') Or the following environment variable: MYSQL_ROOT_PASSWORD (regex: '^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:|]+$') Or both. ...output omitted...Remove the
db_01container and create it again with environment variables. Provide the required environment variables.[student@servera ~]$
podman rm db_013e767ae6eea4578152a216beb5ae98c8ef03a2d66098debe2736b8b458bab405 [student@servera ~]$podman run -d --name db_01 \--network frontend \-e MYSQL_USER=dev1 \-e MYSQL_PASSWORD=devpass \-e MYSQL_DATABASE=devdb \-e MYSQL_ROOT_PASSWORD=redhat \registry.lab.example.com/rhel8/mariadb-105948c4cd767b561432056e77adb261ab4024c1b66a22af17861aba0f16c66273bView the current running containers.
[student@servera ~]$
podman psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e20dfed7e392 registry.lab.example.com/ubi8/ubi:latest sleep infinity 56 seconds ago Up 56 seconds ago db_client 948c4cd767b5 registry.lab.example.com/rhel8/mariadb-105:latest run-mysqld 11 seconds ago Up 12 seconds ago db_01
Create persistent storage for the containerized MariaDB service, and map the local machine 13306 port to the 3306 port in the container. Allow traffic to the 13306 port on the
serveramachine.Create the
/home/student/databasesdirectory on theserveramachine.[student@servera ~]$
mkdir /home/student/databasesObtain the
mysqlUID and GID from thedb_01container, and then remove thedb01container.[student@servera ~]$
podman exec -it db_01 grep mysql /etc/passwdmysql:x:27:27:MySQL Server:/var/lib/mysql:/sbin/nologin [student@servera ~]$podman stop db_01db_01 [student@servera ~]$podman rm db_01948c4cd767b561432056e77adb261ab4024c1b66a22af17861aba0f16c66273bRun the
chowncommand inside the container namespace, and set the user and group owner to27on the/home/student/databasedirectory.[student@servera ~]$
podman unshare chown 27:27 /home/student/databases/[student@servera ~]$ls -l /home/student/total 0 drwxr-xr-x. 2 100026 100026 6 May 9 17:40 databasesCreate the
db_01container, and mount the/home/student/databasesdirectory from theserveramachine to the/var/lib/mysqldirectory inside thedb_01container. Use theZoption to apply the required SELinux context.[student@servera ~]$
podman run -d --name db_01 \--network frontend \-e MYSQL_USER=dev1 \-e MYSQL_PASSWORD=devpass \-e MYSQL_DATABASE=devdb \-e MYSQL_ROOT_PASSWORD=redhat \-v /home/student/databases:/var/lib/mysql:Z \-p 13306:3306 \registry.lab.example.com/rhel8/mariadb-105Install the
mariadbpackage in thedb_clientcontainer.[student@servera ~]$
podman exec -it db_client dnf install -y mariadb...output omitted... Complete!Create the
crucial_datatable in thedev_dbdatabase in thedb_01container from thedb_clientcontainer.[student@servera ~]$
podman exec -it db_client mysql -u dev1 -p -h db_01Enter password:devpass...output omitted... MariaDB [(none)]>USE devdb;Database changed MariaDB [devdb]>CREATE TABLE crucial_data(column1 int);Query OK, 0 rows affected (0.036 sec) MariaDB [devdb]>SHOW TABLES;+-----------------+ | Tables_in_devdb | +-----------------+ | crucial_data | +-----------------+ 1 row in set (0.001 sec) MariaDB [devdb]>quitByeAllow port 13306 traffic in the firewall on the
serveramachine.[student@servera ~]$
sudo firewall-cmd --add-port=13306/tcp --permanent[sudo] password for student:studentsuccess [student@servera ~]$sudo firewall-cmd --reloadsuccessOpen a second terminal on the
workstationmachine and use the MariaDB client to connect to theserveramachine on port13306, to show tables inside thedb_01container that are stored in the persistent storage.[student@workstation ~]$
mysql -u dev1 -p -h servera --port 13306 \devdb -e 'SHOW TABLES';Enter password:devpass+-----------------+ | Tables_in_devdb | +-----------------+ | crucial_data | +-----------------+
Create a second container network called
backend, and connect thebackendnetwork to thedb_clientanddb_01containers. Test network connectivity and DNS resolution between the containers.Create the
backendnetwork with the10.90.0.0/24subnet and the10.90.0.1gateway.[student@servera ~]$
podman network create --subnet 10.90.0.0/24 \--gateway 10.90.0.1 backendbackendConnect the
backendcontainer network to thedb_clientanddb_01containers.[student@servera ~]$
podman network connect backend db_client[student@servera ~]$podman network connect backend db_01Obtain the IP addresses of the
db_01container.[student@servera ~]$
podman inspect db_01...output omitted... "Networks": { "backend": { "EndpointID": "", "Gateway": "10.90.0.1", "IPAddress": "10.90.0.3", ...output omitted... "frontend": { "EndpointID": "", "Gateway": "10.89.1.1", "IPAddress": "10.89.1.5", ...output omitted...Install the
iputilspackage in thedb_clientcontainer.[student@servera ~]$
podman exec -it db_client dnf install -y iputils...output omitted... Complete!Ping the
db_01container name from thedb_clientcontainer.[student@servera ~]$
podman exec -it db_client ping -c4 db_01PING db_01.dns.podman (10.90.0.3) 56(84) bytes of data. ...output omitted... --- db_01.dns.podman ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3048ms rtt min/avg/max/mdev = 0.043/0.049/0.054/0.004 msExit the
serveramachine.[student@servera ~]$
exitlogout Connection to servera closed. [student@workstation ~]$
This concludes the section.