极客时间运维进阶训练营第一周作业
Namespace
What is Namespace
We all know that the operating system uses virtual memory technology to make each user process think that it has all the physical memory, which is the virtualization of the memory by the operating system.
Also, through the time-sharing scheduling system, each process can be scheduled and executed fairly, that is, each process can obtain the CPU, so that each process thinks that it has all the CPU time during the process activity, which is the virtualization of the CPU by the operating system.
However, the above two virtualization technologies only virtualize the “physical resources”. In fact, on a host, there are many “non-physical resources” in the operating system, such as user permission, network protocol stack resources, and file system mount path resources. Through the namespace feature of Linux, these non-physical global resources can be virtualized.
According to wiki, Namespaces are a feature of the Linux kernel that partitions kernel resources such that one set of processes sees one set of resources while another set of processes sees a different set of resources.
The feature works by assigning the same namespace for a set of resources and processes, but those namespaces refer to distinct resources. Namespaces are a fundamental aspect of containers on Linux.
How Namespace Works
Namespace is the underlying concept of the Linux system. It is implemented at the kernel layer.
Each container runs in the same container runtime process and shares the same host system kernel.
Each container must have an isolated running space similar to a virtual machine, but the container technology implements a running environment for running specified services within a process, and can also protect the host kernel from the interference and influence of other processes, such as file system space, network space, process space, etc.
Namespaces are currently mainly isolated from each other in the container running space through the following technologies:
Common Namespaces Used in Containers
MNT Namespace
MNT namespace
provides isolation of disk mount points and file systems:
Each container must have its own independent root file system and an independent user space
It providing the opportunity for different processes on a system to have different views of the host’s filesystem
A new mount namespace is created using either clone
or unshare
syscalls with the CLONE_NEWNS
flag. When a new mount namespace is created, its mount list is initialized as follows:
If the namespace is created using
clone
, the mount list of the child’s namespace is a copy of the mount list in the parent process’s mount namespace.If the namespace is created using
unshare
, the mount list of the new namespace is a copy of the mount list in the caller’s previous mount namespace.
Demo
Run a shell in a new mount
namespace
Create a /tmp/mnt
directory
Mount /usr/local/bin
Exit
IPC Namespace
The IPC namespace is used for isolating System V IPC objects, and POSIX message queues. The clone flag used to achieve this is CLONE_NEWIPC
. Each IPC namespace has its own set of System V IPC identifiers and its own POSIX message queue filesystem.
Objects created in an IPC namespace are visible to all other processes that are members of that namespace, but are not visible to processes in other IPC namespaces.
Demo
List current IPC namespace
Create IPC namespace:
UTS Namespace
The UTS namespace is used to isolate two specific elements of the system that relate to the uname
system call. The UTS(UNIX Time Sharing) namespace is named after the data structure used to store information returned by the uname
system call.
Specifically, the UTS namespace isolates the hostname and the NIS domain name. It enables a container to have its own hostname, which is independent of the host system and other containers on it.
Demo
Create UTS namespace:
Now if you log into the server with a new terminal:
PID Namespace
PID namespaces isolate the process ID number space, meaning that processes in different PID namespaces can have the same PID. PID namespaces allow containers to provide functionality such as suspending/resuming the set of processes in the container and migrating the container to a new host while the processes inside the container maintain the same PIDs.
Demo
Check current PID
As you can see, the current Bash
shell has PID 28877, let’s create a new PID namespace.
Net Namespace
Network namespaces can virtualize network stacks, and each network namespace has its own resources, such as network interfaces, IP addresses, routing tables, tunnels, firewalls, etc. For example, rules added to a network namespace by iptables
will only affect traffic entering and leaving that namespace.
Each container is similar to a virtual machine. It has its own network card, listening port, TCP/IP protocol stack, etc. For example, Docker runtime uses the network namespace to start a vethX
interface, so that your container will have its own bridge IP address, usually docker0
, and docker0
is essentially a Linux virtual network bridge.
Demo
Create NET namespace
List current NET namespace
Check new namespace interface
User Namespace
User namespaces isolate security-related identifiers and attributes, in particular, user IDs and group IDs, the root directory, keys, and capabilities. A process’s user and group IDs can be different inside and outside a user namespace.
In particular, a process can have a normal unprivileged user ID outside a user namespace while at the same time having a user ID of 0 inside the namespace; in other words, the process has full privileges for operations inside the user namespace, but is unprivileged for operations outside the namespace.
Demo
Check current USER namespace
Create a new USER namespace and attach it
Try to update hostname as the new root user
Install Docker
The procedure to install Docker on AMI 2 (Amazon Linux 2) running on either EC2 or Lightsail instance is as follows:
Login into remote AWS server using the ssh command:
$ ssh ec2-user@ec2-ip-address-dns-name-here
Apply pending updates using the yum command:
$ sudo yum update
Search for Docker package:
$ sudo yum search docker
Get version information:
$ sudo yum info docker
Install docker
sudo yum install docker
Enable docker service at AMI boot time:
$ sudo systemctl enable docker.service
Start the Docker service
sudo systemctl start docker.service
Check docker status
Docker volume
Bind mounts
Bind mounts have been around since the early days of Docker. Bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine. By contrast, when you use a volume, a new directory is created within Docker’s storage directory on the host machine, and Docker manages that directory’s contents.
The file or directory does not need to exist on the Docker host already. It is created on demand if it does not yet exist. Bind mounts are very performant, but they rely on the host machine’s filesystem having a specific directory structure available.
Example:
You can use the following command to do ReadOnly" bind mount:
Volumes
In containers, volumes are preferred mechanism for persisting data. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker.
In addition, volumes are often a better choice than persisting data in a container’s writable layer, because a volume does not increase the size of the containers using it, and the volume’s contents exist outside the lifecycle of a given container.
Create volume
Remove volume
Mount volume
Docker network
Bridge network
In terms of networking, a bridge network is a Link Layer device which forwards traffic between network segments. A bridge can be a hardware device or a software device running within a host machine’s kernel.
In terms of Docker, a bridge network uses a software bridge which allows containers connected to the same bridge network to communicate, while providing isolation from containers which are not connected to that bridge network. The Docker bridge driver automatically installs rules in the host machine so that containers on different bridge networks cannot communicate directly with each other.
评论