搭建Keepalived+LNMP架构web动态博客 实现高可用与负载均衡

环境准备:

192.168.193.80  node1

192.168.193.81 node2

关闭防火墙

[root@node1 ~]# systemctl stop firewalld        #两台都关闭
[root@node1 ~]# setenforce 0
setenforce: SELinux is disabled

host文件

[root@node1 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.193.80 node1
192.168.193.81 node2

keepalived为主备模式

master为node1节点

backup为node2节点

 

yum源

[epel]
name=epel
enabled=1
gpgcheck=0
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
[centos]
name=centos base
enabled=1
gpgcheck=0
baseurl=http://mirrors.163.com/centos/7/os/x86_64/

 

master节点配置

下载keeplived

[root@node1 ~]# yum -y install keepalived
[root@node1 ~]# vim /etc/keepalived/keepalived.conf
5,7 s/^/#/g    #5-7行加注释
9,16 s/^/#/g    #9-16
36,157 s/^/#/g  #注释掉

 

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100            #优先级
    advert_int 1         #一秒检查一次  一秒给baskup发送一个报文
    authentication {
        auth_type PASS      #这个是密码认证,和51相辅相成,证明在一个网段里
        auth_pass 1111      #密码
    }
    virtual_ipaddress {
        192.168.193.252      #这个就vip   可以有一个也可以有多个
    }
}
[root@node1 ~]# scp /etc/keepalived/keepalived.conf 192.168.193.81:/etc/keepalived/keepalived.conf  #把配置文件传到node2节点

配置backup端

[root@node2 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.193.252
    }
}
[root@node1 ~]# systemctl start keepalived          #启动
[root@node2 ~]# systemctl start keepalived
[root@node1 ~]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:84:7a:50 brd ff:ff:ff:ff:ff:ff
    inet 192.168.193.80/24 brd 192.168.193.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.193.252/32 scope global ens33       #这个就是虚拟IP
       valid_lft forever preferred_lft forever
    inet6 fe80::9a7d:fb7d:d10f:e5e8/64 scope link 
       valid_lft forever preferred_lft forever
[root@node2 ~]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:5b:98:47 brd ff:ff:ff:ff:ff:ff
    inet 192.168.193.81/24 brd 192.168.193.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::80ce:27dd:68f9:5adc/64 scope link 
       valid_lft forever preferred_lft forever

实验停止node1节点IP是否发生漂移

[root@node1 ~]# systemctl stop  keepalived
[root@node2 ~]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:5b:98:47 brd ff:ff:ff:ff:ff:ff
    inet 192.168.193.81/24 brd 192.168.193.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.193.252/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::80ce:27dd:68f9:5adc/64 scope link 
       valid_lft forever preferred_lft forever

发生漂移,这样就成功了,利用了VRRP协议

配置LVS

node1配置

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.193.252
    }
}

virtual_server 192.168.193.252 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    protocol TCP

    real_server 192.168.193.80 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.193.81 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

node2配置

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

virtual_server 192.168.193.252 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    protocol TCP

    real_server 192.168.193.80 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.193.81 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@node1 ~]# yum -y install 
[root@node1 ~]# bash  1.sh  start
启动成功
[root@node2 ~]# bash  1.sh  start
启动成功

 

#!/bin/bash
case $1 in
start)
        echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
        ifconfig ens33:0 192.168.193.252/32 broadcast 192.168.193.252 up
        if [ $? -eq 0 ];then
                route add -host 192.168.193.252 dev ens33:0
        fi
        echo "启动成功"
;;
stop)
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
        ifconfig ens33:0 down
        route del -host 192.168.193.252
        echo "删除成功"
;;
*)
        echo "usage start|stop"
;;
esac
[root@node1 ~]# systemctl restart   keepalived   
[root@node2 ~]# systemctl restart   keepalived   
[root@node1 ~]# yum -y install ipvsadm 

[root@node1 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.193.252:80 rr -> 192.168.193.80:80 Route 1 0 0 -> 192.168.193.81:80 Route 1 0 0

这样Keeplived就继承了LVS的功能  高可用与负载均衡

 继续搭建nginx

以下操作2台节点同样操作

[root@node1 ~]# yum install nginx php php-fpm mariadb-server  php-mysql  -y

[root@node2 ~]# yum install nginx php php-fpm mariadb-server  php-mysql  -y
 38     server {
 39         listen       80 default_server;
 40         listen       [::]:80 default_server;
 41         server_name  _;
 42         root         /var/www/html;   #修改网站访问目录
 43         index      index.php  index。html;     #修改动态静态



 47 
 48         location / {              #复制
 49         }
 50  
 51         location ~ ^/.*\.php$ {             #添加
 52         fastcgi_pass 127.0.0.1:9000; 
 53         include fastcgi.conf;
 54         }
 55         

检测配置文件是否有错

[root@node1 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node1 ~]# systemctl restart mariadb     #启动数据库
[root@node1 ~]# cd /var/www/html/   #进入网站根目录
[root@node1 html]# rz          #上传动态博客压缩包
rz waiting to receive.
 zmodem trl+C ȡ

  100%    4548 KB 4548 KB/s 00:00:01       0 Errorszip...

[root@node1 html]# ls
wordpress-3.3.1-zh_CN.zip

[root@node1 html]# unzip wordpress-3.3.1-zh_CN.zip

[root@node1 html]# mv wordpress/*  . 

[root@node1 html]# cp wp-config-sample.php wp-config.php

 

[root@node1 html]#  vim wp-config.php
 18 define('DB_NAME', 'king');
 19 
 20 /** MySQL 数据库用户名 */
 21 define('DB_USER', 'king');
 22 
 23 /** MySQL 数据库密码 */
 24 define('DB_PASSWORD', '123');
[root@node1 html]# mysql -uroot
MariaDB [(none)]> create database king;
MariaDB [(none)]> grant all on *.* to king@'localhost' identified by '123';
MariaDB [(none)]> exit
Bye
[root@node1 ~]# systemctl restart nginx        #两台同样操作
[root@node1 ~]# systemctl restart php-fpm
[root@node1 ~]# systemctl restart keepalived
[root@node1 ~]# ipvsadm -Ln                 
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.193.252:80 rr
  -> 192.168.193.80:80            Route   1      0          0         
  -> 192.168.193.81:80            Route   1      0          0      

[root@node2 ~]# ipvsadm -Ln                 
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.193.252:80 rr
  -> 192.168.193.80:80            Route   1      0          0         
  -> 192.168.193.81:80            Route   1      0          0   

现在访问虚拟IP试验

 搭建Keepalived+LNMP架构web动态博客 实现高可用与负载均衡_第1张图片

 

转载于:https://www.cnblogs.com/kingzhe/p/11156433.html

你可能感兴趣的:(php,运维,开发工具)