这节我们来做个关于高可用的小实验,什么是高可用同学们可以自行去网上搜索相关文章进行学习。

实验规划:

两台虚拟机,分别是192.168.26.127(centos7 master),192.168.56.128(centos7 backup)

我们分别用简称来代替上面的两台机器

c7m-->centos7 master

c7b-->centos7 backup

 

c7m 应用规划:

nginx(端口号8601),spingboot web application x 2(端口号分别是8701,8702)

其中nginx 负责负载均衡

其配置文件内容如下:

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;


    upstream upstream_name{
        server 192.168.56.127:8701;
        server 192.168.56.127:8702;
    }

    server {
        listen       8601;
        server_name  localhost;
        location / {
            proxy_pass http://upstream_name;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}

c7b 应用规划:

nginx(端口号8601),spingboot web application x 2(端口号分别是8801,8802)

其中nginx 负责负载均衡

其配置文件内容如下:

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    upstream upstream_name{
        server 192.168.56.128:8801;
        server 192.168.56.128:8802;
    }

    server {
        listen       8601;
        server_name  localhost;

        location / {
            proxy_pass http://upstream_name;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
   
    }

}

spingboot web application 的内容如下:

package com.nginxtest.it.nginxtest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class NginxtestApplication {

    @Value("${server.port:}")
    private String port;

    public static void main(String[] args) {
        SpringApplication.run(NginxtestApplication.class, args);
    }

    @GetMapping("")
    public String hello() {
        System.out.println("call me " + port);
        return "i am " + port;
    }

}

启动nginx 和两个springboot web application 命令如下:

1 nginx
2 nohup java -jar nginxtest-0.0.1-SNAPSHOT.jar --server.port=8701
3 nohup java -jar nginxtest-0.0.1-SNAPSHOT.jar --server.port=8702

 

完成上面的准备工作我们可以打开浏览器验证一下:

输入地址:http://192.168.56.128/127:8601

 

 

 出现上面的情况说明成功配置了负载均衡,我们来做高可用

 

keepalived 安装与配置

 

1,keepalived 工作原理

首先Keepalived可以在主机上产生一个虚拟的ip, 这里叫做vip(v是virtual的意思):192.168.56.150, keepalived会将这个vip绑定到交换机上.
当用户访问主机:192.168.56.127时, 交换机会通过这个ip和vip的对应找到192.168.56.127上的Nginx进行处理.
如果当有一天192.168.56.127上的Nginx挂掉的时候, Keepalived会立即在备机上生成一个相同的vip: 192.168.56.150, 当用户继续访问192.168.56.127时, 交换机上已经绑定了vip, 这时发现这个vip是存在于192.168.56.128上面的, 所以直接将请求转发到了备机上.
如果主机被修复好能够继续对外提供服务时, 这时keepalived会将主机上继续生成这个vip, 同时回收在备机上生成的vip. 这个是通过心跳检查来判断主机已恢复使用.

 

keepalived 下载地址:

https://www.keepalived.org/download.html

里面有很多版本可以根据项目需要进行选择

这里我们选择最新的版本

下载源码包:

wget https://www.keepalived.org/software/keepalived-2.1.5.tar.gz

解压缩压缩包:

tar -xvf keepalived-2.1.5.tar.gz

 

进入keepalived-2.1.5目录

执行下面的命令,提前可以安装好 gcc, openssl,openssl-devel等依赖包

./configure --prefix=/usr/local/keepalived
make && make install

执行完成后就会出现上图的安装好的keepalived,被安装在了/usr/local/keepalived下

执行以下命令让keepalived 成为systemd的应用程序,受systemctl管理,并将其二进制程序加入环境变量

#使keepalived成为systemd的程序,并受systemctl管理,
cp /usr/local/keepalived-2.1.5/keepalived/etc/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived-2.1.5/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ #加入环境变量中 cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#将配置文件放入/etc/keepalived/下,因为在启动keepalived时默认找的是这里的配置文件,不放的化会报此配置文件找不到 install -d /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

 

修改c7m keepalived 配置文件,内容如下:

! Configuration File for keepalived

global_defs {
   router_id 127master
}
vrrp_script chk_nginx {
        script "/usr/local/nginx_check.sh"
        interval 2
        weight -20
}

vrrp_instance VI_1 {
    state MASTER
    interface enp0s8
    virtual_router_id 51
    priority 200
    advert_int 1
    unicast_src_ip 192.168.56.127
    unicast_peer {
        192.168.56.128
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.56.150/24
    }
}

nginx_check.sh内容如下:

#! /bin/bash
pidof nginx
if [ $? -ne 0 ];then
/etc/init.d/keepalived stop
fi

修改c7b keepalived配置文件,内容如下:

! Configuration File for keepalived

global_defs {
   router_id 128salve
}
vrrp_script chk_nginx {
        script "/usr/local/nginx_check.sh"
        interval 2
        weight -20
}
vrrp_instance VI_1 {
    state BACKUP
    interface enp0s8
    virtual_router_id 51
    priority 100
    advert_int 1
    unicast_src_ip 192.168.56.128
    unicast_peer {
        192.168.56.127
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.56.150/24
    }
}

nginx_check.sh文件和上面的一样。

下面分别启动c7m,和c7b下面的nginx,springboot web application,keepalived,命令如下:

c7m

1 nginx
2 nohup java -jar nginxtest-0.0.1-SNAPSHOT.jar --server.port=8701
3 nohup java -jar nginxtest-0.0.1-SNAPSHOT.jar --server.port=8702
4 systemctl start keepalived

c7b

1 nginx
2 nohup java -jar nginxtest-0.0.1-SNAPSHOT.jar --server.port=8801
3 nohup java -jar nginxtest-0.0.1-SNAPSHOT.jar --server.port=8802
4 systemctl start keepalived

上述配置解释:

首先当主节点启动时,会在网卡enp0s8上产生一个vip 192.168.56.150 的虚拟地址,这是我们可以测试一下,打开浏览器输入http://192.168.56.158:8601,这是我们能访问到的通过nginx(192.168.56.127:8601)负载均衡的后端web应用8701,8702,这里有个监测脚本nginx_check.sh 如果此时将192.168.56.127上的nginx停掉,此脚本就会将c7m上的keepalived进程杀死停掉,这时c7m,enp0s8网卡上的vip就会消失(被删除掉),这时c7b上的keepalived通过心跳监测发现c7m上的keepalived已经停掉了,这时c7b自己就会启动一个跟c7m上的keepalived一样的vip(192.168.56.150),这时c7b enp0s8网卡上就会出现一个vip(192.168.56.150),这时 我们在浏览器继续访问刚才的地址(http://192.168.56.150:8601)此时会发现我们会访问到通过nginx(192.168.56.128:8601)负载均衡的web应用8801,8802,此时我们就完成了通过keepalived 实现的nginx的高可用,此时如果恢复c7m上的nginx和keepalived,这时c7m通过心跳监测重新获取主控权,并且c7b的vip 会被删除,c7m会重新生成vip(192.168.56.150). 此时继续访问的话,访问的又会恢复成8701,8702

下面我们来试试看是不是我们预期的效果:

c7m,c7b都启动的时候:

 

 停掉c7m的nginx

 

 恢复c7m的nginx和keepalived

 

 好了 我们的实验就到此完成了,写的不对的地方请在下方留言。万分感激。

 

 

参考文档:

https://www.cnblogs.com/xxoome/p/8621677.html

posted on 2020-12-30 16:39  EZgod  阅读(677)  评论(0编辑  收藏  举报