Lab #5: Test Service Discovery

Service Discovery which allows containers on the same network can access each other by name. Service Discovery is achive through Docker’s embedded DNS server(DNS IP 127.0.0.11). Embedded DNS server which provides built-in service discovery for any container created with a valid name(–name) or net-alias(–network-alias) or aliased by link. Service discovery which wont work on default networks.

Pre-requisite:

Tested Infrastructure

Platform Number of Instance Reading Time
Play with Docker 2 10 min

Pre-requisite

Testing service discovery in default network

Running an nginx server

$ docker container run -d --rm --name default_ntwrk nginx:alpine

Testing wehther able to resolve using service name

$ docker run --rm byrnedo/alpine-curl default_ntwrk
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: default_ntwrk

We are getting error that Could not resolve host: default_ntwrk

Creating a bridge network

$ docker network create --driver=bridge --subnet=192.168.1.0/24 --gateway=192.168.1.250 my_bridge

Running an nginx server in user defined network

$ docker container run -d --rm --name usr_ntwrk --network my_bridge nginx:alpine

Testing service discovery in User defined network

$ docker run --rm --network my_bridge byrnedo/alpine-curl usr_ntwrk

You will be getting nginx homepage on success.

Testing service discovery in Swarm Mode

Creating an nginx service on default overlay(ingress)

$ docker service create --name myWeb --replicas 2 --publish 8080:80 nginx:alpine

Creating another Service myCentos

$ docker service create --replicas 2 --name myCentos centos sleep 1d

Getting container ID of Centos

$ docker container ls

Testing wehther able to resolve using service name from centos

$ docker exec -it <Container_ID> curl myWeb

You will be getting error curl: (6) Could not resolve host: myWeb; Unknown error

Lets remove the Services

$ docker service rm myWeb myCentos

Create a Overlay network

$ docker network create -d overlay --opt encrypted CustomOverlay

Create nginx service with CustomOverlay network

$ docker service create --name myWeb --replicas 2 --publish 8080:80 --network=CustomOverlay  nginx:alpine

Creating another Service myCentos with CustomOverlay network

docker service create --replicas 2 --name myCentos --network=CustomOverlay centos sleep 1d

Getting container ID of Centos

$ docker container ls

Testing wehther able to resolve using service name from centos

$ docker exec -it <Container_ID> curl myWeb

Now you will get response of nginx home page

Contributor

Savio Mathew