实现负载均衡的方式有很多,例如基于DNS的负载均衡、基于四层交换技术的负载均衡、基于七层交换技术的负载均衡,其中nginx大家不陌生,我们可以通过配置实现负载均衡,可是问题也很明显,如果nginx宕机了,那么如何进行负载?这个时候会想多来几台nginx,综合考虑,今天介绍一种基于四层+七层交换技术的负载均衡,以虚拟ip来负载给我们多台nginx,再由nginx来实现负载均衡过程。本文采用CentOS 7.6作为操作系统示例。
VMware创建2台虚拟机,XShell工具连接虚拟机进行操作,以下操作集群2台设备都需要完成:
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
[root@localhost ~]# yum install -y gcc
[root@localhost ~]# yum install -y pcre pcre-devel
[root@localhost ~]# yum install -y zlib zlib-devel
[root@localhost ~]# yum install -y openssl openssl-devel
[root@localhost ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
[root@localhost ~]# tar -zxvf nginx-1.8.0.tar.gz
[root@localhost ~]# cd nginx-1.8.0/
[root@localhost nginx-1.8.0]#
[root@localhost nginx-1.8.0]# ./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
[root@localhost nginx-1.8.0]# make
[root@localhost nginx-1.8.0]# make install
/var/
下创建temp目录和nginx目录::[root@localhost /]# mkdir -p /var/temp/nginx/
[root@localhost nginx]# pwd
/var/temp/nginx
[root@localhost ~]# cd /var/run/
[root@localhost run]# mkdir nginx
[root@localhost nginx]# pwd
/usr/local/nginx
[root@localhost nginx]# cd sbin/
[root@localhost sbin]# ./nginx
# 64519是nginx主进程的进程id,64520是nginx工作进程的进程id
[root@localhost sbin]# ps aux|grep nginx
root 64519 0.0 0.0 24892 772 ? Ss 09:36 0:00 nginx: master process ./nginx
nobody 64520 0.0 0.1 27404 1520 ? S 09:36 0:00 nginx: worker process
root 64531 0.0 0.0 112808 964 pts/1 R+ 09:36 0:00 grep --color=auto nginx
注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:
./nginx -c /usr/local/nginx/conf/nginx.conf
如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件,此文件的地址也可以在编译安装nginx时指定./configure的参数(--conf-path= 指向配置文件(nginx.conf))
VMware创建1台虚拟机,XShell工具连接虚拟机进行操作:
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
[root@localhost ~]# yum install -y gcc gcc-c++ makepcre pcre-devel kernel-devel openssl-devel libnl-devel popt*
[root@localhost ~]# cat /etc/system-release
CentOS Linux release 7.9.2009 (Core)
# centos7已经没有集成modprobe的“-l”参数了
[root@localhost ~]# modprobe -l | grep ipvs
modprobe: invalid option -- 'l'
[root@localhost ~]#
# 可以使用如下命令:
[root@localhost ~]# find /lib/modules/$(uname -r)/ -iname "**.ko*" | cut -d/ -f5-
.........略..................
kernel/net/netfilter/ipvs/ip_vs.ko.xz
kernel/net/netfilter/ipvs/ip_vs_dh.ko.xz
kernel/net/netfilter/ipvs/ip_vs_ftp.ko.xz
kernel/net/netfilter/ipvs/ip_vs_lblc.ko.xz
kernel/net/netfilter/ipvs/ip_vs_lblcr.ko.xz
kernel/net/netfilter/ipvs/ip_vs_lc.ko.xz
kernel/net/netfilter/ipvs/ip_vs_nq.ko.xz
kernel/net/netfilter/ipvs/ip_vs_pe_sip.ko.xz
kernel/net/netfilter/ipvs/ip_vs_rr.ko.xz
kernel/net/netfilter/ipvs/ip_vs_sed.ko.xz
kernel/net/netfilter/ipvs/ip_vs_sh.ko.xz
kernel/net/netfilter/ipvs/ip_vs_wlc.ko.xz
kernel/net/netfilter/ipvs/ip_vs_wrr.ko.xz
.........略..................
[root@localhost ~]# cd /usr/local/
[root@localhost local]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
[root@localhost local]# tar zxvf ipvsadm-1.26.tar.gz
[root@localhost local]# cd ipvsadm-1.26/
[root@localhost ipvsadm-1.26]# make
[root@localhost ipvsadm-1.26]# make install
[root@localhost ipvsadm-1.26]# ipvsadm -h
ipvsadm v1.26 2008/5/15 (compiled with popt and IPVS v1.2.1)
Usage:
ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]] [-M netmask] [--pe persistence_engine]
ipvsadm -D -t|u|f service-address
ipvsadm -C
ipvsadm -R
ipvsadm -S [-n]
.........略..................
[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# vi ifcfg-ens33:0
# 将以下配置拷贝进去
DEVICE="ens33:0"
BOOTPROTO="static"
ONBOOT="yes"
IPADDR=192.168.1.200
NETMASK=255.255.255.255
BROADCAST=192.168.1.200
# 刷新
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# route add -host 192.168.1.200 dev ens33:0
[root@localhost network-scripts]# echo "1" >/proc/sys/net/ipv4/ip_forward
[root@localhost network-scripts]# ipvsadm --clear
添加虚拟IP规则:
[root@localhost network-scripts]# ipvsadm -A -t 192.168.1.200:80 -s rr
[root@localhost network-scripts]# ipvsadm -a -t 192.168.1.200:80 -r 192.168.1.13:80 -g
[root@localhost network-scripts]# ipvsadm -a -t 192.168.1.200:80 -r 192.168.1.14:80 -g
[root@localhost network-scripts]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP localhost.localdomain:http rr
-> 192.168.1.13:http Route 1 0 0
-> 192.168.1.14:http Route 1 0 0
[root@localhost sbin]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.1.200
BROADCAST=192.168.1.200
NETMASK=255.255.255.255
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
ONBOOT=yes
NAME=loopback
[root@localhost network-scripts]# systemctl restart network
[root@localhost sbin]# /sbin/route add -host 192.168.1.200 dev lo:0
关闭arp解析:
[root@localhost conf]# pwd
/proc/sys/net/ipv4/conf
[root@localhost conf]# echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
[root@localhost conf]# echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
[root@localhost conf]# echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
[root@localhost conf]# echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
#使用修改生效
[root@localhost conf]# sysctl -p
[root@localhost conf]# vi nginx.conf
# 配置如下
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
upstream tomcat_server_pool{
server 192.168.1.20:8080 weight=10;
server 192.168.1.21:8080 weight=10;
}
server {
listen 80;
server_name localhost;
location / {
root html;
proxy_pass http://tomcat_server_pool;
index index.html index.htm;
}
}
Enjoy GreatSQL :)
GreatSQL是由万里数据库维护的MySQL分支,专注于提升MGR可靠性及性能,支持InnoDB并行查询特性,是适用于金融级应用的MySQL分支版本。
相关链接: GreatSQL社区 Gitee GitHub Bilibili
捉虫活动详情:https://greatsql.cn/thread-97-1-1.html
社区博客有奖征稿详情:https://greatsql.cn/thread-100-1-1.html