[GCP] Google Cloud 생성

https://console.cloud.google.com/?hl=ko

최초 사용시 12개월간 사용할수 있는 크레딧 $300 달러를 줍니다.

준비물 : Google 계정 , 신용카드

무료사용이 끝난후 사용료가 부과되지 않습니다 🙂

무료평가판 신청을 클릭 합니다.

사용자 등록정보 입력후 아래와 같은 화면을 볼수 있습니다.

My First Project 를 클릭합니다.

 

+버튼을 클릭하여 프로젝트를 생성합니다.

 

프로젝트 이름을 기입 합니다.

ex) docker-test

 

중간메뉴에 Cloud Launcher 를 클릭합니다.

 

Cloud Launcher 를 클릭하면 아래와 같은 화면을 볼수 있습니다.

운영체체 부분에 Ubuntu Trusty 를 클릭합니다.

 

 

 

COMPUTE ENGINE에서 실행을 클릭하여 VM 인스턴스를 생성합니다.

약 3~5분후 아래와 같은 화면을 볼수 있습니다.

vCPU 의 경우 사용량에 따라 선택 하시면 됩니다.

테스트로 만들기 위하여 초소형(공유 vCPU 1개) 0.6GB , f1-micro 를 선택 합니다.

영역에서 리전을 선택 할수 있습니다. asia-northeast1 도쿄 리전을 선택 합니다.

리전확인 참고: https://cloud.google.com/about/locations/?hl=ko

체크항목

모든 Cloud API에 대한 전체 액세스 허용

HTTP 트래픽 허용

HTTPS 트래픽 허용

관리, 디스크, 네트워킹, SSH 키 를 눌러 세부설정을 할수 있습니다.

만들기 버튼을 클릭합니다.

 

IP 부분의 경우 보안상 삭제 하였습니다. 

브라우저 창에서 열기를 클릭 합니다.

 

패키지 설치시 apt-get / apt 명령어를 사용하기 위하여 sudo passwrd 로 root 패스워드를 설정합니다.

test@ubuntu-trusty-1:~$ sudo passwd
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
test@ubuntu-trusty-1:~$

 

sudo apt update 를 테스트 합니다.

 

docker swarm mode 구성

description:

To use Docker in swarm mode, install Docker. See installation instructions for all operating systems and platforms.

Current versions of Docker include swarm mode for natively managing a cluster of Docker Engines called a swarm. Use the Docker CLI to create a swarm, deploy application services to a swarm, and manage swarm behavior.

If you are using a Docker version prior to 1.12.0, you can use standalone swarm, but we recommend updating.

official page: https://docs.docker.com/engine/swarm/

 

vm 준비

docker-manager 192.168.192.10

docker-worker01 192.168.192.20

docker-worker02 192.168.192.30

3대의 vm 에는 docker가 설치 되어 있습니다.

Docker-manager
test@docker-manager:~$ docker -v
Docker version 18.02.0-ce, build fc4de44
test@docker-manager:~$

Docker-worker01
test@docker-worker01:~$ docker -v
Docker version 18.02.0-ce, build fc4de44
test@docker-worker01:~$

Docker-worker02
test@docker-worker02:~$ docker -v
Docker version 18.02.0-ce, build fc4de44
test@docker-worker02:~$

 

1. Create Docker Swarm cluster

docker-manager 에서 작업

command : docker swarm init –advertise-addr $docker-manager ip

test@docker-manager:~$ docker swarm init --advertise-addr 192.168.192.10
Swarm initialized: current node (mzsiydsvbyhafre4cppmfupvb) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-5ar8gen44ij5gi7hlkmbbhdji2e06nvod2u86e5db7zwip9707-8f5nxu48rh841xdcj0na1vp0s 192.168.192.10:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

test@docker-manager:~$

 

docker manager node 확인

test@docker-manager:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
mzsiydsvbyhafre4cppmfupvb *   docker-manager      Ready               Active              Leader
test@docker-manager:~$

test@docker-manager:~$ docker info
Containers: 1
~중략
Swarm: active
 NodeID: mzsiydsvbyhafre4cppmfupvb
 Is Manager: true
 ClusterID: rnqp34pegl5rj9fqd1ex30x0h
 Managers: 1
 Nodes: 1
 Orchestration:
  Task History Retention Limit: 5
 Raft:
  Snapshot Interval: 10000
  Number of Old Snapshots to Retain: 0
  Heartbeat Tick: 1
  Election Tick: 3
 Dispatcher:
  Heartbeat Period: 5 seconds
 CA Configuration:
  Expiry Duration: 3 months
  Force Rotate: 0
 Autolock Managers: false
 Root Rotation In Progress: false
 Node Address: 192.168.192.10
 Manager Addresses:
  192.168.192.10:2377
~중략

join-token 생성

docker-manager host 에서 작업

test@docker-manager:~$ docker swarm join-token worker
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-5ar8gen44ij5gi7hlkmbbhdji2e06nvod2u86e5db7zwip9707-8f5nxu48rh841xdcj0na1vp0s 192.168.192.10:2377

test@docker-manager:~$

 

 

docker-worker swarm 등록

docker-manager에서 생성한 join-token을 이용하여 worker node 를 등록 합니다.

docker-worker01 host 에서 작업

test@docker-worker01:~$ docker swarm join  --token SWMTKN-1-5ar8gen44ij5gi7hlkmbbhdji2e06nvod2u86e5db7zwip9707-8f5nxu48rh841xdcj0na1vp0s 192.168.192.10:2377
This node joined a swarm as a worker.
test@docker-worker01:~$ docker info |grep -i swarm
WARNING: No swap limit support
Swarm: active

 

docker-worker02 host 에서 작업

test@docker-worker02:~$ docker swarm join  --token SWMTKN-1-5ar8gen44ij5gi7hlkmbbhdji2e06nvod2u86e5db7zwip9707-8f5nxu48rh841xdcj0na1vp0s 192.168.192.10:2377
This node joined a swarm as a worker.
test@docker-worker02:~$ docker info |grep -i swarm
WARNING: No swap limit support
Swarm: active
test@docker-worker02:~$

 

docker-manager node 확인

test@docker-manager:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
mzsiydsvbyhafre4cppmfupvb *   docker-manager      Ready               Active              Leader
jvitfs6bmgr699p3nltzok0fw     docker-worker01     Ready               Active
hwkew1ktumxynqhqexjx5gn8l     docker-worker02     Ready               Active
test@docker-manager:~$

 

nginx test

docker run 의 경우 아래와 같이 사용하지만 docker swarm 의 경우 docker service create 명령어를 사용합니다.

docker run 실행시는 아래와 같이 사용 합니다. docker swarm 에서는 docker service create 명령어를 사용합니다. 🙂

test@docker-test:~$ docker run --name web-service -d -p 80:80 -p 443:443 nginx

 

docker service create 명령어

test@docker-manager:~$ docker service create --name web-service -p 80:80 -p 443:443 nginx
pgdc990uyesjmw399ayuraie9
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged
test@docker-manager:~$

 

!!! docker service create 명령어 사용시 –publish 옵션을 사용해야 합니다. 

test@docker-master:~$ docker service create --name web-service --publish 80:80 nginx
md8pzk0owb4s8ida55voqzlrb
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged
test@docker-master:~$

 

 

service 확인

test@docker-manager:~$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
pgdc990uyesj        web-service         replicated          1/1                 nginx:latest        *:80->80/tcp, *:443->443/tcp
test@docker-manager:~$ docker service ps web-service
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
wd1vyqp9eg2x        web-service.1       nginx:latest        docker-manager      Running             Running 29 seconds ago
test@docker-manager:~$

 

 

docker service scale 명령어를 사용하여 service 개수를 늘려 줍니다.

service 갯수를 줄일때도 동일하게 scale을 사용 합니다.

test@docker-manager:~$ docker service scale web-service=3
web-service scaled to 3
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service converged
test@docker-manager:~$


test@docker-manager:~$ docker service ps web-service
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
wd1vyqp9eg2x        web-service.1       nginx:latest        docker-manager      Running             Running 20 minutes ago
bezi55mn78lf        web-service.2       nginx:latest        docker-worker01     Running             Running 42 seconds ago
vwpxbid1ez53        web-service.3       nginx:latest        docker-worker02     Running             Running 42 seconds ago
test@docker-manager:~$

 

docker service rm 

swarm 에서 사용중인 컨테이너 삭제시는 docker servier rm $service_name 으로 삭제 할수 있습니다.

test@docker-manager:~$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
pgdc990uyesj        web-service         replicated          1/1                 nginx:latest        *:80->80/tcp, *:443->443/tcp
test@docker-manager:~$ docker service rm web-service
web-service
test@docker-manager:~$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
test@docker-manager:~$

 

 

nginx-proxy multi-wordpress site

 

Site:https://www.draw.io

Nginx-proxy 를 이용하여 여러대의 wordpress container 사용할수 있습니다.

Nginx-proxy container 는 jwilder/nginx-proxy image 를 이용합니다.

 

Github

test@docker-test:~$ git clone https://github.com/visualwork/Docker-test.git

/Docker-test/test05 에 있습니다. 🙂

 

Nginx-proxy docker-compose.yml 파일 생성

test@docker-test:~$ mkdir -p web-service/nginx-proxy
test@docker-test:~$ cd web-service/nginx-proxy/
test@docker-test:~/web-service/nginx-proxy$ vi docker-compose.yml
## notice ##  $ docker network create nginx-proxy
version: "3"

services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
    ports:
      - "80:80"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
    restart: always

networks:
  default:
    external:
      name: nginx-proxy

 

Docker network 생성

Nginx-proxy container 구동전 docker network create commnad 를 이용하여 docker network 를 생성해야 합니다.

test@docker-test:~/web-service/nginx-proxy$ docker network create nginx-proxy
a5955ea1067f169fd1849ecd4c959c83319183dcb39188ddffe125423f9e5322
test@docker-test:~/web-service/nginx-proxy$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
e64ced1895e7        bridge              bridge              local
a2b1f4b8cfba        host                host                local
a5955ea1067f        nginx-proxy         bridge              local
bb45871e893c        none                null                local
test@docker-test:~/web-service/nginx-proxy$

 

 

WordPress container

blog1 docker-compose.yml 파일 생성

container 사용시 db가 저장될 디렉토리와 web 이 저장될 디렉토리를 생성 합니다.

test@docker-test:~/web-service$ mkdir blog1
test@docker-test:~/web-service$ cd blog1/
test@docker-test:~/web-service/blog1$ mkdir web-data
test@docker-test:~/web-service/blog1$ mkdir db-data
test@docker-test:~/web-service/blog1$ tree
.
├── db-data
└── web-data

 

docker-compose 파일 생성

도메인의 경우 test.com 을 Test dns-server 로 미리 준비해 놓았습니다.

test@docker-test:~/web-service/blog1$ vi docker-compose.yml
version: "3"

services:
  db:
     image: mariadb
     volumes:
       - ./db-data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: test1234
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: test1234
     container_name: wp_blog_db

  wordpress:
     image: wordpress:latest
     expose:
       - 80
     restart: always
     volumes:
       - ./web-data:/var/www/html
     environment:
       VIRTUAL_HOST: blog1.test.com
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: test1234
     container_name: wp_blog

networks:
  default:
    external:
      name: nginx-proxy

VIRTUAL_HOST 부분에 도메인을 설정해 줍니다. ex) blog1.test.com

 

Nginx-proxy 와 blog Wordprss 컨테이너를 구동 합니다.

Nginx-proxy 컨테이너 실행

test@docker-test:~/web-service$ cd ..
test@docker-test:~$ cd web-service/nginx-proxy/
test@docker-test:~/web-service/nginx-proxy$ docker-compose up -d --build
Pulling nginx-proxy (jwilder/nginx-proxy:latest)...
latest: Pulling from jwilder/nginx-proxy
e7bb522d92ff: Pull complete
6edc05228666: Pull complete
cd866a17e81f: Pull complete
d9f2d6a1f8f6: Pull complete
e9c7e986c8c1: Pull complete
a51bcd518fd9: Pull complete
66df98413ed2: Pull complete
aff8c6473b42: Pull complete
1c91fd608be1: Pull complete
7319453a5fbe: Pull complete
Digest: sha256:41506b2095779e6e64f34e26ccba35cb3668ee56a735cd740ac8c183af583294
Status: Downloaded newer image for jwilder/nginx-proxy:latest
Creating nginx-proxy ... done
test@docker-test:~/web-service/nginx-proxy$ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                NAMES
2b9135ebe937        jwilder/nginx-proxy   "/app/docker-entrypo…"   4 seconds ago       Up 3 seconds        0.0.0.0:80->80/tcp   nginx-proxy
test@docker-test:~/web-service/nginx-proxy$

이제부터 시스템의 모든 80 Port 의 경우 Nginx-proxy 에서 받아 들이게 됩니다.

 

blog1 컨테이너 실행

test@docker-test:~$ cd web-service/blog1/
test@docker-test:~/web-service/blog1$ docker-compose up -d --build
Pulling wordpress (wordpress:latest)...
latest: Pulling from library/wordpress
8176e34d5d92: Pull complete
f6c81892adaa: Pull complete
c8125c73b868: Pull complete
5ef22f6299b6: Pull complete
8537460e9a8c: Pull complete
b837671b83f0: Pull complete
366ea9d8b411: Pull complete
c4dd539af472: Pull complete
445753fb3ee6: Pull complete
6811f6b5d500: Pull complete
2ca365cdc65d: Pull complete
c91023a57f04: Pull complete
dcf0735fda8a: Pull complete
6b23fdc3538b: Pull complete
2eed880b86fe: Pull complete
798b2e547e59: Pull complete
159be43511e4: Pull complete
b5605b08f665: Pull complete
20d8e9ec2764: Pull complete
Digest: sha256:670e4156377063df1a02f036354c52722de0348d46222ba30ef6a925c24cd46a
Status: Downloaded newer image for wordpress:latest
Pulling db (mariadb:latest)...
latest: Pulling from library/mariadb
4176fe04cefe: Pull complete
d1e86691d483: Pull complete
c3f621b14a80: Pull complete
7b3f1b17db07: Pull complete
add1151625d9: Pull complete
d40e879009a8: Pull complete
0f888e49f29b: Pull complete
bd51d7af1551: Pull complete
6cf37448a3b3: Pull complete
0519e00d29e5: Pull complete
8f83e3487101: Pull complete
Digest: sha256:d5f0bc88ba397233677ff75b7b1de693d5e84527ecf2b4f59adebf8d0bcac3c4
Status: Downloaded newer image for mariadb:latest
Creating wp_blog ... done
Creating wp_blog_db ...
test@docker-test:~/web-service/blog1$ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                NAMES
e83e15350e05        wordpress:latest      "docker-entrypoint.s…"   4 seconds ago       Up 2 seconds        80/tcp               wp_blog
ce6d5afb2986        mariadb               "docker-entrypoint.s…"   4 seconds ago       Up 4 seconds        3306/tcp             wp_blog_db
2b9135ebe937        jwilder/nginx-proxy   "/app/docker-entrypo…"   3 minutes ago       Up 3 minutes        0.0.0.0:80->80/tcp   nginx-proxy
test@docker-test:~/web-service/blog1$

 

web browser  접속

http://blog1.test.com 으로 접속합니다.

blog1 컨테이너 구성이 끝났습니다. Wordprss 설정을 한후 blog2 컨테이너를 구성 합니다.

 

 

blog2 docker-compose.yml 파일 생성

test@docker-test:~$ cd web-service/
test@docker-test:~/web-service$ mkdir blog2
test@docker-test:~/web-service$ cd blog2
test@docker-test:~/web-service/blog2$ mkdir db-data
test@docker-test:~/web-service/blog2$ mkdir web-data
test@docker-test:~/web-service/blog2$ vi docker-compose.yml
version: "3"

services:
  db2:
     image: mariadb
     volumes:
       - ./db-data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: test1234
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: test1234
     container_name: wp_blog_db2

  wordpress2:
     image: wordpress:latest
     expose:
       - 80
     restart: always
     volumes:
       - ./web-data:/var/www/html
     environment:
       VIRTUAL_HOST: blog2.test.com
       WORDPRESS_DB_HOST: db2:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: test1234
     container_name: wp_blog2

networks:
  default:
    external:
      name: nginx-proxy

 

docker-compose.yml 파일 변경 사항

db -> db2

wp_blog_db ->  wp_blog_db2

wordpress -> wordpress2

VIRTUAL_HOST: blog2.test.com

WORDPRESS_DB_HOST: db2:3306

container_name: wp_blog2

 

blog2 컨테이너 실행

test@docker-test:~/web-service/blog2$ docker-compose up -d --build
Creating wp_blog2 ... done
Creating wp_blog2 ...
test@docker-test:~/web-service/blog2$ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED              STATUS              PORTS                NAMES
468a1f13cee1        wordpress:latest      "docker-entrypoint.s…"   About a minute ago   Up About a minute   80/tcp               wp_blog2
39d91c742082        mariadb               "docker-entrypoint.s…"   About a minute ago   Up About a minute   3306/tcp             wp_blog_db2
fa9442409280        mariadb               "docker-entrypoint.s…"   9 minutes ago        Up 9 minutes        3306/tcp             wp_blog_db
416704fdb038        wordpress:latest      "docker-entrypoint.s…"   9 minutes ago        Up 9 minutes        80/tcp               wp_blog
2b9135ebe937        jwilder/nginx-proxy   "/app/docker-entrypo…"   21 minutes ago       Up 21 minutes       0.0.0.0:80->80/tcp   nginx-proxy
test@docker-test:~/web-service/blog2$

 

 

web browser  접속

http://blog2.test.com 으로 접속합니다.