Coder Social home page Coder Social logo

getreddy / docswarm Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 87 KB

Launch micro-service as a docker container using overlay networks and discover containers using service discovery..

Java 55.13% Shell 44.87%
docker service disocvery swarm-cluster swarm docker-compose docker-stack docker-service overlay-network discover-containers

docswarm's Introduction

Goal

  • Launch microservice using docker service
  • Use overlay networks
  • Discovering containers

Prerequisites

  • You need 2 ubuntu nodes i.e. one host will be swarm master and other node will act as a worker node.
  • Make sure docker service is installed on both the hosts . Refer docker setup file docker service installation.

Setup

Follow step 1 and 2 on both the nodes i.e. master and worker.

  1. Build microservice project
    git clone https://github.com/getreddy/docswarm.git
    cd microproj
    // Below command will build required jar files under microproj/build/libs
    gradlew clean && gradlew build
    // Container will make use of this jar file by launching command as 'java -jar docMicroservice-0.1.0.jar'
    cd ..
    mkdir jars
    cp microproj/build/libs/docMicroservice-0.1.0.jar jars/

  2. Build docker image
    Below command will make use of docker file
    sudo docker build -t ubuntuswarmtest .

  3. Create overlay network.

Docker introduced user-defined bridge networks to control which containers can communicate with each other, and also to enable automatic DNS resolution of container names to IP addresses.

By default when we create swarm cluster, it creates overlay network “ingress”

We will create a new overlay network for our docker to docker communication.

sudo docker network create --attachable -d overlay overnet

  1. create swarm cluster.
    Run below command on master node :
    sudo docker swarm init
    copy and paste output of below command on worker node.
    sudo docker swarm join-token worker
    confirm the setup by running the below command on master node:
    sudo docker node ls

Launch a Microservice

You can launch a service as a docker service. A service will be the image for a microservice within the context of some larger application. Examples of services might include an HTTP server, a database, or any other type of executable program that you wish to run in a distributed environment. This service which we are launching is a simple microservice which just returns a "message" when you hit "curl http://servicename:8080. In below command, the service name is "systemservice".

You have 2 options of launching docker service.

  1. Using docker command line: Run below command on master node:

sudo docker service create --network overnet --detach=true --replicas 2 --publish 8080:8080 --name systemservice ubuntuswarmtest java -jar /opt/jars/docMicroservice-0.1.0.jar

  1. Using docker-compose :
    using checked in docker compose yml file, run below command to launch services:

sudo docker stack deploy --compose-file docker-compose.yml stackdemo

When you deploy the service to the swarm, the swarm manager accepts your service definition as the desired state for the service. Then it schedules the service on nodes in the swarm as one or more replica (in above command its 2) tasks. The tasks run independently of each other on nodes in the swarm.

Discovering containers on the same service or stack

A container can always discover other containers on the same stack using just the container name as hostname. This includes containers of the same service. Similarly, a container can always discover other services on the same stack using the service name.

Below is the example on how a new container launched under a same overlay network can discover above created service:
Launch a container:

sudo docker run -it --network="overnet" ubuntuswarmtest /bin/bash
On the container prompt run:
curl http://systemservice:8080 you should see below output from the service:
"Service...Root index API returned..need to work on build:-)"

docswarm's People

Contributors

getreddy avatar

Watchers

 avatar  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.