Docker---------consul deployment
One, consul introduction
Consul is an open source tool launched by HashiCorp to realize service discovery and configuration of distributed systems
1. The characteristics of consul
Support health check, allowing storage of key-value pairs
Based on Golang language, strong portability
Support ACL access control
Works seamlessly with lightweight containers such as Docker
2. Consul internal port description
port | Description |
---|---|
TCP/8300 | Port 8300 is used for the server node, and the client uses the port RPC protocol to call the server node |
TCP/UDP/8301 | Port 8301 is used to communicate between all nodes in a single data center, that is, to synchronize the information of the LAN pool. It enables the entire data center to automatically discover server addresses, detect node failures in a distributed manner, and broadcast events (such as leader election events) |
TCP/UDP/8302 | Port 8302 is used for the information synchronization of server nodes between single or multiple data centers, that is, the synchronization of LAN pool information. It is optimized for the high latency of the Internet and can realize cross-data center requests |
8500 | Port 8500 is based on HTTP protocol, user API interface or WEB UI access |
8600 | Port 8600 is used as a DNS server, which allows us to query node information by node name |
Two, build consul experiment
1. Environment deployment
Prepare centos with docker in advance
Host | Software package |
---|---|
192.168.132.50 | Docker-ce 、 Compose 3 、 Consul 、 Consul-temple |
192.168.132.60 | Docker-ce、Registrator |
2. Install the consul software package (192.168.132.50)
mkdir /root/consul
cd /root/consul
unzip consul_0.9.2_linux_amd64.zip
mv consul /usr/bin
consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.132.50 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
3. View the cluster
consul members
consul info | grep leader
4. Obtain the cluster through http api
curl 127.0.0.1:8500/v1/status/peers //查看集群server成员
curl 127.0.0.1:8500/v1/status/leader //集群Raf leader
curl 127.0.0.1:8500/v1/catalog/services //注册的所有服务
curl 127.0.0.1:8500/v1/catalog/nginx //查看nginx服务信息
curl 127.0.0.1:8500/v1/catalog/nodes //集群节点详细信息
5. Operate on another server (192.168.132.60)
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.132.60 \
consul://192.168.132.50:8500
6. Test service function
docker run -itd -p 83:80 --name test-1 -h test1 nginx
docker run -itd -p 84:80 --name test-2 -h test2 nginx
docker run -itd -p 88:80 --name test-3 -h test3 httpd
docker run -itd -p 89:80 --name test-4 -h test4 httpd
7. Enter http://192.168.132.50:8500 in the browser
View on consul server
[root@consul consul]#curl 127.0.0.1:8500/v1/catalog/services
{
"consul":[],"httpd":[],"nginx":[]}
vim /root/consul/nginx.ctmpl
upstream http_backend {
#群集模块模板
{
{
range service "nginx"}} #指定nginx服务项
server {
{
.Address}}:{
{
.Port}}; #指向真实服务器的地址和端口“{
{
.Address}}” 此参数会根据consul群集的参数自动设置上去
{
{
end}} #结束语句(以上为轮询请求方式)
}
server {
listen 88; #对外提供的端口,可自行设置,只要不起冲突
server_name localhost 192.168.132.50; #本地反向代理的节点地址
access_log /var/log/nginx/kgc.cn-access.log; #访问日志文件目录(需手动创建)
index index.html index.php; #指定访问的index 首页类型
location / {
proxy_set_header HOST $host; #反向代理的请求头部信息
proxy_set_header X-Real-IP $remote_addr; #真实服务器IP
proxy_set_header Client-IP $remote_addr; #客户IP
proxy_set_header X-Fprwarded-For $proxy_add_x_forwarded_for; #Forward转发地址
proxy_pass http://http_backend; #反向代理指向upstream地址池
}
}
8. Manually compile and install nginx service
yum install gcc gcc-c++ pcre-devel zlib-devel -y
tar zxvf nginx-1.12.0.tar.gz -C /opt
cd /opt/nginx-1.12.0/
./configure --prefix=/usr/local/nginx
make && make install
Configure nginx
vim /usrlocal/nginx/conf/nginx.conf
http {
include mime.types;
include vhost/*.conf; #vhost目录需手动创建
#在http模块下 添加include vhost/*.conf
mkdir /usr/local/nginx/conf/vhost
mkdir /var/log/nginx
/usr/local/nginx/sbin/nginx
9. Configure and start the template
Upload the consul-template_0.19.3_linux_amd64.zip package to the /root directory
unzip consul-template_0.19.3_linux_amd64.zip
mv consul-template /usr/bin/
consul-template -consul-addr 192.168.132.50:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/test.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
Open a terminal to view the generated configuration file
cat /usr/local/nginx/conf/vhost/test.conf
Add a nginx container node, test server discovery and configuration update functions
docker run -itd -p 85:80 --name test-5 -h test5 nginx
Enter http://192.168.132.50:83 in the browser