Keepalived+Nginx 高可用集群(主从模式)
集群架构图:
说明:Keepalived机器同样是nginx负载均衡器。
实现思路:将keepalived 中的vip作为nginx负载均衡的监听地址,并且域名绑定的也是vip的地址。
说明:Nginx 负载均衡实现高可用,需要借助Keepalived地址漂移功能。
1)实验环境准备(此处都是使用的centos7系统)
# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
在所有节点上面进行配置
# systemctl stop firewalld //关闭防火墙
# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux //关闭selinux,重启生效
# setenforce 0 //关闭selinux,临时生效
# ntpdate 0.centos.pool.ntp.org //时间同步
# yum install nginx -y //安装nginx
或者 系统防火墙打开对应的端口 80
# vi /etc/sysconfig/iptables
## Nginx
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
# service iptables restart
keepalived 服务开机自启动
systemctl enable keepalived.service
systemctl disable keepalived.service 取消开机自动启动
systemctl start keepalived.service 启动
systemctl stop keepalived.service停止
systemctl status keepalived.service
2)配置后端web服务器(两台一样)
# echo "`hostname` `ifconfig ens33 |sed -n 's#.*inet \(.*\)netmask.*#\1#p'`" > /usr/share/nginx/html/index.html //准备测试文件,此处是将主机名和ip写到index.html页面中
# vim /etc/nginx/nginx.conf //编辑配置文件
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.mtian.org;
location / {
root /usr/share/nginx/html;
}
access_log /var/log/nginx/access.log main;
}
}
启动nginx
# systemctl start nginx //启动nginx
# systemctl enable nginx //加入开机启动
3)配置LB服务器(两台都一样)
# vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main