1. 程式人生 > >Docker(二) docker-machine和docker-compose

Docker(二) docker-machine和docker-compose

docker images

docker container

單個應用程式 --非叢集

Docker machine

docker machine是一個便於在多平臺上部署Docker的一個工具;多平臺比如:

  • Amazon Web Services
  • Microsoft Azure
  • Digital Ocean
  • Exoscale
  • Google Compute Engine
  • Generic
  • Microsoft Hyper-V
  • OpenStack
  • Rackspace
  • IBM Softlayer
  • Oracle VirtualBox
  • VMware vCloud Air
  • VMware Fusion
  • VMware vSphere
  • VMware Workstation(plugin)

install docker-machine

$ curl -L https://github.com/docker/machine/releases/download/v0.13.0/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine &&
chmod +x /tmp/docker-machine &&
sudo cp /tmp/docker-machine /usr/local/bin/docker-machine

Configure Script

scripts=( docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine.bash ); for i in "${scripts[@]}"; do sudo wget https://raw.githubusercontent.com/docker/machine/v0.13.0/contrib/completion/bash/${i} -P /etc/bash_completion.d; done

A demo

docker-machine create -d virtualbox local

docker-machine create --driver generic --generic-ip-address 10.0.0.4 vm02

[email protected]
:/home/marion# docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS local - virtualbox Stopped Unknown vm01 - generic Running tcp://10.0.0.3:2376 v17.11.0-ce [email protected]:/home/marion# docker-machine create -d virtualbox default Running pre-create checks... Creating machine... (default) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/default/boot2docker.iso... (default) Creating VirtualBox VM... (default) Creating SSH key... (default) Starting the VM... (default) Check network to re-create if needed... (default) Waiting for an IP... Waiting for machine to be running, this may take a few minutes... Detecting operating system of created instance... Waiting for SSH to be available... Detecting the provisioner... Provisioning with boot2docker... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon... Checking connection to Docker... Docker is up and running! To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env default # 這是一個沒有提供驅動的並且已經安裝好docker engine的docker host. 但是依舊可以使用docker-machine管理 $ docker-machine create --driver none --url=tcp://50.134.234.20:2376 custombox $ docker-machine ls NAME ACTIVE DRIVER STATE URL custombox * none Running tcp://50.134.234.20:2376

docker machine預設使用的作業系統

docker machine的新特性

Docker machine的provider

docker machine provider

  • 常見的linux系統
  • 虛擬化平臺-virtualbox,vmware,hyper-v
  • openstack
  • Amazon web services,Microsoft Azure,Google computer Engine,Digital Ocean等

安裝docker machine

curl -L https://github.com/docker/machine/releases/download/v0.13.0/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine &&
chmod +x /tmp/docker-machine &&
sudo cp /tmp/docker-machine /usr/local/bin/docker-machine

安裝bash completion指令碼

scripts=( docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine.bash ); for i in "${scripts[@]}"; do sudo wget https://raw.githubusercontent.com/docker/machine/v0.13.0/contrib/completion/bash/${i} -P /etc/bash_completion.d; done

安裝virtualbox

Virtualbox package download url

# 下載deb安裝包安裝
# 注意:如果你是在vmware上做的實驗,請開啟虛擬化Inter-VT/AMD-V

Docker-machine命令使用

Usage: docker-machine [OPTIONS] COMMAND [arg...]

Create and manage machines running Docker.

Version: 0.13.0, build 9ba6da9

Author:
  Docker Machine Contributors - <https://github.com/docker/machine>

Options:
  --debug, -D                   Enable debug mode
  --storage-path, -s "/root/.docker/machine"    Configures storage path [$MACHINE_STORAGE_PATH]
  --tls-ca-cert                 CA to verify remotes against [$MACHINE_TLS_CA_CERT]
  --tls-ca-key                  Private key to generate certificates [$MACHINE_TLS_CA_KEY]
  --tls-client-cert                 Client cert to use for TLS [$MACHINE_TLS_CLIENT_CERT]
  --tls-client-key              Private key used in client TLS auth [$MACHINE_TLS_CLIENT_KEY]
  --github-api-token                Token to use for requests to the Github API [$MACHINE_GITHUB_API_TOKEN]
  --native-ssh                  Use the native (Go-based) SSH implementation. [$MACHINE_NATIVE_SSH]
  --bugsnag-api-token               BugSnag API token for crash reporting [$MACHINE_BUGSNAG_API_TOKEN]
  --help, -h                    show help
  --version, -v                 print the version

Commands:
  active        Print which machine is active
  config        Print the connection config for machine
  create        Create a machine
  env           Display the commands to set up the environment for the Docker client
  inspect       Inspect information about a machine
  ip            Get the IP address of a machine
  kill          Kill a machine
  ls            List machines
  provision     Re-provision existing machines
  regenerate-certs  Regenerate TLS Certificates for a machine
  restart       Restart a machine
  rm            Remove a machine
  ssh           Log into or run a command on a machine with SSH.
  scp           Copy files between machines
  mount         Mount or unmount a directory from a machine with SSHFS.
  start         Start a machine
  status        Get the status of a machine
  stop          Stop a machine
  upgrade       Upgrade a machine to the latest version of Docker
  url           Get the URL of a machine
  version       Show the Docker Machine version or a machine docker version
  help          Shows a list of commands or help for one command

參考連結

Docker compose

docker compose就是通過yml檔案來定義和執行多個容器docker應用程式的工具,三步過程就能跑起一個compose:

  • 定義應用程式的環境(yml中)
  • 定義組成應用程式的服務(docker-compose.yml)
  • docker-compose up啟動整個應用程式

安裝docker compose

依賴:docker engine

# download the latest version
sudo curl -L https://github.com/docker/compose/releases/download/1.17.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# excutable
sudo chmod +x /usr/local/bin/docker-compose
# check version
docker-compose --version

docker compose

Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate Compose file (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name (default: directory name)
  --verbose                   Show more output
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the name specified
                              in the client certificate (for example if your docker host
                              is an IP address)
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)

  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  config             Validate and view the compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pulls service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

6.3 create a demo

  • Create a directory for the project
  • create application
  • Create another file called requirements.txt in your project directory and paste this in
  • Create a Dockerfile
  • Define services in a Compose file (docker-compose.yml)
  • Build and run your app with Compose
  • access your application

6.4 deploy wordpress with docker-compose

version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: rootpasswd
       MYSQL_DATABASE: wordpress
       MYSQL_USER: marion
       MYSQL_PASSWORD: marionpasswd

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: marion
       WORDPRESS_DB_PASSWORD: marionpasswd
volumes:
    db_data:

docker compose ref link

docker service

分散式負載均衡服務方式,定義了服務在叢集中是怎麼執行的

docker swarm

  • docker daemon port(2376)
  • docker swarm manager port(2377)
# install docker-machine
# 如果是在虛擬機器中使用virtualbox,請開啟虛擬化模式
docker-machine create \
 --driver virtualbox \
 --virtualbox-boot2docker-url file:///home/marion/boot2docker-v17.09.1-ce.iso \
 manager

# docker-machine在建立docker host時會下載預設最新版本的boot2docker,但是因為網路會下載很慢甚至報錯,因此先下載到本地
docker-machine create \
 --driver virtualbox \
 --virtualbox-boot2docker-url file:///home/marion/boot2docker-v17.09.1-ce.iso \
 worker

# 檢視docker host分配的資訊
[email protected]:~# docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
manager   -        virtualbox   Running   tcp://192.168.99.100:2376           v17.09.1-ce   
worker    -        virtualbox   Running   tcp://192.168.99.101:2376           v17.09.1-ce   

# 將manager節點啟用叢集管理模式,設定當前的manager為叢集管理器
docker-machine ssh manager "docker swarm init --advertise-addr 192.168.99.100"
# 初始化後會返回新增node的命令
# 新增worker加入swarm
[email protected]:~# docker-machine ssh worker "docker swarm join --token SWMTKN-1-2kcyacweb1kno0szfjrjob5ao2v8ie0k428riedz042hnzwhmq-7eegju6j34jisuhqbq9pet3mk 192.168.99.100:2377"
This node joined a swarm as a worker.
[email protected]:~# 

# 在manager上檢視swarm中的節點
[email protected]:~# docker-machine ssh manager "docker node ls"
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
p0fmsvxbpptktwwjjey7hzfat *   manager             Ready               Active              Leader
i8ef198m02bowqkx99nisgsjc     worker              Ready               Active              

# 配置你的docker-machine shell終端
scripts=( docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine.bash ); for i in "${scripts[@]}"; do sudo wget https://raw.githubusercontent.com/docker/machine/v0.13.0/contrib/completion/bash/${i} -P /etc/bash_completion.d; done

# 檢視docker host的環境變數配置
docker-machine env manager

# Run this command to configure your shell
eval $(docker-machine env manager)

# 檢視處在Active狀態的docker host(有星號提示)
[email protected]ckermaster:~# docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
manager   *        virtualbox   Running   tcp://192.168.99.100:2376           v17.09.1-ce   
worker    -        virtualbox   Running   tcp://192.168.99.101:2376           v17.09.1-ce 

[email protected]:~# docker stack deploy -c docker-compose.yaml managertest
Creating network managertest_webnet
Creating service managertest_web
[email protected]:~# docker stack ps managertest
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE              ERROR               PORTS
kno7kg5g2eil        managertest_web.1   nginx               manager             Running             Preparing 25 seconds ago                       
kzu28xbn6th6        managertest_web.2   nginx               worker              Running             Preparing 25 seconds ago                       
706xagf904f2        managertest_web.3   nginx               worker              Running             Preparing 25 seconds ago                       
6phm8f6c9n2p        managertest_web.4   nginx               manager             Running             Preparing 25 seconds ago                       
3ee4hhlspmme        managertest_web.5   nginx               worker              Running             Preparing 25 seconds ago                       
[email protected]:~# eval $(docker-machine env -u)
[email protected]:~# docker ps
CONTAINER ID        IMAGE                       COMMAND                CREATED             STATUS              PORTS                    NAMES
a7ea6e30b068        grafana/grafana             "/run.sh"              6 hours ago         Up 39 minutes       3000/tcp                 grafana1
a31b65a7fd4d        prom/node-exporter:latest   "/bin/node_exporter"   18 hours ago        Up 39 minutes       9100/tcp                 monitoring_node_exporter
7c8311be2b4f        grafana/grafana             "/run.sh"              18 hours ago        Up 39 minutes       0.0.0.0:3000->3000/tcp   grafana

[email protected]:~# eval $(docker-machine env worker)
[email protected]:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
73745f698d12        nginx:latest        "nginx -g 'daemon ..."   2 minutes ago       Up 2 minutes        80/tcp              managertest_web.2.kzu28xbn6th6c7dxnotn7f90v
99c0c638629f        nginx:latest        "nginx -g 'daemon ..."   2 minutes ago       Up 2 minutes        80/tcp              managertest_web.3.706xagf904f2j97tlwy2kkh1o
67dc57a4278e        nginx:latest        "nginx -g 'daemon ..."   2 minutes ago       Up 2 minutes        80/tcp              managertest_web.5.3ee4hhlspmmef8ksdstb92drd

[email protected]:~# eval $(docker-machine env manager)
[email protected]:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
4ca0c1899c55        nginx:latest        "nginx -g 'daemon ..."   3 minutes ago       Up 3 minutes        80/tcp              managertest_web.1.kno7kg5g2eilylcybo642493k
354e61f3665b        nginx:latest        "nginx -g 'daemon ..."   3 minutes ago       Up 3 minutes        80/tcp              managertest_web.4.6phm8f6c9n2pb9811ud6e7wmp

[email protected]:~# curl -I http://192.168.99.100
HTTP/1.1 200 OK
Server: nginx/1.13.7
Date: Tue, 26 Dec 2017 08:32:24 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 21 Nov 2017 14:28:04 GMT
Connection: keep-alive
ETag: "5a1437f4-264"
Accept-Ranges: bytes

[email protected]:~# curl -I http://192.168.99.101
HTTP/1.1 200 OK
Server: nginx/1.13.7
Date: Tue, 26 Dec 2017 08:32:28 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 21 Nov 2017 14:28:04 GMT
Connection: keep-alive
ETag: "5a1437f4-264"
Accept-Ranges: bytes

# 增加副本或者減少副本,執行修改docker-compose.yaml檔案
vim docker-compose.yaml(replicats:3)
eval $(docker-machine env manager)
docker stack deploy -c docker-compose.yaml managertest
docker ps
[email protected]:~# docker service ps managertest_web
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
706xagf904f2        managertest_web.3   nginx               worker              Running             Running 14 minutes ago                       
6phm8f6c9n2p        managertest_web.4   nginx               manager             Running             Running 15 minutes ago                       
3ee4hhlspmme        managertest_web.5   nginx               worker              Running             Running 14 minutes ago

docker stack ls                                            # List stacks or apps
docker stack deploy -c <composefile> <appname>  # Run the specified Compose file
docker service ls                 # List running services associated with an app
docker service ps <service>                  # List tasks associated with an app
docker inspect <task or container>                   # Inspect task or container
docker container ls -q                                      # List container IDs
docker stack rm <appname>                             # Tear down an application
docker swarm leave --force      # Take down a single node swarm from the manager