Docker 기반 기술

2023. 12. 10. 11:46b정리/rocky8로 도커핵심 이해하기

728x90
반응형

도커는 GO 프로그래밍 언어로 작성되었으며, 리눅스 커널의 여러 기능을 활용하여 기능을 제공한다.

도커는 컨테이너를 실행할 때 Namespace, chroot, cgroups를 생성한다. namespace는 겨길 계층을 제공하고, 컨테이너의 각 측면은 별도의 namespace에서 실행되며 액세스는 해당 namespace로 제한된다.

chroot

A chroot environment provides functionality similar to that of a virtual machine, but it is a lighter solution. 

The captive system shares your existing kernel.

 

 chroot environments are closer to containers such as LXC than to virtual machines. 

 

 루트 디렉터리를 변경하면 특정 프로세스(K)가 상위 디렉터리에 접근할 수 없도록 격리 시킬 수 있습니다. 정확히 이 역할을 하는 것이 chroot 명령어입니다.

네임스페이스

시스템 리소스를 격리하고 가상화하는 리눅스 커널 기능

네임스페이스로 제한된 프로세스는 동일한 네임스페이스의 일부인 리소스 또는 프로세스 간에만 상호 작용할 수 있다.

네트워크 네임스페이스는 ip 명령어의 netns 서브 커맨드로 제어할 수 있습니다. add는 네임스페이스를 생성하고, list는 네임스페이스를 보여줍니다. direct_netns라는 네트워크 네임스페이스를 추가합니다.

$ ip netns add direct_netns
$ ip netns list
direct_netns

 

ip netns에는 exec라는 서브 커맨드가 있습니다. 마치 docker exec처럼 특정 네트워크 네임스페이스에서 프로세스를 실행시키는 기능입니다. direct_netns 네임스페이스에서 ip --br link를 실행해 네트워크 디바이스 목록을 확인해봅니다.

# ip netns exec direct_netns ip --br link
lo               DOWN           00:00:00:00:00:00 <LOOPBACK>

 


여기에 보이는 장비들은 디폴트 네트워크 네임스페이스에 속해있는 네트워크 디바이스의 목록

 ip 명령어의 netns 서브 커맨드로 제어할 수 있습니다. add는 네임스페이스를 생성하고, list는 네임스페이스를 보여줍니다. direct_netns라는 네트워크 네임스페이스를 추가

 

lo 네트워크 디바이스 ddown 에서 up으로 변경

 DOWN에서 UNKNOWN이 되었습니다(이게 동작 상태입니다 )

 

nginx 설치 후 mynet네임스페이스에 포그라운드 실행 ( nginx 정시 상태)

 

앞에서 veth0, veth1 인터페이스를 만들어놓기만 하고 사용하지는 않았습니다. 현재 veth0(@veth1) veth1(@veth0) 인터페이스는 모두 디폴트 네임스페이스에 있습니다. 디폴트 네트워크 네임스페이스와 direct_netns 네임스페이스를 연결하기 위해, veth1을 direct_netns 네임스페이스로 옮겨보겠습니다.

아이피 설정 및 인터페이스 donw에서up 설정 후 ping test

브릿지 생성

$ ip link add br0 type bridge

$ ip link set br0 up

네트워크 스페이스 생성, 가상 디바이스 생성, 연결 그리고 활성화

$ ip netns add container4

$ ip link add brid4 type veth peer name veth4

$ ip link set veth4 netns container4

$ ip netns exec container4 ip a add 10.201.0.4/24 dev veth4

$ ip netns exec container4 ip link set dev lo up

$ ip netns exec container4 ip link set dev veth4 up

$ ip link set brid4 master br0

$ ip link set dev brid4 up

 

$ ip netns add container5

$ ip link add brid5 type veth peer name veth5

$ ip link set veth5 netns container5

$ ip netns exec container5 ip a add 10.201.0.5/24 dev veth5

$ ip netns exec container5 ip link set dev lo up

$ ip netns exec container5 ip link set dev veth5 up

$ ip link set brid5 master br0

$ ip link set dev brid5 up

 

 

NAT 셋업을 위해서는 리눅스의 IP 포워드 기능을 활성화

sysctl -w net.ipv4.ip_forward=1

Nat 설정을 추가해주

iptables -t nat -A POSTROUTING -s 10.201.0.0/24 -j MASQUERADE

 

** ping이 안가면 방화벽 확인 **

$ iptables -L | grep FORWARD

$ iptables --policy FORWARD ACCEPT

 

cgroup

프로세스 모음의 자원 사용량(CPU, Memory, Disk I/O, network 등)을 제한, 확인, 격리하는 리눅스 커널 기능

도커는 cgroups을 사용하여 자원 제한을 제어하고 격리한다.

 


참고사이트:

https://www.howtogeek.com/441534/how-to-use-the-chroot-command-on-linux/

 

How to Use the chroot Command on Linux

Go directly to jail and do not pass root. chroot makes it simple to set up ring-fenced environments In Linux.

www.howtogeek.com

https://www.geeksforgeeks.org/chroot-command-in-linux-with-examples/

 

chroot command in Linux with examples - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

https://www.redhat.com/sysadmin/cgroups-part-one

 

A Linux sysadmin's introduction to cgroups

Defining cgroups and how they help with resource management and performance tuning in this first article kicking off a four-part series covering cgroups and resource management.

www.redhat.com

https://c0wb3ll.tistory.com/entry/chroot

 

chroot

chroot chroot 란? chroot란 Change Root Directiory의 줄임말로 현재 실행중인 프로세스와 자녀 프로세스의 루트 디렉토리를 변경하는 작업이다. chroot를 이용해 변경된 루트 디렉토리에서는 그 상위에 디렉

c0wb3ll.tistory.com

https://www.44bits.io/ko/post/change-root-directory-by-using-chroot

 

컨테이너 기초 - chroot를 사용한 프로세스의 루트 디렉터리 격리

chroot는 프로세스의 루트 디렉터리를 변경하는 리눅스 시스템콜/명령어입니다. 루트를 변경함으로써 아주 기초적인 단계의 컨테이너를 구현해볼 수 있습니다. chroot는 아주 단순한 프로그램입니

www.44bits.io

https://www.44bits.io/ko/post/container-network-2-ip-command-and-network-namespace

 

ip로 직접 만들어보는 네트워크 네임스페이스와 브리지 네트워크 - 컨테이너 네트워크 기초 2편

네트워크 네임스페이스는 프로세스 간의 네트워크를 격리해주는 매우 강력한 도구입니다. 리눅스에서는 ip를 사용해 네트워크를 조회하는 것 뿐만 아니라 네트워크 네임스페이스를 제어하는

www.44bits.io

https://velog.io/@whattsup_kim/Linux-Kernel-%EC%BB%A8%ED%85%8C%EC%9D%B4%EB%84%88%EB%A5%BC-%EC%9C%84%ED%95%9C-%EB%A6%AC%EB%88%85%EC%8A%A4%EC%9D%98-%ED%94%84%EB%A1%9C%EC%84%B8%EC%8A%A4-%EA%B2%A9%EB%A6%AC-%EA%B8%B0%EB%8A%A5-cgroup-namespace-union-mount

 

컨테이너를 위한 리눅스 기능: cgroup, namespace, union mount

컨테이너 기술의 근간이 되는 Cgroup, Namespace, Union mount에 대해 알아보자.

velog.io

 

반응형

'b정리 > rocky8로 도커핵심 이해하기' 카테고리의 다른 글

Docker의 개요 및 설치  (0) 2024.01.07