Skip to content

Instantly share code, notes, and snippets.

@potikanond
Last active August 26, 2018 15:12
Show Gist options
  • Save potikanond/89b5096cb23785f2f7c7240d95cfbb30 to your computer and use it in GitHub Desktop.
Save potikanond/89b5096cb23785f2f7c7240d95cfbb30 to your computer and use it in GitHub Desktop.
Example: DNS round-robin

Example: DNS round-robin

This mechanism can be use for loadbalancing web application

Here we will implement load balance 'Elasticsearch". What is Elasticsearch?

Create a virtual network and two elasticsearch containers with the same 'alias name'

$ docker network create dude
$ docker container run -d --net dude --net-alias search elasticsearch:2
$ docker container run -d --net dude --net-alias search elasticsearch:2

Inspect the 'dude' virtual network

$ docker network inspect dude

sample output:

[
    {
        "Name": "dude",
        "Id": "4834f7a480e2c1ce54cec251c888466896f40a36d07a2955dde376da366141f1",
        "Created": "2018-08-21T15:25:25.53002801Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "465cf0425e43233946d89acf4a6473c3e6406d63cd1189cc864862a3a47d83af": {
                "Name": "elas2",
                "EndpointID": "3866b46493d8e459ff28687e76a125a234f597460bf0d706857743789391012e",
                "MacAddress": "02:42:ac:12:00:04",
                "IPv4Address": "172.18.0.4/16",
                "IPv6Address": ""
            },
            "cfeba4ef421de14e0c820b1aeb857dcc9d1f40eacd1d8c24c80d32cc58e0a470": {
                "Name": "elas1",
                "EndpointID": "1df92ddb794c2dc43ec750b1e3ab1a7c117254b63d709f15edbf74a19df57953",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

Create an alpine container and install CURL package

$ docker container run -it --net dude --name alpine1 alpine sh
/# apk update
/# apk add curl

Make multiple search requests, the two Elasticsearch container will take turn provide service to the requests

/# curl -s search:9200
/# curl -s search:9200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment