一: 网络拓扑结构图
二: keepalived 及 nginx 安装
源码安装 nginx
- [[email protected] ~]#yum -y install pcre-devel zlib-devel make gcc gcc-c++ openssl-devel #安装 Nginx 依赖包
- [[email protected] ~]#useradd -M -s /sbin/nologin nginx #创建一个 Nginx 测试用户
- [[email protected] ~]#tar xf nginx -C /usr/src #将 Nginx 解包到 / usr/src 下
- [[email protected] ~]#cd /usr/src/nginx
- [[email protected] ~]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre && make && make install #对 Nginx 进行编译安装
--prefix 设定 Nginx 的安装目录
--user 和 --group 指定 Nginx 运行用户和组
--with-http_stub_status_module 启用 http_stub_status_module 模块以支持状态统计
--with-http_ssl_module 启用 SSL 模块
--with-http_flv_module 启用 FLV 模块, 提供寻求内存使用基于时间的偏移量文件
- [[email protected] ~]#ln -s /usr/local/nginx/sbin/nginx /usr/local/bin #给 Nginx 一个软连接
- [[email protected] ~]#nginx -t #检查语法
- [[email protected] ~]# nginx #开启 Nginx 服务
- [[email protected] ~]#netstat -lnpt | grep 80 #查看端口
yum 安装 nginx
- # 安装 nginx
- [[email protected] ~]# yum -y install nginx
输入命令:
whereis nginx
即可看到类似于如下的内容:
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx
以下是 Nginx 的默认路径:
(1) Nginx 配置路径:/etc/nginx/
(2) PID 目录:/var/run/nginx.pid
(3) 错误日志:/var/log/nginx/error.log
(4) 访问日志:/var/log/nginx/access.log
(5) 默认站点目录:/usr/share/nginx/html
事实上, 只需知道 Nginx 配置路径, 其他路径均可在 / etc/nginx/nginx.conf 以及 / etc/nginx/conf.d/default.conf 中查询到.
常用命令
(1) 启动:
nginx
(2) 测试 Nginx 配置是否正确:
nginx -t
(3) 优雅重启:
nginx -s reload
该命令与以下命令类似:
kill -HUP nginx 进程号
- # 安装 keepalived
- [[email protected] ~]# yum -y install keepalived
三: keepalived 配置
主 eepalived 设置
- [[email protected] keepalived]# pwd
- /etc/keepalived
- [[email protected] keepalived]# cp keepalived.conf keepalived.conf.bak
- [[email protected] ~]# VIM /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived #全局定义
- global_defs {
- notification_email {
- #指定 keepalived 在发生事件时 (比如切换) 发送通知邮件的邮箱
- [email protected]
- [email protected] #设置报警邮件地址, 可以设置多个, 每行一个. 需开启本机的 sendmail 服务
- [email protected]
- }
- notification_email_from [email protected] #keepalived 在发生诸如切换操作时需要发送 email 通知地址
- smtp_server 192.168.200.1 #指定发送 email 的 smtp 服务器
- smtp_connect_timeout 30 #设置连接 smtp server 的超时时间
- router_id LVS_DEVEL #运行 keepalived 的机器的一个标识, 通常可设为 hostname. 故障发生时, 发邮件时显示在邮件主题中的信息.
- vrrp_skip_check_adv_addr
- vrrp_strict
- vrrp_garp_interval 0
- vrrp_gna_interval 0
- }
- vrrp_script check_nginx {
- #引入脚本文件
- script "/shell/nginx_check.sh"
- interval 2
- weight -20
- }
- vrrp_instance VI_1 {
- #vrrp 实例定义部分
- state MASTER #指定 Keepalived 的角色, MASTER 表示此主机是主用服务器, BACKUP 表示是备用服务器. 备份服务器上将 MASTER 改为 BACKUP.
- interface ens32 #指定 HA 监测网络的接口
- virtual_router_id 51 #虚拟路由标识, 这个标识是一个数字, 并且同一个 vrrp 实例使用唯一的标识, 即同一个 vrrp_instance 下, MASTER 和 BACKUP 必须是一致的.(0-255)
- priority 100 #定义优先级, 数字越大, 优先级越高, 在一个 vrrp_instance 下, MASTER 的优先级必须大于 BACKUP 的优先级. 备份服务上将 100 改为 50
- advert_int 1 #设定 MASTER 与 BACKUP 负载均衡器之间同步检查的时间间隔, 单位是秒
- authentication {
- #设定验证类型和密码
- auth_type PASS #设置验证类型, 主要有 PASS 和 AH 两种
- auth_pass 1111 #设置验证密码, 在一个 vrrp_instance 下, MASTER 与 BACKUP 必须使用相同的密码才能正常通信.
- }
- virtual_ipaddress {
- #设置虚拟 IP 地址, 可以设置多个虚拟 IP 地址, 每行一个
- 192.168.200.254
- }
- track_script {
- check_nginx #引用脚本
- }
- }
- # 准备测试文件
- [[email protected] ~]# echo "1111111"> /usr/share/nginx/HTML/index.HTML
从 keepalived 设置
- [[email protected] ~]# VIM /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- [email protected]
- [email protected]
- [email protected]
- }
- notification_email_from [email protected]
- smtp_server 192.168.200.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- vrrp_skip_check_adv_addr
- vrrp_strict
- vrrp_garp_interval 0
- vrrp_gna_interval 0
- }
- vrrp_script check_nginx {
- script "/shell/nginx_check.sh"
- interval 2
- weight -20
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eno16777728
- virtual_router_id 51
- priority 90
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.200.254
- }
- track_script {
- check_nginx
- }
- }
- # 准备测试文件
- [[email protected] ~]# echo "22222"> /usr/share/nginx/HTML/index.HTML
四: 测试
主服务器工作时
从服务器工作时
五: nginx_check.sh shell 文件, 配置为周期性任务
- #!/bin/bash
- count="$(ps -C nginx --no-header|wc -l)"
- if [ $count -eq 0 ];then
- systemctl restart nginx
- sleep 2
- if [ ps -c nginx --no-header|wc -l -eq 0 ];then
- systemctl stop keepalived
- fi
- fi
来源: http://www.bubuko.com/infodetail-3229854.html