Last active
December 25, 2016 07:44
-
-
Save ajeetraina/f6f2336b221ede53e2f73f8fcef37fd7 to your computer and use it in GitHub Desktop.
Example of Swarm Mode: Testing Simple Web Application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Setup: | |
ajeetsraina81@ucp-1:~$ sudo docker node ls | |
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS | |
8szgdxh310tkad373ourepch8 worker5 Ready Active | |
e2xjlg4610mmyym98sk91qu43 worker1 Ready Active | |
is52hxuxlrcclwd6hbujvflao worker2 Ready Active | |
nnnofsbqqbprgp4m4tw2oh2z1 worker4 Ready Active | |
sv2uuuy3spbh9oriue0mnyx83 * ucp-1 Ready Active Leader | |
sx1c598mlz6r6hg6zrbamrpsn worker3 Ready Active | |
Frontend: SimpleWeb | |
Backend: Redis | |
================================================ | |
Example: Demonstrating How Load Balancing Works? | |
==master==>sudo docker network create -d overlay collabnet | |
j5ioa811szddvo1qo27lq76o7 | |
==master==>sudo docker service create --name backend-db --network collabnet redis | |
ywmmezbehzj23nxid061p3w2g | |
==master==>sudo docker service ls | |
ID NAME MODE REPLICAS IMAGE | |
ywmmezbehzj2 backend-db replicated 0/1 redis:latest | |
==master==>sudo docker service ls | |
ID NAME MODE REPLICAS IMAGE | |
ywmmezbehzj2 backend-db replicated 1/1 redis:latest | |
==master==>sudo docker service create --name frontend --network collabnet --publish 80:80/tcp yeasy/simple-web | |
58nm866cjhw7oudw8b7tu0ryc | |
==master==>sudo docker service ls | |
ID NAME MODE REPLICAS IMAGE | |
58nm866cjhw7 frontend replicated 0/1 yeasy/simple-web:latest | |
ywmmezbehzj2 backend-db replicated 1/1 redis:latest | |
==master==>sudo docker service ls | |
ID NAME MODE REPLICAS IMAGE | |
58nm866cjhw7 frontend replicated 0/1 yeasy/simple-web:latest | |
ywmmezbehzj2 backend-db replicated 1/1 redis:latest | |
==master==>sudo docker service ls | |
ID NAME MODE REPLICAS IMAGE | |
58nm866cjhw7 frontend replicated 0/1 yeasy/simple-web:latest | |
ywmmezbehzj2 backend-db replicated 1/1 redis:latest | |
==master==>sudo docker service ls | |
ID NAME MODE REPLICAS IMAGE | |
58nm866cjhw7 frontend replicated 1/1 yeasy/simple-web:latest | |
ywmmezbehzj2 backend-db replicated 1/1 redis:latest | |
==master==>sudo curl http://localhost | |
<!DOCTYPE html> <html> <body><center><h1><font color="blue" face="Georgia, Arial" size=8><em>Real</em></font> Visit | |
Results</h1></center><p style="font-size:150%" >#2016-12-24 11:06:11: <font color="red">1</font> requests from < | |
<font color="blue">10.255.0.7</font>> to WebServer <<font color="blue">10.0.0.5</font>></p><p style="font-siz | |
e:150%" >#2016-12-24 11:06:11: <font color="red">1</font> requests from <<font color="blue">LOCAL: 10.255.0.3</fo | |
nt>> to WebServer <<font color="blue">10.0.0.5</font>></p></body> </html>==master==> | |
==master==>sudo docker system df | |
TYPE TOTAL ACTIVE SIZE RECLAIMABLE | |
Images 1 1 676.2 MB 0 B (0%) | |
Containers 1 1 47.98 kB 0 B (0%) | |
Local Volumes 0 0 0 B 0 B | |
==master==>sudo docker system df -v | |
Images space usage: | |
REPOSITORY TAG IMAGE ID CREATED SIZE SHARED SIZE | |
UNIQUE SIZE CONTAINERS | |
<none> <none> 46e5f91fc9a9 36 hours ago 676.2 MB 0 B | |
676.2 MB 1 | |
Containers space usage: | |
CONTAINER ID IMAGE COMM | |
AND LOCAL VOLUMES SIZE CREATED STATUS NAMES | |
deef3d8af6b8 yeasy/simple-web@sha256:806adeaf8f5a5cf1ae7ff01493f556dfc24865e6efd4d52fad6e9b7437c0f169 "/bi | |
n/sh -c 'pytho..." 0 48 kB 4 minutes ago Up 4 minutes frontend.1.9ec | |
d3fj5gtmfxrrf2i5uqd5mj | |
Local Volumes space usage: | |
VOLUME NAME LINKS SIZE | |
==master==> | |
Example:3> | |
Frontend : Wordpress | |
Backend: MYSQL | |
====================================== | |
Example:4> | |
Frontend: | |
Backend: MongoDB Sharding | |
Sharding is a method for distributing data across multiple machines. MongoDB uses sharding to support deployments with very large data sets and high throughput operations. | |
1 Master node | |
5 worker nodes | |
Master Node: | |
docker network create --driver overlay --opt encrypted mongoshard | |
sudo mkdir -p /opt/mongoshard/db-data | |
sudo chmod 777 /opt/mongoshard/db-data | |
Pull the mongodb.conf template from https://github.com/mongodb/mongo/blob/master/rpm/mongod.conf | |
File: mongodb.conf | |
======================= | |
# network interfaces | |
net: | |
port: 27017 | |
# bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces. | |
#security: | |
#operationProfiling: | |
#replication: | |
sharding: | |
clusterRole: configsvr | |
## Enterprise-Only Options | |
... | |
===================== | |
We want one mongocfg on the main node (The main node will be the one that runs mongos). | |
$sudo docker service create -constraint "node.id==sv2uuuy3spbh9oriue0mnyx83" --mount type=bi | |
nd,src=/opt/mongoshard/db-data/,dst=/var/lib/mongodb/ --mount type=bind,src=/opt/mongoshard/mongodb.conf,dst=/etc/m | |
ongodb.conf --network mongoshard --name mongocfg1 --restart-condition on-failure --restart-max-attempts 3 --endpoin | |
t-mode dnsrr mongo:latest --config /etc/mongodb.conf | |
To try: | |
Now, from server1 (swarm leader) we can check nodes ids with the following command: | |
´´´ | |
# docker node ls | |
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS | |
3ebujl8qci0pk2oiuww8p5us4 server3 Ready Active | |
7dt8zo3zrhj0m9tyvwq5yes5r server2 Ready Active | |
e4wiamxehzk4ramu1l9nntf9d * server1 Ready Active Leader | |
´´´ | |
We want one mongocfg on the main node (The main node will be the one that runs mongos). | |
# docker service create --constraint "node.id==e4wiamxehzk4ramu1l9nntf9d" --mount type=bind,src=/opt/mongoshard/db-data/,dst=/var/lib/mongodb/ --mount type=bind,src=/opt/mongoshard/mongodb.conf,dst=/etc/mongodb.conf --network mongoshard --name mongocfg1 --restart-condition on-failure --restart-max-attempts 3 --endpoint-mode dnsrr mongo:latest --config /etc/mongodb.conf | |
9kpdd7bj445d3rh0g83ajm5j4 | |
Now we need to create mongos. mongos is a service to redirect the queries to the different mongo shards. | |
It ships in the same docker package as mongodb, but it is a seperate executable. | |
We create a file called mongos with the following content: | |
´´´ | |
FROM mongo:latest | |
ENTRYPOINT ["mongos"] | |
´´´ | |
Then we create an image: | |
´´´ | |
docker build -f mongos -t crimsonglory/mongos . | |
´´´ | |
And we push it to our docker hub account: | |
´´´ | |
docker login | |
´´´ | |
Browse to hub.docker.com, login and create a repository. | |
´´´ | |
docker push crimsonglory/mongos | |
´´´ | |
We only have to do this once. or zero, you can use my crimsonglory/mongos hub. | |
# docker service create --constraint "node.id==e4wiamxehzk4ramu1l9nntf9d" --network mongoshard --name mongos1 --restart-condition on-failure --restart-max-attempts 3 -p 27017:27017 crimsonglory/mongos --configdb mongocfg1:27017 | |
We use only 1 config server because Docker Swarm increases latency and using more config servers will only decrease performance. | |
(this last command may fail if you already have Mongo installed on the main node host. That's because the container can't bind to 27017 because its on use. You may want to change the 27017:27017 to 27018:27017 or something.) | |
Now the shards: | |
On each host that will be used for shards do: | |
´´´ | |
mkdir /opt/mongoshard/db-shard/; chmod 777 /opt/mongoshard/db-shard | |
´´´ | |
´´´ | |
# docker service create --constraint "node.id==7dt8zo3zrhj0m9tyvwq5yes5r" --network mongoshard --name mongosh1 --restart-condition on-failure --restart-max-attempts 3 --mount type=bind,src=/opt/mongoshard/db-shard,dst=/var/lib/mongodb --mount type=bind,src=/opt/mongoshard/mongo-sh.conf,dst=/etc/mongod.conf --endpoint-mode dnsrr mongo:latest --config /etc/mongod.conf | |
1l6yx68fvvjy7x7713tlwgsy3 | |
# docker service create --constraint "node.id==3ebujl8qci0pk2oiuww8p5us4" --network mongoshard --name mongosh2 --restart-condition on-failure --restart-max-attempts 3 --mount type=bind,src=/opt/mongoshard/db-shard,dst=/var/lib/mongodb --mount type=bind,src=/opt/mongoshard/mongo-sh.conf,dst=/etc/mongod.conf --endpoint-mode dnsrr mongo:latest --config /etc/mongod.conf | |
d0xxgmihl4u2ff23awbu90brt | |
´´´ | |
Now tell mongos about the shards. We should be able to connect to mongos from outside the Docker container (because we map the port) | |
´´´ | |
mongo 127.0.0.1:27017/admin | |
MongoDB shell version: 3.2.8 | |
connecting to: 127.0.0.1:27017/admin | |
Welcome to the MongoDB shell. | |
For interactive help, type "help". | |
For more comprehensive documentation, see | |
http://docs.mongodb.org/ | |
Questions? Try the support group | |
http://groups.google.com/group/mongodb-user | |
Server has startup warnings: | |
2016-09-06T03:16:02.582+0000 I CONTROL [main] ** WARNING: You are running this process as the root user, which is not recommended. | |
2016-09-06T03:16:02.582+0000 I CONTROL [main] | |
mongos> sh.addShard("mongosh1") | |
{ "shardAdded" : "shard0000", "ok" : 1 } | |
mongos> sh.addShard("mongosh2") | |
{ "shardAdded" : "shard0001", "ok" : 1 } | |
mongos> | |
db.runCommand({"enablesharding": "DB_files"}) | |
mongos> db.runCommand({"shardCollection": "DB_files.fs.chunks", "key": {"files_id": 1, "n": 1}, "numInitialChunks": 1000}) | |
{ "collectionsharded" : "DB_files.fs.chunks", "ok" : 1 } | |
´´´ | |
Not everything is perfect. With docker swarm mode (as 2016-10-05) we can't pass ulimit to the service. It's a pendding issue on docker repo. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment