本文是参考各位网友们的博客成功部署consul+consul-template+nginx实现负载均衡和服务发现注册的。环境不同,不一定在你的环境上合适。
1. nginx网上基本上douk都可以
2. consul配置,参考以下:
http://www.cnblogs.com/knmax/p/9454740.html
https://www.jianshu.com/p/1d36a6277c3b
https://yq.aliyun.com/ziliao/318073
https://blog.csdn.net/liaokailin/article/details/51612247
https://blog.csdn.net/u010246789/article/details/51777011
https://www.cnblogs.com/cuishuai/p/8194345.html
3. consul-template配置:
https://blog.csdn.net/delongcpp/article/details/80119307
https://www.cnblogs.com/MrCandy/p/7152312.html
https://blog.csdn.net/songhaifengshuaige/article/details/79111676
附使用到的脚本:
cat /opt/consul/conf/consul.json
{
"datacenter": "DC1", // 数据中心名称
"data_dir": "/opt/consul/data", // Server 节点数据目录
"log_level": "INFO", // 日志级别
"node_name": "consul-01", // 当前节点名称
"server": true, // 是否为 Server 模式,false 为 Client 模式
"ui": true, // 是否开启 UI 访问
"bootstrap_expect": 1, // 启动时期望的就绪节点,1 代表启动为 bootstrap 模式,等待其他节点加入
"bind_addr": "192.168.0.31", // 绑定的 IP
"client_addr": "192.168.0.31", // 同时作为 Client 接受请求的绑定 IP;端口使用了 HTTP: 8500, DNS: 8600
"retry_join": ["192.168.0.135","192.168.0.136"], // 尝试加入的其他节点
"retry_interval": "3s", // 每次尝试间隔
"raft_protocol": 3, // Raft 协议版本
"enable_debug": false, // 是否开启 Debug 模式
"rejoin_after_leave": true, // 允许重新加入集群
"enable_syslog": false
}
cat /opt/consul/conf/consul.json
{
"datacenter": "DC1",
"data_dir": "/opt/consul/data",
"log_level": "INFO",
"node_name": "consul-02",
"server": true,
"ui": true,
"bootstrap_expect": 2,
"bind_addr": "192.168.0.135",
"client_addr": "192.168.0.135",
"retry_join": ["192.168.0.31","192.168.0.136"],
"retry_interval": "3s",
"raft_protocol": 3,
"enable_debug": false,
"rejoin_after_leave": true,
"enable_syslog": false
}
cat /opt/consul/conf/consul.json
{
"datacenter": "DC1",
"data_dir": "/opt/consul/data",
"log_level": "INFO",
"node_name": "consul-03",
"server": true,
"ui": true,
"bootstrap_expect": 3,
"bind_addr": "192.168.0.136",
"client_addr": "192.168.0.136",
"retry_join": ["192.168.0.31","192.168.0.135"],
"retry_interval": "3s",
"raft_protocol": 3,
"enable_debug": false,
"rejoin_after_leave": true,
"enable_syslog": false
}
{
"service":{
"name":"web",
"tags":[
"python"
],
"port":9000,
"check":{
"name":"scheck",
"http":"http://localhost:9000",
"interval":"3s"
}
}
}
web_check.sh
curl -i "127.0.0.1:9000/" >> /dev/null 2>&1
consul agent -data-dir /data/consul1 -node=WebService1 -bind=192.168.0.30 -config-dir /etc/consul.d -enable-script-checks=true -datacenter=dc1 -join 192.168.0.31
sudo consul-template --consul-addr 192.168.0.31:8500 --template "./nginx.ctmpl:vhost.conf:/usr/sbin/nginx -s reload" --log-level=info
sudo consul-template --consul-addr 192.168.0.31:8500 -template /opt/consul-template/nginx.ctmpl:/etc/nginx/consul/nginx.conf:"/usr/sbin/nginx -s reload"
upstream web {
{{range service "web"}}
server {{ .Address }}:{{ .Port }};
{{ end }}
}
server {
listen 7000;
server_name localhost;
location / {
proxy_pass http://web;
}
}
最后,有图有真相: