一、yum安装consul
#安装yum-utils
yum install -y yum-utils
#配置consul的下载仓库
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
#必须上面步骤,不然会找不到仓库
yum -y install consul
#查看版本
consul -v
二、启动consul(单节点)
#创建consul目录
mkdir /home/consul-data
#后台启动
nohup consul agent -server -data-dir=/home/consul-data/ -node=node1 -bind=0.0.0.0 -bootstrap-expect=1 -client=0.0.0.0 -ui > /home/consul-data/consul.log &
#关闭防火墙或开放端口8500
systemctl stop firewalld
-
–server 指定server agent
-
–data-dir l数据存储路径
-
–bootstrap-expect 期望的server节点数目,consul一直等到指定sever数目才会引导整个集群
-
–bind:该地址用来在集群内部的通讯,集群内的所有节点到地址都必须是可达的,默认是0.0.0.0
-
–node:节点在集群中的名称,在一个集群中必须是唯一的,默认是该节点的主机名
-
–ui: web的管理ui,查看服务和节点,可以通过访问:8500端口要访问UI
-
–client:提供HTTP、DNS、RPC等服务,默认是127.0.0.1,不对外提供服务,如果需要则改成0.0.0.0
单节点扩容(需要扩容时再做)
#扩容192.168.100.30
nohup consul agent -bind=0.0.0.0 -client=0.0.0.0 -data-dir=/home/consul-data/ -node=node2 -join=192.168.100.30 > /home/consul-data/consul.log 2>&1 &
三、访问consul
http://192.168.100.7:8500
或使用命令访问
curl localhost:8500/v1/catalog/nodes
返回内容
[{"ID":"3f9f219a-f15e-82a9-0a23-889f324b1fe9","Node":"node1","Address":"192.168.100.7","Datacenter":"dc1","TaggedAddresses":{"lan":"192.168.100.7","lan_ipv4":"192.168.100.7","wan":"192.168.100.7","wan_ipv4":"192.168.100.7"},"Meta":{"consul-network-segment":""},"CreateIndex":12,"ModifyIndex":17}]
四、关闭consul
#关闭命令
consul leave
#后台启动使用这个
ps -ef | grep consul
kill -9 PID(Consul进程ID)
五、使用PostMan 注册Http服务
//地址 使用put请求
http://192.168.100.7:8500/v1/catalog/register
//发送json格式数据
//参数1
{"Datacenter": "dc1",
"Node":"skus1",
"Address":"192.168.100.37",
"Service": {
"Id" :"skuprice-skus1",
"Service": "skuprice",
"tags": ["prod"],
"Port": 80
}}
有几个注册几个
六、发现Http服务
http://192.168.100.7:8500/v1/catalog/service/skuprice
[{"ID":"","Node":"skus1","Address":"192.168.100.37","Datacenter":"dc1","TaggedAddresses":null,"NodeMeta":null,"ServiceKind":"","ServiceID":"skuprice-skus1","ServiceName":"skuprice","ServiceTags":["prod"],"ServiceAddress":"","ServiceWeights":{"Passing":1,"Warning":1},"ServiceMeta":{},"ServicePort":80,"ServiceSocketPath":"","ServiceEnableTagOverride":false,"ServiceProxy":{"Mode":"","MeshGateway":{},"Expose":{}},"ServiceConnect":{},"CreateIndex":96,"ModifyIndex":310},{"ID":"","Node":"skus2","Address":"192.168.100.39","Datacenter":"dc1","TaggedAddresses":null,"NodeMeta":null,"ServiceKind":"","ServiceID":"skuprice-skus2","ServiceName":"skuprice","ServiceTags":["prod"],"ServiceAddress":"","ServiceWeights":{"Passing":1,"Warning":1},"ServiceMeta":{},"ServicePort":80,"ServiceSocketPath":"","ServiceEnableTagOverride":false,"ServiceProxy":{"Mode":"","MeshGateway":{},"Expose":{}},"ServiceConnect":{},"CreateIndex":101,"ModifyIndex":308}]
七、删除http服务
http://
192.168.100.7:8500/v1/agent/service/deregister/{ID}
八、nginx安装-使用编译安装需要把upsync模块编译进去
get -c https://github.com/weibocom/nginx-upsync-module/archive/master.zip
unzip nginx-upsync-module-master.zip
#编译需要增加upsync模块
--add-module=/root/nginx-upsync-module
nginx -V 查看已安装模块
九、实现nginx动态负载均衡配置
user www www;
worker_processes auto;
error_log /www/wwwlogs/nginx_error.log crit;
pid /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
stream {
log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time';
access_log /www/wwwlogs/tcp-access.log tcp_format;
error_log /www/wwwlogs/tcp-error.log;
include /www/server/panel/vhost/nginx/tcp/*.conf;
}
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http
{
include mime.types;
#include luawaf.conf;
include proxy.conf;
default_type application/octet-stream;
server_names_hash_bucket_size 512;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
server_tokens off;
access_log off;
upstream api.sku.broadout.cn {
server 192.168.100.3:80;
#server 192.168.100.27:80;
#server 192.168.100.29:80;
upsync 192.168.100.7:8500/v1/kv/upstreams/skuprice upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
upsync_dump_path /www/server/nginx/conf/vhost/servers_skuprice.conf;
#include /www/server/nginx/conf/vhost/servers_skuprice.conf;
}
server
{
listen 80;
server_name api.sku.broadout.cn;
location / {
proxy_pass http://api.sku.broadout.cn;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 显示具体负载的机器的ip,X-Route-Ip随便命名
add_header X-Route-Ip $upstream_addr;
add_header X-Route-Status $upstream_status;
}
}
include /www/server/panel/vhost/nginx/*.conf;
}
改动只有两处
#实现动态负载均衡
upstream api.sku.broadout.cn {
server 192.168.100.3:80;
#server 192.168.100.27:80;
#server 192.168.100.29:80;
upsync 192.168.100.7:8500/v1/kv/upstreams/skuprice upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
upsync_dump_path /www/server/nginx/conf/vhost/servers_skuprice.conf;
#include /www/server/nginx/conf/vhost/servers_skuprice.conf;
}
server
{
listen 80;
server_name api.sku.broadout.cn;
location / {
proxy_pass http://api.sku.broadout.cn;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 显示具体负载的机器的ip,X-Route-Ip随便命名
add_header X-Route-Ip $upstream_addr;
add_header X-Route-Status $upstream_status;
}
}
重启生效
十、负载均衡测试
# 使用curl 请求,一定要是put请求
curl -X PUT http://192.168.100.7:8500/v1/kv/upstreams/skuprice/192.168.100.27:80
curl -X PUT http://192.168.100.7:8500/v1/kv/upstreams/skuprice/192.168.100.29:80
curl -X PUT http://192.168.100.7:8500/v1/kv/upstreams/skuprice/192.168.100.3:80
#或者使用postman调用consul提供的api来添加key,value
http://192.168.254.134:8500/v1/kv/upstreams/springbootserver/192.168.0.116:8081
# 甚至可以图形化界面进行操作,手动添加key value,手动添加时注意如果是创建文件夹需要在最后加一个正斜杠 :/
十一、在consul图形中添加key/value,设置权重
{"weight":1, "max_fails":2, "fail_timeout":10, "down":0}
在linux下实现Nginx + consul + upsync 完成动态负载均衡已完成