Bookmark this page

Guided Exercise: Manage Container Storage and Network Resources

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

  1. Log in to the servera machine as the student user.

    [student@workstation ~]$ ssh student@servera
    ...output omitted...
    [student@servera ~]$
  2. Create the frontend container network. Create the db_client and db_01 containers and connect them to the frontend network.

    1. Use the podman network create command --subnet and --gateway options to create the frontend network with the 10.89.1.0/24 subnet and the 10.89.1.1 gateway.

      [student@servera ~]$ podman network create --subnet 10.89.1.0/24 \
      --gateway 10.89.1.1 frontend
      frontend
    2. Log in to the registry.lab.example.com registry.

      [student@servera ~]$ podman login registry.lab.example.com
      Username: admin
      Password: redhat321
      Login Succeeded!
    3. Start a container named db_client in the background, and connect it to the frontend network. To be able to install packages in the db_client container, mount the /etc/yum.repos.d DNF repositories directory at the /etc/yum.repos.d container path. Run the sleep infinity command in the db_client container to prevent the container from exiting. Use the registry.lab.example.com/ubi9-beta/ubi image.

      [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 infinity
      e20dfed7e392abe4b7bea3c25e9cb17ef95d16af9cedd50d68f997a663ba6c15
    4. Start in the background a container named db_01 that is connected to the frontend network. Use the registry.lab.example.com/rhel8/mariadb-105 image.

      [student@servera ~]$ podman run -d --name db_01 --network frontend \
      registry.lab.example.com/rhel8/mariadb-105
      3e767ae6eea4578152a216beb5ae98c8ef03a2d66098debe2736b8b458bab405
    5. View all containers.

      [student@servera ~]$ podman ps -a
      CONTAINER 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
  3. Troubleshoot the db_01 container and determine why it is not running. Re-create the db_01 container by using the required environment variables.

    1. 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...
    2. Remove the db_01 container and create it again with environment variables. Provide the required environment variables.

      [student@servera ~]$ podman rm db_01
      3e767ae6eea4578152a216beb5ae98c8ef03a2d66098debe2736b8b458bab405
      [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-105
      948c4cd767b561432056e77adb261ab4024c1b66a22af17861aba0f16c66273b
    3. View the current running containers.

      [student@servera ~]$ podman ps
      CONTAINER 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
  4. 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 servera machine.

    1. Create the /home/student/databases directory on the servera machine.

      [student@servera ~]$ mkdir /home/student/databases
    2. Obtain the mysql UID and GID from the db_01 container, and then remove the db01 container.

      [student@servera ~]$ podman exec -it db_01 grep mysql /etc/passwd
      mysql:x:27:27:MySQL Server:/var/lib/mysql:/sbin/nologin
      [student@servera ~]$ podman stop db_01
      db_01
      [student@servera ~]$ podman rm db_01
      948c4cd767b561432056e77adb261ab4024c1b66a22af17861aba0f16c66273b
    3. Run the chown command inside the container namespace, and set the user and group owner to 27 on the /home/student/database directory.

      [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 databases
    4. Create the db_01 container, and mount the /home/student/databases directory from the servera machine to the /var/lib/mysql directory inside the db_01 container. Use the Z option 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-105
    5. Install the mariadb package in the db_client container.

      [student@servera ~]$ podman exec -it db_client dnf install -y mariadb
      ...output omitted...
      Complete!
    6. Create the crucial_data table in the dev_db database in the db_01 container from the db_client container.

      [student@servera ~]$ podman exec -it db_client mysql -u dev1 -p -h db_01
      Enter 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]> quit
      Bye
    7. Allow port 13306 traffic in the firewall on the servera machine.

      [student@servera ~]$ sudo firewall-cmd --add-port=13306/tcp --permanent
      [sudo] password for student: student
      success
      [student@servera ~]$ sudo firewall-cmd --reload
      success
    8. Open a second terminal on the workstation machine and use the MariaDB client to connect to the servera machine on port 13306, to show tables inside the db_01 container 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    |
      +-----------------+
  5. Create a second container network called backend, and connect the backend network to the db_client and db_01 containers. Test network connectivity and DNS resolution between the containers.

    1. Create the backend network with the 10.90.0.0/24 subnet and the 10.90.0.1 gateway.

      [student@servera ~]$ podman network create --subnet 10.90.0.0/24 \
      --gateway 10.90.0.1 backend
      backend
    2. Connect the backend container network to the db_client and db_01 containers.

      [student@servera ~]$ podman network connect backend db_client
      [student@servera ~]$ podman network connect backend db_01
    3. Obtain the IP addresses of the db_01 container.

      [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...
    4. Install the iputils package in the db_client container.

      [student@servera ~]$ podman exec -it db_client dnf install -y iputils
      ...output omitted...
      Complete!
    5. Ping the db_01 container name from the db_client container.

      [student@servera ~]$ podman exec -it db_client ping -c4 db_01
      PING 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 ms
    6. Exit the servera machine.

      [student@servera ~]$ exit
      logout
      Connection to servera closed.
      [student@workstation ~]$

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish containers-resources

This concludes the section.

Revision: rh199-9.0-4fecb06