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
Pull the
docker.io/library/mysql
container image from the repository:podman pull docker.io/library/mysql
Verify the container image is on the system:
podman images
Create the
db_dir
directory:mkdir db_demo
Verify the directory was created:
ls -ld db_demo
📝Start the First MySQL Container
Create the first MySQL container using the pulled image, expose the container port
3306
to localhost port33061
, and setMYSQL_ROOT_PASSWORD
todemo123
: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.
Verify the container is running:
podman ps
Verify the database instance is running inside the container:
podman exec -it mysql-1 bash
Connect to the MySQL instance:
mysql -pdemo123
Clear your screen by pressing Ctrl + L.
View the databases:
show databases;
Exit the
mysql
instance and container:exit exit
Confirm port
33601
is connected to the port3306
onmysql-1
:mysql -h localhost -P 33061 -u root -pdemo123 --protocol=tcp -e "show databases;"
Confirm database files are going into the directory that was shared with the container:
ls db_demo/
Clear your screen by pressing Ctrl + L.
📝Start the Second MySQL Container
Create a Podman volume for this container:
podman volume create db_demovol
Verify the volume was created:
podman volume list
Create the second MySQL container, expose the container port
33062
to3306
, and setMYSQL_ROOT_PASSWORD
todemo123
:podman run -d --name mysql-2 -v db_demovol:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=demo123 -p 33062:3306 mysql
Verify the container is running:
podman ps
Verify the MySQL instance is running inside the container and localhost port
33062
is connected to the port3306
in themysql-2
instance:mysql -h localhost -P 33062 -u root -pdemo123 --protocol=tcp -e "show databases;"
Verify the Containers and MySQL Are Running
Confirm the database files are being stored in the persistent storage:
podman volume inspect db_demovol
Copy the
PATH
listed next toMountpoint
.Clear your screen by pressing Ctrl + L.
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