docker-compose实现容器集群编排
docker-compose工具作用
该工具可以编排容器并且让容器可以在集群上执行。
docker-compose安装
[root@ecs-140051 sunpy-docker]# curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 423 100 423 0 0 538 0 --:--:-- --:--:-- --:--:-- 538
100 16.2M 100 16.2M 0 0 9552k 0 0:00:01 0:00:01 --:--:-- 1136k
[root@ecs-140051 sunpy-docker]# docker-compose -version
-bash: /usr/local/bin/docker-compose: Permission denied
[root@ecs-140051 sunpy-docker]# sudo chmod +x /usr/local/bin/docker-compose
[root@ecs-140051 sunpy-docker]# docker-compose -version
docker-compose version 1.25.1, build a82fef07
docker-compose实现nginx反向代理Tomcat
编写tomcat集群和nginx的docker-compose.yml
version: "3"
# 虚拟网络配置,分配虚拟IP地址,当前配置使容器之间组成局域网(与driver的选择有关)
networks:
my-net:
driver: bridge
ipam:
driver: default
config:
- subnet: 192.168.0.0/24
services:
mynginx:
image: nginx
container_name: "mynginx"
networks:
my-net:
ipv4_address: 192.168.0.80 #指定nginx容器IP地址
ports:
- "3500:80" #对主机80端口的访问会映射到容器的80端口
volumes:
- /home/nginx-conf/nginx.conf:/etc/nginx/nginx.conf
- /home/nginx-conf/conf:/etc/nginx/conf.d
- /home/nginx-conf/html:/usr/share/nginx/html
- /home/nginx-conf/logs:/var/log/nginx
#tomcat启动完成再启动nginx
depends_on:
- tomcat-1
- tomcat-2
- tomcat-3
tomcat-1:
image: billygoo/tomcat8-jdk8:latest
container_name: "tomcat-1"
networks:
my-net:
ipv4_address: 192.168.0.81
ports:
- "8081:8080"
restart: "always"
volumes:
- /home/tomcat-1/conf:/usr/local/tomcat/conf
- /home/tomcat-1/logs:/usr/local/tomcat/logs
- /home/tomcat-1/webapps:/usr/local/tomcat/webapps
tomcat-2:
image: billygoo/tomcat8-jdk8:latest
container_name: "tomcat-2"
networks:
my-net:
ipv4_address: 192.168.0.82
ports:
- "8082:8080"
restart: "always"
volumes:
- /home/tomcat-2/conf:/usr/local/tomcat/conf
- /home/tomcat-2/logs:/usr/local/tomcat/logs
- /home/tomcat-2/webapps:/usr/local/tomcat/webapps
tomcat-3:
image: billygoo/tomcat8-jdk8:latest
container_name: "tomcat-3"
networks:
my-net:
ipv4_address: 192.168.0.83
ports:
- "8083:8080"
restart: "always"
volumes:
- /home/tomcat-3/conf:/usr/local/tomcat/conf
- /home/tomcat-3/logs:/usr/local/tomcat/logs
- /home/tomcat-3/webapps:/usr/local/tomcat/webapps
启动集群中的部分服务:
docker-compose -f docker-compose.yml up -d tomcat-1
docker-compose -f docker-compose.yml up -d tomcat-2
docker-compose -f docker-compose.yml up -d tomcat-3
docker-compose -f docker-compose.yml up -d mynginx
配置nginx的配置文件:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
keepalive_timeout 65;
sendfile on;
# 需要注意的是如果tomcat都部署在docker内,这里的ip地址填对应docker容器的ip和端口号
# 注意ip和端口的对应,这里容易把端口号写成宿主机开放的端口号
upstream tomcat-cluster {
server 192.168.0.81:8080;
server 192.168.0.82:8080;
server 192.168.0.83:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcat-cluster;
}
# redirect server error pages to the static page.
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
注意
- 在docker-compose在使用volumes挂载容器卷的时候,需要先将挂载的文件和文件夹创建出来,要不然就会无法启动容器。
- 分配的网段和接口,需要注意是否与docker容器中存在的接口是否会冲突,否则无法启动容器。