# curl -fsSL https://get.docker.com/ | sh
# yum -y install bash-completion wget unzip net-tools mysql telnet rdate
# rdate -s time.bora.net && clock -w
# curl https://raw.githubusercontent.com/docker/docker-ce/master/components/cli/contrib/completion/bash/docker -o /etc/bash_completion.d/docker.sh
# systemctl enable --now docker
- Volume
# docker volume create my-vol01
# docker volume list
# docker volume inspect my-vol01
"Mountpoint": "/var/lib/docker/volumes/my-vol01/_data"
# docker network list
# docker network inspect bridge
"com.docker.network.bridge.name": "docker0",
# docker network create new-net --subnet 10.14.0.0/16 --ip-range 10.14.0.0/20 --gateway 10.14.0.1
# docker network ls
# docker inspect new-net
--- ์ด์์ ์ญํ
# mkdir onbuild && cd $_
# vi Dockerfile.base
FROM ubuntu:18.04
RUN sed -i 's/archive.ubuntu.com/ftp.daumkakao.com/g' /etc/apt/sources.list
RUN apt-get -y update
RUN apt-get -y install nginx
EXPOSE 80
ONBUILD ADD website*.tar /var/www/html/
CMD ["nginx", "-g", "daemon off;"]
# docker build -t mj030kk/web-base:v2.0 -f Dockerfile.base .
# docker login
# docker push mj030kk/web-base:v2.0
# vi Dockerfile
FROM mj030kk/web-base:v2.0
--- ๊ฐ๋ฐ์ ์ญํ
# mkdir onbuild && cd $_
# ls
website.tar
Dockerfile
# docker build -t mj030kk/web-site:v2.0 . ##์ฌ๊ธฐ์ onbuild ์คํ๋จ.
# docker run -d -p 80:80 --name=web-site mj030kk/web-site:v2.0
# docker login
# docker push mj030kk/web-site:v2.0
AWS- EC2 ์์ฑ
-์ด๋ฆ: docker
-amazon linux
-t2.micro
-์ ํค ์์ฑ : docker-key2
#!/bin/bash
sudo amazon-linux-extras install docker -y
sudo systemctl start docker && systemctl enable docker
curl https://raw.githubusercontent.com/docker/docker-ce/master/components/cli/contrib/completion/bash/docker -o /etc/bash_completion.d/docker.sh
sudo usermod -a -G docker ec2-user
docker run -d -p 80:80 --name=test-site mj030kk/web-site:v2.0
docker run -d -p 5000:5000 --restart=always --name private-docker-registry registry
route53์์ ๋๋ฉ์ธ ์์ฑ (ec2 ์ธ์คํด์ค IP)
manager 1์์,
# vi /etc/docker/daemon.json # ํด๋ผ์ด์ธํธ
{ "insecure-registries":["docker.lovemj.shop:5000"] }
# systemctl restart docker
# docker tag mj030kk/web-site:v2.0 docker.lovemj.shop:5000/web-site:v2.0
#[root@localhost onbuild]# docker images
docker compose ๋ช ๋ น์ด ์ํ ์ yml ํ์ผ์ด ์๋ ๊ณณ์์ ํด์ผํจ.
# curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose
# mkdir my_wordpress && cd $_
# vi docker-compose.yml
version: "3.3"
services:
dbserver:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql ##๋ณผ๋ฅจ์ง์ ์๊ตฌ์ ๋ณผ๋ฅจ.
restart: always
environment: ##-e
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wpuser
MYSQL_PASSWORD: wppass
wordpress: ## ์น์๋ฒ
depends_on: ## ํ์์๋ก ์์ฑ. db์๋ฒ ๋จผ์ ์์ฑํด๋ผ.
- dbserver
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html
ports:
- "8888:80"
restart: always
environment:
WORDPRESS_DB_HOST: dbserver:3306
WORDPRESS_DB_USER: wpuser
WORDPRESS_DB_PASSWORD: wppass
WORDPRESS_DB_NAME: wordpress
volumes: ## ์ ์ธ
db_data: {}
wordpress_data: {}
# docker-compose up -d
# docker-compose ps
# docker volume ls
# docker inspect my_wordpress_db_data
# cd /var/lib/docker/volumes/my_wordpress_db_data/_data
[root@localhost _data]# ls
[root@localhost my_wordpress]# docker inspect my_wordpress_wordpress_data
[root@localhost my_wordpress]# cd /var/lib/docker/volumes/my_wordpress_wordpress_data/_data
[root@localhost _data]# ls
index.php wp-comments-post.php wp-includes wp-signup.php
license.txt wp-config-docker.php wp-links-opml.php wp-trackback.php
readme.html wp-config.php wp-load.php xmlrpc.php
wp-activate.php wp-config-sample.php wp-login.php
wp-admin wp-content wp-mail.php
wp-blog-header.php wp-cron.php wp-settings.php
# docker-compose pause ## ์ผ์์ ์ง
# docker-compose unpause ## ์ฌ์์
# docker-compose port wordpress 80 ##ํธ์คํธํฌํธ ๋ฒํธ ํ์ธ
# docker-compose config ## ymlํ์ผ ์ถ๋ ค์ ๋ณด์ฌ์ค
# docker-compose stop wordpress ## pause์ ๋น์ท.(์ผ์์ ์ง) stop - ์๋น์ค ์ง์ฐ๊ณ ์ถ์๋
# docker-compose ps
# docker-compose rm wordprss
# docker-compose down ## ๋ฉ์ถ๋ฉด์ ๋์์ ์ง์ฐ๊ธฐ.
<์ง์ ๋๋ฐ ๋ฐ์ดํฐ ๋จ์์์.>
[root@localhost my_wordpress]# cd /var/lib/docker/volumes/my_wordpress_wordpress_data/_data
[root@localhost _data]# ls
index.php wp-comments-post.php wp-includes wp-signup.php
license.txt wp-config-docker.php wp-links-opml.php wp-trackback.php
readme.html wp-config.php wp-load.php xmlrpc.php
wp-activate.php wp-config-sample.php wp-login.php
wp-admin wp-content wp-mail.php
wp-blog-header.php wp-cron.php wp-settings.php
[root@localhost _data]# cd /var/lib/docker/volumes/my_wordpress_db_data/_data
[root@localhost _data]# ls
auto.cnf client-key.pem ib_logfile1 private_key.pem sys
ca-key.pem ib_buffer_pool mysql public_key.pem wordpress
ca.pem ibdata1 mysql.sock server-cert.pem
client-cert.pem ib_logfile0 performance_schema server-key.pem
[root@localhost my_wordpress]# docker-compose up -d
๋๊ธ ๊ทธ๋๋ก
# docker-compose down -v ##๋ณผ๋ฅจ,์ปจํ
์ด๋๊น์ง ์ญ์
# docker-compose down --rmi all ## ์ด๋ฏธ์ง๊น์ง ์ญ์
//(์ฃผ์) use the latest release version from https://github.com/google/cadvisor/releases
[root@localhost my_wordpress]# VERSION=v0.44.0
[root@localhost my_wordpress]# docker run \
--volume=/:/rootfs:ro \
--volume=/var/run:/var/run:ro \
--volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--volume=/dev/disk/:/dev/disk:ro \
--publish=8080:8080 \
--detach=true \
--name=cadvisor \
--privileged \
--device=/dev/kmsg \
gcr.io/cadvisor/cadvisor:$VERSION
[root@localhost my_wordpress]# docker ps
shares ; 1024; CPU2๊ฐ
512 ; CPU1๊ฐ
# firewall-cmd --permanent --zone=public --add-port=2377/tcp
# firewall-cmd --reload
? ์ด๊ฑฐ๋ญ์ง
์ปดํฌ์ฆ์ ์ค์์ ๋ํ๋ฉด ์ฟ ๋ฒ๋คํฐ์ค.
worker 1, 2 ๋งฅ address ์๋ก๊ณ ์นจํด์ฃผ๊ณ
์์คํ
๊ธฐ๋ณธ๋ฉ๋ชจ๋ฆฌ : 1024
ํ๋ก์ธ์ 1๊ฐ
์์.
multiexecution์ผ๋ก ์๋ ๋ช ๋ น์ด 3๊ฐ ์๋ฒ์ ์ ๋ ฅ.(๋งค๋์ ,์์ปค1,2)
# cat <<EOF >> /etc/hosts
192.168.0.180 manager1
192.168.1.139 worker1
192.168.1.144 worker2
EOF
๊ฐ ์๋ฒ์ ํธ์คํธ๋ค์ ๋ฐ๊ฟ์ฃผ๊ธฐ
# hostnamectl set-hostname master1
๊ฐ ์๋ฒ ์ด๋ฆ์ผ๋ก ํ ๋๊ฐ ํ์ธ (hosts์ ๋ฑ๋กํด๋์๊ธฐ ๋๋ฌธ.)
manager1์์ ์๋ ๋ช ๋ น์ด ์งํ.
# docker swarm init --advertise-addr 192.168.0.180
docker swarm join --token SWMTKN-1-3ma85wdp4elgdw5qnvl6ydang2g1562vqgocjh3f8oapdn4ql5-dk98lw7wk2utg8ilqh4bm8tcc 192.168.0.180:2377
-> ์ด ๋ด์ฉ ๋ณต์ฌ
๋ณต์ฌํ ๊ฒ์ worker1, 2 ์ ๋ถ์ฌ๋ฃ๊ธฐ.
# docker swarm join --token SWMTKN-1-3ma85wdp4elgdw5qnvl6ydang2g1562vqgocjh3f8oapdn4ql5-dk98lw7wk2utg8ilqh4bm8tcc 192.168.0.180:2377
๋ค์ manager1์์ ์งํ.
[root@manager1 ~]# docker node ls
[root@manager1 ~]# docker stop cadvisor
# docker service create --name my_web --replicas 3 --publish published=8080,target=80 nginx
## replicas ; desired State control(manager1)๊น์ง ํฌํจํด์ ์ด 3๊ฐ ? ์ดํด ์ ๋ชปํจ
# docker service ls
# docker service ps my_web
[root@worker1 ~]# docker ps
worker 1,2 ip๋ก ์ ์ํด๋ ์น๋ธ๋ผ์ฐ์ ์ ์ ๊ฐ๋ฅ. ๋ก๋๋ฐธ๋ฐ์ฑ ๋๊ณ ์์.
๋ก๋๋ฐธ๋ฐ์ฑ ํ์ธํด๋ณด์. (worker1,2์๋ ์ปจํ ์ด๋ ID๋ ๋ฌธ๊ตฌ๋ง ๋ฐ๊ฟ์ ์งํ)
[root@manager1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
696989f42e52 nginx:latest "/docker-entrypoint.โฆ" 14 minutes ago Up 14 minutes 80/tcp my_web.1.k83kmvxad8v7dakwg26q7am95
8e1189d78f48 mj030kk/web-site:v2.0 "nginx -g 'daemon ofโฆ" 5 hours ago Up 3 hours 0.0.0.0:80->80/tcp, :::80->80/tcp web-site
[root@manager1 ~]# docker exec 696989f42e52 sh -c "echo "manager1" >> /usr/share/nginx/html/index.html"
์น๋ธ๋ผ์ฐ์ ๋ ๋ผ์ด๋๋ก๋น ์๋.
curl 192.168.0.180:8080 ์ผ๋ก ํ๋ฉด ๋ผ์ด๋๋ก๋น์ฒ๋ผ ๋ณด์.
workerIP๋ก ํด๋ ๋จ.
-> ์๋ ๋๋ฌธ.
Endpoint Mode: vip
Ports:
PublishedPort = 8080
Protocol = tcp
TargetPort = 80
PublishMode = ingress
[root@manager1 ~]# docker service logs my_web ##์ ์ ๋ก๊ทธ ํ์ธ
# docker service inspect --pretty my_web
# docker service scale my_web=5 //3๊ฐ์ ์ปจํ
์ด๋ 5๊ฐ๋ก scale out
# docker service ps my_web
->worker 2๊ฐ์ฉ manager1๊ฐ ๋ฐ์. ๋ง์ฝ 6๊ฐ๋ก ๋๋ ธ์ผ๋ฉด 2, 2, 2 ๋ฑ ๋ง์์ ๊ฒ.(์ค์ผ์ค๋ง. ๊ณตํํ๊ฒ ๋ฐฐ๋ถ)
๋กค๋ง ์ ๋ฐ์ดํธ (์ฌ์ดํธ ์ ๋ฐ์ดํธ ํ ๋)
[root@manager1 ~]# docker service update --image halilinux/web-site:v1.0 my_web
[root@manager1 ~]# docker service ps my_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
kqwjeym181kz my_web.1 mj030kk/web-site:v1.0 manager1 Running Running 4 minutes ago
k83kmvxad8v7 \_ my_web.1 nginx:latest manager1 Shutdown Shutdown 4 minutes ago
7v45oe4ox8r9 my_web.2 mj030kk/web-site:v1.0 worker1 Running Running 3 minutes ago
dpg9rrlx3si6 \_ my_web.2 nginx:latest worker1 Shutdown Shutdown 3 minutes ago
0rkebziyy49k my_web.3 mj030kk/web-site:v1.0 worker2 Running Running 4 minutes ago
o70dmhuah8y5 \_ my_web.3 nginx:latest worker2 Shutdown Shutdown 4 minutes ago
jbp6p6u6obwm my_web.4 mj030kk/web-site:v1.0 worker2 Running Running 4 minutes ago
ztuc693rg6go \_ my_web.4 nginx:latest worker2 Shutdown Shutdown 4 minutes ago
ooxls9ox3oxj my_web.5 mj030kk/web-site:v1.0 worker1 Running Running 3 minutes ago
adfphwdtjzui \_ my_web.5 nginx:latest worker1 Shutdown Shutdown 3 minutes ago
1bu36ldivfer my_web.6 mj030kk/web-site:v1.0 manager1 Running Running 4 minutes ago
kgn76j3t7gx3 \_ my_web.6 nginx:latest manager1 Shutdown Shutdown 4 minutes ago
์ด์ ์๋ฒ๋ก ๋์๊ฐ์ผํ ๋ (๋ฒ์ ๊ด๋ฆฌ)
[root@manager1 ~]# docker service rollback my_web
[root@manager1 ~]# docker service rm my_web // ํ๋ฒ ์ ๋ฆฌ
[root@manager1 ~]# docker service create --name my_web --replicas 3 --publish published=8080,target=80 mj030kk/web-site:v1.0
task ์ฌ์์ฑ
# docker node ls
# docker service ps my_web
# docker node update --availability drain worker1
//worker1์์ ๋ค๋ฅธ ๊ณณ์ผ๋ก ๋์ด๊ฐ (manager1 or worker2)
[root@manager1 ~]# docker rm -f 79ac791c65b4
๋งค๋์ ๋ก ํ ๋น๋ฐ์๊ฑฐ ์ญ์ ํด๋ณด๊ธฐ
[root@manager1 ~]# docker service ps my_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
oh22zjyze4u8 my_web.1 mj030kk/web-site:v1.0 worker2 Running Running 17 seconds ago
rl6w61gufuch \_ my_web.1 mj030kk/web-site:v1.0 manager1 Shutdown Failed 23 seconds ago "task: non-zero exit (137)"
hxrwh7lna8ib \_ my_web.1 mj030kk/web-site:v1.0 worker1 Shutdown Shutdown 4 minutes ago
mxte7mbor29q my_web.2 mj030kk/web-site:v1.0 worker2 Running Running 6 minutes ago
87g9gutjz9zy my_web.3 mj030kk/web-site:v1.0 manager1 Running Running 6 minutes ago
->์ด๋ฒ์ replicas๋ก ์ธํด worker2์ ํ ๋น๋ฐ์.
# docker node inspect --pretty worker1
Availability: Drain
# docker service ps my_web
[root@manager1 ~]# docker node ls
# docker node update --availability active worker1
[root@manager1 ~]# docker service ps my_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
oh22zjyze4u8 my_web.1 mj030kk/web-site:v1.0 worker2 Running Running 3 minutes ago
rl6w61gufuch \_ my_web.1 mj030kk/web-site:v1.0 manager1 Shutdown Failed 3 minutes ago "task: non-zero exit (137)"
hxrwh7lna8ib \_ my_web.1 mj030kk/web-site:v1.0 worker1 Shutdown Shutdown 7 minutes ago
mxte7mbor29q my_web.2 mj030kk/web-site:v1.0 worker2 Running Running 9 minutes ago
87g9gutjz9zy my_web.3 mj030kk/web-site:v1.0 manager1 Running Running 9 minutes ago
-> ๋ค์ active ํด๋ ์๋์ผ๋ก worker1์๊ฒ ํ ๋นํ์ง ์์.
[root@manager1 ~]# docker service scale my_web=3
-> scale 3 ๋ค์ํด๋ ๋ง์ฐฌ๊ฐ์ง.
[root@manager1 ~]# docker service scale my_web=2
-> ์ค์ผ์ผ ์ค์๋ค๊ฐ
[root@manager1 ~]# docker service scale my_web=3
my_web scaled to 3
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
[root@manager1 ~]# docker service ps my_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
oh22zjyze4u8 my_web.1 mj030kk/web-site:v1.0 worker2 Running Running 7 minutes ago
rl6w61gufuch \_ my_web.1 mj030kk/web-site:v1.0 manager1 Shutdown Failed 8 minutes ago "task: non-zero exit (137)"
hxrwh7lna8ib \_ my_web.1 mj030kk/web-site:v1.0 worker1 Shutdown Shutdown 12 minutes ago
hwthalyjbv49 my_web.2 mj030kk/web-site:v1.0 worker1 Running Running 6 seconds ago
87g9gutjz9zy my_web.3 mj030kk/web-site:v1.0 manager1 Running Running 14 minutes ago
-> ๋๋ฆฌ๋ฉด ๋ค์ ๊ฐ ๋
ธ๋์ ํ ๋น.
pause์ผ์์ ์ง
[root@manager1 ~]# docker node update --availability pause worker2
worker2
[root@manager1 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
96zkn9063w151ousxzg0ia1is * manager1 Ready Active Leader 20.10.17
pt276x5gbdg9btk6hjslxbz93 worker1 Ready Active 20.10.17
acg4rud6as7wh2l4cxw40ikkr worker2 Ready Pause 20.10.17
[root@manager1 ~]# docker service scale my_web=5
my_web scaled to 5
overall progress: 5 out of 5 tasks
1/5: running
2/5: running
3/5: running
4/5: running
5/5: running
verify: Service converged
-> scale out ํ๋ฉด,
[root@manager1 ~]# docker service ps my_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
oh22zjyze4u8 my_web.1 mj030kk/web-site:v1.0 worker2 Running Running 25 minutes ago
rl6w61gufuch \_ my_web.1 mj030kk/web-site:v1.0 manager1 Shutdown Failed 25 minutes ago "task: non-zero exit (137)"
hxrwh7lna8ib \_ my_web.1 mj030kk/web-site:v1.0 worker1 Shutdown Shutdown 29 minutes ago
hwthalyjbv49 my_web.2 mj030kk/web-site:v1.0 worker1 Running Running 17 minutes ago
87g9gutjz9zy my_web.3 mj030kk/web-site:v1.0 manager1 Running Running 31 minutes ago
3ya52u1zjwlv my_web.4 mj030kk/web-site:v1.0 worker1 Running Running 8 seconds ago
nnsecu0may7l my_web.5 mj030kk/web-site:v1.0 manager1 Running Running 10 seconds ago
->worker 2 ์๋ ์์ฑ๋์ง ์์.(์ฌ๋์ค!)
๐โ๏ธโ๏ธ๐ขโญ๏ธ๐