Nginx+keepalived双击热备

一、 安装nginx和keepalived
1、 安装依赖程序和管路用户创建
1)
开三台虚拟机

Nginx+keepalived双击热备
Nginx+keepalived双击热备
Nginx+keepalived双击热备
2)
删除系统自带yum源并挂载光盘

[[email protected] ~]# rm -rf /etc/yum.repos.d/CentOS-*
[[email protected] ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载

[[email protected] ~]# rm -rf /etc/yum.repos.d/CentOS-*
[[email protected] ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
3)
安装依赖程序、创建nginx管理用户

[[email protected] ~]# yum -y install pcre-devel zlib-devel kernel-devel popt-devel openssl-devel
[[email protected] ~]# useradd -M -s /sbin/nologin nginx

[[email protected] ~]# yum -y install pcre-devel zlib-devel kernel-devel popt-devel openssl-devel
[[email protected] ~]# useradd -M -s /sbin/nologin nginx

2、 安装nginx
1)
安装nginx

[[email protected] ~]# umount /mnt/
[[email protected] ~]# umount /mnt/
Nginx+keepalived双击热备
[[email protected] ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[[email protected] ~]# tar zxvf /mnt/nginx-1.6.0.tar.gz -C /usr/src/

[[email protected] ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[[email protected] ~]# tar zxvf /mnt/nginx-1.6.0.tar.gz -C /usr/src/
2)
配置nginx并配置安装nginx

[[email protected] nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --with-http_stub_status_module
[[email protected] nginx-1.6.0]# make && make install

[[email protected] nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --with-http_stub_status_module
[[email protected] nginx-1.6.0]# make && make install
3)
优化nginx命令和修改nginx网站主页

[[email protected] ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[[email protected] ~]# echo “www.benet.com” > /usr/local/nginx/html/index.html

[[email protected] ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[[email protected] ~]# echo “www.accp.com” > /usr/local/nginx/html/index.html
4)
启动nginx服务

[[email protected] ~]# nginx
[[email protected] ~]# netstat -anptu | grep 80
Nginx+keepalived双击热备
[[email protected] ~]# nginx
[[email protected] ~]# netstat -anptu | grep 80
Nginx+keepalived双击热备
5)
设置nginx服务开机自动启动

[[email protected] ~]# vim /etc/rc.d/rc.local
/usr/local/sbin/nginx
[[email protected] ~]# chmod +x /etc/rc.d/rc.local

[[email protected] ~]# vim /etc/rc.d/rc.local
/usr/local/sbin/nginx
[[email protected] ~]# chmod +x /etc/rc.d/rc.local

6)
重新启动查看

[[email protected] ~]# reboot
[[email protected] ~]# reboot
Nginx+keepalived双击热备
Nginx+keepalived双击热备
3、 安装keepalived
1)
配置keepaliced****
[[email protected] ~]# tar zxvf /mnt/keepalived-1.2.13.tar.gz -C /usr/src/
[[email protected] keepalived-1.2.13]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/

[[email protected] ~]# tar zxvf /mnt/keepalived-1.2.13.tar.gz -C /usr/src/
[[email protected] keepalived-1.2.13]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/

2)
安装keepalived

[[email protected] keepalived-1.2.13]# make && make install

[[email protected] keepalived-1.2.13]# make && make install
3)
添加系统服务设置开机自动启动

[[email protected] ~]# chkconfig --add keepalived
[[email protected] ~]# chkconfig --level 35 keepalived on

[[email protected] ~]# chkconfig --add keepalived
[[email protected] ~]# chkconfig --level 35 keepalived on
4、 配置主keepalived和从keepalived
1)
配置主keepalived

[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
router_id Nginx_Master
}

vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.254
}
vrrp_script check_nginx {
script “/opt/nginx.sh”
interval 2
weight 1
}
Nginx+keepalived双击热备
2)
配置从keepalived

[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
router_id Nginx_Backup
}

vrrp_instance VI_1 {
state Backup
interface ens32
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.254
}
vrrp_script check_nginx {
script “/opt/nginx.sh”
interval 2
weight 1
}
}
Nginx+keepalived双击热备
3)
配置监控nginx服务和keepalived脚本

[[email protected] ~]# vim /opt/nginx.sh
#!/bin/bash
counter=(psCnginxnoheadingwcl)if["(ps -C nginx --no-heading|wc -l) if [ "{counter}" = “0” ]; then
/usr/local/nginx/sbin/nginx
sleep 2
counter=(psCnginxnoheadingwcl)if["(ps -C nginx --no-heading|wc -l) if [ "{counter}" = “0” ]; then
/etc/init.d/keepalived stop
fi
fi
Nginx+keepalived双击热备
[[email protected] ~]# chmod +x /opt/nginx.sh
[[email protected] ~]# systemctl start keepalived
4)
配置从监控nginx服务和keepalived脚本

[[email protected] ~]# vim /opt/nginx.sh
#!/bin/bash
counter=(psCnginxnoheadingwcl)if["(ps -C nginx --no-heading|wc -l) if [ "{counter}" = “0” ]; then
/usr/local/nginx/sbin/nginx
sleep 2
counter=(psCnginxnoheadingwcl)if["(ps -C nginx --no-heading|wc -l) if [ "{counter}" = “0” ]; then
/etc/init.d/keepalived stop
fi
fi
Nginx+keepalived双击热备
[[email protected] ~]# chmod +x /opt/nginx.sh
[[email protected] ~]# systemctl start keepalived
5)
测试关闭主,查看从

[[email protected] ~]# systemctl stop keepalived
Nginx+keepalived双击热备
开启主查看
Nginx+keepalived双击热备
6)
客户端配置IP地址访问

Nginx+keepalived双击热备
Nginx+keepalived双击热备
停掉主节点f5刷新
[[email protected] ~]# systemctl stop keepalived
Nginx+keepalived双击热备
二、 配置防火墙
1、 防火墙配置ip地址伪装和端口映射
1)
配置网卡2

[[email protected] ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens32 /etc/sysconfig/network-scripts/ifcfg-ens34
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens34
NAME=ens34
DEVICE=ens34
IPADDR=192.168.200.30
NETMASK=255.255.255.0
[[email protected] ~]# systemctl restart network
Nginx+keepalived双击热备
2)
其余两台配置网关

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
GATEWAY=192.168.100.30
[[email protected] ~]# systemctl restart network

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
GATEWAY=192.168.100.30
[[email protected] ~]# systemctl restart network
3)
设置防火墙开机自启

[[email protected] ~]# systemctl start firewalld
[[email protected] ~]# systemctl enable firewalld
4)
设置默认区域,并查看

[[email protected] ~]# firewall-cmd --set-default-zone=external
Success
[[email protected] ~]# firewall-cmd --add-interface=ens34 --zone=external
[[email protected] ~]# firewall-cmd --add-interface=ens32 --zone=truste
[[email protected] ~]# firewall-cmd --get-active-zones
external
interfaces: ens34
trusted
interfaces: ens32
5)
配置地址伪装

[[email protected] ~]# firewall-cmd --remove-masquerade
success
[[email protected] ~]# firewall-cmd --list-all
Nginx+keepalived双击热备
[[email protected] ~]# firewall-cmd --zone=external --add-rich-rule=‘rule family=ipv4 source address=192.168.100.0/24 masquerade’
success
[[email protected] ~]# firewall-cmd --list-all
Nginx+keepalived双击热备
6)
配置地址转发

[[email protected] ~]# firewall-cmd --zone=external --add-rich-rule=‘rule family=ipv4 destination address=192.168.200.30/32 forward-port port=80 protocol=tcp to-addr=192.168.100.254’
Success
Nginx+keepalived双击热备
7)
允许外网使用http、dns访问

[[email protected] ~]# firewall-cmd --zone=external --add-service=http
success
[[email protected] ~]# firewall-cmd --zone=external --add-service=dns
success
三、 配置dns
1)
配置dns
使用系统自带光盘

Nginx+keepalived双击热备
2)
删除系统自带yum源并挂载光盘

[[email protected] ~]# rm -rf /etc/yum.repos.d/CentOS-*
[[email protected] ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
3)
安装dns

[[email protected] ~]# yum -y install bind bind-chroot bind-utils
4)
配置dns

[[email protected] ~]# echo “” > /etc/named.conf
[[email protected] ~]# vim /etc/named.conf
options {
listen-on port 53 { any; };
directory “/var/named/”;
};
zone “benet.com” IN {
type master;
file “benet.com.zone”;
};

[[email protected] ~]# vim /var/named/benet.com.zone

$TTL 86400
@ SOA benet.com. root.benet.com (
2020031710
1H
15M
1W
1D
)
@ NS centos03.benet.com.
centos03 A 192.168.200.30
www A 192.168.200.30

[[email protected] ~]# chmod +x /var/named/benet.com.zone
[[email protected] ~]# chown named:named /var/named/benet.com.zone
[[email protected] ~]# named-checkconf /etc/named.conf
[[email protected] ~]# named-checkzone benet.com /var/named/benet.com.zone
zone benet.com/IN: loaded serial 2020031710
OK
5)
启动dns

[[email protected] ~]# systemctl start named
[[email protected] ~]# systemctl enable named
6)
开启路由转发

[[email protected] ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[[email protected] ~]# sysctl -p
net.ipv4.ip_forward = 1
7)
客户端配置IP地址访问

Nginx+keepalived双击热备
Nginx+keepalived双击热备
Nginx+keepalived双击热备
关闭主节点f5刷新
[[email protected] ~]# systemctl stop keepalived
Nginx+keepalived双击热备

相关文章: