Skip to content

Instantly share code, notes, and snippets.

@victorskl
Last active April 27, 2018 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorskl/ed8893efbfedf39532c1a8d08f2906a5 to your computer and use it in GitHub Desktop.
Save victorskl/ed8893efbfedf39532c1a8d08f2906a5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# NOTE: invoke this from MacOS host
# REF https://github.com/AURIN/comp90024/tree/master/couchdb
# Cluster setup
# Pull Docker image
docker pull couchdb:2.1.1
# Set node IP addresses, electing the first as "master node" and admin credentials
declare -a nodes=(172.17.0.2 172.17.0.3 172.17.0.4)
export masternode=`echo ${nodes} | cut -f1 -d' '`
export othernodes=`echo ${nodes[@]} | sed s/${masternode}//`
export size=${#nodes[@]}
export user=admin
export pass=admin
# Create Docker containers
# Create other nodes without EXPOSE port to host
for node in ${othernodes[@]}}; do docker create couchdb:2.1.1 --ip=${node}; done
sleep 3
# Create master node with EXPOSE port to host
docker create -p 4369:4369 -p 5984:5984 -p 9100:9100 couchdb:2.1.1 --ip=${masternode}
# Put in conts the Docker container IDs
declare -a conts=(`docker ps --all | grep couchdb | cut -f1 -d' ' | xargs -n${size}`)
# Start the containers
for cont in "${conts[@]}"; do docker start ${cont}; done
sleep 3
# Write the cookie name and node name to the CouchDB configuration on every node
for (( i=0; i<${size}; i++ )); do
docker exec ${conts[${i}]} \
bash -c "echo \"-setcookie couchdb_cluster\" >> /opt/couchdb/etc/vm.args"
docker exec ${conts[${i}]} \
bash -c "echo \"-name couchdb@${nodes[${i}]}\" >> /opt/couchdb/etc/vm.args"
done
# Restart containers to pick-up changes to CouchDB configurations
for cont in "${conts[@]}"; do docker restart ${cont}; done
sleep 3
echo ""
echo -e "Container ID - \tName - \tIP"
# Get Container ID, Name and IP
docker inspect --format '{{ .Config.Hostname }} - {{ .Name }} - {{ .NetworkSettings.IPAddress }}' $(docker ps|grep couchdb|cut -f1 -d' ')
echo ""
echo "Your master node is IP: " ${masternode}
echo "You should enter to this master node by 'docker exec -it <ContainerID_Or_ContainerName> bash'"
echo ""
echo "Alternatively, you can enter into container using https://kitematic.com"
echo ""
echo "Then continue to part2..."
echo ""
docker ps
# ---
#!/usr/bin/env bash
# NOTE: invoke this inside master node i.e. 172.17.0.2
# How to
#
# 1. You must enter into the master node container
# docker exec -it <ContainerID_Or_ContainerName> bash
#
# 2. Then download this part2 script - note the following is one line
# curl -s https://gist.githubusercontent.com/victorskl/ed8893efbfedf39532c1a8d08f2906a5/raw/dab73ac3b859dc0081284ea23bf60528a0b7dc09/couchdb_mac_docker_part2.sh -o couchdb_mac_docker_part2.sh
#
# 3. Then invoke like this
# bash couchdb_mac_docker_part2.sh
#
# REF https://github.com/AURIN/comp90024/tree/master/couchdb
# We still need this variables
# Set node IP addresses, electing the first as "master node" and admin credentials
declare -a nodes=(172.17.0.2 172.17.0.3 172.17.0.4)
export masternode=`echo ${nodes} | cut -f1 -d' '`
export othernodes=`echo ${nodes[@]} | sed s/${masternode}//`
export size=${#nodes[@]}
export user=admin
export pass=admin
# Set the CouchDB cluster (deleting the default nonode@nohost node from the configuration)
for node in "${nodes[@]}"; do
curl -XPUT "http://${node}:5984/_node/_local/_config/admins/${user}" --data "\"${pass}\""
curl -XPUT "http://${user}:${pass}@${node}:5984/_node/couchdb@${node}/_config/chttpd/bind_address" --data '"0.0.0.0"'
done
for node in "${nodes[@]}"; do
curl -XPOST "http://${user}:${pass}@${masternode}:5984/_cluster_setup" \
--header "Content-Type: application/json" \
--data "{\"action\": \"enable_cluster\", \"bind_address\":\"0.0.0.0\", \
\"username\": \"${user}\", \"password\":\"${pass}\", \"port\": \"5984\", \
\"remote_node\": \"${node}\", \
\"remote_current_user\":\"${user}\", \"remote_current_password\":\"${pass}\"}"
done
for node in "${nodes[@]}"; do
curl -XPOST "http://${user}:${pass}@${masternode}:5984/_cluster_setup" \
--header "Content-Type: application/json" \
--data "{\"action\": \"add_node\", \"host\":\"${node}\", \
\"port\": \"5984\", \"username\": \"${user}\", \"password\":\"${pass}\"}"
done
curl -XPOST "http://${user}:${pass}@${masternode}:5984/_cluster_setup" \
--header "Content-Type: application/json" --data "{\"action\": \"finish_cluster\"}"
rev=`curl -XGET "http://172.17.0.2:5986/_nodes/nonode@nohost" --user "${user}:${pass}" | sed -e 's/[{}"]//g' | cut -f3 -d:`
curl -X DELETE "http://172.17.0.2:5986/_nodes/nonode@nohost?rev=${rev}" --user "${user}:${pass}"
# Check the correct cluster configuration
for node in "${nodes[@]}"; do curl -X GET "http://${user}:${pass}@${node}:5984/_membership"; done
# Adding a database to one node of the cluster cause it to be created on all other nodes as well
curl -XPUT "http://${user}:${pass}@${masternode}:5984/twitter"
for node in "${nodes[@]}"; do curl -X GET "http://${user}:${pass}@${node}:5984/_all_dbs"; done
#--
#!/usr/bin/env bash
# NOTE: invoke this from MacOS host
# REF https://github.com/AURIN/comp90024/tree/master/couchdb
# We still need this variables
# Set node IP addresses, electing the first as "master node" and admin credentials
declare -a nodes=(172.17.0.2 172.17.0.3 172.17.0.4)
export masternode=`echo ${nodes} | cut -f1 -d' '`
export othernodes=`echo ${nodes[@]} | sed s/${masternode}//`
export size=${#nodes[@]}
export user=admin
export pass=admin
# Cluster management
# (First run the "Set node IP addresses, electing the first as "master node" and admin credentials" above)
# Put in conts the Docker container IDs
declare -a conts=(`docker ps --all | grep couchdb | cut -f1 -d' ' | xargs -n${size}`)
# Starts the cluster
#for cont in "${conts[@]}"; do docker start ${cont}; done
#sleep 3
# Shutdowns the cluster nicely
for cont in "${conts[@]}"; do docker stop ${cont}; done
# Deletes the cluster containers
for cont in "${conts[@]}"; do docker rm --force ${cont}; done
echo ""
echo "All Gone!"
#--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment