버전 비교

  • 이 줄이 추가되었습니다.
  • 이 줄이 삭제되었습니다.
  • 서식이 변경되었습니다.

...

정보
  • Control groups : 리소스 사용량(CPU/Memory/Network/Storage) 결정

  • Namespaces : 자원을 격리(네트워크/프로세스/사용자정보 - 공개 범위를 결정)

  • Union mount file system : 컨테이너 이미지를 효율적으로 관리

...

1.4.1

...

컨테이너

  • 컨테이너는 애플리케이션을 실행하는 데 사용될 수 있는 OS 가상화의 한 형태입니다

  • 이를 위해 컨테이너는 리눅스 커널의 몇 가지 새로운 기능으로 제작되었으며, 그 중 두 가지 주요 기능은 "namespace"와 "cgroups"입니다.

...

1.4.2

...

리눅스 네임스페이스

  • 네임스페이스는 리눅스 커널의 기능 중 하나이며 리눅스의 컨테이너의 기본적인 측면입니다.반면에 네임스페이스는 격리 계층을 제공합니다.네임스페이스는 한 프로세스 집합은 한 리소스 집합을 보고 다른 프로세스 집합은 다른 리소스 집합을 보도록 커널 리소스를 분할하는 리눅스 커널의 기능입니다.

...

코드 블럭
root@ubuntu-focal:~# exit
exit
root@ubuntu-focal:~# ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.8 102540  8840 ?        Ss   12:04   0:02 /sbin/init
root           2  0.0  0.0      0     0 ?        S    12:04   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   12:04   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   12:04   0:00 [rcu_par_gp]
root           6  0.0  0.0      0     0 ?        I<   12:04   0:00 [kworker/0:0H-kblockd]

...

1.4.3

...

Cgroup

리눅스 네임스페이스로 다른 프로세스와는 별도의 프로세스를 만들 수도 있었습니다.

...

정보

cgroups로 제어할 수 있는 일반적인 리소스는 다음과 같습니다.

  • CPU: 프로세스 그룹에서 사용하는 CPU 시간을 제한

  • 메모리: 프로세스 그룹에서 사용하는 메모리 양을 제한합니다제한합니다.

  • I/O: 프로세스 그룹에서 사용하는 Disk I/O 양 제한

  • 네트워크: 프로세스 그룹에서 사용하는 네트워크 대역폭의 양을 제한합니다제한합니다.

1.5 컨테이너의 장점

(1) 가벼움

  • 사용자의 Request Traffic 이 증가함에 따라, 가상머신이나 컨테이너를 추가적으로 배포합니다.

  • 가상머신의 크기는 최소 몇 GB이지만, 컨테이너의 경우 Guest OS가 없기에 MB단위의 크기를 가집니다.

  • 가상머신은 배포하는데 수분에서 수 십분의 시간이 소요되지만, 컨테이너는 배포에 소요되는 몇 초 밖에 걸리지 않습니다.

...

코드 블럭
$mkdir -p ~/vagrant/ubuntu
$cd ~/vagrant/ubuntu 
$vagrant init sysnet4admin/Ubuntu-k8s
$vagrant up

2.5.1. 기동 된 VM 확인

  1. vagrant 명령어로 확인

    1. $ vagrant box list

      image-20240514-034351.png
    2. 코드 블럭
      $ vagrant ssh default
      Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.19.0-28-generic x86_64)
      
       * Documentation:  https://help.ubuntu.com
       * Management:     https://landscape.canonical.com
       * Support:        https://ubuntu.com/advantage
      
       * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
         just raised the bar for easy, resilient and secure K8s cluster deployment.
      
         https://ubuntu.com/engage/secure-kubernetes-at-the-edge
      
      Last login: Sat Mar 20 18:04:46 2021 from 10.0.2.2
      vagrant@k8s:~$

2.5.2. Image 가져오기

(1) docker image 저장소

...

  • 기본 개념에서와 같이 도커는 저장소(registry)에서 이미지를 가져와 사용을 합니다.

코드 블럭
vagrant@ubuntu-focal:~$ sudo apt update 
vagrant@ubuntu-focal:~$ docker images
vagrant@ubuntu-focal:~$ sudo -i
root@ubuntu-focal:~# apt install docker.io

...

코드 블럭
저장소 이름 (Repository Name) : default == docker hub 
이미지 이름 (Image Name) : nginx
이미지 태그 (Image Tag) : lastest (default)

(4) 이미지 가지고 오기

코드 블럭
$ docker pull nginx:latest
$ docker images
코드 블럭
# tag를 지정해주지 않으면 default로 latest 버전을 가져옵니다.
# tag를 지정할 경우 
# docker pull nginx:latest 
# 위의 명령어 대로 pull 가능
root@ubuntu-focal:~# docker pull nginx:latest
latest: Pulling from library/nginx
52d2b7f179e3: Pull complete
fd9f026c6310: Pull complete
055fa98b4363: Pull complete
96576293dd29: Pull complete
a7c4092be904: Pull complete
e3b6889c8954: Pull complete
da761d9a302b: Pull complete
Digest: sha256:104c7c5c54f2685f0f46f3be607ce60da7085da3eaa5ad22d3d9f01594295e9c
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
# 이미지 확인
root@ubuntu-focal:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    eea7b3dcba7e   2 weeks ago   187MB

 

...

 

2.5.3. 컨테이너 기동

(1) 현재 실행 중인 컨테이너 확인

코드 블럭
# docker ps 라는 명령어로 현재 실행중인 컨테이너를 확인합니다.
# docker ps -a -> 중지된 컨테이너까지 모두 출력
root@ubuntu-focal:~# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

...

코드 블럭
# docker run 이라는 명령어로 컨테이너를 실행시켜줍니다.
# docker run <옵션> <이미지 이름> <실행할 파일>
root@ubuntu-focal:~# docker run -it -d -p 80:80 --name=nginx nginx:latest
1303feec17205609e6303e72831084babeaebdfaec3bf9bf0f6f4e4b39082dd1
# 옵션 -i(interactive), -t(Pseudo-tty) -> Bash Shell에 입력 및 출력을 할 수 있습니다.
# 옵션 --name -> 컨테이너의 이름을 지정해 줍니다.
# 옵션 -d -> daemonized
# 옵션 -p -> 포트포워딩
  ex) 80:80 = [호스트의 포트] : [컨테이너의 포트]
root@ubuntu-focal:~# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                   NAMES
1303feec1720   nginx:latest   "/docker-entrypoint.…"   14 seconds ago   Up 14 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx
root@ubuntu-focal:~# curl http://localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
정보

Docker docker create : 도커 이미지에서 새로운 컨테이너를 생성합니다. 그러나 즉시 실행되지는 않습니다

Docker docker start : 중지된 컨테이너를 시작합니다. docker create 명령을 사용하여 컨테이너를 만든 경우 이 명령으로 시작할 수 있습니다.

Docker docker run : create와 start의 조합으로 새 컨테이너를 생성하고 시작합니다. docker run 명령은 로컬 시스템에서 이미지를 찾지 못하는 경우 Docker Hub에서 이미지를 가져와서 컨테이너를 생성하고 실행합니다.

2.5.4. Container 내부 탐험

(1) exec 명령어를 이용한 컨테이너 접속

코드 블럭
root@ubuntu-focal:~# docker exec -it nginx /bin/bash
root@1303feec1720:/# hostname
root@1303feec1720:/# cat /etc/issue
root@1303feec1720:/# exit

# 혹시docker를 daemon으로 설치하지 않고, 아래와같이 bash로 들어갔다면
# exit로 나왔을 경우 container 도 쉘 종료메세지(exit 0)을 받고 자연스럽게 종료 되기때문에 docker start [컨테이더 ID ] 명령어로 재시작 혹은 ctrl+ pq 로 실행을 유지한 채 터미널로 빠져 나올 수 있습니다.
# docker run -it  -p 8090:80 --name=nginx80999 nginx:latest bash

...

2.5.5. Image 삭제

  • 보통은 이미지를 삭제하기 전 컨테이너를 먼저 삭제한 후 진행됩니다.

...

코드 블럭
root@ubuntu-focal:~# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: dncs0725
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded

 

2.5.6. Linux 서버에 웹서버 구축

(1) ubuntu 설치 & 패키지 업데이트

코드 블럭
## 1. ubuntu 설치 & 2. 패키지 업데이트
root@ubuntu-focal:~# apt-update

...

코드 블럭
root@ubuntu-focal:~# apt install nginx

#설치 확인
root@ubuntu-focal:~# nginx -v
nginx version: nginx/1.18.0 (Ubuntu)
정보

socket() [::]:80 failed 에러가 나면, /etc/nginx/sites-enabled/default 에서

listen [::]:80 default_server; 삭제

(3) Nginx 확인

  • nginx를 패키지를 통하여 설치 하였기에 Default directory가 /etc/nginx 아래에 위치하게 됩니다.

  • 직접 compile한 경우에 경로는 /usr/local/nginx/conf 혹은 /use/local/etc/nginx 에 위치합니다.

...

코드 블럭
# index.html 를 만든 후 찍어 봅니다.
root@ubuntu-focal:/etc/nginx# cat index.html
Hello~~ docker
root@ubuntu-focal:/etc/nginx# cp index.html /var/www/html/
root@ubuntu-focal:/etc/nginx# service nginx restart
root@ubuntu-focal:/etc/nginx# curl localhost
Hello~~ docker

2.5.7. Dockerfile을 통한 도커 이미지 생성

  • Docker File이란 Docker Image를 만들기 위한 여러가지 명령어의 집합

...

코드 블럭
cd /home/vagrant
vi Dockerfile

3/Dockerfile

코드 블럭
FROM ubuntu:20.04
MAINTAINER Hojin kim "khoj@osci.kr" 
RUN apt update
RUN apt install -y nginx
WORKDIR /etc/nginx 
CMD ["nginx", "-g", "daemon off;"]
EXPOSE 80 

...

이 밖에 사용되는 ENV VOLUME 같은 명령어는 공식문서 를 참고 바랍니다

2.5.8. 이미지 Build

(1) 작성한 Dockerfile을 빌드합니다.

...

참고로 명렁어를 주르륵 나열하기보단 최대한 간결하고 ‘&&’ 명령어를 이용해 줄여서 적어주는것이 좋습니다! (스토리지 엔진에 따라 이미지 개수가 제한되는 경우도 있기때문)

...

2.5.9. 이미지 태그

(1) 이미지 이름 구조

  • 저장소 주소는 기본적으로 Docker hub를 바라보고 있고, 사용자 ID를 지정해주지 않으면 기본으로 library를 사용합니다.

  • docker pull 명령어 시, 모두 같은 동작을 의미합니다.

...

코드 블럭
root@ubuntu-focal:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
mynginx      0.1       03de8991a4f8   11 minutes ago   178MB
ubuntu       20.04     6df894023726   5 weeks ago      72.8MB
root@ubuntu-focal:~# docker tag mynginx:0.1 mynginx:0.2
root@ubuntu-focal:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
mynginx      0.1       03de8991a4f8   12 minutes ago   178MB
mynginx      0.2       03de8991a4f8   12 minutes ago   178MB
ubuntu       20.04     6df894023726   5 weeks ago      72.8MB

 

성능이슈가 발생할수 있으므로, vagrant를 halt 시켜놓는다.

코드 블럭
root@k8s:/home/vagrant# exit
logout
vagrant@k8s:~$ exit
logout

$ vagrant halt -f default
==> default: Forcing shutdown of VM...

...

3. 컨테이너 Orchestration

Container Orchestration은 애플리케이션을 구성하고 있는 수십 또는 수백 개의 컨테이너와 호스트들을 배포하고 관리하기 위한 도구입니다.

...