1、Haproxy+Keepalived实现站点高可用
后端搭建http服务器:
yum install httpd
vim /etc/httpd/conf.d/vhost.conf
ServerName 192.168.0.241
DocumentRoot "/data/web/vhost1"
Options FollowSymLinks
AllowOverride None
Require all granted
ServerName 192.168.0.242
DocumentRoot "/data/web/vhost2"
Options FollowSymLinks
AllowOverride None
Require all granted
mkdir -p /data/web/vhost{1,2}
vim /data/web/vhost1/index.html
vhost1 192.168.0.241
vim /data/web/vhost2/index.html
vhost2 192.168.0.242
systemctl start httpd
搭建Haproxy+Keepalived主站点:
yum install haproxy keepalived
vi /etc/haproxy/haproxy.cfg
frontend main *:80
default_backend app
backend app
balance roundrobin
server app1 192.168.0.241:80 check
server app2 192.168.0.242:80 check
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
script_user root
enable_script_security
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id n1
vrrp_mcast_group4 224.1.101.33
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100 ##数值大为master
advert_int 1
authentication {
auth_type PASS
auth_pass ZRhbGtAZ
}
virtual_ipaddress {
192.168.0.100/24 dev ens33 label ens33:0
}
}
systemctl start keepalived haproxy
搭建Haproxy+Keepalived备用站点:
yum install haproxy keepalived
vi /etc/haproxy/haproxy.cfg
frontend main *:80
default_backend app
backend app
balance roundrobin
server app1 192.168.0.241:80 check
server app2 192.168.0.242:80 check
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
script_user root
enable_script_security
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id n2
vrrp_mcast_group4 224.1.101.33
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 95 ##数值大为master
advert_int 1
authentication {
auth_type PASS
auth_pass ZRhbGtAZ
}
virtual_ipaddress {
192.168.0.100/24 dev ens33 label ens33:0
}
}
systemctl start keepalived haproxy
测试:
2、搭建tomcat服务器,并通过nginx反向代理访问
yum install /home/admin/下载/jdk-13.0.1_linux-x64_bin.rpm
vim /etc/profile.d/java.sh
JAVA_HOME=/usr/java/latest
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
exec bash
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.47/bin/apache-tomcat-8.5.47.tar.gz
tar -xvf apache-tomcat-8.5.47.tar.gz
vim /etc/profile.d/tomcat.sh
CATALINA_BASE=/usr/local/tomcat
PATH=$CATALINA_BASE/bin:$PATH
export CATALINA_BASE PATH
useradd tomcat
chown -R :tomcat ./apache-tomcat-8.5.47/
cd apache-tomcat-8.5.47/
chown -R tomcat logs/ temp/ work/
chmod g+r conf/*
/bin/catalina.sh start
yum install nginx
vim /etc/nginx/nginx.conf
location / {
root /root/apache-tomcat-8.5.47/webapps/ROOT/;
}
location ~* \.jsp$ {
index index.jsp index.html;
proxy_pass http://192.168.0.133:8080;
}
systemctl start nginx
nginx -s reload
客户端浏览器访问 http://192.168.0.133:8080
3、搭建Tomcat,并基于memcached实现会话共享
安装第一台tomcat及memcached
yum install /home/admin/下载/jdk-13.0.1_linux-x64_bin.rpm memcached
vim /etc/profile.d/java.sh
JAVA_HOME=/usr/java/latest
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
exec bash
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.47/bin/apache-tomcat-8.5.47.tar.gz
tar -xvf apache-tomcat-8.5.47.tar.gz
useradd tomcat
chown -R :tomcat ./apache-tomcat-8.5.47/
cd apache-tomcat-8.5.47/
chown -R tomcat logs/ temp/ work/
chmod g+r conf/*
vim conf/server.xml
memcachedNodes="n1:192.168.0.133:11211,n2:192.168.0.143:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
mkdir -pv /root/apache-tomcat-8.5.47/webapps/test/WEB-INF/{classes,lib}
vim /root/apache-tomcat-8.5.47/webapps/test/index.jsp
<%@ page language="java" %>
TomcatA
TomcatA.magedu.com
Session ID <% session.setAttribute("magedu.com","magedu.com"); %>
<%= session.getId() %>
Created on
<%= session.getCreationTime() %>
cp /root/{asm-7.2.jar,memcached-session-manager-tc8-2.3.2.jar,objenesis-3.1.jar,kryo-4.0.2.jar,minlog-1.3.1.jar,reflectasm-1.11.9.jar,kryo-serializers-0.45.jar,msm-kryo-serializer-2.3.2.jar,spymemcached-2.12.3.jar,memcached-session-manager-2.3.2.jar} /root/apache-tomcat-8.5.47/lib/
/bin/catalina.sh start
systemctl start memcached
安装第二台tomcat及memcached
yum install /home/admin/下载/jdk-13.0.1_linux-x64_bin.rpm memcached
vim /etc/profile.d/java.sh
JAVA_HOME=/usr/java/latest
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
exec bash
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.47/bin/apache-tomcat-8.5.47.tar.gz
tar -xvf apache-tomcat-8.5.47.tar.gz
useradd tomcat
chown -R :tomcat ./apache-tomcat-8.5.47/
cd apache-tomcat-8.5.47/
chown -R tomcat logs/ temp/ work/
chmod g+r conf/*
vim conf/server.xml
memcachedNodes="n1:192.168.0.133:11211,n2:192.168.0.143:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
mkdir -pv /root/apache-tomcat-8.5.47/webapps/test/WEB-INF/{classes,lib}
vim /root/apache-tomcat-8.5.47/webapps/test/index.jsp
<%@ page language="java" %>
TomcatB
TomcatB.magedu.com
Session ID <% session.setAttribute("magedu.com","magedu.com"); %>
<%= session.getId() %>
Created on
<%= session.getCreationTime() %>
cp /root/{asm-7.2.jar,memcached-session-manager-tc8-2.3.2.jar,objenesis-3.1.jar,kryo-4.0.2.jar,minlog-1.3.1.jar,reflectasm-1.11.9.jar,kryo-serializers-0.45.jar,msm-kryo-serializer-2.3.2.jar,spymemcached-2.12.3.jar,memcached-session-manager-2.3.2.jar} /root/apache-tomcat-8.5.47/lib/
/bin/catalina.sh start
systemctl start memcached
搭建nginx服务器:
yum install nginx
http {
upstream tomcat {
server 192.168.0.133:8080;
server 192.168.0.143:8080;
}
server {
listen 8080;
server_name 192.168.0.241;
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://tomcat;
}
}
}
systemctl start nginx
nginx -s reload
客户端测试: