tonykang22 / study

0 stars 0 forks source link

[컨테이너를 구성하는 리눅스 기술] namespace의 이해 #101

Open callmeaxxe opened 2 years ago

callmeaxxe commented 2 years ago

09. namespace의 이해

namespaces

cgroups vs namespaces

image

image

image

namespaces 종류

PID 네임스페이스

Network 네임스페이스

User 네임스페이스

Mount 네임스페이스

image

IPC 네임스페이스

UTS 네임스페이스

callmeaxxe commented 2 years ago

10. (실습) namespace - Mount 네임스페이스

새로운 Mount 네임스페이스로 마운트한 위치에 파일 생성

image

unshare command
root@mobius-pub:~# man unshare

# bash shell pid 확인
root@mobius-pub:~# echo $$
2657

# 새로운 mount namespace 생성
root@mobius-pub:~# unshare -m /bin/bash

# bash shell pid 확인 (새로운 ns 가 생성되어 pid 가 달라졌다)
root@mobius-pub:~# echo $$
2953

# 부모로부터 분리된 pid 가 할당된 것을 확인할 수 있음
root@mobius-pub:~# ps auxf
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
...
root        2657  0.0  0.0   9836  4216 pts/0    Ss   16:46   0:00                      |   \_ /usr/bin/bash --init-file /root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/out/vs/wor
root        2953  0.0  0.0   9836  4092 pts/0    S    16:49   0:00                      |       \_ /bin/bash
...

# tmpfs 마운트
root@mobius-pub:~# mount -t tmpfs tmpfs /mnt
root@mobius-pub:~# mount | grep /mnt
tmpfs on /mnt type tmpfs (rw,relatime)

# 새로 마운트한 tmpfs 인 /mnt 하위에 파일 생성
root@mobius-pub:/mnt# echo "Hello" > /mnt/hello.txt

# 생성된 파일 확인
root@mobius-pub:~# ll /mnt
total 8
drwxrwxrwt  2 root root   60 Sep  4 16:56 ./
drwxr-xr-x 21 root root 4096 May 11 16:05 ../
-rw-r--r--  1 root root    6 Sep  4 16:56 hello.txt

# 터미널을 새로 열면 ns 가 분리되어 해당 파일을 확인할 수 없다
# 따라서 해당 ns 에 진입하도록 nsenter 명령을 사용한다
root@mobius-pub:~# cd /mnt
root@mobius-pub:/mnt# ll
total 8
drwxr-xr-x  2 root root 4096 Aug  1  2020 ./
drwxr-xr-x 21 root root 4096 May 11 16:05 ../

root@mobius-pub:/mnt# nsenter -t 2953 -a
root@mobius-pub:/# ll /mnt
total 8
drwxrwxrwt  2 root root   60 Sep  4 16:56 ./
drwxr-xr-x 21 root root 4096 May 11 16:05 ../
-rw-r--r--  1 root root    6 Sep  4 16:56 hello.txt
callmeaxxe commented 2 years ago

11. (실습) namespace – Pid 네임스페이스

image

pid 1 값이 systemd 에 할당되지 않고 bash shell 에 할당이 된다

# 자신의 PID 확인
root@mobius-pub:~# echo $$
2953

# 새로운 PID 네임스페이스 생성
# -p PID 네임스페이스 생성
# -m mount 네임스페이스 생성
# -i IPC 네임스페이스 생성
# -f 자식 프로세스 생성
root@mobius-pub:~# unshare -pmif

# 현재 PID 확인
root@mobius-pub:~# echo $$
1

# 프로세스 리스트 확인
root@mobius-pub:~# ps aux | head -n5
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0 167288 11220 ?        Ss   16:43   0:01 /sbin/init
root           2  0.0  0.0      0     0 ?        S    16:43   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   16:43   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   16:43   0:00 [rcu_par_gp]

# ps 명령어는 proc fs 에서 정보를 읽어오게 되어있는데 기존 host 의 proc 결과를 보여주고 있다
# 새로운 네임스페이스에 맞는 새로운 Proc filesystem 마운트
root@mobius-pub:~# mount -t proc none /proc

# 프로세스 리스트 다시 확인
# 부모로부터 독립된 환경의 process list 만 보여지게 된다
root@mobius-pub:~# ps aux | head -n5
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0  10868  5064 pts/0    S    17:22   0:00 -bash
root          13  0.0  0.0  11492  3292 pts/0    R+   17:25   0:00 ps aux
root          14  0.0  0.0   8092   592 pts/0    S+   17:25   0:00 head -n5
root@mobius-pub:~# 
callmeaxxe commented 2 years ago

12. (실습) namespace – Network 네임스페이스

Network 네임스페이스 실습

image

리눅스의 가상 네트워크 인터페이스

# 새로운 네트워크 네임스페이스(ns0, ns1) 생성
root@mobius-pub:~# ip netns add ns0
root@mobius-pub:~# ip netns add ns1

# 네트워크 네임스페이스 생성 확인
root@mobius-pub:~# ip netns list
ns1
ns0

# host 의 모든 인터페이스 정보 확인
root@mobius-pub:~# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 8950 qdisc mq state UP mode DEFAULT group default qlen 1000
    link/ether f2:20:af:7c:9b:1e brd ff:ff:ff:ff:ff:ff
...

# 네임스페이스 내의 모든 인터페이스 정보 확인
root@mobius-pub:~# ip netns exec ns0 ip link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

# 네트워크 인터페이스 구동
root@mobius-pub:~# ip netns exec ns0 ip link set lo up
root@mobius-pub:~# ip netns exec ns1 ip link set lo up

# 생성한 netns 간 연결을 하기 위한 bridge 생성

# 브릿지 정보 확인
root@mobius-pub:~# ip link show type bridge
3: br-0fe382f76a51: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
    link/ether 02:42:01:78:2c:88 brd ff:ff:ff:ff:ff:ff
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
    link/ether 02:42:b8:8f:f7:6a brd ff:ff:ff:ff:ff:ff
5: br-d55794a341b6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
    link/ether 02:42:1e:c2:7a:a8 brd ff:ff:ff:ff:ff:ff

# 호스트에 새로운 브릿지 네트워크 인터페이스 생성
root@mobius-pub:~# ip link add br0 type bridge
# bridge 인터페이스 구동
root@mobius-pub:~# ip link set br0 up

# 생성한 브릿지 ip 확인 (ip 할당 안되어 있음)
root@mobius-pub:~# ip addr
...
6: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
    link/ether 62:97:74:61:3a:a0 brd ff:ff:ff:ff:ff:ff

# 브릿지 네트워크에 IP 설정
root@mobius-pub:~# ip addr add 192.168.2.1/24 dev br0
root@mobius-pub:~# ip addr
...
6: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
    link/ether 62:97:74:61:3a:a0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.1/24 scope global br0
       valid_lft forever preferred_lft forever

# ping 날려서 확인
root@mobius-pub:~# ping -c 2 192.168.2.1
PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.
64 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=0.022 ms
64 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.019 ms

--- 192.168.2.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1024ms
rtt min/avg/max/mdev = 0.019/0.020/0.022/0.001 ms

# veth*, ceth*를 위한 veth 페어 생성 (케이블을 연결하는 과정)
root@mobius-pub:~# ip link add veth0 type veth peer name ceth0
root@mobius-pub:~# ip link add veth1 type veth peer name ceth1

# veth* 를 br0에 연결
root@mobius-pub:~# ip link set veth0 master br0
root@mobius-pub:~# ip link set veth1 master br0

# veth* 시작
root@mobius-pub:~# ip link set veth0 up
root@mobius-pub:~# ip link set veth1 up

# ceth*를 ns* 네임스페이스에 연결
ip link set ceth0 netns ns0
ip link set ceth1 netns ns1

# ceth* 시작 
ip netns exec ns0 ip link set ceth0 up
ip netns exec ns1 ip link set ceth1 up

# ns1 의 네트워크 인터페이스 연결 확인 (ceth1 이 떠있으나 ip 가 할당 x)
root@mobius-pub:~# ip netns exec ns1 ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
9: ceth1@if10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 82:d7:17:23:8a:1e brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::80d7:17ff:fe23:8a1e/64 scope link 
       valid_lft forever preferred_lft forever

# ns*의 ceth* 인터페이스에 IP 할당
ip netns exec ns0 ip addr add 192.168.2.2/24 dev ceth0
ip netns exec ns1 ip addr add 192.168.2.3/24 dev ceth1

# ns* 의 네트워크 인터페이스 연결 확인 (ip 할당됨)
root@mobius-pub:~# ip netns exec ns0 ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
7: ceth0@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 2e:57:c8:0d:61:04 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.2.2/24 scope global ceth0
       valid_lft forever preferred_lft forever
    inet6 fe80::2c57:c8ff:fe0d:6104/64 scope link 
       valid_lft forever preferred_lft forever
root@mobius-pub:~# ip netns exec ns1 ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
9: ceth1@if10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 82:d7:17:23:8a:1e brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.2.3/24 scope global ceth1
       valid_lft forever preferred_lft forever
    inet6 fe80::80d7:17ff:fe23:8a1e/64 scope link 
       valid_lft forever preferred_lft forever

# ns0 -> ns1 로 연결(ping)
root@mobius-pub:~# ip netns exec ns0 ping -c 2 192.168.2.3

(ping 이 안감..)