docker三剑客之应用machine,compose,swarm

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker三剑客之应用machine,compose,swarm相关的知识,希望对你有一定的参考价值。

利用Docker-Machine创建虚拟Docker主机

## 下载docker-machine  https://github.com/docker/machine/releases
[email protected]:~$ curl -L https://github.com/docker/machine/releases/download/v0.16.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
## 查看版本号
[email protected]:~$ docker-machine -v
docker-machine version 0.16.0, build 702c267f
## 下载并安装virtualbox 参见 http://blog.51cto.com/autophp/2320962
[[email protected] ~]$ VBoxManage -v 报错
[[email protected] ~]$ sudo /sbin/vboxconfig
提示先安装gcc
[[email protected] ~]$ sudo yum -y install gcc
[[email protected] ~]$ sudo /sbin/vboxconfig
[[email protected] ~]$ VBoxManage -v
5.2.22r126460
成功
## 下载boot2docker.iso(一个基于Tiny Core Linux的系统,里面Docker啥的都帮你搞定了 v18.06.1-ce版本) 
https://github.com/boot2docker/boot2docker/releases/
## 首先检查 /home/sucd/.docker/machine/cache 是否存在。不存在则创建
## 将 boot2docker.iso 文件 拷贝到 **/cache 上述目录下
## 创建虚拟机v1
[[email protected] ~]$ docker-machine create --driver=virtualbox v1
[[email protected] ~]$ docker-machine ls
NAME   ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER     ERRORS
v1     -        virtualbox   Running   tcp://192.168.99.100:2376           v18.09.0   
说明虚拟机创建成功
## docker-machine相关命令
doceker-machine ls   ## 列出所有虚拟机
doceker-machine ip 虚拟机名称  ## 查看IP地址
doceker-machine stop/start 虚拟机名称 ## 停止和开启
doceker-machine env 虚拟机名称 ## 环境变量
doceker-machine ssh 虚拟机名称 ## SSH方式连接
## 移植images,具体操作步骤如下
[[email protected] ~]$ docker pull registry
[[email protected] ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              nginx               14d81ad3d755        44 hours ago        303MB
centos              httpd               bbe52af5c400        44 hours ago        333MB
centos              latest              75835a67d134        6 weeks ago         200MB
registry            latest              2e2f252f3c88        2 months ago        33.3MB
[[email protected] ~]$ docker run -d -p 5000:5000 --name registry registry
85dfccab3f3a70da31068871be7ef56fd2e9eb651f900ee0c72d914090f3a46d
[[email protected] ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
85dfccab3f3a        registry            "/entrypoint.sh /etc…"   12 seconds ago      Up 7 seconds        0.0.0.0:5000->5000/tcp   registry
[[email protected] ~]$ docker tag centos:httpd localhost:5000/web
[[email protected] ~]$ docker push localhost:5000/web
[[email protected] ~]$ curl http://localhost:5000/v2/_catalog
{"repositories":["web"]}
## 在虚拟机中pull镜像
[[email protected] ~]$ docker-machine ssh v1
[email protected]:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[email protected]:~$ docker pull 192.168.99.1:5000/web
Using default tag: latest
Error response from daemon: Get https://192.168.99.1:5000/v2/: http: server gave HTTP response to HTTPS client
提示报错,需要用hppts,我们可以让其不用https, 在主机 /etc/docker/daemon.json文件中加入 "insecure-registies":["192.168.99.1:5000"] 
-- 主机中
[[email protected] ~]$ sudo vim /etc/docker/daemon.json
[[email protected] ~]$ cat /etc/docker/daemon.json 
{
  "registry-mirrors": ["https://z6z6dcky.mirror.aliyuncs.com"],
  "insecure-registries":["192.168.99.1:5000"]
}
[[email protected] ~]$ sudo systemctl daemon-reload 
[[email protected] ~]$ sudo systemctl restart docker
[[email protected] ~]$ docker start registry 
-- 虚拟机中(先配置文件后退出)
[email protected]:~$ cat /etc/docker/daemon.json 
{
  "insecure-registries":["192.168.99.1:5000"]   
}
[[email protected] ~]$ docker-machine restart v1
[[email protected] ~]$ docker-machine ssh v1
[email protected]:~$ docker pull 192.168.99.1:5000/web

利用Docker-Machine在虚拟机中使用compose启动nginx负载均衡实例,设置IP转发在windows中访问

## 配置虚拟机docker用户环境变量(1、虚拟机中创建 .local/bin/目录  2、复制宿主机的.bash_profile到虚拟机 3、重新登录虚拟机验证)
[[email protected] ~]$ docker-machine ssh v1
[email protected]:~$ mkdir -p .local/bin/
[[email protected] ~]$ docker-machine scp /home/sucd/.bash_profile v1:/home/docker/
[[email protected] ~]$ docker-machine ssh v1
[email protected]:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/apps/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/docker/.local/bin:/home/docker/bin
## 拷贝 docker-compose
[[email protected] ~]$ docker-machine scp /usr/local/bin/docker-compose v1:/home/docker/.local/bin/
/home/docker/.local/bin/ 是虚拟机用户的一个环境变量(也可以自己设置)
[email protected]:~$ docker-compose -v
docker-compose version 1.23.1, build b02f1306
## 拷贝之前做的 compose配置文件
[[email protected] ~]$ docker-machine scp -r mycompose/ v1:/home/docker
## 虚拟机创建一个空文件夹 nginx(不用进入虚拟机)
[[email protected] ~]$ docker-machine ssh v1 mkdir nginx
## 挂载虚拟机目录到宿主机目录
[[email protected] ~]$ mkdir mnginx
[[email protected] ~]$ docker-machine mount v1:/home/docker/nginx /home/sucd/mnginx/
You must have a copy of the sshfs binary locally to use the mount feature
## 提示需要安装 sshfs
[[email protected] ~]$ sudo yum -y install epel-release
[[email protected] ~]$ sudo yum -y install fuse-sshfs
## 继续挂载
[[email protected] ~]$ docker-machine mount v1:/home/docker/nginx /home/sucd/mnginx/
## 将nginx目录文件全部复制到 mnginx/
[[email protected] mnginx]$ cd /home/sucd/nginx/
[[email protected] nginx]$ cp . /home/sucd/mnginx/ -r
## 进入虚拟机中并修改compose配置文件
[[email protected] ~]$ docker-machine ssh v1
[email protected]:~$ vi mycompose/docker-compose.yaml
[email protected]:~$ cat mycompose/docker-compose.yaml  
services: 
  nginx: 
    container_name: mynginx
    image: "nginx"
    ports: 
      - "9090:80"
    privileged: true
    volumes: 
      - "/home/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
    networks:
      mynginx-net:
        ipv4_address: 192.156.0.2
    healthcheck: 
      test: ["CMD", "curl","-s","-f", "http://localhost:80"]
      interval: 5s
      timeout: 5s
      retries: 3

  web1: 
    container_name: web1
    image: "web"
    ports: 
      - "8080:80"
    privileged: true
    volumes: 
      - "/home/docker/nginx/web1/:/var/www/html/"
    networks:
      mynginx-net:
        ipv4_address: 192.156.0.6
    healthcheck: 
      test: ["CMD", "curl","-s","-f", "http://localhost:80"]
      interval: 5s
      timeout: 5s
      retries: 3

  web2: 
    container_name: web2
    image: "web"
    ports: 
      - "8081:80"
    privileged: true
    volumes: 
      - "/home/docker/nginx/web2/:/var/www/html/"
    networks: 
      mynginx-net: 
        ipv4_address: 192.156.0.3
    healthcheck:
      test: ["CMD", "curl","-s","-f", "http://localhost:80"]
      interval: 5s
      timeout: 5s
      retries: 3

networks: 
  mynginx-net: 
    driver: bridge
    ipam: 
      config: 
        - subnet: 192.156.0.0/16 
version: "3"
## 进入mycompose并且启动多个服务
[email protected]:~$ cd mycompose/
[email protected]:~/mycompose$ docker-compose up -d
## 测试,宿主机中访问虚拟机服务web1和web2交替出现说明成
[[email protected] ~]$ curl $(docker-machine ip v1):9090
web1
[[email protected] ~]$ curl $(docker-machine ip v1):9090
web2

Docker Swarm + Docker-machine创建集群

## 先创建两个虚拟机
[[email protected] ~]$ docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.90.1/24" m1
[[email protected] ~]$ docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.90.1/24" v1
[[email protected] ~]$ docker-machine ls
NAME   ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER     ERRORS
m1     -        virtualbox   Running   tcp://192.168.90.100:2376           v18.09.0   
v1     -        virtualbox   Running   tcp://192.168.90.101:2376           v18.09.0 
## 初始化一个集群 
[[email protected] ~]$ docker-machine ssh m1
[email protected]:~$ docker swarm init --advertise-addr 192.168.99.100
[email protected]:~$ docker swarm join-token worker 
## 加入节点
[[email protected] ~]$ docker-machine ssh v1 docker swarm join --token SWMTKN-1-0zlzmet1ggrmlbeijj9oalsotaj2edlqnkoezuyi7h397srims-5kxodjp43mklqwfp1uh46diak 192.168.90.100:2377
This node joined a swarm as a worker.
[email protected]:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
4a9jw24oq7hx0qenh5ayqw3v7 *   m1                  Ready               Active              Leader              18.09.0
hy5hw7xpvwmlhuzsikp40x39m     v1                  Ready               Active                                  18.09.0
## 创建一个纯净的apache镜像
[[email protected] ~]$ docker pull httpd
[[email protected] ~]$ docker tag httpd:latest localhost:5000/httpd
[[email protected] ~]$ docker push localhost:5000/httpd
[[email protected] ~]$ docker rmi localhost:5000/httpd:latest 
镜像的网站路径: /usr/local/apache2/htdocs/
配置文件: /usr/local/apach2/conf/httpd.conf
## 创建服务
在m1中  加入/etc/docker/daemon.json文件
sudo /etc/init.d/docker restart   # 重启docker
docker pull 192.168.90.1:5000/httpd
docker tag 192.168.90.1:5000/httpd httpd && docker rmi 192.168.90.1:5000/httpd
在m1中创建空文件夹web,并添加一个index.html文件
doceker-machine ssh m1 mkdir web
在v1中执行相同的步骤
## 最后在m1中执行创建服务
[email protected]:~$ docker service create   --replicas 2   --name myweb   --publish 9091:80   --mount type=bind,source=/home/docker/web/,target=/usr/local/apache2/htdocs/   httpd
## 查看服务列表
[email protected]:~$ docker service ls 
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
v03oigi0wp0z        myweb               replicated          2/2                 httpd:latest        *:9090->80/tcp

以上是关于docker三剑客之应用machine,compose,swarm的主要内容,如果未能解决你的问题,请参考以下文章

docker——三剑客之Docker Machine

docker三剑客之docker-machine

Docker:Docker 三剑客之 Docker Machine

Docker三剑客之docker-machine

Docker三剑客之Machine

Linux企业运维——Docker三剑客之Docker Machine