现有服务my-server-demo,由于机器比较少,故将nginx和Keepalived也部署在服务的机器上。现有两台机器238和187。
一、部署服务:
1、修改test接口返回内容为238并部署到238机器上:
请求:xxx.xx.xxx.238:9999/demo/index/test
2、 修改test接口返回内容为187并部署到187机器上:
请求:xxx.xx.xxx187:9999/demo/index/test
二、配置nginx代理:
1、配置238机器nginx代理这两个节点的服务:
upstream mydemoservice{
server xxx.xx.xxx.238:9999 weight=5;
server xxx.xx.xxx.187:9999 weight=5;
}
server {
listen 80;
server_name localhost;
charset utf-8,gbk;
#charset koi8-r;
location /mydemoservice/ {
proxy_pass http://mydemoservice/;
}
}
连续请求:xxx.xx.xxx.xx.238/mydemoservice/demo/index/test,
可以看到在238和187之间轮询
2、配置187机器nginx代理这两个节点的服务:nginx配置同238机器。
连续请求:xxx.xx.xxx187/mydemoservice/demo/index/test
同样的,接口在187和238之间轮询:
三、Keepalived:https://blog.csdn.net/l1028386804/article/details/72801492、https://www.cnblogs.com/qinlulu/p/12855440.html
vip需要单独申请,这里写个大概的步骤
1、238 配置:
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 172.0.0.1
smtp_connect_timeout 30
router_id mytest
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 60
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
xxx.xx.xxx.xx
}
}
2、187配置:
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id mytest
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 60
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
xxx.xx.xxx.xx
}
}