建立consul server虛擬主機node
docker-machine create consul
複製代碼
出現以下內容即建立成功nginx
Running pre-create checks...
Creating machine...
(consul) Copying /Users/enoch/.docker/machine/cache/boot2docker.iso to /Users/enoch/.docker/machine/machines/consul/boot2docker.iso...
(consul) Creating VirtualBox VM...
(consul) Creating SSH key...
(consul) Starting the VM...
(consul) Check network to re-create if needed...
(consul) 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 consul
複製代碼
建立工做目錄consul
web
mkdir -p ~/workspace/consul && cd ~/workspace/consul
複製代碼
建立docker-compose.ymldocker
version: "3"
services:
consul:
image: consul:latest
ports:
- 8500:8500
command: agent -server -bootstrap -ui -client=0.0.0.0 -node=server01
複製代碼
切換環境到consulbootstrap
eval $(docker-machine env consul)
複製代碼
執行docker-compose.yml瀏覽器
docker-compose up -d
複製代碼
查看虛擬主機IPbash
docker-machine ip consul
# output 192.168.99.103
複製代碼
經過IP在瀏覽器訪問8500端口,便可看到Consul的UI界面ui
建立反向代理虛擬主機this
docker-machine create reverseproxy
複製代碼
出現如下內容表明建立成功spa
Running pre-create checks...
Creating machine...
(reverseproxy) Copying /Users/enoch/.docker/machine/cache/boot2docker.iso to /Users/enoch/.docker/machine/machines/reverseproxy/boot2docker.iso...
(reverseproxy) Creating VirtualBox VM...
(reverseproxy) Creating SSH key...
(reverseproxy) Starting the VM...
(reverseproxy) Check network to re-create if needed...
(reverseproxy) 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 reverseproxy
複製代碼
切換當前環境到虛擬主機
eval $(docker-machine env reverseproxy)
複製代碼
建立工做目錄reverseproxy
mkdir -p ~/workspace/reverseproxy && cd ~/workspace/reverseproxy
複製代碼
建立docker-compose.yml
version: "3"
services:
nginx:
image: nginx:latest
ports:
- 8081:80
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./conf.d:/etc/nginx/conf.d
consul-template:
image: hashicorp/consul-template:alpine
volumes:
- ./conf.d:/etc/nginx/conf.d
command: consul-template --consul-addr 192.168.99.103:8500 --template "/etc/nginx/conf.d/nginx.ctmpl:/etc/nginx/conf.d/vhost.conf" --log-level=info
複製代碼
把容器跑起來
docker-compose up -d
複製代碼
查看IP
docker-machine ip reverseproxy
# output 192.168.99.102
複製代碼
瀏覽器訪問reverseproxy的8081端口,出現以下內容表明啓動成功
建立虛擬主機webserver1
docker-machine create webserver1
eval $(docker-machine env webserver1)
mkdir webserver1 & cd webserver1
複製代碼
建立docker—compose.yml
version: "3"
services:
nginx1:
image: nginx:latest
ports:
- 81:80
nginx2:
image: nginx:latest
ports:
- 82:80
registrator:
image: shaharil/registrator:latest
volumes:
- /var/run/docker.sock:/tmp/docker.sock
command: consul://192.168.99.103:8500
複製代碼
跑起來
docker-compose up -d
複製代碼
打開reverseproxy/conf.d/vhost.conf
能夠看到webserver啓動的nginxIP地址都被加了進去
upstream http_backend {
server 172.18.0.2:81;
server 172.18.0.3:82;
}
server {
listen 8000;
server_name localhost;
location / {
proxy_pass http://http_backend;
}
}
複製代碼
作個試驗,如今將其中一臺nginx容器關閉
eval $(docker-machine env webserver1)
docker stop webserver1_nginx2_1
複製代碼
此時能夠看到vhost.conf中的IP被去掉了
upstream http_backend {
server 172.18.0.2:81;
}
server {
listen 8000;
server_name localhost;
location / {
proxy_pass http://http_backend;
}
}
複製代碼
再將nginx容器打開,該容器的IP地址隨之也會被加進去
docker start webserver1_nginx2_1
//如下是文件內容
upstream http_backend {
server 172.18.0.2:81;
}
server {
listen 8000;
server_name localhost;
location / {
proxy_pass http://http_backend;
}
}
複製代碼