【Linux】【Services】【SaaS】Docker+kubernetes(9. 安装consul实现服务注册发现)

1. 简介

1.1. 官方网站:

https://www.consul.io

1.2. Consul的功能:

服务发现:通过DNS或HTTP接口使得消费者发现服务,应用程序可以轻松找到所依赖的服务。

健康检查:防止将请发转发不健康的主机。

键值存储:可以使用分层键/值存储,比如功能标记、动态配置等。

多数据中心:开箱即用,不需要复杂的配置。这就意味着不用建立抽象的逻辑来扩展多个地区。

1.3. Consul集群介绍:

-- Consul agent是Consul核心工作,分为client和server两种工作模式。默认以client模式运行,提供服务注册、健康检查、转发查询给server leader。server模式启动时使用-server选项指定,用于维护Consul集群状态、Raft协议进行选举。

-- agent必须在每个Consul节点运行,所有运行Consul agent节点构成Consul集群。

-- 官方建议Consul集群至少3或5个节点运行Consul agent server模式,client节点不限。

-- 通过join或rejoin加入集群。一旦加入,集群信息使用gossip算法同步到整个集群节点。

2. 环境

2.1. 机器列表

2.2. 版本:

3. 安装与配置

3.1. Nginx

yum安装

yum -y install nginx

下载upsync模块

git clone https://github.com/weibocom/nginx-upsync-module.git

 

3.2. Consul

下载

https://releases.hashicorp.com/consul/1.0.6/consul_1.0.6_linux_amd64.zip?_ga=2.258678857.2141400986.1520232419-1122875496.1520232419

 安装

在server端,10.30.2.45/46/47

创建一个独立分区给consul用

~]# df -h /data/consul
Filesystem                       Size  Used Avail Use% Mounted on
/dev/mapper/vg_system-lv_consul   10G   33M   10G   1% /data/consul

修改/usr/lib/systemd/system/consul.service

[Unit]
Description=Consul service discovery agent
Requires=network-online.target
After=network-online.target
[Service]
#User=consul
#Group=consul
EnvironmentFile=-/etc/consul.conf
#Environment=GOMAXPROCS=2
Restart=on-failure
ExecStartPre=[ -f "/var/run/consul/consul.pid" ] && /usr/bin/rm -f /var/run/consul/consul.pid
ExecStart=/usr/bin/consul $CONSUL_FLAGS $CONSUL_CLIENT $CONSUL_BIND
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGTERM
TimeoutStopSec=5
[Install]
WantedBy=multi-user.target

修改/etc/consul.conf,把172.16.0.45/46/47

CONSUL_FLAGS="agent -server -bootstrap-expect=2 -syslog -ui -data-dir=/data/consul -config-dir=/etc/consul.conf.d/ -pid-file=/var/run/consul/consul.pid -node=hctjoscache01 -datacenter=hccos -retry-join consul.hccos.cn:8301 -advertise=172.16.0.45 -enable-script-checks"
CONSUL_CLIENT="-client=0.0.0.0"
CONSUL_BIND="-bind=172.16.0.45"

 

CONSUL_FLAGS="agent -server -bootstrap-expect=2 -syslog -ui -data-dir=/data/consul -config-dir=/etc/consul.conf.d/ -pid-file=/var/run/consul/consul.pid -node=hctjoscache02 -datacenter=hccos -retry-join consul.hccos.cn:8301 -advertise=172.16.0.46 -enable-script-checks"
CONSUL_CLIENT="-client=0.0.0.0"
CONSUL_BIND="-bind=172.16.0.46"

 

CONSUL_FLAGS="agent -server -bootstrap-expect=2 -syslog -ui -data-dir=/data/consul -config-dir=/etc/consul.conf.d/ -pid-file=/var/run/consul/consul.pid -node=hctjoscache03 -datacenter=hccos -retry-join consul.hccos.cn:8301 -advertise=172.16.0.47 -enable-script-checks"
CONSUL_CLIENT="-client=0.0.0.0"
CONSUL_BIND="-bind=172.16.0.47"

 

修改DNS记录,添加两条A记录,这两条记录都指向Nginx机器

consul                  IN      A       172.16.0.148
consul                  IN      A       172.16.0.149

 

在Nginx机器上添加TCP转发

server {
    listen          8301;
    proxy_connect_timeout 5s;
    proxy_timeout 30s;
        proxy_pass consul-server;
    }
upstream consul-server {
    server 172.16.0.45:8301 max_fails=3 fail_timeout=10s;
    server 172.16.0.46:8301 max_fails=3 fail_timeout=10s;
    server 172.16.0.47:8301 max_fails=3 fail_timeout=10s;
}

 

启动服务

systemctl start consul

 

查看consul的节点

[root@hctjoscache03 ~]# consul members
Node           Address           Status  Type    Build  Protocol  DC     Segment
hctjoscache01  172.16.0.45:8301  alive   server  1.0.6  2         hccos  <all>
hctjoscache02  172.16.0.46:8301  alive   server  1.0.6  2         hccos  <all>
hctjoscache03  172.16.0.47:8301  alive   server  1.0.6  2         hccos  <all>

 

 在10.30.2.95/96/97/98上分别默认安装和启动httpd,用来测试(过程略)

 

在/etc/consul.conf.d/下面建立httpd.json和web_check.sh

[root@hctjoscache01 consul.conf.d]# cat httpd.json 
{  
  "service": {  
    "name": "httpd",  
    "tags": ["primary"],  
    "address": "172.16.0.95",  
    "port": 80,
    "enableTagOverride": false,  
    "checks": [  
      {  
        "script": "/etc/consul.conf.d/web_check.sh",  
        "interval": "30s"  
      }  
    ]  
  }  
}  
[root@hctjoscache01 consul.conf.d]# cat web_check.sh 
curl -i "172.16.0.95:80" >> /dev/null 2>&1

记得给web_check.sh一个执行权限,如果脚本没执行成功也会认为服务不可用

 

使用这个接口来查询服务状态

curl 'http://localhost:8500/v1/health/service/httpd?passing'

 

 

3.3. Consul-template

4. 命令详解

4.1. consul

选项 描述
-advertise 通告地址
-bind 集群节点之间通讯地址
-bootstrap 设置服务器为bootstrap模式,在一个DC中只有一个server处于bootstrap模式。一般初始化第一台Consul时指定,自选举为leader
-bootstrap-expect 在一个DC钟期望提供server节点数目,consul会一直等到指定的server数目才会引导整个集群,选举leader,不能与bootstrap同时使用
-client 设置客户端访问地址,包括RPC、DNS,默认为127.0.0.1
-config-file 从JSON配置文件中读取
-data-dir 指定存放agent server集群状态目录,以免系统重启丢失
-dc 数据中心名称,默认dc1
-http-port HTTP API监听端口
-join 加入一个已经启动的agent,可以指定多个agent地址
-node 节点名称,必须在集群中唯一的,默认是主机名
-rejoin 忽略先前的离开,再次启动后尝试加入集群
-server 切换agent模式到server模式,每个集群至少有一个server
-ui 启用内置的Web UI
-ui-dir Web UI的资源目录
posted @ 2018-01-26 15:52  炼狱腾蛇  阅读(476)  评论(0编辑  收藏  举报