timqian / my-notes

https://github.com/timqian/my-notes/issues
7 stars 2 forks source link

Docker toolbox: Machine, Compose, and Swarm #140

Open timqian opened 7 years ago

timqian commented 7 years ago

Docker Machine

Create container hosts on many of the most popular Infrastructure-as-a-Service platforms including: VMware Fusion and VirtualBox, AWS, Azure, DigitalOcean, Exoscale, Google Compute Engine, OpenStack, RackSpace, SoftLayer, VMware vSphere, and vCloud Air.

# Sample usage to create a container host using VirtualBox driver
docker-machine create --driver virtualbox containerhost 

Docker Compose

Running some useful containers on container host. Define container info using docker-compose.yml

# Example docker-compose.yml
# The application is built using two containers web and redis
# To run: `docker-compose up`
web: 
  build: . # built from a Dockerfile in the current working directory
  ports: # shows which ports will be exposed on the host and which port the traffic will be forwarded to into the container
    - "5000:5000" 
  volumes: # mount a Docker volume into the container, containing the application code. 
    - .:/code 
  links:  # link to another container
    - redis 
redis: 
  image: redis

Docker swarm

turn that small test environment into a larger setup of clustered container hosts that can be used to scale your operations

Ref

timqian commented 7 years ago

Manage servers Install software, start server on server Scan servers(不用重新配置

Swarm managers are the only machines in a swarm that can execute your commands, or authorize other machines to join the swarm as workers. Workers are just there to provide capacity and do not have the authority to tell any other machine what it can and cannot do.

The basic concept is simple enough: run docker swarm init to enable swarm mode and make your current machine a swarm manager, then run docker swarm join on other machines to have them join the swarm as workers.

About port Always run docker swarm init and docker swarm join with port 2377 (the swarm management port), or no port at all and let it take the default. The machine IP addresses returned by docker-machine ls include port 2376, which is the Docker daemon port. Do not use this port or you may experience errors.

only swarm managers like myvm1 execute Docker commands; workers are just for capacity.

Deploy the app on the swarm manager, and workers 自动部署了一样的?

Connecting to VMs with docker-machine env and docker-machine ssh

You can access your app from the IP address of any of the nodes

Iterating and scaling your app using docker From here you can do everything you learned about in parts 2 and 3. Scale the app by changing the docker-compose.yml file. Change the app behavior by editing code, then rebuild, and push the new image. (To do this, follow the same steps you took earlier to build the app and publish the image). In either case, simply run docker stack deploy again to deploy these changes. You can join any machine, physical or virtual, to this swarm, using the same docker swarm join command you used on myvm2, and capacity will be added to your cluster. Just run docker stack deploy afterwards, and your app will take advantage of the new resources.

eval $(docker-machine env -u) : Unsetting docker-machine shell variable settings

不使用内置的 load balancer https://github.com/moby/moby/issues/23813 Docker 1.13 introduces a mode=host when publishing service ports (see the pull-request here: docker#27917, and docker#28943). Using this mode, ports of the containers (tasks) backing a service are published directly on the host they are running on, bypassing the Routing Mesh (and thus, load-balancer). Keep in mind that as a consequence, only a single task of that service can run on a node. On docker 1.13 and up; the following example creates a myservice service, an port 80 of the task is published on port 8080 of the node that the task is deployed on. docker service create \ --name=myservice \ --publish mode=host,target=80,published=8080,protocol=tcp \ nginx:alpine Contrary to tasks that publish ports through the routing mesh, docker ps also shows the ports that are published for tasks that use "host-mode" publishing (see the PORTS column); CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES acca053effcc nginx@sha256:30e3a72672e4c4f6a5909a69f76f3d8361bd6c00c2605d4bf5fa5298cc6467c2 "nginx -g 'daemon ..." 3 seconds ago Up 2 seconds 443/tcp, 0.0.0.0:8080->80/tcp myservice.1.j7nbqov733mlo9hf160ssq8wd Hope this helps

We can run the file with the docker stack deploy command (also only supported on Compose files version 3.x and up). You could use docker-compose up to run version 3 files with non swarm configurations, Before we can use the docker stack deploy command we’ll first run: docker swarm init docker stack deploy -c docker-compose.yml getstartedlab

docker service ls: swarm 模式下看跑了几个 servise

用 node 写 load balancer http://thecodebarbarian.com/building-your-own-load-balancer-with-express-js http://taobaofed.org/blog/2017/08/31/nodejs-stream/ https://github.com/nodejitsu/node-http-proxy

Use jwt https://news.ycombinator.com/item?id=11895440 https://github.com/davidguttman/authentic

Node + mongo + docker swarm http://clusterhq.com/2016/03/11/fun-with-swarm-part3/

Share image https://github.com/moby/moby/issues/26852 using for TF in $(docker images | sed '1d' | awk '{print $1}'); do \ docker save $TF | ssh node "docker load"; \ done to transfer all locally stored images from a manager to a worker node. I would hope that this could be automated from within the docker cli, so that locally stored images are sent / updated to the node on demand, without requiring a private registry.

$ VPC=vpc-abcd1234 # the VPC to create your nodes in $ REGION=eu-west-1 # the region to use $ SUBNET=subnet-abcd1234 # the subnet to attach your nodes $ ZONE=b # the zone to use

VPC=vpc-2ac8ef4c REGION=us-west-2 SUBNET=subnet-2e5c6875 ZONE=c

VPC=vpc-d041ffb4 REGION=cn-north-1 SUBNET=subnet-3160af55 ZONE=a

timqian commented 7 years ago

Cn steps

cat ~/.aws/credentials

export AWS_PROFILE= timqian\'sUp

VPC=vpc-d041ffb4 REGION=cn-north-1 SUBNET=subnet-3160af55 ZONE=a

docker-machine create -d amazonec2 \ --amazonec2-ami ami-0220b23b \ --engine-install-url https://raw.githubusercontent.com/timqian/docker-realted/master/get_docker.sh \ --engine-registry-mirror https://registry.docker-cn.com \ --amazonec2-vpc-id $VPC \ --amazonec2-region $REGION \ --amazonec2-zone $ZONE \ --amazonec2-instance-type t2.micro \ --amazonec2-subnet-id $SUBNET \ --amazonec2-security-group download-swarm \ BIM-docker-swarm-manager

https://www.alibabacloud.com/help/zh/doc-detail/44779.htm

https://fangs.work/blog/%E7%A8%8B%E5%BA%8F/%E6%9D%82%E9%A1%B9/Common-Repo-Mirror/

https://docs.docker.com/machine/reference/create/

VPC=vpc-5f9f653b REGION=cn-north-1 SUBNET=subnet-be1020db ZONE=a