LVS+Keepalived+Ngingx+Tomcat+Mariadb+Nfs
1.首先安装LVS+keepalived,这里用yum安装即可
LVS+KEEPAIVED
IP规划
KEEPAIVED 主为MASTER
10.0.0.40
KEEPAIVED 备用BACKUP
10.0.0.41
VIP地址
10.0.0.42
NFS
10.0.0.43
WEB-1
nginx-1
10.0.0.45
WEB-2
nginx-2
10.0.0.46
Tomcat-1
10.0.0.50
Tomcat-2
10.0.0.51
Mariadb-1
10.0.0.55
Mariadb-2
10.0.0.56
安装
yum install ipvsadm keepalived -y
备份一下源配置文件:
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak -a
keepalived
主
vim /etc/keepalived/keepalived.conf
改成一下内容
! Configuration File for keepalived
global_defs {
router_id Director1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.42/24 dev eth0
}
}
virtual_server 10.0.0.42 80 {
delay_loop 3
lb_algo rr
lb_kind DR
protocol TCP
real_server 10.0.0.45 80 {
weight 1
TCP_CHECK {
connect_timeput 3
}
}
real_server 10.0.0.46 80 {
weight 1
TCP_CHECK {
connect_timeput 3
}
}
}
备用
vim /etc/keepalived/keepalived.conf
改成一下内容
! Configuration File for keepalived
global_defs {
router_id Director2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.42/24 dev eth0
}
}
virtual_server 10.0.0.42 80 {
delay_loop 3
lb_algo rr
lb_kind DR
protocol TCP
real_server 10.0.0.45 80 {
weight 1
TCP_CHECK {
connect_timeput 3
}
}
real_server 10.0.0.46 80 {
weight 1
TCP_CHECK {
connect_timeput 3
}
}
}
解释:
! Configuration File for keepalived
global_defs {
router_id Director1 #两边的ID不一样
}
vrrp_instance VI_1 {
state MASTER #主为MASTER 备用的为BACKUP
interface eth0 #心跳网卡设备名称
virtual_router_id 51 #虚拟路由编号,主备要一致
priority 100 #优先级1-255,数字越大优先级越高
advert_int 1 #检测间隔,单位秒
authentication { #
auth_type PASS #
auth_pass 1111 #
} #
virtual_ipaddress { #虚拟VIP地址
10.0.0.42/24 dev eth0 #VIP地址和工作端口和设备名称
} #
} #
#
virtual_server 10.0.0.42 80 { #LVS的虚拟IP地址和端口
delay_loop 3 #服务轮询的时间间隔
lb_algo rr #LVS调度算法
lb_kind DR #LVS集群模式
protocol TCP #
#
real_server 10.0.0.45 80 { #后端web服务器IP和端口
weight 1 #主机权重为1
TCP_CHECK { #TCP链接校验
connect_timeput 3 #链接超时时间为3,如果超过3秒连不上,服务端就会认为这台服务器挡掉了,就会把这台机器踢出去
} #
} #
real_server 10.0.0.46 80 {
weight 1
TCP_CHECK {
connect_timeput 3
}
}
}
不需要启动LVS,只启动keepalived即可!!
不需要启动LVS,只启动keepalived即可!!
主节点启动
启动keepalived
systemctl start keepalived
开机自启动
systemctl enable keepalived
备用节点启动
启动keepalived
systemctl start keepalived
开机自启动
systemctl enable keepalived
web-1安装nginx
10.0.0.45
脚本安装
vim nginx.sh
输入以下内容
#!/bin/bash
#install nginx
yum install wget gcc pcre pcre-devel openssl-devel openssl gzip make cmake zlib-devel tar lrzsz -y
cd
wget http://nginx.org/download/nginx-1.18.0.tar.gz
useradd -s /sbin/nologin -M nginx
tar zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0/
./configure --prefix=/usr/local/nginx/ --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_mp4_module
make
make install
mkdir -p /usr/local/nginx/conf.d/
echo "
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
">/lib/systemd/system/nginx.service
systemctl daemon-reload
systemctl start nginx.service
systemctl enable nginx.service
再来执行脚本
sh nginx.sh
添加测试页面
echo Nginx-1 > /usr/local/nginx/html/index.html
测试访问
[root@nginx-1 ~]# curl 127.0.0.1
Nginx-1
添加虚拟网卡,并编写虚拟网卡配置文件
cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:0 -a
修改虚拟网卡配置文件
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
改成以下内容
DEVICE=lo:0
IPADDR=10.0.0.42
NETMASK=255.255.255.255
ONBOOT=yes
解释:
DEVICE=lo:0 #设备名称lo:0
IPADDR=10.0.0.42 #VIP地址也是就是keepalived里面写的VIP地址
NETMASK=255.255.255.255 #子网掩码32位,不要写成24位
ONBOOT=yes #开机启动此设备
添加路由规则,这样开机就可以自动添加了
echo /sbin/route add host 10.0.0.42 dev lo:0 >> /etc/rc.local
添加权限
chmod +x /etc/rc.local
配置ARP,防止这个设备在局域网内广播,在最后一行添加
vim /etc/sysctl.conf
在最后一行添加以下内容
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
重启让配置生效
reboot
web-2
编译安装nginx,执行以上nginx的脚本即可
安装好以后再来添加测试页面
echo Nginx-2 > /usr/local/nginx/html/index.html
测试访问
[root@nginx-2 ~]# curl 127.0.0.1
Nginx-2
添加虚拟网卡,并编写虚拟网卡配置文件
cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:0 -a
修改虚拟网卡配置文件
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
改成以下内容
DEVICE=lo:0
IPADDR=10.0.0.42
NETMASK=255.255.255.255
ONBOOT=yes
解释:
DEVICE=lo:0 #设备名称lo:0
IPADDR=10.0.0.42 #VIP地址也是就是keepalived里面写的VIP地址
NETMASK=255.255.255.255 #子网掩码32位,不要写成24位
ONBOOT=yes #开机启动此设备
添加路由规则,这样开机就可以自动添加了
echo /sbin/route add host 10.0.0.42 dev lo:0 >> /etc/rc.local
添加权限
chmod +x /etc/rc.local
配置ARP,防止这个设备在局域网内广播,在最后一行添加
vim /etc/sysctl.conf
在最后一行添加以下内容
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
重启让配置生效
reboot
MASTER上查询LVS
ipvsadm -Ln
返回结果:
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.42:80 rr
-> 10.0.0.45:80 Route 1 0 0
-> 10.0.0.46:80 Route 1 0 0
这样LVS调度就完成,可以用浏览器访问vip地址 10.0.0.42
10.0.0.42
修改nginx主配置,两台nginx修改成同样的配置即可
vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream tomcat {
server 10.0.0.50:8080 weight=100 max_fails=2 fail_timeout=15;
server 10.0.0.51:8080 weight=100 max_fails=2 fail_timeout=15;
# server 10.0.0.10:8080 weight=100 max_fails=2 fail_timeout=15;
# server 10.0.0.11:8080 weight=100 max_fails=2 fail_timeout=15;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcat;
proxy_set_header host $host;
root html/;
index index.jsp index.html index.htm;
}
# location ~ .*\.(htm|html|txt|js|css|png|jpg|gif|jpeg|doc|xml)$
# {
# root /data/webapps/;
# expires 30d;
# }
}
}
删除默认测试页面
rm -rf /usr/local/nginx/html/index.html
重新加载nginx配置文件,两台nginx修改好以后都要执行
systemctl reload nginx
10.0.0.50和10.0.0.51安装tomcat
先在服务器上传jdk的包和tomcat的包
apache-tomcat-8.5.50.tar.gz
jdk-8u231-linux-x64.tar.gz
脚本安装
vim install_tomcat.sh
写入以下内容
#!/bin/bash
. /etc/init.d/functions
DIR=`pwd`
JDK_FILE="jdk-8u231-linux-x64.tar.gz"
TOMCAT_FILE="apache-tomcat-8.5.50.tar.gz"
JDK_DIR="/usr/local"
TOMCAT_DIR="/usr/local"
install_jdk(){
if ! [ -f "$DIR/$JDK_FILE" ];then
action "$JDK_FILE 文件不存在" false
exit;
elif [ -d $JDK_DIR/jdk ];then
action "JDK 已经安装" false
exit
else
[ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR
fi
tar xvf $DIR/$JDK_FILE -C $JDK_DIR
cd $JDK_DIR && ln -s jdk1.8.* jdk
cat > /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=$JDK_DIR/jdk
export JRE_HOME=\$JAVA_HOME/jre
export CLASSPATH=\$JAVA_HOME/lib/:\$JRE_HOME/lib/
export PATH=\$PATH:\$JAVA_HOME/bin
EOF
. /etc/profile.d/jdk.sh
java -version && action "JDK 安装完成" || { action "JDK 安装失败" false ; exit; }
}
install_tomcat(){
if ! [ -f "$DIR/$TOMCAT_FILE" ];then
action "$TOMCAT_FILE 文件不存在" false
exit;
elif [ -d $TOMCAT_DIR/tomcat ];then
action "TOMCAT 已经安装" false
exit
else
[ -d "$TOMCAT_DIR" ] || mkdir -pv $TOMCAT_DIR
fi
tar xf $DIR/$TOMCAT_FILE -C $TOMCAT_DIR
cd $TOMCAT_DIR && ln -s apache-tomcat-*/ tomcat
echo "PATH=$TOMCAT_DIR/tomcat/bin:"'$PATH' > /etc/profile.d/tomcat.sh
id tomcat &> /dev/null || useradd -r -s /sbin/nologin tomcat
cat > $TOMCAT_DIR/tomcat/conf/tomcat.conf <<EOF
JAVA_HOME=$JDK_DIR/jdk
JRE_HOME=\$JAVA_HOME/jre
EOF
chown -R tomcat.tomcat $TOMCAT_DIR/tomcat/
cat > /lib/systemd/system/tomcat.service <<EOF
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target
[Service]
Type=forking
EnvironmentFile=$TOMCAT_DIR/tomcat/conf/tomcat.conf
ExecStart=$TOMCAT_DIR/tomcat/bin/startup.sh
ExecStop=$TOMCAT_DIR/tomcat/bin/shutdown.sh
PrivateTmp=true
User=tomcat
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now tomcat.service
systemctl is-active tomcat.service &> /dev/null && action "TOMCAT 安装完成" || { action "TOMCAT 安装失败" false ; exit; }
}
install_jdk
install_tomcat
修改好以后保存退出
执行脚本
sh install_tomcat.sh
安装好以后查看tomcat的ID
#id tomcat
返回结果,你的ID不一定和我的一样
uid=995(tomcat) gid=995(tomcat) groups=995(tomcat)
下载zrlog的war包
官方地址
https://www.zrlog.com/
把下载的好war包,复制到/usr/local/tomcat/webapps/并重命名为ROOT.war
cp zrlog-2.2.0-5e8a51f-release.war /usr/local/tomcat/webapps/ROOT.war
给这个目录tomcat的属主属组的权限
chown -R tomcat. /usr/local/tomcat/webapps/ROOT/
安装Maridb,两台10.0.0.55和10.0.0.56都安装
链接:
http://www.9527edu.org/post/45.html
安装好Maridb以后,两台10.0.0.55和10.0.0.56,设置为主从
vim /etc/my.cnf
Mariadb里面有单独的log-bin=mysql-bin和server-id=1,只需要单独的修改即可,无需添加进去
log-bin=mysql-bin
server-id=1
链接:
http://www.9527edu.org/post/115.html
10.0.0.55为主
10.0.0.56为从
进入主数据库创建zrlog的数据库并授权
进入数据库
mysql -uroot -p密码
创建
create database zrlogdb character set utf8 collate utf8_bin;
授权
grant all on zrlogdb.* to 'zrlogdb'@'%' identified by '123456';
用浏览器访问10.0.0.50:8080
http://10.0.0.50:8080
完成安装
写入信息
数据库服务器
10.0.0.55
数据库名
zrlogdb
数据库用户名
zrlogdb
数据库密码
123456
数据库端口
3306
点击下一步
根据自己的喜好填写
安装好以后,把/usr/local/tomcat/webapps/ROOT这个目录拷贝到/tmp下
cp /usr/local/tomcat/webapps/ROOT /tmp/ -ra
打包/tmp/ROOT目录
进入/tmp下
cd /tmp
打包到/root目录
tar -cvf /root/zrlog.tar.gz ROOT
再通过scp把这个目录拷贝到NFS服务器上
scp /root/zrlog.tar.gz root@10.0.0.43:/root/
安装NFS,10.0.0.43
首先创建一个tomcat用户uid和gid都是995
创建tomcat组,gid为995
groupadd tomcat -g 995
创建tomcat用户,并指定gid995和uid995
useradd tomcat -s /sbin/nologin -M -u 995 -g 995
安装nfs
yum install nfs-utils -y
创建NFS共享文件夹
mkdir -p /zrlog
修改配置文件
vim /etc/exports
修改如下
/zrlog 10.0.0.0/24(rw,sync,all_squash,anonuid=995,anongid=995)
解压zrlog.tar.gz
tar -xf zrlog.tar.gz -C /zrlog
####给tomcat属主属组权限
这条命令可以不用操作,默认是有权限的,如果输入了,可能需要重新解压包才能使用
chown -R tomcat. /zrlog
开机启动nfs
systemctl enable nfs-server
修改好配置文件以后重启nfs
systemctl restart nfs-server
10.0.0.50和10.0.0.51修改一下/etc/fstab,这样开机自动挂载nfs了
先安装nfs
yum install nfs-utils -y
修改配置文件
vim /etc/fstab
添加一下内容,在最后一行添加
10.0.0.43:/zrlog /usr/local/tomcat/webapps/ nfs defaults 0 0
wq保存退出
挂载
mount -a