Last active
December 19, 2017 12:44
-
-
Save safizn/29b0995a0549c45e4f559e65cd29dc6e to your computer and use it in GitHub Desktop.
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
# send request through proxy | |
curl -i $(docker-machine ip node-1)/demo/hello |
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
# List docker swarm nodes/machine in the cluster: (send to manager) | |
docker node ls | |
# update Node and add variables: as a way to identify nodes and deploy only to specific nodes. | |
docker node update --label-add <key>=<value> <node> | |
# check node in which service instace is running on: gets service instance row and prints the 4th coloumn which is the nodes. | |
NODE=$(docker service ps <service> | grep "<service>.<instanceNumber>" | awk '{print $4}') | |
# get Manager | |
docker info --format "{{.Swarm.NodeAddr}}" | |
# create swarm cluster of docker machines | |
docker swarm init --advertise-addr $(docker-machine ip node-1) --listen-addr $(docker-machine ip node-1):2377 | |
# Get token key that allows joining the swarm: | |
docker swarm join-token -q worker | |
# Join the swarm as worker | |
TOKEN=$(docker swarm join-token -q worker) | |
eval $(docker-machine env node-2) | |
docker swarm join --token $TOKEN $(docker-machine ip node-1):2377 | |
# Command sent to the leader machine to output existing agents of the cluster. | |
docker node ls | |
# promote worker to be manager node: | |
docker node promote <nodeName> | |
#demote Manager to become worker: | |
docker node demote <nodeName> | |
# Network List: | |
docker network ls | |
docker network ls -f "driver=overlay" | |
# create network for inter container communication of a service: | |
docker network create --driver overlay go-demo | |
# check existing networks: | |
docker network ls |
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
# Services list: | |
docker service ls | |
# Service information: | |
docker service --pretty inspect <serviceTagName> | |
# Service detailed infomation about running service: | |
docker service ps <serviceName> | |
# remove Service | |
docker service rm <serviceName> | |
# create service that runs on all nodes (spans the whole cluster) - set mode to global: | |
docker service create --name util --network network --mode global alpine sleep 1000000000 | |
# constrain service to specific labeled node | |
docker service create --name util --constraint 'node.labels.env == prod-like' alpine sleep 1000000000 | |
# create container service and make it join a specific network. | |
docker service create --name container-database -p 27017 --network demo-network mongoImage | |
# remove service including replicas | |
docker service rm go-demo | |
# show all services | |
docker service ls | |
# show more information including network id for the service: | |
docker service inspect demo-database | |
# Replicate service (container) instances: | |
docker service update --replicas 5 demo-database | |
# Show more information of specific service and all it’s replicas. | |
docker service ps <nameofservice> | |
# service container logs - it appears there is not an easy way to get logs of container. (doesn’t always work). https://github.com/docker/docker/issues/23710 | |
docker logs $(docker inspect --format "{{.Status.ContainerStatus.ContainerID}}" <containerID>) | |
# Volume --> Mount: in docker service volume is replaced with mount. | |
# check container id of service | |
service="" | |
for f in $(docker service ps -q $service);do docker inspect --format '{{.NodeID}} {{.Status.ContainerStatus.ContainerID}}' $f; done |
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
# Stack of services: | |
docker stack deploy --compose-file <file> <stackName> | |
docker stack ps |
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
#From https://gist.github.com/vfarcic/92eb4eb4397b2cb8fc66779fa665fee7 | |
# docker-compose definition for consul server and worker agents. | |
curl -o docker-compose-proxy.yml https://raw.githubusercontent.com/vfarcic/docker-flow-proxy/master/docker-compose.yml | |
# usage: | |
export DOCKER_IP=$(docker-machine ip <machine>) | |
docker-compose -f docker-compose-proxy.yml up -d consul-server | |
# Test consul functioning: | |
curl -X PUT -d 'this is a test' http://$(docker-machine ip <machine>):8500/v1/kv/msg1 | |
curl http://$(docker-machine ip <machine>):8500/v1/kv/msg1?raw | |
# worket agents for consul | |
export CONSUL_SERVER_IP=$DOCKER_IP | |
eval $(docker-machine env swarm-$i) | |
docker-compose -f docker-compose-proxy.yml up -d consul-agent; | |
# get all keys from consul | |
curl http://$(docker-machine ip <machine>):8500/v1/kv/?recurse | |
# get keys starting with prefix: | |
curl http://$(docker-machine ip <VMNode>):8500/v1/kv/<prefixString>?recurse |
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
#!/usr/bin/env bash | |
for i in 1 2 3; do | |
docker-machine create -d virtualbox swarm-$i | |
done | |
eval $(docker-machine env swarm-1) | |
docker swarm init \ | |
--advertise-addr $(docker-machine ip swarm-1) | |
TOKEN=$(docker swarm join-token -q manager) | |
for i in 2 3; do | |
eval $(docker-machine env swarm-$i) | |
docker swarm join \ | |
--token $TOKEN \ | |
--advertise-addr $(docker-machine ip swarm-$i) \ | |
$(docker-machine ip swarm-1):2377 | |
done | |
for i in 1 2 3; do | |
eval $(docker-machine env swarm-$i) | |
docker node update \ | |
--label-add env=prod \ | |
swarm-$i | |
done | |
echo ">> The swarm cluster is up and running" |
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
docker network create --driver overlay <networkName> |
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
# Ports are not exposed because it will be accessible only by network. | |
docker service create --name <tagName> --network <network1> --network <network2> -e VAR=<value> <image:latest> |
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
# filter results according to conditions: | |
docker ps -q --filter label=com.docker.swarm.service.name=<serviceName> | |
# or | |
docker service ps -f desired-state=Running <serviceName> | |
# more complex | |
eval $(docker service ps -f desired-state=Running proxy | \ | |
tail -n 1 | \ | |
awk '{print "docker-machine ssh "$4" docker rm -f "$2"."$1}') |
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
curl "$(docker-machine ip swarm-1):8080/v1/docker-flow-proxy/reconfigure?serviceName=go-demo&servicePath=/demo&port=8080&distribute=true" |
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
# "drill" is a tool that gets all sorts of infomation out of DNS: Alpine linux uses "apk" | |
# $ID = id of instance/container. | |
docker exec -it $ID apk add --update drill | |
# get information about DNS in network: and IP of service | |
docker exec -it $ID drill <nameOfInstance> | |
# get DNS information with IPs of instances of a service | |
docker exec -it $ID drill tasks.<nameOfInstance> |
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
# "quiet" mode allow only IDs to be returned. | |
ID=$(docker ps --quiet --filter label=com.docker.swarm.service.name=util) | |
# find ID using image that used to create it: (e.g. imageName = vfarcic/docker-flow-proxy) | |
ID=$(docker ps -q --filter "ancestor=<imageName>") |
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
# get current pwd | |
pwd | |
# ssh into node manager | |
docker-machine ssh swarm-1 | |
# Replace $PWD from mount command to the pwd that resulted before ssh to machine. $PWD is a variable that exists and prints the same as pwd command. | |
# I created a variable called PWDHOST | |
$PWDHOST=<result of previous pwd before ssh. | |
--mount "type=bind,source=$PWDHOST,target=<targetOnService>" |
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
TOKEN=$(docker swarm join-token -q manager) |
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
TOKEN=$(docker swarm join-token -q worker) |
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
ManagerVM=; | |
eval $(docker-machine env ManagerVM) | |
docker swarm init --advertise-addr $(docker-machine ip ManagerVM) |
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
# Issue - Docker swarm doesn't expose port | |
this problem happens after docker engine upgrade or reset of cloud vm or stopping & starting vm, will all prevent the swarm network mesh from exposing the ports. |
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
# Define token either worker or manager. | |
TOKEN=$(); | |
VM=; | |
ManagerVM=; | |
eval $(docker-machine env VM) | |
docker swarm join --token $TOKEN --advertise-addr $(docker-machine ip VM) $(docker-machine ip ManagerVM):2377 |
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
_ |
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
# find out on which node a service is running: | |
# tail -n +2 : remove header | |
# awk '{print $4}' : get 4th column values. | |
NODE=$(docker services ps <service> | tail -n +2 | awk '{print $4}') |
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
docker service scale <serviceName>=<numberInstances> |
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
dockerservice update --image <image> <serviceName> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment