RHCSA Rapid Track
In this exercise, you will configure a container that is managed as a systemd service, and then use systemctl commands to manage that container so that it automatically starts when the host machine starts.
Outcomes
You should be able to:
Create
systemdunit files for managing containers.Start and stop containers using
systemctlcommands.Configure user accounts for
systemduser services to start when the host machine starts.
On the workstation machine, log in as the student user with student as the password.
On the workstation machine, run the lab containers-services start command.
This command runs a start script that determines if the servera machine is reachable on the network.
It also installs the container tools on servera.
[student@workstation ~]$lab containers-services start
Procedure 16.5. Instructions
Use the
sshcommand to log in toserveraas thestudentuser. The systems are configured to use SSH keys for authentication, so a password is not required.[student@workstation ~]$ssh student@servera...output omitted...[student@servera ~]$Use the
sudo -icommand to switch to therootuser. The password for thestudentuser isstudent.[student@servera ~]$sudo -i[sudo] password for student:student[root@servera ~]#Create a user account named
contsvcusingredhatas the password. Configure the account to access the container image registry atregistry.lab.example.com. You will use this account to run containers assystemdservices, instead of using your regular user account.Use the
useraddcommand to create the account, and then use thepasswdcommand to set the password toredhat.[root@servera ~]#useradd contsvc[root@servera ~]#passwd contsvcChanging password for user contsvc.New password:redhatBAD PASSWORD: The password is shorter than 8 charactersRetype new password:redhatpasswd: all authentication tokens updated successfully.To manage the
systemduser services with thecontsvcaccount, you must log in directly as thecontsvcuser. You cannot use thesuandsudocommands.Log out of
servera, and then use thesshcommand to log in as thecontsvcuser. The systems are configured to use SSH keys for authentication, so a password is not required.[root@servera ~]#exitlogout[student@servera ~]$exitlogout Connection to servera closed.[student@workstation ~]$ssh contsvc@servera...output omitted...[contsvc@servera ~]$Create the
~/.config/containers/directory.[contsvc@servera ~]$mkdir -p ~/.config/containers/[contsvc@servera ~]$The
labscript prepared theregistries.conffile in the/tmp/containers-services/directory. Copy that file to~/.config/containers/. The followingcpcommand is very long and should be entered as a single line.[contsvc@servera ~]$cp /tmp/containers-services/registries.conf ~/.config/containers/To confirm that you can access the
registry.lab.example.comregistry, run thepodman search ubicommand as a test. If everything works as expected, then the command should list some images.[contsvc@servera ~]$podman search ubiINDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED example.com registry.lab.example.com/ubi8/ubi 0 example.com registry.lab.example.com/ubi7/ubi 0
Create the
/home/contsvc/webcontent/html/directory, and then create anindex.htmltest page. You will use that directory as persistent storage when you deploy a web server container.Create the
~/webcontent/html/directory.[contsvc@servera ~]$mkdir -p ~/webcontent/html/[contsvc@servera ~]$Create the
index.htmlfile and add some content.[contsvc@servera ~]$echo "Hello World" > ~/webcontent/html/index.html[contsvc@servera ~]$Confirm that everyone has access to the directory and the
index.htmlfile. The container uses an unprivileged user that must be able to read theindex.htmlfile.[contsvc@servera ~]$ls -ld webcontent/html/drwxrwxr-x. 2 contsvc contsvc 24 Aug 28 04:56 webcontent/html/[contsvc@servera ~]$ls -l webcontent/html/index.html-rw-rw-r--. 1 contsvc contsvc 12 Aug 28 04:56 webcontent/html/index.html
Create a detached container named
myweb. Redirect port 8080 on the local host to the container port 8080. Mount the~/webcontentdirectory from the host to the/var/wwwdirectory in the container. Use theregistry.lab.example.com/rhel8/httpd-24:1-105image.Log in to the
registry.lab.example.comregistry as theadminuser withredhat321as the password.[contsvc@servera ~]$podman login registry.lab.example.comUsername:adminPassword:redhat321Login Succeeded!Create the container. You can copy and paste the following command from the
/tmp/containers-services/start-container.txtfile. The followingpodman runcommand is very long and should be entered as a single line.[contsvc@servera ~]$podman run -d --name myweb -p 8080:8080 -v ~/webcontent:/var/www:Z registry.lab.example.com/rhel8/httpd-24:1-105...output omitted...To verify your work, use the
curlcommand to access the web content on port 8080.[contsvc@servera ~]$curl http://localhost:8080/Hello World
Create the
systemdunit file for managing themywebcontainer withsystemctlcommands. When finished, stop and then delete themywebcontainer. Systemd manages the container and does not expect the container to exist initially.Create the
~/.config/systemd/user/directory.[contsvc@servera ~]$mkdir -p ~/.config/systemd/user/[contsvc@servera ~]$Change to the
~/.config/systemd/user/directory, and then run thepodman generate systemdcommand to create the unit file for themywebcontainer. Use the--newoption so thatsystemdcreates a new container when starting the service and deletes the container when stopping the service.[contsvc@servera ~]$cd ~/.config/systemd/user[contsvc@servera user]$podman generate systemd --name myweb --files --new/home/contsvc/.config/systemd/user/container-myweb.serviceStop and then delete the
mywebcontainer.[contsvc@servera user]$podman stop myweb2f4844b376b78f8f7021fe3a4c077ae52fdc1caa6d877e84106ab783d78e1e1a[contsvc@servera user]$podman rm myweb2f4844b376b78f8f7021fe3a4c077ae52fdc1caa6d877e84106ab783d78e1e1a
Force
systemdto reload its configuration, and then enable and start your newcontainer-mywebuser service. To test your work, stop and then start the service and control the container status with thecurlandpodman pscommands.Use the
systemctl --user daemon-reloadcommand forsystemdto take the new unit file into account.[contsvc@servera user]$systemctl --user daemon-reload[contsvc@servera user]$Enable and start the
container-mywebservice.[contsvc@servera user]$systemctl --user enable --now container-mywebCreated symlink /home/contsvc/.config/systemd/user/multi-user.target.wants/container-myweb.service → /home/contsvc/.config/systemd/user/container-myweb.service. Created symlink /home/contsvc/.config/systemd/user/default.target.wants/container-myweb.service → /home/contsvc/.config/systemd/user/container-myweb.service.Use the
podman psandcurlcommands to verify that the container is running.[contsvc@servera user]$podman psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa648c286c653registry.lab.example.com/rhel8/httpd-24:1-105 /usr/bin/run-http... About a minute ago Up About a minute ago 0.0.0.0:8080->8080/tcp myweb[contsvc@servera user]$curl http://localhost:8080/Hello WorldTake note of the container ID. You will use this information to confirm that
systemdcreates a new container when you restart the service.Stop the
container-mywebservice, and then confirm that the container does not exist anymore. When you stop the service,systemdstops and then deletes the container.[contsvc@servera user]$systemctl --user stop container-myweb[contsvc@servera user]$podman ps --allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESStart the
container-mywebservice, and then confirm that the container is running.[contsvc@servera user]$systemctl --user start container-myweb[contsvc@servera user]$podman psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES6f5148b27726registry.lab.example.com/rhel8/httpd-24:1-105 /usr/bin/run-http... 5 seconds ago Up 4 seconds ago 0.0.0.0:8080->8080/tcp mywebNotice that the container ID has changed. When you start the service,
systemdcreates a new container.
To ensure user services for the
contsvcuser start with the server, run theloginctl enable-lingercommand. When done, restartservera.Run the
loginctl enable-lingercommand.[contsvc@servera user]$loginctl enable-linger[contsvc@servera user]$Confirm that the
Lingeroption is set for thecontsvcuser.[contsvc@servera user]$loginctl show-user contsvc...output omitted...Linger=yesSwitch to the
rootuser, and then use thesystemctl rebootcommand to restartservera.[contsvc@servera user]$su -Password:redhatLast login: Fri Aug 28 07:43:40 EDT 2020 on pts/0[root@servera ~]#systemctl rebootConnection to servera closed by remote host. Connection to servera closed.[student@workstation ~]$
Wait for the
serveramachine to restart, which takes a few minutes, then, log in toserveraas thecontsvcuser. Confirm thatsystemdstarted themywebcontainer and that the web content is available.From
workstation, use thesshcommand to log in toserveraas thecontsvcuser.[student@workstation ~]$ssh contsvc@servera...output omitted...[contsvc@servera ~]$Use the
podman pscommand to confirm that the container is running.[contsvc@servera ~]$podman psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d174e79f08b registry.lab.example.com/rhel8/httpd-24:1-105 /usr/bin/run-http... 3 minutes ago Up 3 minutes ago 0.0.0.0:8080->8080/tcp mywebUse the
curlcommand to access the web content.[contsvc@servera ~]$curl http://localhost:8080/Hello WorldExit from
servera.[contsvc@servera ~]$exitlogout Connection to servera closed.[student@workstation ~]$
This concludes the guided exercise.