Tag: swarm

Learning – Docker Swarm – Basic

Learning - Docker Swarm - Basic

Files

server.js

const express = require("express");
const os = require("os");

const app = express();

app.get("/", (req, res) => {
  res.send("Hello from Swarm " + os.hostname());
});

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

Dockerfile

FROM node:11.1.0-alpine

WORKDIR /home/node

COPY . .

RUN npm install

CMD npm start

docker-compose.yml

version: "3"

services:
  web:
    build: .
    image: takacsmark/swarm-example:1.0
    ports:
      - 80:3000
    networks:
      - mynet
    deploy:
      replicas: 6
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

  visualizer:
    image: dockersamples/visualizer:stable
    ports:
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      placement:
        constraints: [node.role == manager]

networks:
  mynet:

requirements.txt

flash
redis

Nodes

docker swarm init
docker swarm init --advertise-addr eth1
docker swarm join ...
docker swarm leave -f
docker demote
docker promote

Stack

docker stack ls
docker stack ps

Services

docker service ls
docker service ps
docker service create

To run a service in all nodes

docker service create --name 'service_name' -p 8000:8000 --mode global demoapp

To run a server in 3 nodes

docker service create --name 'service_name' -p 8000:8000 --replicas 2 demoapp

Tasks

It is scheduling the container, which is managed using docker service command.

docker service ps nodeapp_web

Container

The containers should be managed using swarm commands, but they also can be seen using container command.

docker ps
docker kill

Deploy

docker stack deploy -c docker-compose.yml nodeapp

This will create network nodeapp_mynet, and two services (nodeapp_web and nodeapp_db)

Note: The images need to be pre-built using docker-compose build command, and push into docker hub.

List

docker stack ls
docker service ls
docker stack services ls

scale

docker service scale nodeapp_web=4

To access all 4 services, just need to access http://localhost, then the 4 services will be supporting same port on localhost.

Note: The hostname appeared in http://localhost is changing

Sample of overlay network

  • Create overlay network
docker network create -d overlay myoverlay1
  • Create webapp on overlay network
docker service create --name webapp1 -d --network myoverlay1 -p 8001:80 test/webapp
  • Create database on overlay network
docker service create --name mysql -d --network myoverlay1 -p 3306:3306 test/mysql:5.5

Sample with docker-machine

Docker machine is to create a virtual machine with minimum Linux packages installed with docker running.

docker-machine ls
docker-machine start myvm1
docker-machine start myvm2
docker-machine ssh myvm1

Init swarm

docker swarm init --advertise-addr eth1
docker swarm join --token ...

Push image

docker-compose push

Access master from docker machine

To access docker swarm master in docker machine from other host

docker-machine env myvml
eval $(docker-machine env myvm1)

Deploy

docker stack deploy -c docker-compose.yml nodeapp
docker stack ls
docker stack services nodeapp
docker-machine ls

After the services started, all nodes, including master and worker nodes, will provide the services by routing to the correct host.

replicas and parallelism

    deploy:
      replicas: 6
      update_config:
        parallelism: 2
        delay: 10s

This indicates total of 6 replicas, and deploy 2 in parallel with 10 seconds delay.

constraints

    deploy:
      placement:
        constraints: [node.role == manager]

This indicates that only manager node will be deployed.

Scale

docker service scale nodeapp_web=4

Monitor

docker stack deploy -c docker-compose.monitoring.yml nodemon
docker stack ls
docker stack services nodemon

The service nodemon_visualizer running on port 8080

Deploy to specific node

Label

docker node update myvm1 --label-add db=mongo
docker node inspect myvm1 -f {{.Spec.Labels}}

placement

    deploy:
      placement:
        constraints: [node.labels.db == mongo]

Redeploy

docker stack deploy -c docker-compose.yml nodeapp

Drain & Active

docker node update --availability drain myvm2
docker node update --availability active myvm2

force update

docker node update --availability=active myvm2
docker service update --force nodeapp_web

Change version number

Update image: option in docker-compose.yml file

Then build again

docker-compose build
docker-compose push
docker stack deploy -c docker-compose.yml nodeapp
docker stack ps nodeapp

Then the containers will redeploy 2 at each time.

Cloud

AWS supports Docker Swarm.

References

Introduction to Docker Swarm | Tutorial for Beginners | Examples | Typical Tasks (Video)

Docker Swarm Tutorial | Code Along | Zero to Hero under 1 Hour

Learning – Docker Swarm Network Drivers

Learning - Docker Swarm Network Drivers

Bridge

The default network driver. Needs to map the port to host in order to access port of container.

Host

Removes network isolation between the container and the Docker host, and uses the host's networking directly. So the containers can not have port conflicting with other containers and also host.

The IP will be the same as host.

None

Disables all networking for containers. Usually used in conjunction with a custom network drive.

Overlay

Connect multiple Docker daemons together and enable swarm services to communicate with each other daemons.

Using this overlay network, the container on different hosts can communicate with each other.

Macvlan

Allow you to assign a MAC address to a container, making it appears as a physical device on the network. The Docker daemon routes traffic to container by their MAC addresses.

This allows container has different IP address on the host network.

Selection of container orchestration platform

Selection of container orchestration platform

I'm trying to manage my docker containers using container orchestration platform. There are a few of them can be used.

Variations

Docker Swarm

Easy to setup and manage existing docker machines.

Kubernetes (K8s)

Widely used.

K3S

K3s is Rancher’s k3s Kubernetes distro, lightweight, single binary, and low resource usage.

Openshift

Complex

Consideration

Unlike company environments, most of my docker containers applications have individual database. The application usage is low, autoscaling isn't a requirement for me but the backup and restore is important.

References

k8s vs k3s
Docker Swarm vs Kubernetes: how to choose a container orchestration tool