A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 微小笑 于 2017-9-16 15:02 编辑

一、环境准备
五台Linux服务器
Linux:CentOS
我这里使用五台虚拟机
192.168.142.131(安装tomcat)
192.168.142.136(安装nginx和keepalived)
192.168.142.137(安装nginx和keepalived)
192.168.142.138(安装tomcat)
192.168.142.139(安装tomcat)
Tomcat:apache-tomcat-7.0.47.tar.gz
Nginx:nginx-1.8.1.tar.gz
Keepalived:keepalived-1.2.19.tar.gz
二、安装Tomcat
2.1 下载Tomcat
http://tomcat.apache.org/
2.2 上传并解压Tomcat
mkdir /usr/local/java
tar -zxf apache-tomcat-7.0.47.tar.gz -C /usr/local/java
2.3 简单配置一下
cd /usr/local/java/apache-tomcat-7.0.47/webapps/ROOT
mv index.jsp _index.jsp
touch index.html
vim index.html
加入以下内容:
[HTML] 纯文本查看 复制代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
        <h1>192.168.142.131</h1>
</body>
</html>

分别在192.168.142.131、192.168.142.136、192.168.142.139上安装Tomcat
2.4 启动Tomcat
分别在192.168.142.131、192.168.142.136、192.168.142.139虚拟机上启动Tomcat
/usr/local/java/apache-tomcat-7.0.47/bin/startup.sh


三台Tomcat安装成功,没有问题。
三、安装Nginx
3.1 下载nginx
官网:http://nginx.org/
3.2 上传并解压nginx
tar -zxf nginx-1.8.1.tar.gz -C /usr/local/src
3.3 编译nginx
#进入到nginx的源码目录
cd /usr/local/src/nginx-1.8.1
#检查安装环境,并指定将来要安装的路径
./configure --prefix=/usr/local/nginx
#缺包报错 ./configure:error:C compiler cc is not found
#使用yum安装缺少的包
yum -y install gcc pcre-devel openssl openssl-devel
#重新执行configure
./configure --prefix=/usr/local/nginx#编译安装
make && make intall
3.4 修改nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
内容如下:
[Shell] 纯文本查看 复制代码
worker_processes  1;

events {
    worker_connections  1024;
}


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

    sendfile        on;

    keepalive_timeout  65;

        upstream tomcat_servers {
                server 192.168.142.131:8080;
                server 192.168.142.136:8080;
                server 192.168.142.139:8080;
        }

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass   http://tomcat_servers;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

3.5 启动nginx
/usr/local/nginx/sbin/nginx
分别在192.168.142.137、和192.168.142.138上安装nginx,并做以上配置,最后启动nginx
四、利用keepalived实现高可用
4.1 高可用概念
HA(High Available), 高可用性集群,是保证业务连续性的有效解决方案,一般有两个或两个以上的节点,且分为活动节点及备用节点。
4.2 高可靠软件keepalived
keepalive是一款可以实现高可靠的软件,通常部署在2台服务器上,分为一主一备。Keepalived可以对本机上的进程进行检测,一旦Master检测出某个进程出现问题,将自己切换成Backup状态,然后通知另外一个节点切换成Master状态。
4.3 下载keepalived
http://www.keepalived.org/
4.4 安装keepalived
#将keepalived解压到/usr/local/src目录下
tar -zxvf  keepalived-1.2.19.tar.gz -C /usr/local/src
#进入到/usr/local/src/keepalived-1.2.19目录
cd /usr/local/src/keepalived-1.2.19
#开始configure
./configure --prefix=/usr/local/keepalived
#编译并安装
make && make install
4.5 将keepalived添加到系统服务中
#拷贝执行文件
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#将init.d文件拷贝到etc下,加入开机启动项
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
#将keepalived文件拷贝到etc下
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#创建keepalived文件夹
mkdir -p /etc/keepalived
#将keepalived配置文件拷贝到etc下
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
#添加可执行权限
chmod +x /etc/init.d/keepalived

##以上所有命令一次性执行:

[Shell] 纯文本查看 复制代码
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 
mkdir -p /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
chmod +x /etc/init.d/keepalived
chkconfig --add keepalived        
chkconfig keepalived on

4.6 配置keepalived虚拟IP
修改配置文件: /etc/keepalived/keepalived.conf
#MASTER节点
[Shell] 纯文本查看 复制代码
global_defs {
}
vrrp_instance VI_1 {
    state MASTER   #指定A节点为主节点 备用节点上设置为BACKUP即可
    interface eth0    #绑定虚拟IP的网络接口
    virtual_router_id 51   #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
    priority 100   #主节点的优先级(1-254之间),备用节点必须比主节点优先级低
    advert_int 1  #组播信息发送间隔,两个节点设置必须一样
    authentication {    #设置验证信息,两个节点必须一致
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {    #指定虚拟IP, 两个节点设置必须一样
        192.168.142.200/24    #配置虚拟ip
    }
}

#BACKUP节点
[Shell] 纯文本查看 复制代码
global_defs {
}
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 {
        192.168.142.200/24
    }
}

#分别启动两台机器上的keepalived
service keepalived start
测试:
杀掉master上的keepalived进程,你会发现,在slave机器上的eth0网卡多了一个ip地址
查看ip地址的命令:  ip addr


此时keepalived为master的节点的服务器多了一个ip地址
4.7 配置keepalived心跳检查
原理:
Keepalived并不跟nginx耦合,但是keepalived提供一个机制:让用户自定义一个shell脚本去检测用户自己的程序,返回状态给keepalived就可以了。
#MASTER节点
[Shell] 纯文本查看 复制代码
global_defs {
}

vrrp_script chk_health {
    script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"
    interval 1    #每隔1秒执行上述的脚本,去检查用户的程序ngnix
    weight -2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 1
    priority 100
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    track_script {
        chk_health
    }

    virtual_ipaddress {
        192.168.142.200/24
    }

    notify_master "/usr/local/keepalived/sbin/notify.sh master"
    notify_backup "/usr/local/keepalived/sbin/notify.sh backup"
    notify_fault "/usr/local/keepalived/sbin/notify.sh fault"
}

#BACKUP节点
[Shell] 纯文本查看 复制代码
global_defs {
   
}

vrrp_script chk_health {
    script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"
    interval 1
    weight -2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

        track_script {
        chk_health
    }

    virtual_ipaddress {
        192.168.142.200/24
    }

        notify_master "/usr/local/keepalived/sbin/notify.sh master"
    notify_backup "/usr/local/keepalived/sbin/notify.sh backup"
    notify_fault "/usr/local/keepalived/sbin/notify.sh fault"
}

#添加切换通知脚本
vim /usr/local/keepalived/sbin/notify.sh
[Shell] 纯文本查看 复制代码
#!/bin/bash

case "$1" in
    master)
        /usr/local/nginx/sbin/nginx
        exit 0
    ;;
backup)
        /usr/local/nginx/sbin/nginx -s stop
        /usr/local/nginx/sbin/nginx
        exit 0
    ;;
    fault)
        /usr/local/nginx/sbin/nginx -s stop
        exit 0
    ;;
    *)
        echo 'Usage: notify.sh {master|backup|fault}'
        exit 1
    ;;
esac

#添加执行权限
chmod +x /usr/local/keepalived/sbin/notify.sh
#重启keepalived
service keepalived restart
分别在192.168.142.137、192.168.142.138上安装keepalived,并做以上配置,最后启动
我是将192.168.142.138作为了MASTER,将192.168.142.137作为了BACKUP
五 测试
通过浏览器访问http://192.168.142.200
不停的刷新网页,网页的内容在不断的改变
在MASTER的服务器上(192.168.142.138)停止nginx或者keepalived
/usr/local/nginx/sbin/nginx -s stop

service keepalived stop
停止以后通过浏览器访问http://192.168.142.200仍然可以访问到
在BACKUP服务器上(192.168.142.137)停止nginx或者keepalived
/usr/local/nginx/sbin/nginx -s stop

service keepalived stop

停止以后通过浏览器访问http://192.168.142.200仍然可以访问到
这样就保证了网站的高可用

nginx-1.8.1.tar.gz

813.94 KB, 下载次数: 25

keepalived-1.2.19.tar.gz

322.43 KB, 下载次数: 28

2 个回复

倒序浏览
自己能给自己回复吗?
回复 使用道具 举报
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马