[Docker] 4일차 수업 정리

soyeon·2022년 9월 14일
0
post-thumbnail

kill

linux kill : kill -9 [PID] -> session kill

docker kill : force shutdown(강제종료) SIGKILL
docker stop : graceful shutdown(정상종료) SIGTERM

kevin@hostos1:~$ docker run -it --name=myos7 ubuntu:14.04 bash
kevin@hostos1:~$ docker kill myos7
myos7

root@587549ea72b7:/# kevin@hostos1:~$ docker run -it --name=myos8 ubuntu:14.04 bash

## myos8 killed
root@a4cf3e9ce1d3:/# Killed

## linux kill
kevin@hostos1:~$ ps -ef | grep myos8 | grep -v grep
kevin       2667    2025  0 09:20 pts/0    00:00:00 docker run -it --name=myos8 ubuntu:14.04 bash
kevin@hostos1:~$ sudo kill -9 2667
[sudo] password for kevin:

## kill이 되지 않았다.
kevin@hostos1:~$ docker ps -a
CONTAINER ID   IMAGE                    COMMAND                  CREATED              STATUS                        PORTS                                         NAMES
a4cf3e9ce1d3   ubuntu:14.04             "bash"                   About a minute ago   Up About a minute                                                           myos8

save / export

: backup을 위해 사용한다.
: tar로 묶는 것은 image와 container가 계층구조로 되어 있기 때문이다.
: 인터넷이 되지 않는 상황에서는 usb로 옮겨야 하기 때문에 파일로 만들어진다.

  • save : image를 묶는다.

  • export : container를 묶는다.
    backup 할 때는 정적인 것을 backup 하는 것이 좋기 때문에 save를 사용하는 것이 좋다.
    save는 바로 docker run이 되지만 export는 바로 docker run이 되지 않는다.

  • container migration

## webserver container를 webserver.tar 파일로 저장
kevin@hostos1:~/save_lab$ docker run --name=webserver -d -p 9999:80 nginx:1.23.1
kevin@hostos1:~/save_lab$ docker export webserver > webserver.tar
kevin@hostos1:~/save_lab$ sudo scp webserver.tar kevin@hostos2:/home/kevin/backup/webserver.tar
kevin@hostos2:~/backup$ cat webserver.tar | docker import - myweb:3.0
sha256:bccc92b77403cc32ddbc97a4ee41fe2842c022b2ccb35f41f0463c3a58cc6874
kevin@hostos2:~/backup$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
myweb        3.0       bccc92b77403   4 seconds ago    140MB
phpserver    1.0       cfb59ea52b0d   11 minutes ago   410MB
kevin@hostos2:~/backup$ docker run -d --name=myweb3 -p 8002:80 myweb:3.0
docker: Error response from daemon: No command specified.
See 'docker run --help'.
  • image migration
kevin@hostos1:~$ mkdir save_lab && cd $_
kevin@hostos1:~/save_lab$ docker image save phpserver:1.0 > phpserver1.tar
kevin@hostos1:~/save_lab$ ls -lh
total 400M
-rw-rw-r-- 1 kevin kevin 400M  914 09:47 phpserver1.tar
kevin@hostos1:~/save_lab$ docker image save phpserver:1.0 | gzip > phpserver1.tar.gz
kevin@hostos1:~/save_lab$ ls -lh
total 539M
-rw-rw-r-- 1 kevin kevin 400M  914 09:47 phpserver1.tar
-rw-rw-r-- 1 kevin kevin 139M  914 09:48 phpserver1.tar.gz
kevin@hostos1:~/save_lab$ docker image save phpserver:1.0 | bzip2 > phpserver1.tar.bz2
kevin@hostos1:~/save_lab$ ls -lh
total 663M
-rw-rw-r-- 1 kevin kevin 400M  914 09:47 phpserver1.tar
-rw-rw-r-- 1 kevin kevin 125M  914 09:49 phpserver1.tar.bz2
-rw-rw-r-- 1 kevin kevin 139M  914 09:48 phpserver1.tar.gz
kevin@hostos1:~/save_lab$ scp phpserver1.tar.gz kevin@hostos2:/home/kevin/backup/phpserver1.tar.gz

kevin@hostos2:~/backup$ docker image load < phpserver1.tar.gz
kevin@hostos2:~/backup$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
phpserver    1.0       cfb59ea52b0d   5 minutes ago   410MB
kevin@hostos2:~/backup$ docker run -itd -p 8200:80 phpserver:1.0
8ebcd99a3271ee16b597dccef4da529c25fac18dce9da8b05ab59e60032d5a06
kevin@hostos2:~/backup$ curl localhost:8200
<html>
<body>
<div style="font-size:25px">
Container Name : 8ebcd99a3271<p> Welcome to the Container world~! </p>
</div>
</body>
</html>
  • bridge 연결 확인하기
## 설치하기
kevin@hostos1:~$ sudo apt install bridge-utils

## bridge가 하나이기 때문에 container를 만들때마다 docker0에 생긴다.
kevin@hostos1:~$ brctl show
bridge name     bridge id               STP enabled     interfaces
docker0         8000.0242001b5fe0       no              veth3949a37
                                                        vethd4abaab
                                                        vethe87f7cd
## container 띄우기
kevin@hostos1:~$ docker run -itd --name=myos5 ubuntu:14.04
35ebdcc72e1324a10c62bb112f87e6cfc9c660f89a268bb711fd5f33b4ea7a58

## 새로운 virtual eth가 생겼다(ifconfig 명령으로도 확인가능)
kevin@hostos1:~$ brctl show
bridge name     bridge id               STP enabled     interfaces
docker0         8000.0242001b5fe0       no              veth3949a37
                                                        vethc71fef1
                                                        vethd4abaab
                                                        vethe87f7cd

## ubuntu ip 조회하기1
kevin@hostos1:~$ docker exec -it myos5 ip addr
...
13: eth0@if14: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ac:11:00:05 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.5/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
       
## ubuntu ip 조회하기2
kevin@hostos1:~$ docker inspect myos5 | grep IPA
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.5",
                    "IPAMConfig": null,
                    "IPAddress": "172.17.0.5",

## routing table
kevin@hostos1:~$ docker exec -it myos5 route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         172.17.0.1      0.0.0.0         UG    0      0        0 eth0
172.17.0.0      *               255.255.0.0     U     0      0        0 eth0

kevin@hostos1:~$ docker run -itd --name=myos9 ubuntu:14.04
62cb44da57e4329b6b3fb452b793a9e8476f6f81f8ab08ead8f6ae1f05315370
kevin@hostos1:~$ brctl show
bridge name     bridge id               STP enabled     interfaces
docker0         8000.0242001b5fe0       no              veth089949a
                                                        veth3949a37
                                                        vethc71fef1
                                                        vethd4abaab
                                                        vethe87f7cd
## 어떤 container가 어떤 interface에 연결되었는지 알 수 없다.
kevin@hostos1:~$ docker exec -it myos9 ip a
...
15: eth0@if16: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ac:11:00:06 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.6/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
       
## eth0@16으로 확인할 수 있다.
kevin@hostos1:~$ sudo cat /sys/class/net/veth089949a/ifindex
16
kevin@hostos1:~$ docker exec -it myos9 cat /sys/class/net/eth0/iflink
16
  • add host
kevin@hostos1:~$ docker run -it --name=myos11 --add-host=hostos1:192.168.56.101 centos:7
[root@5102d65afd50 /]# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.56.101  hostos1
172.17.0.8      5102d65afd50
  • dns 설정
## dns 주소를 8.8.8.8로 설정하고 확인하기
kevin@hostos1:~$ docker run -it --name=myos12 --dns=8.8.8.8 centos:7 cat /etc/resolv.conf
nameserver 8.8.8.8
  • docker run -P 옵션
    알아서 expose된 port를 알아내고, 앞에는 아무 port를 붙인다.
## 알아서 post 번호 붙여주세요
kevin@hostos1:~$ docker run -d --name=webserver2 -P nginx:1.23.1-alpine
665bf91940b12b5399d586474905fafe8d6e44ded288c841b4b49beec9ee7498

## port 확인하기
kevin@hostos1:~$ docker port webserver2
80/tcp -> 0.0.0.0:49153
80/tcp -> :::49153
  • 관리자용 port 열기
## 방화벽에서 1000번 port도 열어주세요
kevin@hostos1:~$ docker run -d --name=webserver3 --expose=1000 -P nginx:1.23.1-alpine
69a7f53cf0db0d1c319ec9ff768a8f3af967ef5a07eff47b33be0c342d0d37d3

## port 확인하기
kevin@hostos1:~$ docker port webserver3
1000/tcp -> 0.0.0.0:49154
1000/tcp -> :::49154
80/tcp -> 0.0.0.0:49155
80/tcp -> :::49155
## 설치하기
kevin@hostos1:~$ sudo apt -y install iptraf-ng

## 실행하기
kevin@hostos1:~$ sudo iptraf-ng

kevin@hostos1:~$ docker run -it ubuntu:14.04 ping 192.168.56.1

pps : 초당 패킷 유입량

  • network host 기법
    container가 host에 붙어버린다. network를 host것을 쓴다.
    포트 포워딩 없이 바로 사용할 수 있다. 너무 많이 쓰면 host의 트래픽이 올라가게 된다.
## host 옵션 설정하기
kevin@hostos1:~$ docker run -d --name=hostnet --net=host nginx:1.23.1-alpine
92db34f6030bca6ef0bbb25893ae7b682e949043b25ce512171df523613210f5

## 브라우저에서 192.168.56.101로 접속하면 nginx가 연결된다.

## 포트 열려있는지 확인하기
kevin@hostos1:~$  sudo netstat -nlp | grep 80
...
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7953/nginx: master
...
tcp6       0      0 :::80                   :::*                    LISTEN      7953/nginx: master
...

## host에 붙었기 때문에 ip가 없다
kevin@hostos1:~$ docker inspect hostnet | grep IPA
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAMConfig": null,
                    "IPAddress": "",
  • 새로운 사용자 정의 bridge network 만들기
## terminal 1
## 새로운 bridge network 생성
kevin@hostos1:~$ docker network create web-net

## 확인하기
kevin@hostos1:~$ route
...
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-11d9023bb702
...

kevin@hostos1:~$ ifconfig
br-11d9023bb702: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 02:42:07:66:a0:1a  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
...

kevin@hostos1:~$ docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
9a5e07f53b88   bridge    bridge    local
dabe73bda74b   host      host      local
089668ed9531   none      null      local
11d9023bb702   web-net   bridge    local

kevin@hostos1:~$ brctl show
bridge name     bridge id               STP enabled     interfaces
br-11d9023bb702         8000.02420766a01a       no
docker0         8000.0242001b5fe0       no              veth089949a
                                                        veth10106da
                                                        veth3041511
                                                        veth3949a37
                                                        veth3d349d9
                                                        vethc71fef1
                                                        vethd4abaab
                                                        vethe87f7cd
                                                        vethecd8b92

## terminal 2
## 컨테이너를 web-net에서 실행
kevin@hostos1:~$ docker run --net=web-net -it --name=net-check1 ubuntu:14.04 bash

## ip 확인
root@355f158ca2c6:/# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:ac:12:00:02
          inet addr:172.18.0.2  Bcast:172.18.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:30 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:4021 (4.0 KB)  TX bytes:0 (0.0 B)

## route 명령어
root@355f158ca2c6:/# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         hostos1         0.0.0.0         UG    0      0        0 eth0
172.18.0.0      *               255.255.0.0     U     0      0        0 eth0

## terminal 3
kevin@hostos1:~$ brctl show
bridge name     bridge id               STP enabled     interfaces
br-11d9023bb702         8000.02420766a01a       no              veth373c1fd
docker0         8000.0242001b5fe0       no              veth089949a
                                                        veth10106da
                                                        veth3041511
                                                        veth3949a37
                                                        veth3d349d9
                                                        vethc71fef1
                                                        vethd4abaab
                                                        vethe87f7cd
                                                        vethecd8b92
                                                        
## Subnet 확인                                                       
kevin@hostos1:~$ docker network inspect web-net
[
    {
        "Name": "web-net",
        "Id": "11d9023bb7025e0931b1370222db89b29b29d57a8eb60603de6f0be181579742",
        "Created": "2022-09-14T12:07:18.62386487+09:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },

cloud, container 사용 시, CIDR(private network를 지향) 기법으로 IP 대역을 지정한다.
private network (RFC 1918 국제표준) 권고안
1) 10.0.0.0 ~ 10.255.255.255 -> 10.0.0.0/8
2) 172.16.0.0 ~ 172.31.0.0 -> 172.16.0.0/12
3) 192.168.0.0 ~ 192.168.255.255 -> 192.168.0.0/16

벗어나도 상관은 없다.

  • 새로운 사용자 정의 bridge network 만들기 2
    subnet이 ip-range보다 범위가 크다.
## vswitch-net 만들기
kevin@hostos1:~$ docker network create \
> --driver bridge \
> --subnet 172.30.1.0/24 \
> --ip-range 172.30.1.0/24 \
> --gateway 172.30.1.1 \
> vswitch-net

## bridge network가 생겼는지 확인하기
kevin@hostos1:~$ docker network ls
NETWORK ID     NAME          DRIVER    SCOPE
9a5e07f53b88   bridge        bridge    local
dabe73bda74b   host          host      local
089668ed9531   none          null      local
13500fdd939b   vswitch-net   bridge    local
11d9023bb702   web-net       bridge    local

## container 만들기
kevin@hostos1:~$ docker run --net=vswitch-net -itd --name=net1 ubuntu:14.04
## ip를 직접 지정할 수 있다.
kevin@hostos1:~$ docker run --net=vswitch-net -itd --name=net2 --ip 172.30.1.100 ubuntu:14.04

## vswitch-net 정보 확인하기
kevin@hostos1:~$ docker network inspect vswitch-net
...
 "Containers": {
            "15159759ef39245874f7433ade92a12bfc66dd57f1eda2274ec76876fc7971c7": {
                "Name": "net2",
                "EndpointID": "99d6595a6a395fe55d8a247a70954590faa759a500d7313c0a2b5ad5d34ae7ab",
                "MacAddress": "02:42:ac:1e:01:64",
                "IPv4Address": "172.30.1.100/24",
                "IPv6Address": ""
            },
            "1ea222c70c686df818e7fbbe92aa7b24b201736ef386e148ac941d9d6edb4ba4": {
                "Name": "net1",
                "EndpointID": "3719a4918571862edf263b2e0b1fc59612375468898d0da7883e69b7186270d4",
                "MacAddress": "02:42:ac:1e:01:02",
                "IPv4Address": "172.30.1.2/24",
                "IPv6Address": ""
            }
        },
...

## ip 확인
kevin@hostos1:~$ docker inspect net1 | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "172.30.1.2",

## ip 확인
kevin@hostos1:~$ docker inspect net2 | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "172.30.1.100",

같은 네트워크에 있는 컨테이너끼리는 서로를 알고 있다. (DNS 기능)
ping [컨테이터 이름] 가능하다.

kevin@hostos1:~/LABs$ docker run -it --name=appsrv3 --net=app-service ubuntu:14.04 bash

## ping 보내기
root@fdf64349add5:/# ping appsrv2
PING appsrv2 (172.19.0.3) 56(84) bytes of data.
64 bytes from appsrv2.app-service (172.19.0.3): icmp_seq=1 ttl=64 time=0.095 ms

kevin@hostos1:~$ docker run -it --name=appsrv2 --net=app-service ubuntu:14.04 bash

Container load balancer

: switch 장비와 같은 역할
container를 proxy로 바꿔서 사용할 수 있다.
proxy를 HAproxy로 바꿀 수 있다. / nginx, apache를 LB로 사용할 수 있다.

  1. docker container self LB
    : 자체적으로 내장된 DNS 서버(서비스)로 구현한다.
  • 구현 순서
    1) 사용자 정의 Bridge Network 생성
    2) --net-alias(target group : workload(트래픽)를 받을 서버(컨테이너)의 그룹)
    3) 자체 DNS 서비스 활성화 : 127.0.0.11(docker DNS = Service Discovery)
    : 컨테이너명을 ip로 바꿔준다.
    ㄴ dns-utils -> dig
## terminal 1
## 사용자 정의 Bridge Network 생성하기
kevin@hostos1:~$ docker network create \
> --driver bridge \
> --subnet 172.200.1.0/24 \
> --ip-range 172.200.1.0/24 \
> --gateway 172.200.1.1 \
> netlb

## 생성된 것 확인하기
kevin@hostos1:~$ docker network ls
9ab5a2927af1   netlb         bridge    local

## 생성된 것 확인하기
kevin@hostos1:~$ route
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.200.1.0     0.0.0.0         255.255.255.0   U     0      0        0 br-9ab5a2927af1
...

## 컨테이너 생성하기 --net-alias 설정
kevin@hostos1:~$ docker run -itd --name=lb-test1 --net=netlb --net-alias=tg-net ubuntu:14.04
kevin@hostos1:~$ docker run -itd --name=lb-test2 --net=netlb --net-alias=tg-net ubuntu:14.04
kevin@hostos1:~$ docker run -itd --name=lb-test3 --net=netlb --net-alias=tg-net ubuntu:14.04

## 생성된 컨테이너의 ip 확인
kevin@hostos1:~$ docker inspect lb-test1 | grep IPA
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAMConfig": null,
                    "IPAddress": "172.200.1.2",
kevin@hostos1:~$ docker inspect lb-test2 | grep IPA
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAMConfig": null,
                    "IPAddress": "172.200.1.3",
kevin@hostos1:~$ docker inspect lb-test3 | grep IPA
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAMConfig": null,
                    "IPAddress": "172.200.1.4",

## 새로운 컨테이너를 하나 더 만든다. 이번에는 --net-alias 설정 X
kevin@hostos1:~$ docker run -it --name=frontend --net=netlb ubuntu:14.04 bash

## tg-net 그룹에 ping을 보내면 랜덤으로 보내진다.
root@2e63d6fbe6c7:/# ping -c 2 tg-net
PING tg-net (172.200.1.3) 56(84) bytes of data.
64 bytes from lb-test2.netlb (172.200.1.3): icmp_seq=1 ttl=64 time=0.097 ms
64 bytes from lb-test2.netlb (172.200.1.3): icmp_seq=2 ttl=64 time=0.122 ms
--- tg-net ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1018ms
rtt min/avg/max/mdev = 0.097/0.109/0.122/0.016 ms

## 이번엔 다른 곳으로 보내진다.
root@2e63d6fbe6c7:/# ping -c 2 tg-net
PING tg-net (172.200.1.4) 56(84) bytes of data.
64 bytes from lb-test3.netlb (172.200.1.4): icmp_seq=1 ttl=64 time=0.152 ms
64 bytes from lb-test3.netlb (172.200.1.4): icmp_seq=2 ttl=64 time=0.118 ms
--- tg-net ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1004ms
rtt min/avg/max/mdev = 0.118/0.135/0.152/0.017 ms

## 업데이트 하고 dnsutils 받기
root@2e63d6fbe6c7:/# apt -y update
root@2e63d6fbe6c7:/# apt -y install dnsutils

## tg-net 정보가 자동으로 저장되어 있는 것 확인
root@2e63d6fbe6c7:/# dig tg-net
;; QUESTION SECTION:
;tg-net.                                IN      A

;; ANSWER SECTION:
tg-net.                 600     IN      A       172.200.1.3
tg-net.                 600     IN      A       172.200.1.2
tg-net.                 600     IN      A       172.200.1.4

## terminal 2
## 새로운 컨테이터 생성
kevin@hostos1:~$ docker run -itd --name=lb-test4 --net=netlb --net-alias=tg-net ubuntu:14.04

## terminal 1
## 자체 DNS가 172.200.1.5를 사용하기 때문에 172.200.1.6을 할당받는다.
root@2e63d6fbe6c7:/# dig tg-net
;; QUESTION SECTION:
;tg-net.                                IN      A

;; ANSWER SECTION:
tg-net.                 600     IN      A       172.200.1.6
tg-net.                 600     IN      A       172.200.1.4
tg-net.                 600     IN      A       172.200.1.2
tg-net.                 600     IN      A       172.200.1.3
  1. nginx container를 proxy로 전환하여 LB로 구성
## nginx 설치하기
kevin@hostos1:~$ sudo apt -y install nginx
kevin@hostos1:~$ sudo netstat -nlp | grep 80
kevin@hostos1:~$ sudo nginx -v
nginx version: nginx/1.18.0 (Ubuntu)

## active(running)이 나와야 한다.
kevin@hostos1:~$ sudo systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running)

## 이미지 가져오기
kevin@hostos1:~$ docker pull dbgurum/nginxlb:1.0

## 세개의 컨테이너 생성
kevin@hostos1:~$ docker run -itd -e SERVER_PORT=5001 -p 5001:5001 -h alb-node01 -u root --name=alb-node01 dbgurum/nginxlb:1.0
kevin@hostos1:~$ docker run -itd -e SERVER_PORT=5002 -p 5002:5002 -h alb-node02 -u root --name=alb-node02 dbgurum/nginxlb:1.0
kevin@hostos1:~$ docker run -itd -e SERVER_PORT=5003 -p 5003:5003 -h alb-node03 -u root --name=alb-node03 dbgurum/nginxlb:1.0

## nginx.conf 파일 수정하기
kevin@hostos1:~$ cd /etc/nginx/
kevin@hostos1:/etc/nginx$ sudo vi nginx.conf
events { worker_connections 1024; }
http{
   upstream backend-alb{
     server 127.0.0.1:5001;
     server 127.0.0.1:5002;
     server 127.0.0.1:5003;
   }
   server {
     listen 80 default_server;
   location / {
     proxy_pass  http://backend-alb;
     }
   }
}

## 다시 시작해서 설정 적용하기
kevin@hostos1:/etc/nginx$ sudo systemctl restart nginx.service
kevin@hostos1:/etc/nginx$ sudo systemctl status nginx.service

## http://192.168.56.101/ 접속해서 확인

## 실습 끝 제거
kevin@hostos1:~$ docker stop alb-node03 alb-node02 alb-node01
kevin@hostos1:~$ cexrm
kevin@hostos1:~$ sudo apt -y autoremove nginx
kevin@hostos1:~$ sudo netstat -nlp | grep 80
  1. [LAB]
kevin@hostos1:~/LABs$ cd docker-phpserver/

## Dockerfile 수정하기
kevin@hostos1:~/LABs/docker-phpserver$ vi Dockerfile
FROM php:7.2-apache

MAINTAINER datastory Hub <hylee@dshub.cloud>

ADD index.php3 /var/www/html/index.php

EXPOSE 80

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

## phpserver:2.0 이미지 생성
kevin@hostos1:~/LABs/docker-phpserver$ docker build -t phpserver:2.0 .

## 컨테이너 동작 테스트
kevin@hostos1:~/LABs/docker-phpserver$ docker run -it -d -p 8004:80 -h phpserver --name=phpserver phpserver:2.0
9d728646b059d1897c059ee8d1470132f0906e7706b14c13c7ad69578c48e749
kevin@hostos1:~/LABs/docker-phpserver$ docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS          PORTS                                       NAMES
9d728646b059   phpserver:2.0            "docker-php-entrypoi…"   3 seconds ago   Up 2 seconds    0.0.0.0:8004->80/tcp, :::8004->80/tcp       phpserver

## 컨테이터 정지하고, 삭제
kevin@hostos1:~/LABs/docker-phpserver$ docker stop 9d728646b059
kevin@hostos1:~/LABs/docker-phpserver$ cexrm

## apache-net 별도의 docker bridge network 생성
kevin@hostos1:~/LABs/docker-phpserver$ docker network create apache-net

## nginx container를 proxy 서버로 하기
kevin@hostos1:~/LABs/docker-phpserver$ docker run -d --name=frontend --net=apache-net -p 80:80 nginx:1.21-alpine

## 컨테이너 안의 nginx.cof를 가져온다.
kevin@hostos1:~$ docker cp frontend:/etc/nginx/nginx.conf nginx.conf

## 수정
kevin@hostos1:~$ vi nginx.conf
events { worker_connections 1024; }
http{
   upstream backend-alb{
     server 172.20.0.1:10001;
     server 172.20.0.1:10002;
     server 172.20.0.1:10003;
   }
   server {
     listen 80 default_server;
   location / {
     proxy_pass  http://backend-alb;
     }
   }
}

## 다시 가져다 놓기
kevin@hostos1:~$ docker cp nginx.conf frontend:/etc/nginx/nginx.conf

## 컨테이너 다시 시작
kevin@hostos1:/etc/nginx$ docker restart frontend

## phpserver:2.0 컨테이너 3개 구성
kevin@hostos1:~/LABs/docker-phpserver$ docker run -itd -e SERVER_PORT=10001 -p 10001:80 -h alb-php01 -u root --net=apache-net --name=alb-php01 phpserver:2.0
kevin@hostos1:~/LABs/docker-phpserver$ docker run -itd -e SERVER_PORT=10002 -p 10002:80 -h alb-php02 -u root --net=apache-net --name=alb-php02 phpserver:2.0
kevin@hostos1:~/LABs/docker-phpserver$ docker run -itd -e SERVER_PORT=10003 -p 10003:80 -h alb-php03 -u root --net=apache-net --name=alb-php03 phpserver:2.0

## 연결 확인
kevin@hostos1:~$ curl localhost:80

0개의 댓글