系统架构
Keepalived配置主主备份,对外虚拟IP为172.16.1.88,外部可通过虚拟IP访问A和B,当A或B其中一台宕机时,keepalived依旧保持外部访问不受影响。
Nginx配置tomcat服务器负载均衡,由tomcatA与tomcatB均衡负载用户请求,即当外部发起10个http请求时,tomcatA与tomcatB分别负载处理5个请求。
Mysql主主同步保证了数据库数据的一致性,当一个数据库发生数据变化时,另一个会自动更新。
准备:服务器A(172.16.1.200)、服务器B(172.16.1.201)
软件安装配置,两台服务器均相同:
Mysql(Ubuntu 16.04 Mysql主主备份配置_IT小白-北极冰雪的博客-CSDN博客)
KeepAlived (Ubuntu 16.04 在线搭建keepalived双机热备_IT小白-北极冰雪的博客-CSDN博客)
Nginx(Ubuntu 16.04在线安装nginx_IT小白-北极冰雪的博客-CSDN博客)
Linux下只需要修改配置文件即可。
#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;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#配置负载服务器
upstream tomcat{
server 172.16.1.200:8082 weight=10;
server 172.16.1.201:8082 weight=10;
}
#gzip on;
server {
listen 80;#Nginx启动端口
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
#配置代理,需要与负载同名(此处命名为tomcat)
proxy_pass http://tomcat;
proxy_redirect default;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}