Code Monkey home page Code Monkey logo

docker-swarm-webapp's Introduction

Deploying a Web-app using stack to a docker-swarm.

[Docker-compose + Swarm + docker stack deploy]

Hi Geeks,

Here I have dockerised Nginx webserver and php-fpm in different containers and deployed into docker swarm for high performance. The complete infrastructure of this project is explained in the below diagram.

I have used docker compose feature to create services for nginx and php-fpm which will be then deployed from my docker swarm's master node to worker nodes. Also configured a NFS server to server the website contents to my worker nodes. So the developer can update the code from the nfs server.

Features

Architecture

image text

Setup

The first thing you have to do is, of course, install Docker (if you haven’t already). The second prerequisite is getting Docker Compose (it is included in the Mac toolbox)

We’ll start by getting ourselves a web server and based on our requirements this will be containers running nginx and php-fpm. These containers will be running on the alpine os to minimalize the disk usage.

Below is my docker-compose.yml file for creating nginx and php-fpm services.

version: '3'
services:

  php:
    image: php:fpm-alpine3.13
    restart: always
    container_name: php
    volumes:
      - /var/nfs:/var/www/html
    networks:
      - newnet
  nginx:
    image: nginx:alpine
    restart: always
    container_name: nginx
    volumes:
      - /var/nfs:/var/www/html/
      - /var/nfs/nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "80:80"
    networks:
      - newnet

networks:
  newnet:
    driver: overlay

I have already setup my NFS server and mounted the /var/nfs under all workers nodes. The image for the nginx with web app and php-fpm will be built using the Dockerfile defined above.

Server block configuration for my web-app,

server {
  listen 80;
  listen [::]:80;
  access_log off;
  root /var/www/html;
  index index.php;
  server_name example.com;
  server_tokens off;
  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ /index.php?$args;
  }
  # pass the PHP scripts to FastCGI server listening on wordpress:9000
  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php:9000; <<<<<<<<<<<<<<<
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  }
}

In the above server block configuration I have used php-fpm's container name to server the php request for the nginx webserver.

When running Docker Engine in swarm mode, you can use "docker stack deploy" to deploy a complete application stack to the swarm. The deploy command accepts a stack description in the form of a Compose file.

Deploying my webapp as a stack to a swarm.

To avoid creating the container under master node, I'm setting the master node's availability as drain.

docker node update --availability=drain <Manager_IP_Address>

Steps:

  • Set up a Docker registry. A registry is required to distribute images to all of the docker engines. Start the registry as a service on your swarm:
$ docker service create --name registry --publish published=5000,target=5000 registry:2
  • Check its status with docker service ls
$ docker service ls

Once it reads 1/1 under REPLICAS, it’s running. If it reads 0/1, it’s probably still pulling the image.

  • Test the web-app with docker compose.
$ docker-compose up -d
  • Check that the app is running with docker-compose ps:
$ docker-compose ps
  • Confirm the webapp is running fine and Bring the app down:
$ docker-compose down --volumes
  • Push the generated image to the previously registry,
$ docker-compose push

The stack is now ready to be deployed.

Deploy the stack to the swarm

  • Create the stack with docker stack deploy:
$ docker stack deploy --compose-file docker-compose.yml webapp-stack

The last argument is a name for the stack. Each network, volume and service name is prefixed with the stack name.

  • Check that it’s running with docker stack services webapp-stack:
$ docker stack services webapp-stack

Once it’s running, you should see 1/1 under REPLICAS for both services. This might take some time if you have a multi-node swarm, as images need to be pulled.

I have also setup an AWS Application load balancer by using my worker nodes to manage the traffic into the webapp.

Thank you

docker-swarm-webapp's People

Contributors

akhiljithpb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.