Skip to content

Latest commit

 

History

History
248 lines (211 loc) · 8.93 KB

DOCKER-SWARM.md

File metadata and controls

248 lines (211 loc) · 8.93 KB

How To Run Docker swarm mode with docker-stack.yml

Introduction Scale Out with Docker Swarm

CW-OVP can have many background job for video packaging and encoding with ffmpeg. You must do scale out in production environment due to you require HA and saving time. If you want get scale in, you can get it but you know scale in cost is expensive more than scale out. I provide scale out of solution with docker swarm. I recommend 16 core for each server of worker node but mininum spec is 4 or 8 or more core.

aws-cli config in local-PC

https://www.changwoo.org/[email protected]/2020-11-04/docker-machine-create-AWS-ec2-eefe152281

Docker-machine

If you want run on local PC or docker-machine driver with aws or digital ocean or another cloud service that support docker-machine, Do use docker-machine. But if you don't wanna docker-machine, bypass this section go to next section of Private registry

Docker-machine Setup on macOS

brew install virtualbox
brew install docker-machine
docker-machine create -d virtualbox master
docker-machine create -d virtualbox worker1
docker-machine create -d virtualbox worker2
docker-machine ls

aws

docker-machine create -d amazonec2 \
--amazonec2-region us-west-1 \
--amazonec2-instance-type "m5.large" \  #--amazonec2-instance-type "t2.micro" \
--amazonec2-ssh-keypath ~/.ssh/id_rsa \ #--amazonec2-ssh-keypath ~/.ssh/ssh_key \
aws-test

if you don't have virtualbox. you got blow of message.

VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path.

Docker-machine ssh

docker-machine ssh master
### or eval "$(docker-machine env master)"

docker@master:~$ docker swarm init --advertise-addr 192.168.99.100                                                                                                                                        

Swarm initialized: current node (4l396ni602807sb2hk1ujlvm5) is now a manager.

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

    docker swarm join --token SWMTKN-1-5jcphjyj4ykejxphj2o15yh7bz4syyxo5qg8bt25ldkhd4poez-1l2skxj2931mtjolwd43139jy 192.168.99.100:2377

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

if you want get token for worker again

docker swarm join-token worker
docker-machine ssh worker1
   ( '>')
  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
 (/-_--_-\)           www.tinycorelinux.net

docker@worker1:~$ docker swarm join --token SWMTKN-1-5jcphjyj4ykejxphj2o15yh7bz4syyxo5qg8bt25ldkhd4poez-1l2skxj2931mtjolwd43139jy 192.168.99.100:2377
This node joined a swarm as a worker.
docker-machine ssh worker2
   ( '>')
  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
 (/-_--_-\)           www.tinycorelinux.net

docker@worker2:~$ docker swarm join --token SWMTKN-1-5jcphjyj4ykejxphj2o15yh7bz4syyxo5qg8bt25ldkhd4poez-1l2skxj2931mtjolwd43139jy 192.168.99.100:2377
This node joined a swarm as a worker.

Private registry

docker-machine ssh master

docker service create --name registry --insecure-registry=[myip]:5000 --constraint 'node.role == manager' --publish published=5000,target=5000 registry:2
curl http://localhost:5000/v2/

sudo vim /etc/docker/daemon.json
{"insecure-registries":["your ip:5000"] }
sudo service docker restart

https://github.com/docker/distribution/issues/1874

Initial source

git clone https://github.com/x1wins/CW-OVP.git
cd ./CW-OVP
git fetch
git checkout master # or feature/docker-stack

Update source

if you change source, use git pull or fetch

cd CW-OVP/
git pull
git checkout master
git reset --hard origin/master # or origin/feature/docker-machine

Update s3

vi .env.dev.s3

Build image

  • build image and change tag with localhost:5000
    docker build -t cw-ovp:latest .
    docker tag cw-ovp:latest localhost:5000/cw-ovp
    docker tag localhost:5000/cw-ovp:latest 127.0.0.1:5000/cw-ovp
    
  • build image with localhost:5000 for private repository
    docker build -t 127.0.0.1:5000/cw-ovp . 
    

Push Image to Pirvate registry

docker push 127.0.0.1:5000/cw-ovp

Run stack

docker stack deploy --compose-file docker-stack.yml CW-OVP
docker image prune -a -f

docker exec -it 1f7193e6042e bundle exec rails webpacker:install
docker exec -it 1f7193e6042e bundle exec rake db:migrate 

Stop stack

docker stack rm CW-OVP

Changing Scale

docker@master:~/CW-OVP$ docker service scale CW-OVP_web=1                                                                                                                                                 
CW-OVP_web scaled to 1
overall progress: 1 out of 1 tasks 
1/1: running   [==================================================>] 
verify: Service converged 
docker@master:~/CW-OVP$ docker service ls                                                                                                                                                                 
ID                  NAME                MODE                REPLICAS            IMAGE                             PORTS
jc1sqff4pyni        CW-OVP_db           replicated          1/1                 postgres:10.10                    *:5432->5432/tcp
i5mpujb9hqh5        CW-OVP_redis        replicated          1/1                 redis:latest                      *:6379->6379/tcp
7olizwz0kn9g        CW-OVP_sidekiq      replicated          2/2                 127.0.0.1:5000/cw-ovp:latest      
tyu6zey671ky        CW-OVP_visualizer   replicated          1/1                 dockersamples/visualizer:stable   *:8080->8080/tcp
mgwbtswxctjk        CW-OVP_web          replicated          1/1                 127.0.0.1:5000/cw-ovp:latest      *:3000->3000/tcp
kchi30notd2m        registry            replicated          1/1                 registry:2                        *:5000->5000/tcp

Update latest image

docker service update --image 127.0.0.1:5000/cw-ovp CW-OVP_web
# docker service update --image <username>/<repo> <servicename>

Trouble shooting

docker service ps --no-trunc {serviceName}
docker service ps --no-trunc t9zabkgynr8c
docker service ps --no-trunc kged4le7e3jn

mkdir /home/docker/CW-OVP/tmp/redis                                                                                                                                               
mkdir /home/docker/CW-OVP/tmp/db

Swarm management UI

Docker swarm visualizer

docker service create \
  --name=viz \
  --publish=8080:8080/tcp \
  --constraint=node.role==manager \
  --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
  dockersamples/visualizer

Portainer

curl -L https://downloads.portainer.io/portainer-agent-stack.yml -o portainer-agent-stack.yml
docker stack deploy --compose-file=portainer-agent-stack.yml portainer

Swarmprom

$ git clone https://github.com/stefanprodan/swarmprom.git
$ cd swarmprom

ADMIN_USER=admin \
ADMIN_PASSWORD=admin \
SLACK_URL=https://hooks.slack.com/services/TOKEN \
SLACK_CHANNEL=devops-alerts \
SLACK_USER=alertmanager \
docker stack deploy -c docker-compose.yml mon

docker_swarm_visualizer docker_swarm_portainer