一、Centos7 安装 keepalived 负载均衡 Nginx 负载均衡 Tomcat
客户要求,服务器要做负载,服务也需要做负载,同时要做反向代理。
为了达到客户的要求,遇到了几个问题,最后慢慢解决了。过程很惨,但是下一次就没有问题了。
安装 centos7 下面的地址为centos 的地址
http://isoredirect.centos.org/
下载镜像文件即可。
大概30分钟左右下载完成。
客户提供的iso镜像有问题,因此我重新下载了一个,可以正常使用。
开始安装
首先选择中文模式,分区为自动分区,安装模块除了智能卡和备份一起都勾选。Gnome模式。
下面开始 提示正在安装 代表开始安装软件。 如果遇到问题,直接更换镜像,不要犹豫。
防火墙常用命令:
启动: systemctl start firewalld
查看状态: systemctl status firewalld 或者 firewall-cmd --state
停止: systemctl disable firewalld
禁用: systemctl stop firewalld
添加端口
firewall-cmd --permanent --zone=public --add-port=80/tcp
删除端口
firewall-cmd --permanent --zone=public --remove-port=80/tcp
重新加载(修改端口后必须运行)
firewall-cmd --reload
二、安装 keepalived
前期准备工作:
我们需要准备至少3个IP地址,如果需要换成自己的即可。
192.168.91.134 routeid 虚拟IP地址
192.168.91.135 server1 服务器A地址
192.168.91.136 server2 服务器B地址
配置之前要注意获取网卡的名称:服务器每个网卡都有自己的名称,我们可以在以下路径中查看到:
/etc/sysconfig/network-scripts/
/etc/sysconfig/network-scripts/ifcfg-ens33 后面的这个ifcfg-ens33 就是我的网卡文件。
执行下面命令 就可以获取到网卡的名称,其实 NAME=ens33 就是: ens33
cat /etc/sysconfig/network-scripts/ifcfg-ens33
1、安装建议使用 yum 方式安装
首先执行:
yum -y install keepalived
2、如果报错 another app is currently holding the yun lock
那么执行
rm -f /var/run/yum.pid
3、如果没错,那么会提示
已经安装 完毕。
4、安装完成后通过以下命令配置,查看,启动
1:systemctl daemon-reload 重新加载
2:systemctl enable keepalived.service 设置开机自动启动
3:systemctl disable keepalived.service 取消开机自动启动
4:systemctl start keepalived.service 启动
5:systemctl stop keepalived.service 停止
5、配置 keepalived
我们打开 keepalived 的配置文件 ,
路径在: /etc/keepalived/keepalived.conf
首先通过 vi /etc/keepalived/keepalived.conf 命令打开文件直接编辑:
不需要修改的地方: 224.0.38.12 , 127.0.0.1 这些都不用改,就改我让修改的内容。
这里修改的地方:
team0 bond0 两个网卡的名称
192.168.91.134 路由地址 这个地址是虚拟的,不能被使用 用于跳转
192.168.91.135 serverA地址
192.168.91.136 serverB地址
auth_pass x1er44q 密码
#------------serverA 配置文件---------------------------------
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node-00
vrrp_mcast_group4 224.0.38.12
}
vrrp_instance VI_1 {
state MASTER
interface team0
virtual_router_id 38
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass x1er44q
}
virtual_ipaddress {
192.168.91.134/32 dev team0
}
smtp_alert
}
virtual_server 192.168.91.134 80 {
delay_loop 3
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.91.135 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.91.136 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 1
}
}
}
#--- serverB 配置------------------------------------------------------------
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node-01
vrrp_mcast_group4 224.0.38.12
}
vrrp_instance VI_1 {
state BACKUP
interface bond0
virtual_router_id 38
priority 97
advert_int 1
authentication {
auth_type PASS
auth_pass x1er44q
}
virtual_ipaddress {
192.168.91.134 /32 dev bond0
}
smtp_alert
}
virtual_server 192.168.91.134 80 {
delay_loop 3
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.91.135 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.91.136 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 1
}
}
}
模拟测试 关机的情况
执行 systemctl stop keepalived
通过
curl 192.168.91.134 就可以测试配置情况
三、安装 nginx 配置负载以及 反向代理
登录 centos系统,进入
cd /usr/local 进入目录
按照顺序执行
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
wget -c https://nginx.org/download/nginx-1.14.2.tar.gz
tar -zxvf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure
make
make install
进入到目录中
cd /usr/local/nginx/sbin/
./nginx //启动服务
./nginx -s stop //停止服务
./nginx -s quit //退出服务
./nginx -s reload //重新加载文件 这里强调的是,更改完配置文件一定要重新载入,可以不用重新启动。自动生效。
打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
增加 Nginx 开机启动:
vi /etc/rc.local
增加一行 /usr/local/nginx/sbin/nginx
chmod 755 rc.local
四 配置 Nginx 和 反向代理
我的建议直接备份nginx的配置文件。备份成功后,删除原有文件中的内容:
直接使用我的。
这里我们在增加两台服务器:
192.168.91.137 安装 jdk tomcat 配置多个tomcat服务 80 8081
192.168.91.138 安装 jdk tomcat 配置多个tomcat服务 8082 8083
#user nobody;
worker_processes 1; #配置处理器 默认就行
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024; #配置连接数,默认就行 不够了就增加
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream tomcat01{
server 192.168.91.137:80 weight=1; #权重默认都是1(默认状态是轮询)
server 192.168.91.137:8081 weight=2;
}
upstream tomcat02{
server 192.168.91.138:8081 weight=1; #权重默认都是1(默认状态是轮询)
server 192.168.91.138:8082 weight=2;
}
server {
listen 80;
server_name 192.168.91.134;
#默认请求: http://192.168.91.134 直接请求 tomcat01的服务
location / {
proxy_pass http://tomcat01; #负载名称 和上面 upstream tomcat01 后面的名称一样即可。
index index.html index.htm;
}
#跳转请求: http://192.168.91.134/server-web/ 直接请求 tomcat01的服务
location /server-web/ { #代理名称可修改
proxy_redirect off;
proxy_set_header Host $host:${server_port};
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcat02/; #负载名称 和上面 upstream tomcat02 后面的名称一样即可。
proxy_redirect http://$host:$server_port;
index index.html index.htm;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}