拓扑图:

image.png



nginx+keepalived   高可用

主:192.168.80.181

从:192.168.80.182


在两台虚拟机上都做的操作:


image.png

关闭firewalld(systemctl stop firewalld.service,systemctl disable firewalld.service),设置selinux为permissive(setenforce 0 或 vim /etc/selinux/config);同时确保DR1和DR2节点的网卡支持MULTICAST(多播)通信。通过命令ifconfig可以查看到是否开启了MULTICAST:

image.png


释放网络yum源 挂载光盘 NAT模式联网

yum install epel-release -y  //安装一个epel源 

yum install keepalived  nginx -y  //安装keepalived服务

vi /etc/keepalived/keepalived.conf


   //修改后的配置文件

! Configuration File for keepalived

global_defs {
   route_id NGINX-01
}

vrrp_script nginx {
        script "/opt/nginx.sh"
        interval 2
        weight -10
}


vrrp_instance VI_1 {
    state MASTER
    interface ens32
    virtual_router_id 51
! Configuration File for keepalived
! Configuration File for keepalived

global_defs {
   route_id NGINX-01
}

vrrp_script nginx {
        script "/opt/nginx.sh"        
        interval 2
        weight -10
}


vrrp_instance VI_1 {
    state MASTER
    interface ens32
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
}
 track_script {
 nginx
 }

 virtual_ipaddress {
        192.168.80.188
}


image.png


 scp /etc/keepalived/keepalived.conf root@192.168.80.182:/etc/keepalived/keepalived.conf   //发送到从服务器


image.png



image.png

编写脚本主脚本 :

 vi /opt/nginx.sh

 //脚本内容

#!/bin/bash
A=$(ps -ef | grep keepalived | grep -v grep | wc -l )
if [ $A -gt 0 ];then
        systemctl start nginx
else
        systemctl stop nginx
fi

chmod +x /opt/nginx.sh

image.png


image.png

systemctl start keepalived

netstat -anpt | grep nginx

image.png


编写 从 脚本:

#!/bin/bash
A=$(ip addr | grep 192.168.80.188/32 | grep -v grep | wc-l)
if [ $A -gt 0];then
        systemctl start nginx
else
        systemctl stop nginx
fi


因为主服务器keepalived开启的原因,从的并无法开启,可以cat /var/log/messages查看



vi /etc/nginx/nginx.conf

image.png

image.png

scp /etc/nginx/nginx.conf 192.168.80.182:/etc/nginx/nginx.conf


image.png



.

varnish服务器 两台 192.168.80.183  192.168.80.184

yum   install epel-release -y


yum install varnish -y


vi /etc/varnish/varnish.params


image.png


vi /etc/varnish/default.vcl

backend web1 {
    .host = "192.168.80.185";
    .port = "80";
}
backend web2 {
    .host = "192.168.80.186";
    .port = "80";
}

sub vcl_recv {
if ( req.url ~ "(?i)\.php$") {
    set  req.backend_hint = web1;
} else {
    set  req.backend_hint = web2;
}

image.png

image.png



varnishd -C -f /etc/varnish/default.vcl  > /dev/null    //检测语法有没有问题  无输出说明没有问题

systemctl start varnish

netstat -anpt | grep varnish

image.png


两台操作一样。




LAMP 一台提供静态网页一台提供动态网页 192.168.80.185  192.168.80.186   (都是仅主机模式)


192.168.80.185  提供动态网页

systemctl stop firewalld

setenforce 0

 yum install -y httpd mariadb-server mariadb php php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mhash


vi /etc/httpd/conf/httpd.conf

image.png

image.png

echo "<h1>192.168.80.181</h1>" > /var/www/html/index.php


192.168.80.186  提供静态网页

vi /etc/httpd/conf/httpd.conf

image.png

echo "<h1>192.168.80.182</h1>" > /var/www/html/index.html


image.png

现在访问192.168.80.188  vip

image.png