3.4. Correlation between Namespace and Process
Characteristics of Namespace Related to Process
Namespace and Process have a close relationship. First, let’s look at the characteristics of Namespace related to Process. Namespace has the characteristic that it is automatically removed by the Linux kernel when there are no processes belonging to the Namespace. In other words, it also means that a process belonging to the Namespace must exist for the Namespace to be created. For this reason, all system calls related to Namespace are associated with processes.
Below is an explanation of system calls related to Namespace. You can see that clone() and unshare() system calls, which create new Namespaces, not only create Namespaces but also perform the action of assigning processes to the created Namespace.
clone() : This is an extended version of the fork() system call that creates processes. When you proceed with Namespace-related settings with CLONE-NEW* options and call the clone() system call, not only the (Child) process but also a new Namespace to which the process belongs is created. Container runtimes like Docker use the clone() system call to simultaneously create the Namespace used by the container and the container’s Init Process when creating a new container.
unshare() : When the unshare() system call is called, a new Namespace is created, and the process that called the unshare() system call belongs to the newly created Namespace. You can use the unshare() system call through the unshare command.
setns() : The process that calls the setns() system call belongs to another Namespace specified through the setns() system call parameter. The docker exec command used when executing commands inside Docker containers from the Host uses the setns() system call to run processes in the Docker container’s Namespace. You can also use the setns() system call through the nsenter command.
Characteristics of Process Related to Namespace
Let’s look at the characteristics of processes related to Namespace. All processes have the characteristic that they must belong to a specific Namespace of all Namespace types. Therefore, not only container processes but also host processes operate by belonging to the host’s Namespace. Also, when creating child processes using fork() or clone() system calls, unless Namespace-related settings are applied, child processes basically inherit and use the Namespace used by the parent process. Due to this inheritance characteristic, host processes basically belong to the host’s Namespace, and each container’s processes operate by belonging to each container’s Namespace.
|
|
[Shell 1] shows the process of checking the Namespace of Host Process and Container Process. Through the content of [Shell 1], you can confirm the Namespace-related characteristics of processes mentioned above. The Namespace to which a process belongs exists as a symbolic link in the /proc/[PID]/ns directory. There is a symbolic link for each Namespace, and the number means the inode number of the symbolic link. In [Shell 1], you can see IPC, Mount, Network, PID, User, and UTS Namespaces in the /proc/[PID]/ns directory.
In [Shell 1], you can see that the symbolic links of /proc/1/ns and /proc/1328/ns have the same inode number. This means that the Host’s Init Process and the Host’s SSH Daemon Process use the same Namespace. This is because the SSH Daemon Process is a child process of systemd, which is the Host’s Init Process, and since no special Namespace-related settings were applied, the SSH Daemon Process uses the same Namespace as the systemd Process.
In [Shell 1], you can see that the symbolic links of /proc/1/ns and /proc/1000/ns are different. PID 1000 is the PID of the nginx Container’s Init Process as seen from the Host PID Namespace. In other words, you can see that the Host’s Namespace and the nginx Container’s Namespace are different.
Cross-Use of Host Namespace and Container Namespace
A process can use some Namespaces from the Host’s Namespace and use the remaining Namespaces from the Container’s Namespace. In other words, Namespaces can be selected and used crosswise. For example, you can set it to use the Host’s Network Namespace but use the Container’s PID Namespace.
|
|
[Shell 2] shows the process of placing a bash Process in the netshoot Container’s Network Namespace and the Host’s remaining Namespaces using the nsenter command, and checking IP and Process information. When the “ip” command is executed in the bash Process, you can see the Container’s IP information, not the Host’s IP information. However, when you check the process information through the “ps” command in the bash Process, you can see that the PID 1 process is not the netshoot Container’s PID 1 process, but the Host’s PID 1 process. This is because the bash Process uses the netshoot Container’s Network Namespace, but uses the Host’s PID and Mount Namespaces.