Docker

首页 -  Docker  -  docker安装consul,consul nginx集群

docker安装consul,consul nginx集群

docker安装consul,consul nginx集群

1.docker构建consul容器

consul是google开源的一个使用go语言开发的服务发现、配置管理中心服务。内置了服务注册与发现框 架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,不再需要依赖其他工具(比如ZooKeeper等)。服务部署简单,只有一个可运行的二进制的包。每个节点都需要运行agent,他有两种运行模式server和client。每个数据中心官方建议需要3或5个server节点以保证数据安全,同时保证server-leader的选举能够正确的进行。

docker pull consul  #拉取镜像

#创建容器
docker run -p 8500:8500 -d --name consul -v /docker/consul/data:/consul/data --privileged=true -e  CONSUL_BIND_INTERFACE='eth0' consul agent -server -bootstrap-expect 1 -data-dir /consul/data -node=ali -ui -client='0.0.0.0'

2.nginx安装扩展

apt-get install -y gcc autoconf automake make libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev

如果抛出异常

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package libpcre3-dev is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Unable to locate package gcc
E: Unable to locate package autoconf
E: Unable to locate package automake
E: Unable to locate package make
E: Package 'libpcre3-dev' has no installation candidate
E: Unable to locate package libssl-dev
E: Unable to locate package zlib1g-dev

说明源没有安装包我们就来更换一下容器的源

vi  /etc/apt/sources.list

我这里替换的是阿里的源

deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib

更多源访问链接https://debian.cn/articles/626?www

更换之后执行更新

apt-get update

在执行安装扩展如果抛出以下异常说明没同步时间

E: Release file for http://deb.debian.org/debian/dists/buster-updates/InRelease is not valid yet (invalid for another 15h 17min 14s). Updates for this repository will not be applied.

同步容器时间

apt-get -y install ntp ntpdate #安装ntpdate
ntpdate 0.asia.pool.ntp.org #安装依赖

3.添加nginx插件包nginx-upsync-module-master

链接:https://pan.baidu.com/s/1XEE0mvE3T3hJaPXUR0wmGQ 
提取码:3gg9

4.将插件包下载后方到nginx容器中

5.像nginx添加模块相当于要重新安装nginx我们这里准备了nginx的安装包

链接: https://pan.baidu.com/s/1o5rXdE14q-A0M-oBwK5x0A 提取码: mxvb

6.下载nginx安装包复制到容器内部解压

7.进入nginx目录 nginx -V 查看扩展

8.复制扩展内容执行

./configure  后面是原来的扩展包内容在后面加上 --add-module=/home/nginx-upsync-module-master
我的是这样
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.19.10/debian/debuild-base/nginx-1.19.10=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --add-module=/home/nginx-upsync-module-master

9.编译就好了

make && make install

10.配置负载均衡nginx

upstream LMRS { 
   server 192.168.211.129:80;#这个意思就是下面的server_test.conf配置文件里没有找到任何服务默认就访这个
   upsync 192.168.211.128:8500/v1/kv/upstreams/nginx_test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
   upsync_dump_path /etc/nginx/conf.d/server_test.conf;
   include /etc/nginx/conf.d/server_test.conf;
}
server 
{
    listen 80;
    listen [::]:80;
    server_name localhost;
    root /docker/www;
    index index.php index.html;
    location /{
       proxy_pass http://LMRS;
    }
}

11.consul加入服务 这个就是在consul服务里加入新加的nginx服务

curl -X PUT -d '{"weight":1,"max_fails":2,"fail_timeout":10}' http://192.168.211.128:8500/v1/kv/upstreams/nginx_test/192.168.211.128:80

12.在别的虚拟机上创建一个ngixn服务然后在这里加入consul就可以测试了,访问nginx负载均衡对应的ip地址就可以看到效果了

(0)
分享:

本文由:xiasohu168.com 作者:xiaoshu发表,转载请注明来源!

标签:

相关阅读