OpenShift Hands-On Lab - Creating a Persistent Container Volume using Podman

OpenShift Hands-On Lab - Creating a Persistent Container Volume using Podman

OpenShift Learning Path for Cloud and DevOps Engineers

📝Introduction

In this lab, we will be able to attach persistent storage to set up 2 MySQL container instances.

These are the objectives of this lab:

  • Pull the Requested Container Image

  • Start the First MySQL Container

  • Start the Second MySQL Container

  • Verify the Containers and MySQL Are Running

📝Log in to the AWS Management Console

Using your credentials, make sure you're using the right Region. In my case, I am using AWS as my cloud provider and chose us-east-1. However, for this hands-on lab, you can select any cloud provider to create your Linux server VMs (I am using a Red Hat Enterprise Linux 8.7 distro) and Podman 3.0.1 (Link to how to install Podman)

📌Note: You must create the AWS Access Key and AWS Secret Access Key and configure the AWS CLI in the terminal to use it.

You can use link1 and link2 for it.

📝Pull the Requested Container Image

  1. Pull the docker.io/library/mysql container image from the repository:

     podman pull docker.io/library/mysql
    
  2. Verify the container image is on the system:

     podman images
    
  3. Create the db_dir directory:

     mkdir db_demo
    
  4. Verify the directory was created:

     ls -ld db_demo
    

📝Start the First MySQL Container

  1. Create the first MySQL container using the pulled image, expose the container port 3306 to localhost port 33061, and set MYSQL_ROOT_PASSWORD to demo123:

     podman run -d --name mysql-1 -v ~/db_demo:/var/lib/mysql:Z -e MYSQL_ROOT_PASSWORD=demo123 -p 33061:3306 mysql
    

    📌Note: If you use SELinux you can add the z or Z options to modify the SELinux label of the host file or directory being mounted into the container. This affects the file or directory on the host machine itself and can have consequences outside of the scope of Docker/Podman.

    • The z option indicates that the bind mount content is shared among multiple containers.

    • The Z option indicates that the bind mount content is private and unshared.

  2. Verify the container is running:

     podman ps
    
  3. Verify the database instance is running inside the container:

     podman exec -it mysql-1 bash
    
  4. Connect to the MySQL instance:

     mysql -pdemo123
    
  5. Clear your screen by pressing Ctrl + L.

  6. View the databases:

     show databases;
    
  7. Exit the mysql instance and container:

     exit
     exit
    
  8. Confirm port 33601 is connected to the port 3306 on mysql-1:

     mysql -h localhost -P 33061 -u root -pdemo123 --protocol=tcp -e "show databases;"
    
  9. Confirm database files are going into the directory that was shared with the container:

     ls db_demo/
    
  10. Clear your screen by pressing Ctrl + L.

📝Start the Second MySQL Container

  1. Create a Podman volume for this container:

     podman volume create db_demovol
    
  2. Verify the volume was created:

     podman volume list
    
  3. Create the second MySQL container, expose the container port 33062 to 3306, and set MYSQL_ROOT_PASSWORD to demo123:

     podman run -d --name mysql-2 -v db_demovol:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=demo123 -p 33062:3306 mysql
    
  4. Verify the container is running:

     podman ps
    
  5. Verify the MySQL instance is running inside the container and localhost port 33062 is connected to the port 3306 in the mysql-2 instance:

     mysql -h localhost -P 33062 -u root -pdemo123 --protocol=tcp -e "show databases;"
    

  6. Verify the Containers and MySQL Are Running

  7. Confirm the database files are being stored in the persistent storage:

     podman volume inspect db_demovol
    
  8. Copy the PATH listed next to Mountpoint.

  9. Clear your screen by pressing Ctrl + L.

  10. List the database files by pasting the Mountpoint path as shown below:

    ls <PATH>
    

📌Note - At the end of each hands-on Lab, always clean up all previous resources created to avoid being charged if you used a Cloud Provider to provision them.

Congratulations — you have completed this hands-on lab covering the basics of Creating a Persistent Container Volume using Podman.

Thank you for reading. I hope you understood and learned something helpful from my blog.

Please follow me on CloudDevOpsToLearn and LinkedIn, franciscojblsouza