是这样子的,我想让家中全部的应用服务都从nginx中出去,让nginx处于访问的最边缘地带,为了让nginx可靠性增强,因此nginx就得实现高可用,分别是下面两台机器要作nginx的集群nginx
固然上面的机器同时也要安装nginx,总的架构就是文章图片那个样子git
当主10.10.10.2挂了的时候能够自动切换到备10.10.10.3上的nginx,这样就实现了nginx的高可用github
文中的后端应用服务能够有不少个不仅仅只有一个web网站web
咱们从头开始docker
首先用户访问www.bboysoul.com这个网站要通过dns解析,因此咱们要在dns服务器上加一条www.bboysoul.com到10.10.10.10的解析,我使用的dns服务器是dnsmasq,这个怎么安装使用我就不详细解释了,想要了解的能够在个人博客里面搜索vim
address=/www.bboysoul.com/10.10.10.10
后端
以后用户就访问到了咱们的vip,这个时候就是下一步安装keepalived的时候浏览器
安装keepalived,记得两台机器都要安装哦bash
yum install keepalived
服务器
以后修改配置文件,一样两台机器都要 修改
vim /etc/keepalived/keepalived.conf
主的keepalived配置文件
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
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.10.10.10/24
}
}
复制代码
从的keepalived配置文件
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.10.10/24
}
}
复制代码
两个配置文件其实就是
priority 99
state BACKUP
两个地方不同
以后重启服务,两台机器都要重启
systemctl restart keepalived
接着咱们就是在这两台机器上安装nginx来实现反向代理,为了简单呢,我直接使用docker去安装nginx
首先clone我nginx的docker-compose配置文件
git clone https://github.com/bboysoulcn/awesome-dockercompose.git
cd awesome-dockercompose/nginx
启动nginx
docker-compose up -d
以后修改配置文件
vim /var/lib/docker/volumes/nginx_nginx-etc/_data/nginx.conf
user nginx;
worker_processes 4;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
# bboysoul web upstream
proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=bboysoul-tmp:100m inactive=7d max_size=1000g;
upstream bboysoul-web {
server 10.10.10.112:8080;
}
server {
listen 80;
server_name www.bboysoul.com;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://bboysoul-web/;
proxy_cache bboysoul-tmp;
proxy_cache_valid 200 206 304 301 302 10d;
proxy_cache_key $uri;
}
}
include /etc/nginx/conf.d/*.conf;
}
复制代码
以后重启容器
docker-compose restart
最后咱们就要安装一个后端应用服务来作测试
一样的,上面咱们再nginx上配置了后端应用服务的地址是10.10.10.112:8080
因此咱们要在10.10.10.112:8080上启动一个服务,为了方便咱们直接使用docker来起一个服务
在10.10.10.112上
cd awesome-dockercompose/visualizer
docker-compose up -d
以后咱们访问这个服务,直接在浏览器上输入www.bboysoul.com
若是看到visualizer的界面就表示成功了
接着咱们来到10.10.10.2和10.10.10.3这两台机器上
首先执行
systemctl enable keepalived
让keepalived能够开机启动
而且都执行下面的命令来看nginx的日志
docker logs -f nginx
不出意外的话10.10.10.2上nginx是会有日志的,也就是说咱们访问的是10.10.10.2上的nginx
以后咱们模拟10.10.10.2宕机,直接在10.10.10.2上执行reboot
接着快速切换到浏览器上访问www.bboysoul.com若是不出意外的话是能够访问的而且咱们访问的是10.10.10.3上的nginx,当10.10.10.2重启完成以后你会发现流量又回到了10.10.10.2上去
欢迎关注Bboysoul的博客www.bboysoul.com
Have Fun