sh 具有docker-machine-parallels的多主机群网络上的MongoDB集群

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 具有docker-machine-parallels的多主机群网络上的MongoDB集群相关的知识,希望对你有一定的参考价值。

#!/bin/bash

# using homebrew -- http://brew.sh
#docker and docker-machine 
brew install docker
brew install docker-machine
brew install docker-machine-parallels

#consul
brew install consul
#jq
brew install JQ
#!/bin/bash
#https://docs.docker.com/engine/userguide/networking/get-started-overlay/

#clean
docker-machine rm mh-keystore -f > /dev/null 2>&1
docker-machine rm mhs-demo0   -f > /dev/null 2>&1
docker-machine rm mhs-demo1   -f > /dev/null 2>&1
docker-machine rm mhs-demo2   -f > /dev/null 2>&1

#Provision machine called mh-keystore. -- https://github.com/Parallels/docker-machine-parallels
docker-machine create -d parallels --parallels-memory 512 mh-keystore
#Start a progrium/consul container running on the mh-keystore machine.
docker $(docker-machine config mh-keystore) run --restart=always --name=consul -d -p "8500:8500" -h "consul" progrium/consul -server -bootstrap


echo "*****************************************************"
echo "Created Consul Host: $(docker-machine ip mh-keystore)"
echo "*****************************************************"

#create swarm master
docker-machine create -d parallels --parallels-memory 512 --swarm --swarm-master \
--swarm-discovery="consul://$(docker-machine ip mh-keystore):8500" \
--engine-opt="cluster-store=consul://$(docker-machine ip mh-keystore):8500" \
--engine-opt="cluster-advertise=eth0:2376" mhs-demo0

echo "*****************************************************"
echo "Created MHS0 Host: $(docker-machine ip mhs-demo0)    "
echo "*****************************************************"

#create swarm node
docker-machine create -d parallels --parallels-memory 512 --swarm \
    --swarm-discovery="consul://$(docker-machine ip mh-keystore):8500" \
    --engine-opt="cluster-store=consul://$(docker-machine ip mh-keystore):8500" \
    --engine-opt="cluster-advertise=eth0:2376" mhs-demo1

echo "*****************************************************"
echo "Created MHS1 Host: $(docker-machine ip mhs-demo1)    "
echo "*****************************************************"

#create swarm node
docker-machine create -d parallels --parallels-memory 512 --swarm \
    --swarm-discovery="consul://$(docker-machine ip mh-keystore):8500" \
    --engine-opt="cluster-store=consul://$(docker-machine ip mh-keystore):8500" \
    --engine-opt="cluster-advertise=eth0:2376" mhs-demo2

echo "*****************************************************"
echo "Created MHS2 Host: $(docker-machine ip mhs-demo2)    "
echo "*****************************************************"

eval $(docker-machine env --swarm mhs-demo0)
docker network create --driver overlay dockernet

#list of machines created
docker-machine ls

#check all
eval $(docker-machine env --swarm mhs-demo0)

#List of docker nodes
consul watch -type keyprefix -http-addr=$(docker-machine ip mh-keystore):8500 -prefix /docker/nodes | JQ '.[].Key'

docker ps -a
docker info
docker network inspect dockernet
#!/bin/bash
#
echo "Cleanup of old instances"

eval $(docker-machine env mhs-demo0) && docker rm -f  mongo0 > /dev/null 2>&1
eval $(docker-machine env mhs-demo1) && docker rm -f  mongo1 > /dev/null 2>&1
eval $(docker-machine env mhs-demo2) && docker rm -f  mongo2 > /dev/null 2>&1

echo "Starting mongo0 ..."
eval $(docker-machine env mhs-demo0)
docker run -d --name=mongo0 --hostname mongo0 -p 27017:27017 --net=dockernet --env="constraint:node==mhs-demo0" mongo --replSet "rs0"
echo "****************************"

echo "Starting mongo1 ..."
eval $(docker-machine env mhs-demo1)
docker run -d --name=mongo1 --hostname mongo1 -p 27017:27017 --net=dockernet --env="constraint:node==mhs-demo1" mongo --replSet "rs0"
echo "****************************"

echo "Starting mongo2 ..."
eval $(docker-machine env mhs-demo2)
docker run -d --name=mongo2 --hostname mongo2 -p 27017:27017 --net=dockernet --env="constraint:node==mhs-demo2" mongo --replSet "rs0"
echo "****************************"

sleep 5

#list of internal IP's available via dockernet
eval $(docker-machine env --swarm mhs-demo0)
echo "Mongo Containers internal IP's:"
docker inspect --format '{{ .NetworkSettings.Networks.dockernet.IPAddress }}' mongo0
docker inspect --format '{{ .NetworkSettings.Networks.dockernet.IPAddress }}' mongo1
docker inspect --format '{{ .NetworkSettings.Networks.dockernet.IPAddress }}' mongo2
docker ps -a

echo "****************************"
echo "Enabling replica:"

docker exec -i mongo0 mongo << EOF 
use admin
rs.initiate()

sleep(5000)
print("Adding replica member mongo1")
rs.add("mongo1")

sleep(5000)
print("Adding replica member mongo2")
rs.add("mongo2")

sleep(5000)
print("Status:")
rs.conf()
quit()
EOF

echo "****************************"
echo "Mongo replica:"
docker logs -t mongo0 | grep ReplicationExecutor | grep state | grep -v STARTUP | tail -n 2

echo "****************************"
echo "Mongo Replica Cluser: "
echo "$(docker-machine ip mhs-demo0):27017,$(docker-machine ip mhs-demo1):27017,$(docker-machine ip mhs-demo2):27017"
echo "****************************"
echo "done."
echo "****************************"
#!/bin/bash

clear
docker-machine start mh-keystore
docker-machine start mhs-demo0
docker-machine start mhs-demo1
docker-machine start mhs-demo2

eval $(docker-machine env mh-keystore)
echo $DOCKER_MACHINE_NAME
docker start consul
docker ps -a
echo "****************************"

eval $(docker-machine env mhs-demo0)
echo $DOCKER_MACHINE_NAME
docker start swarm-agent-master
docker start swarm-agent
docker start mongo0
docker ps -a
echo "****************************"

eval $(docker-machine env mhs-demo1)
echo $DOCKER_MACHINE_NAME
docker start swarm-agent
docker start mongo1
docker ps -a
echo "****************************"

eval $(docker-machine env mhs-demo2)
echo $DOCKER_MACHINE_NAME
docker start swarm-agent
docker start mongo2
docker ps -a
echo "****************************"

sleep 5
echo "Docker nodes:"
consul watch -type keyprefix -http-addr=$(docker-machine ip mh-keystore):8500 -prefix /docker/nodes | JQ '.[].Key'

eval $(docker-machine env --swarm mhs-demo0)
echo "Docker containers:"
docker ps -a

sleep 3
echo "****************************"
echo "Mongo replica:"
docker logs -t mongo0 | grep ReplicationExecutor | grep state | grep -v STARTUP | tail -n 2

#docker exec -i mongo0 mongo << EOF 
#use admin
#rs.status()
#quit()
#EOF

echo "****************************"
echo "Mongo Replica Cluser: "
echo "$(docker-machine ip mhs-demo0):27017,$(docker-machine ip mhs-demo1):27017,$(docker-machine ip mhs-demo2):27017"
echo "****************************"
echo "done."
echo "****************************"



#!/bin/bash

eval $(docker-machine env mhs-demo0) && docker stop  mongo0 > /dev/null 2>&1
eval $(docker-machine env mhs-demo1) && docker stop  mongo1 > /dev/null 2>&1
eval $(docker-machine env mhs-demo2) && docker stop  mongo2 > /dev/null 2>&1

docker ps -a
echo "****************************"

docker-machine stop mhs-demo1
docker-machine stop mhs-demo2
docker-machine stop mhs-demo0
docker-machine stop mh-keystore

echo "****************************"
echo "done."
echo "****************************"

以上是关于sh 具有docker-machine-parallels的多主机群网络上的MongoDB集群的主要内容,如果未能解决你的问题,请参考以下文章

sh 删除具有指定前缀的文件

sh 列出具有特定父目录的文件

sh 具有短期和长期期权的Bash脚本

sh 具有短期和长期期权的Bash脚本

sh 具有SSH对密钥的服务器

sh [Xen - 列出具有UUID的VM]列出所有具有UUID #Xen的VM