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.
- SectionContainer Concepts
- Quiz: Container Concepts
- Deploy Containers
- Guided Exercise: Deploy Containers
- Manage Container Storage and Network Resources
- Guided Exercise: Manage Container Storage and Network Resources
- Manage Containers as System Services
- Guided Exercise: Manage Containers as System Services
- Lab: Run Containers
- Summary
Abstract
| Goal | Obtain, run, and manage simple lightweight services as containers on a single Red Hat Enterprise Linux server. |
| Objectives |
|
| Sections |
|
| Lab |
Run Containers |
Explain container concepts and the core technologies for building, storing, and running containers.
Software applications typically depend on system libraries, configuration files, or services that their runtime environment provides. Traditionally, the runtime environment for a software application is installed in an operating system that runs on a physical host or a virtual machine. Administrators then install application dependencies on top of the operating system.
In Red Hat Enterprise Linux, packaging systems such as RPM help administrators to manage application dependencies.
When you install the httpd package, the RPM system ensures that the correct libraries and other dependencies for that package are also installed.
The major drawback to traditionally deployed software applications is that these dependencies are entangled with the runtime environment. An application might require earlier or later versions of supporting software than the software that is provided with the operating system. Similarly, two applications on the same system might require different and incompatible versions of the same software.
One way to resolve these conflicts is to package and deploy the application as a container. A container is a set of one or more processes that are isolated from the rest of the system. Software containers provide a way to package applications and to simplify their deployment and management.
Think of a physical shipping container. A shipping container is a standard way to package and ship goods. It is labeled, loaded, unloaded, and transported from one location to another as a single box. The container's contents are isolated from the contents of other containers so that they do not affect each other. These underlying principles also apply to software containers.
Red Hat Enterprise Linux supports containers by using the following core technologies:
Control Groups (cgroups) for resource management
Namespaces for process isolation
SELinux and Seccomp (Secure Computing mode) to enforce security boundaries
Note
For a deeper discussion of container architecture and security, refer to the "Ten Layers of Container Security" white paper.
Containers provide many of the same benefits as virtual machines, such as security, storage, and network isolation.
Both technologies isolate their application libraries and runtime resources from the host operating system or hypervisor, and vice versa.
Containers and virtual machines interact differently with hardware and the underlying operating system.
A virtual machine has the following characteristics:
Enables multiple operating systems to run simultaneously on a single hardware platform.
Uses a hypervisor to divide hardware into multiple virtual hardware systems.
Requires a complete operating system environment to support the application.
A container has the following characteristics:
Runs directly on the host operating system, and it shares resources with all containers on the system.
Shares the host's kernel, but it isolates the application processes from the rest of the system.
Requires far fewer hardware resources than virtual machines, so containers are also quicker to start.
Includes all dependencies, such as system and programming dependencies, and configuration settings.
Note
Some applications might not be suitable to run as a container. For example, applications that access low-level hardware information might need more direct hardware access than containers generally provide.
On the container host, you can run containers as the root user or as a regular, unprivileged user. Containers that a privileged user runs are called rootful containers. Containers that non-privileged users run are called rootless containers.
A rootless container is not allowed to use system resources that are usually reserved for privileged users, such as access to restricted directories, or to publish network services on restricted ports (below 1024). This feature prevents a possible attacker from gaining root privileges on the container host.
Although you can run containers directly as root if necessary, this scenario weakens the security of the system if a bug enables an attacker to compromise the container.
Containers efficiently reuse hosted applications and make them portable. Containers can be moved from one environment to another, such as from development to production. You can save multiple versions of a container and access each one as needed.
Containers are typically temporary, or ephemeral. You can permanently save in persistent storage the data that a running container generates, but the containers themselves usually run when needed, and then they stop and are removed. A new container process is started the next time that particular container is needed.
You could install a complex software application with multiple services in a single container. For example, a web server might need to use a database and a messaging system. However, using one container for multiple services is hard to manage.
A better design runs in separate containers each component, the web server, the database, and the messaging system. This way, updates and maintenance to individual application components do not affect other components or the application stack.
Red Hat Enterprise Linux provides a set of container tools that you can use to run containers in a single server:
podmanmanages containers and container images.skopeoinspects, copies, deletes, and signs images.buildahcreates container images.
These tools are compatible with the Open Container Initiative (OCI). With these tools, you can manage any Linux containers that OCI-compatible container engines create, such as Podman or Docker. These tools are designed to run containers under Red Hat Enterprise Linux on a single-node container host.
In this chapter, you use the podman and skopeo utilities to run and manage containers and existing container images.
Note
Using buildah to construct your own container images is beyond the scope of this course.
It is covered in the Red Hat OpenShift I: Containers & Kubernetes (DO180) Red Hat Training course.
To run containers, you must use a container image. A container image is a static file that contains codified steps, and it serves as a blueprint to create containers. The container images package an application with all its dependencies, such as its system libraries, programming language runtimes and libraries, and other configuration settings.
Container images are built according to specifications, such as the Open Container Initiative (OCI) image format specification. These specifications define the format for container images, as well as the metadata about the container host operating systems and hardware architectures that the image supports.
A container registry is a repository for storing and retrieving container images. A developer pushes or uploads container images to a container registry. You can pull or download container images from a registry to a local system to run containers.
You might use a public registry that contains third-party images, or you might use a private registry that your organization controls. The source of your container images matters. As with any other software package, you must know whether you can trust the code in the container image. Policies vary between registries about whether and how they provide, evaluate, and test container images that are submitted to them.
Red Hat distributes certified container images through two main container registries that you can access with your Red Hat login credentials:
registry.redhat.iofor containers that are based on official Red Hat productsregistry.connect.redhat.comfor containers that are based on third-party products
The Red Hat Container Catalog (https://access.redhat.com/containers) provides a web-based interface to search these registries for certified content.
Note
Red Hat provides the Universal Base Image (UBI) image as an initial layer to build containers. The UBI image is a minimized container image that can be a first layer for an application build.
You need a Red Hat Developer account to download an image from the Red Hat registries.
You can use the podman login command to authenticate to the registries.
If you do not provide a registry URL to the podman login command, then it authenticates to the default configured registry.
[user@host ~]$podman login registry.lab.example.comUsername:RH134Password:EXAMPLEPASSWORDLogin Succeeded!
You can also use the podman login command --username and --password-stdin options, to specify the user and password to log in to the registry.
The --password-stdin option reads the password from stdin.
Red Hat does not recommend using the --password option to provide the password directly, because this option stores the password in the log files.
[user@host ~]# echo $PASSWORDVAR | podman login --username RH134 \
--password-stdin registry.access.redhat.comTo verify that you are logged in to a registry, use the podman login command --get-login option.
[user01@rhel-vm ~]$podman login registry.access.redhat.com --get-loginRH134[user01@rhel-vm ~]$podman login quay.io --get-loginError: not logged into quay.io
In the preceding output, the podman utility is authenticated to the registry.access.redhat.com registry with the RH134 user credentials, but the podman utility is not authenticated to the quay.io registry.
The default configuration file for container registries is the /etc/containers/registries.conf file.
[user@host ~]$ cat /etc/containers/registries.conf
# For more information on this configuration file, see containers-registries.conf(5).
#
...output omitted...
unqualified-search-registries = ["registry.fedoraproject.org", "registry.access.redhat.com", "registry.centos.org", "quay.io", "docker.io"]
# [[registry]]
# # The "prefix" field is used to choose the relevant [[registry]] TOML table;
# # (only) the TOML table with the longest match for the input image name
# # (taking into account namespace/repo/tag/digest separators) is used.
# #
# # The prefix can also be of the form: *.example.com for wildcard subdomain
# # matching.
# #
# # If the prefix field is missing, it defaults to be the same as the "location" field.
# prefix = "example.com/foo"
#
# # If true, unencrypted HTTP as well as TLS connections with untrusted
# # certificates are allowed.
# insecure = false
#
# # If true, pulling images with matching names is forbidden.
# blocked = false
#
...output omitted...Because Red Hat recommends using a non-privileged user to manage containers, you can create a registries.conf file for container registries in the $HOME/.config/containers directory.
The configuration file in this directory overrides the settings in the /etc/containers/registries.conf file, and is used when Podman runs in rootless mode.
If you do not specify the fully qualified name of the container image when using podman commands, then the list of registries in the unqualified-search-registries section of this file is used to search for the container image.
[user@host ~]$ podman pull ubiIf you do specify the fully qualified name of the container image from the command line, then the container utility does not search in this section.
The unqualified-search-registries section can be left blank to ensure that you use the fully qualified name of the container image.
[user@host ~]$ podman pull registry.access.redhat.com/ubi8/ubi:latestNote
Red Hat recommends always using the fully qualified name of container images.
Configure settings for container registries in the [[registry]] sections of the file.
Use a separate [[registry]] section to configure settings for each container registry.
[[registry]] location = "registry.lab.example.com" insecure = true blocked = false
The
locationsetting specifies the location of the container registry.If the
insecuresetting is set totrue, then you can use unencrypted HTTP as well as TLS connections with untrusted certificates to access the registry.If the
blockedsetting is set totrue, then images cannot be downloaded from that registry.
Note
This classroom runs a private insecure registry that is based on Red Hat Quay to provide container images. This registry meets the classroom need; however, you would not expect to work with insecure registries in real-world scenarios. For more information about this software, see https://access.redhat.com/products/red-hat-quay
A container file is a text file with the instructions to build a container image. A container file usually has a context that defines the path or URL where its files and directories are located. The resulting container image consists of read-only layers, where each layer represents an instruction from the container file.
The following output is an example of a container file that uses the UBI image from the registry.access.redhat.com registry, installs the python3 package, and prints the hello string to the console.
[user@host ~]$ cat Containerfile
FROM registry.access.redhat.com/ubi8/ubi:latest
RUN dnf install -y python3
CMD ["/bin/bash", "-c", "echo hello"]Note
Creating a container file and its usage instructions are out of scope for this course. For more information about container files, refer to the DO180 course.
New applications increasingly use containers to implement functional components. Those containers provide services that other parts of the application consume. In an organization, managing a growing number of containers might become an overwhelming task.
Deploying containers at scale in production requires an environment that can adapt to the following challenges:
The platform must ensure the availability of containers that provide essential services.
The environment must respond to application usage spikes by increasing or decreasing the number of running containers and by load balancing the traffic.
The platform must detect the failure of a container or a host and react accordingly.
Developers might need an automated workflow to deliver later application versions transparently and securely.
Kubernetes is an orchestration service that deploys, manages, and scales container-based applications across a cluster of container hosts. Kubernetes redirects traffic to your containers with a load balancer, so that you can scale the number of containers that provide a service. Kubernetes also supports user-defined health checks to monitor your containers and to restart them if they fail.
Red Hat provides a distribution of Kubernetes called Red Hat OpenShift. Red Hat OpenShift is a set of modular components and services that are built on top of the Kubernetes infrastructure. It provides additional features, such as remote web-based management, multitenancy, monitoring and auditing, advanced security features, application lifecycle management, and self-service instances for developers.
Red Hat OpenShift is beyond the scope of this course, and you can learn more about it at https://www.openshift.com.
Note
In the enterprise, individual containers are not generally run from the command line. Instead, it is preferable to run containers in production with a Kubernetes-based platform, such as Red Hat OpenShift.
However, you might need to use commands to manage containers and images manually or at a small scale. This chapter focuses on this use case to improve your grasp of the core concepts behind containers, how they work, and how they can be useful.
References
cgroups(7), namespaces(7), seccomp(2) man pages.
Open Container Initiative (OCI) Image Specification
For more information, refer to the Starting with Containers chapter in the Red Hat Enterprise Linux 9 Building, Running, and Managing Containers guide at https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/building_running_and_managing_containers/index