使用docker-compose搭建consul集群环境

概述:

Docker Compose:
Docker Compose的前身是Fig,它是一个定义及运行多个Docker容器的工具
使用Docker Compose不再需要使用Shell脚本来启动容器
Docker Compose非常适合组合多个容器进行开发的场景
Consul:
Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置
Consul的特性
支持健康检查,允许存储键值对
基于Golong语言,可移植性强
支持ACL访问控制
与Docker等轻量级容器可无缝配合

Docker-compose部署:

上传docker-compose到/root目录下

[root@localhost ~]# cp -p docker-compose /usr/local/bin/
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose 
[root@localhost ~]# mkdir compose_nginx
[root@localhost ~]# cd compose_nginx/
[root@localhost compose_nginx]# yum -y install tree 
[root@localhost compose_nginx]# vim docker.compose.yaml
[root@localhost compose_nginx]# mkdir nginx //这里要制作nginx基于Dockerfile镜像
[root@localhost compose_nginx]# cd nginx/
[root@localhost nginx]# tree 
.
├── Dockerfile
├── nginx-1.12.2.tar.gz
└── run.sh
这里需要把Dockerfile中ADD的东西添加到同一路径
#Dockerfile:
FROM centos:7
MAINTAINER this is nginx images <chen>
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
WORKDIR /usr/local/src
WORKDIR nginx-1.12.2
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
#run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
#docker.compose.yaml
version: '3'
services:
  nginx:
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
     - 1216:80
     - 1218:443
    networks:
     - abc
    volumes:
     - ./wwwroot:/usr/local/nginx/html
networks:
  abc:
  #yaml中不能识别制表符,要注意格式
[root@localhost compose_nginx]# tree 
.
├── docker.compose.yaml
└── nginx
    ├── Dockerfile
    ├── nginx-1.12.2.tar.gz
    └── run.sh
#这里是没有wwwroot的,所以要先发布才会生成挂载目录
[root@localhost compose_nginx]# docker-compose -f docker.compose.yaml up -d //发布任务
[root@localhost compose_nginx]# tree 
.
├── docker.compose.yaml
├── nginx
│   ├── Dockerfile
│   ├── nginx-1.12.2.tar.gz
│   └── run.sh
└── wwwroot //发布后会创建一个wwwroot目录
[root@localhost compose]# cd wwwroot/
[root@localhost wwwroot]# echo '<h1>docker-compose!!</h1>' > index.html
[root@localhost wwwroot]# cat index.html 
<h1>docker-compose!!</h1>

属主机测试:
在这里插入图片描述consul部署:
上传consul_0.9.2_linux_amd64.zip到/root目录下

[root@server1 ~]# mkdir consul
[root@server1 ~]# cp consul_0.9.2_linux_amd64.zip /root/consul
[root@server1 ~]# cd consul/
[root@server1 consul]# unzip consul_0.9.2_linux_amd64.zip 
将consul移动到/usr/local/bin
[root@server1 consul]# mv consul /usr/local/bin/
[root@server1 consul]# consul agent \     #使用代理功能
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.100.100 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &  ##放入后台运行
[root@server1 consul]# consul members  #查看集群信息
Node             Address               Status  Type    Build  Protocol  DC
consul-server01  192.168.100.100:8301  alive   server  0.9.2  2         dc1
[root@localhost ~]# consul info | grep leader
	leader = true
	leader_addr = 192.168.100.100:8300
进入容器
[root@server1 consul]# docker exec -it 3f69ecfc1cda /bin/bash
安装tools工具
[root@nginx nginx-1.12.2]# yum -y install net-tools
查看网络状态
[root@nginx nginx-1.12.2]# ifconfig
[root@nginx nginx-1.12.2]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.2  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 02:42:ac:12:00:02  txqueuelen 0  (Ethernet)
        RX packets 166  bytes 324374 (316.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 135  bytes 8111 (7.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

在另外一台虚拟机上安装Gliderlabs/Registrator

安装registrator
docker run -d \
> --name=registrator \   #容器名称
> --net=host \          #指定网络
> -v /var/run/docker.sock:/tmp/docker.sock \   #挂载
> --restart=always \ 
> gliderlabs/registrator:latest \              #镜像名称
> -ip=192.168.100.101 \
> consul://192.168.100.100:8500

查看镜像
[root@localhost ~]# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
nginx                    latest              bc9a0695f571        7 days ago          133MB
httpd                    latest              0a30f4c29d25        2 weeks ago         138MB
gliderlabs/registrator   latest              3b59190c6c80        4 years ago         23.8MB

查看容器
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                NAMES
cd53ef08935f        nginx                           "/docker-entrypoint.…"   20 hours ago        Up 20 hours         0.0.0.0:88->80/tcp   tom5
fe9466c6fe7f        nginx                           "/docker-entrypoint.…"   20 hours ago        Up 20 hours         0.0.0.0:90->80/tcp   tom4
ff41aabe0bfc        gliderlabs/registrator:latest   "/bin/registrator -i…"   22 hours ago        Up 22 hours                              registrator
291de2c40204        nginx                           "/docker-entrypoint.…"   22 hours ago        Up 22 hours         0.0.0.0:83->80/tcp   test1

网页登录http://192.168.100.100:8500,查看节点服务状态
在这里插入图片描述在第二台虚拟机上安装两个nginx容器

[root@server2 ~]# docker run -dit -p 83:80 --name test1 -h test1 nginx
[root@server2 ~]# docker run -dit -p 88:80 --name test3 -h test3 nginx

在这里插入图片描述将两个nginx服务移除查看,网页监控服务也被移除

[root@localhost ~]# docker stop cd53ef08935f
cd53ef08935f
[root@localhost ~]# docker rm cd53ef08935f
cd53ef08935f
[root@localhost ~]# docker stop 291de2c40204
291de2c40204
[root@localhost ~]# docker rm 291de2c40204
291de2c40204

在这里插入图片描述在第一台虚拟机上安装consul-template:
consul-template是一个守护进程,用于实时查询consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件,更新完成以后,可以选择运行shell命令执行更新操作,重新加载Nginx。consul-template可以查询consul中的服务目录、Key、Key-values等。这种强大的抽象功能和查询语言模板可以使consul-template特别适合动态的创建配置文件

先准备template nginx 模板文件
[root@localhost ~]# vim /root/consul/nginx.ctmpl
 
upstream http_backend {
  {{range service "nginx"}}
   server {{.Address}}:{{.Port}};
  {{end}}
}
server {
  listen 83;
  server_name localhost 192.168.100.100;
  access_log /var/log/nginx/kgc.cn-access.log;
  index index.html index.php;
  location / {
    proxy_set_header HOST $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Client-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://http_backend;
  }
}

编译安装nginx:

yum install -y gcc gcc-c++ pcre-devel zlib-devel
tar zxvf nginx-1.12.2.tar.gz -C /opt/

cd /opt/nginx-1.12.2
./configure --prefix=/usr/local/nginx
make && make install

配置nginx主配置文件使其与子配置文件关联起来

vi /usr/local/nginx/conf/nginx.conf

在这里插入代码片在这里插入图片描述注意:这里的vhost需要自己去建立,不会自动生成

创建虚拟主机目录
[root@server1 nginx-1.12.2]# mkdir /usr/local/nginx/conf/vhost
创建日志文件目录
[root@server1 nginx-1.12.2]# mkdir /var/log/nginx
启动nginx并查看端口
[root@server1 nginx-1.12.2]# /usr/local/nginx/sbin/nginx 
[root@server1 nginx-1.12.2]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      69573/nginx: master 

配置并启动template(上传template到/root目录下)

[root@server1 ~]# unzip consul-template_0.19.3_linux_amd64.zip      #解压缩
Archive:  consul-template_0.19.3_linux_amd64.zip
  inflating: consul-template
[root@server1 ~]# mv consul-template /usr/local/bin    #移动
[root@server1 ~]# consul-template -consul-addr 20.0.0.10:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/yangzihan.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
再开一个server1终端查看
[root@server1 ~]# cd /usr/local/nginx/conf/vhost/
[root@server1 vhost]# cat yangzihan.conf 
upstream http_backend {

    server 192.168.100.101:83;

    server 192.168.100.101:90;

    server 192.168.100.101:88;

    server 192.168.100.101:92;

}

server {
  listen 83;
  server_name localhost 192.168.100.100;
  access_log /var/log/nginx/aa-access.log;
  index index.html index.php;
  location / {
    proxy_set_header HOST $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Client-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://http_backend;

网页访问192.168.100.100:80
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值