Skip to content

Latest commit

 

History

History
191 lines (158 loc) · 6.64 KB

LAB04-DockerStackService.md

File metadata and controls

191 lines (158 loc) · 6.64 KB

LAB-04: Docker Swarm using PlayWithDocker, Creating Swarm Cluster: 3 x WordPress Containers and 1 x MySql Container

This scenario shows how to run multi-containers in the cluster using the Docker Stack file by creating a service in Docker Swarm. It goes following:

  • Creating Docker Swarm Cluster (2 Manager Nodes, 3 Worker Nodes) using PlayWithDocker
  • Copying docker-compose.yml file into the Manager node to create Docker Service
  • Creating Docker Service and showing the service's details
  • Going to one of the Worker Node, stepping into the container, showing the connection using ping.
  • Removing one of the containers in the Worker Node and showing the recreation by Docker Service.
  • Stopping and removing all containers in the Docker Service.

Note

In server, these ports should be opened: (These ports are opened in PlayWithDocker, not needed to open ports)
- TCP port 2377 (Cluster management)
- TCP - UDP port 7946 (Communication between nodes)
- UDP port 4789 (Overlay network) 

Step

  • Go to PlayWithDocker and login (you have to DockerHub user)
  • Create 5 x Instances
  • Initialize Docker Swarm: (Copy-KeyCombination: CTRL+Insert, Paste-KeyCombination: Shift+Insert)
docker swarm init --advertise-addr [ManagerNodeIP]
docker swarm init --advertise-addr 192.168.0.18 

image

  • Paste command which appears after init. Swarm Cluster into 3 Instances:
docker swarm join --token SWMTKN-1-1cfvch2ygu5h0ga357dte1i53ukbigei8qd0uyw93gcegogb3z-eebewiqny6obn9vcoxmdb4moq 192.168.0.18:2377

image

  • Run on the terminal to add second Manager Node into Swarm Cluster:
docker swarm join-token manager
  • Paste command which appears after join-token manager command into the remained instance:
Example token (it will be different in different session)
docker swarm join --token SWMTKN-1-23a47kkzf1hp8j7a51tx0fsc4hj1tprqi3kx04zfm0djf8ozr4-eqcsf6huttmugjf4yu4yoyrxd 192.168.0.13:2377
  • Run on the terminal to show cluster (Manager:2, Worker:3):
docker node ls

image

  • Copy the following "docker-compose.yml" content:
version: "3.8"

services:
  mydatabase:
    image: mysql:5.7
    restart: always
    volumes: 
      - mydata:/var/lib/mysql
    environment: 
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - mynet
  mywordpress:
    image: wordpress:latest
    deploy:
      replicas: 3
      update_config:
        parallelism: 2
        delay: 5s
        order: stop-first
    depends_on: 
      - mydatabase
    restart: always
    ports:
      - "80:80"
      - "443:443"
    environment: 
      WORDPRESS_DB_HOST: mydatabase:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    networks:
      - mynet

volumes:
  mydata:

networks:
  mynet:
    driver: overlay
  • On the leader Manager Node, create "docker-compose.yml" file, paste the content using "Shift+Insert":
cat > docker-compose.yml
(After pasting the content of the Docker-compose.yml, press "Enter" and "CTRL+D" to save the file and click "Editor" to see the content of the docker-compose.yml)

image

  • Start Docker Service using docker-compose.yml file:
docker stack deploy --compose-file docker-compose.yml firststack

image

  • "firststack" service is started and 3 x WordPress containers are created and 1 x MySql container is created. Click "80" port to see "WordPress" Container Service:

image

image

  • Run on the terminal to see details of stack:
docker stack ls 

image

  • Run on the terminal to see containers:
docker stack services firststack

image

  • Run on the terminal to see on which nodes which containers run:
docker stack ps firststack

image

  • Go to Node5 and run on the terminal to see the running container, connected network and networks' details:
docker container ls -a
docker network ls 
docker network inspect firststack_mynet

image

  • Run on the terminal to enter into container:
docker exec -it [containerID] sh
docker exec -it aeb sh 
docker container exec -it aeb sh
  • Run on the terminal to update, install net-tools (ifconfig), iputils-ping (ping) and to see the Ethernet interfaces (ifconfig) and connection of other containers (ping):
apt-get update
apt-get install net-tools
apt-get install iputils-ping -y
ifconfig
ping 10.0.1.2
ping 10.0.1.3

image

  • Go to Node5 and stop containers which are running on Node5 twice times:
docker container ls -a
docker container stop aeb
docker container ls -a (after stopping container, automatically created by docker service using docker-compose.yml)
docker container ls b7f
docker container ls -a

image

  • Go to Node1 (ManagerNode) to see docker containers' status. On Node5, the 'WordPress' container is stopped and created twice times. This works with Docker Service using docker-compose.yml:
docker stack ps firststack

image

  • Run on the terminal to stop and remove containers in Docker Service:
docker stack rm firststack
docker stack ps firststack

image