기술빙자사기

[테스트 노트] docker swarm 모드로 그누보드5 로드밸런스(부하분산) 흉내내기

권성재 2020. 3. 24. 18:39

##  docker swarm 으로 그누보드5 로드밸런스 흉내내기


192.168.219.160 docker-master # 도커 마스터(매니저)
192.168.219.184 docker-node1 # 도커 노동자1


nonots@docker-master:~/mydocker$ docker swarm init --advertise-addr 192.168.219.160
Swarm initialized: current node (j0m1pxp35cgd9ylu3aaaa3t2j) is now a manager.

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

    docker swarm join --token SWMTKN-1-4m5w9f7sar3xq5xpx0lomsrveqqbm5qda723mi24i2rn8u0ebp-ebuk5n1rcxr21a0ddaln4c7dl 192.168.219.160:2377

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

nonots@docker-master:~/mydocker$ 



nonots@docker-node1:~$ docker swarm join --token SWMTKN-1-4m5w9f7sar3xq5xpx0lomsrveqqbm5qda723mi24i2rn8u0ebp-ebuk5n1rcxr21a0ddaln4c7dl 192.168.219.160:2377
This node joined a swarm as a worker.


nonots@docker-master:~/mydocker$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
j0m1pxp35cgd9ylu3aaaa3t2j *   docker-master       Ready               Active              Leader              19.03.6
l0taqdgxz2bfcw65u8zqxbme2     docker-node1        Ready               Active                                  19.03.8





##nfs 설정,

nonots@docker-master:~$ sudo apt-get install nfs-common nfs-kernel-server rpcbind portmap
nonots@docker-master:~$ sudo vi /etc/exports 

# docker-node1 ip 에서 접근 허용
/home/gnuboard_data 192.168.219.184(rw,sync,no_subtree_check)

nonots@docker-master:~$ sudo mkdir /home/mysql_data
nonots@docker-master:~$ sudo mkdir -p /home/gnuboard_data/data
nonots@docker-master:~$ sudo chmod 707 /home/mysql_data/
nonots@docker-master:~$ sudo chmod 707 /home/gnuboard_data/
nonots@docker-master:~$ sudo chmod 707 /home/gnuboard_data/data/

nonots@docker-master:~$ cat /home/gnuboard_data/lb.php 


nonots@docker-node1:~/mydocker$ sudo apt install nfs-common
nonots@docker-node1:~/mydocker$ sudo mkdir /home/gnuboard_data
nonots@docker-node1:~/mydocker$ sudo chmod 707 /home/gnuboard_data
nonots@docker-node1:~/mydocker$ sudo mount 192.168.219.160:/home/gnuboard_data /home/gnuboard_data
nonots@docker-node1:~/mydocker$ cd /home/gnuboard_data/
nonots@docker-node1:/home/gnuboard_data$ ls (하면 그누보드 전체소스가 보여야 정상)






## 서비스 생성


# mymysql - 항상 제일먼저 생성해서 마스터 노드에 컨테이너가 위치하게 해야함.
nonots@docker-master:~$ docker service create --name mymysql --replicas 1 --constraint 'node.role==manager' --mount type=bind,src=/home/mysql_data,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root2349 -e MYSQL_USER=nonots -e MYSQL_DATABASE=nonots_db -e MYSQL_PASSWORD=kwon2349 -p 3306:3306 -d mysql:latest --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

nonots@docker-master:~$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
1kv5g06zn64z        mymysql             replicated          1/1                 mysql:latest        *:3306->3306/tcp
nonots@docker-master:~/mydocker$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
07e9a16b4bab        mysql:latest        "docker-entrypoint.s…"   8 seconds ago       Up 7 seconds        3306/tcp, 33060/tcp   mymysql.1.n6cdjbkvl0xeizy32ciwg9pl1


nonots@docker-master:~$ docker container exec -it 07e9a16b4bab mysql -unonots -pkwon2349 nonots_db

nonots@docker-master:~/mydocker$ docker container inspect 07e9a16b4bab | grep -i ipaddr
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "10.0.0.49",



# mygnu

nonots@docker-master:~$ docker service create --name mygnu --replicas 3 --mount type=bind,src=/home/gnuboard_data,dst=/var/www/html -p 8888:80  -d nonots/gnudocker:v1

nonots@docker-master:~$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                 PORTS
hu2sel7mozt2        mygnu               replicated          3/3                 nonots/gnudocker:v1   *:8888->80/tcp
tgh1jvrqascz        mymysql             replicated          1/1                 mysql:latest          *:3306->3306/tcp


nonots@docker-master:~$ docker service ps mygnu
ID                  NAME                IMAGE                 NODE                DESIRED STATE       CURRENT STATE                ERROR               PORTS
knmz2tgjwkxo        mygnu.1             nonots/gnudocker:v1   docker-master       Running             Running about a minute ago                       
ax7unskk8an0        mygnu.2             nonots/gnudocker:v1   docker-node1        Running             Running about a minute ago                       
7m9antdw76yi        mygnu.3             nonots/gnudocker:v1   docker-node1        Running             Running about a minute ago                       
nonots@docker-master:~$ docker container ls
CONTAINER ID        IMAGE                 COMMAND                  CREATED              STATUS              PORTS                 NAMES
81e4fb7f162f        nonots/gnudocker:v1   "docker-php-entrypoi…"   About a minute ago   Up About a minute   80/tcp                mygnu.1.knmz2tgjwkxorhj2dt5zsyzq6
8d48664abbbb        mysql:latest          "docker-entrypoint.s…"   2 minutes ago        Up 2 minutes        3306/tcp, 33060/tcp   mymysql.1.xk40f7u8n0ou83cw95pg5fkhq



nonots@docker-node1:~$ docker container ls
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
6cd8f31238b1        nonots/gnudocker:v1   "docker-php-entrypoi…"   2 minutes ago       Up About a minute   80/tcp              mygnu.3.7m9antdw76yi2h362jy52rtzq
d2a1e7cf43cd        nonots/gnudocker:v1   "docker-php-entrypoi…"   2 minutes ago       Up About a minute   80/tcp              mygnu.2.ax7unskk8an0mk3fuktr1qkzh




http://192.168.219.160:8888 포트로 웹브라우저로 접속해서 
그누보드 설치화면 떠야 정상임.
Mysql Host 주소는 기본 localhost 가 아니라 마스터 노드 즉 192.168.219.160 으로
입력.









## scale

nonots@docker-master:~$ docker service scale mygnu=5
mygnu 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 
nonots@docker-master:~$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                 PORTS
hu2sel7mozt2        mygnu               replicated          5/5                 nonots/gnudocker:v1   *:8888->80/tcp
tgh1jvrqascz        mymysql             replicated          1/1                 mysql:latest          *:3306->3306/tcp

nonots@docker-master:~$ docker container ls
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                 NAMES
a1a283331eec        nonots/gnudocker:v1   "docker-php-entrypoi…"   22 seconds ago      Up 20 seconds       80/tcp                mygnu.4.xyt8rdnbos5ga0jm6h69xffqh
81e4fb7f162f        nonots/gnudocker:v1   "docker-php-entrypoi…"   11 minutes ago      Up 11 minutes       80/tcp                mygnu.1.knmz2tgjwkxorhj2dt5zsyzq6
8d48664abbbb        mysql:latest          "docker-entrypoint.s…"   12 minutes ago      Up 12 minutes       3306/tcp, 33060/tcp   mymysql.1.xk40f7u8n0ou83cw95pg5fkhq
nonots@docker-master:~$ 

nonots@docker-node1:~$ docker container ls
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
9aca3baf611a        nonots/gnudocker:v1   "docker-php-entrypoi…"   41 seconds ago      Up 37 seconds       80/tcp              mygnu.5.6jozbsc02pz0ukkzry2lv3535
6cd8f31238b1        nonots/gnudocker:v1   "docker-php-entrypoi…"   11 minutes ago      Up 11 minutes       80/tcp              mygnu.3.7m9antdw76yi2h362jy52rtzq
d2a1e7cf43cd        nonots/gnudocker:v1   "docker-php-entrypoi…"   11 minutes ago      Up 11 minutes       80/tcp              mygnu.2.ax7unskk8an0mk3fuktr1qkzh



nonots@docker-master:~$ docker node ps docker-master
ID                  NAME                IMAGE                 NODE                DESIRED STATE       CURRENT STATE                ERROR               PORTS
knmz2tgjwkxo        mygnu.1             nonots/gnudocker:v1   docker-master       Running             Running 12 minutes ago                           
xk40f7u8n0ou        mymysql.1           mysql:latest          docker-master       Running             Running 13 minutes ago                           
xyt8rdnbos5g        mygnu.4             nonots/gnudocker:v1   docker-master       Running             Running about a minute ago                       
nonots@docker-master:~$ docker node ps docker-node1
ID                  NAME                IMAGE                 NODE                DESIRED STATE       CURRENT STATE                ERROR               PORTS
ax7unskk8an0        mygnu.2             nonots/gnudocker:v1   docker-node1        Running             Running 12 minutes ago                           
7m9antdw76yi        mygnu.3             nonots/gnudocker:v1   docker-node1        Running             Running 12 minutes ago                           
6jozbsc02pz0        mygnu.5             nonots/gnudocker:v1   docker-node1        Running             Running about a minute ago                       








##로그인,

nonots@docker-master:~/mydocker$ 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: nonots
Password: 
Login Succeeded
nonots@docker-master:~/mydocker$ docker push nonots/gnudocker:v1
The push refers to repository [docker.io/nonots/gnudocker]
90450ba9ec18: Pushed 
a616f8ab5356: Mounted from library/php 
079d43545924: Mounted from library/php 
d97484483f49: Mounted from library/php 
b242745ebda2: Mounted from library/php 
d0d3b2f87351: Mounted from library/php 
be73e3c8f219: Mounted from library/php 
0fc284fc9cf5: Mounted from library/php 




## stack

nonots@nonots-desktop:~/mydocker$ docker service rm mygnu mymysql

nonots@nonots-desktop:~/mydocker$ cat stack.yaml 
version: "3"
services:
  mymysql:
    image: mysql:latest
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "root2349"  # MYSQL root패스워드 설정 옵션
      MYSQL_USER: "nonots" # 그누보드 사용자
      MYSQL_DATABASE: "nonots_db" # 그누보드 사용 DB
      MYSQL_PASSWORD: "kwon2349" #그누보드 비번
    command: # 명령어 실행
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
    volumes:
      - "/home/mysql_data:/var/lib/mysql"
    deploy:
      replicas: 1
      placement:
        constraints:
          - "node.role==manager"

  mygnu:  #apache php 웹서버
    image: nonots/gnudocker:v1
    ports:
      - "8888:80"
    volumes:
      - "/home/gnuboard_data:/var/www/html"
    deploy:
      replicas: 3



nonots@docker-master:~/mydocker$ docker stack deploy -c ./stack.yaml sir
Creating network sir_default
Creating service sir_mymysql
Creating service sir_mygnu
nonots@docker-master:~/mydocker$ docker stack ls
NAME                SERVICES            ORCHESTRATOR
sir                 2                   Swarm
nonots@docker-master:~/mydocker$ docker stack ps sir
ID                  NAME                IMAGE                 NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
i174wfc3e0z9        sir_mygnu.1         nonots/gnudocker:v1   docker-node1        Running             Running 7 seconds ago                        
jqhx57uy58h5        sir_mymysql.1       mysql:latest          docker-master       Running             Running 18 seconds ago                       
6g30kt5bmt5n        sir_mygnu.2         nonots/gnudocker:v1   docker-node1        Running             Running 8 seconds ago                        
w4slrrr5d0cs        sir_mygnu.3         nonots/gnudocker:v1   docker-master       Running             Running 14 seconds ago                       
nonots@docker-master:~/mydocker$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                 PORTS
6wybc9w12gfw        sir_mygnu           replicated          3/3                 nonots/gnudocker:v1   *:8888->80/tcp
qss522ijv5rp        sir_mymysql         replicated          1/1                 mysql:latest          *:3306->3306/tcp
nonots@docker-master:~/mydocker$ 


http://192.168.219.160:8888 접속 그대로 



nonots@nonots-desktop:~$ curl http://192.168.219.184:8888/lb.php
1fcbd92c4cb7 - 192.168.219.184 - 10.0.0.109
nonots@nonots-desktop:~$ curl http://192.168.219.184:8888/lb.php
02878502d411 - 192.168.219.184 - 10.0.0.110
nonots@nonots-desktop:~$ curl http://192.168.219.184:8888/lb.php
3dd837c2db1a - 192.168.219.184 - 10.0.0.108
nonots@nonots-desktop:~$ curl http://192.168.219.184:8888/lb.php
1fcbd92c4cb7 - 192.168.219.184 - 10.0.0.109