lvs+keepalived+nginx环境部署

简介: Redhat搭建centos镜像作为yum源2.删除原有的yum[root@localhost ~]# rpm -qa | grep yum |xargs rpm -e --nodeps[root@localhost ~]# rpm -qa | grep yum3.


Redhat搭建centos镜像作为yum


wKioL1li4eexBxqaAAAPVU4mqmM195.png


2.删除原有的yum

[root@localhost ~]# rpm -qa | grep yum |xargs rpm -e --nodeps

[root@localhost ~]# rpm -qa | grep yum

3.下载需要的安装包

python-urlgrabber-3.9.1-11.el6.noarch.rpm

yum-metadata-parser-1.1.2-16.el6.x86_64.rpm

yum-3.2.29-81.el6.centos.noarch.rpm

yum-plugin-fastestmirror-1.1.30-40.el6.noarch.rpm

yum-utils-1.1.30-40.el6.noarch.rpm

4.安装rpm包

[root@localhost soft]# rpm -ivh --force python-urlgrabber-3.9.1-11.el6.noarch.rpm

[root@localhost soft]# rpm -ivh yum-*.rpm

[root@localhost ~]# mkdir /yum

上传镜像到/yum目录下面,并且配置yum源

[root@localhost soft]# cd /etc/yum.repos.d/

[root@localhost yum.repos.d]# vi redhat.repo

[base]

name=RedHat

baseurl=file:///yum

enabled=1

gpgcheck=0

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

[root@localhost yum.repos.d]# yum clean all

[root@localhost yum.repos.d]# yum repolist

                          


             Nginx代理HA集群的配置


1.在两台机器上分别安装keepalived,做nginx代理的HA集群。


主机安装keepalived

[root@localhost soft]# tar -zxvf keepalived-1.2.7.tar.gz

[root@localhost soft]# cd keepalived-1.2.7

[root@localhost keepalived-1.2.7]# yum -y install popt-devel

[root@localhost keepalived-1.2.7]# ./configure --prefix=/usr/local/keepalived/

[root@localhost keepalived-1.2.7]# make

[root@localhost keepalived-1.2.7]# make install

[root@localhost keepalived-1.2.7]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

[root@localhost keepalived-1.2.7]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

[root@localhost keepalived-1.2.7]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

[root@localhost keepalived-1.2.7]# mkdir /etc/keepalived

[root@localhost keepalived-1.2.7]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

[root@localhost keepalived-1.2.7]# cd /etc/keepalived/

[root@localhost keepalived-1.2.7]# cat /etc/keepalived/keepalived.conf

! 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_instance VI_1 {

    state MASTER                (主的身份)

    interface eth0                (主的网卡接口)

    virtual_router_id 51

    mcast_src_ip 192.168.0.111        (主的IP地址)

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.0.114

    }

}

备机安装keepalived

安装过程一致如上,配置文件如下

[root@localhost keepalived]# cat /etc/keepalived/keepalived.conf

! 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_instance VI_1 {

    state SLAVER

    interface eth0

    virtual_router_id 51

    mcast_src_ip 192.168.0.112

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.0.114

    }

}

2.两台机器上启动keepalived服务

[root@localhost ~]# service  keepalived restart

3.在主机上查看是否有虚拟IP地址

wKiom1lJ4Ajj50qqAAAtkMRTPnQ912.png-wh_50 

4.监控和主备切换

编写脚本

[root@localhost ~]# cat nginx_check.sh

#!/bin/bash

a=`ps -C nginx --no-header | wc -l`

if [ $a -eq 0 ];then

    /usr/local/nginx/sbin/nginx &> /dev/null

    sleep 3

    if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then

        service keepalived stop &> /dev/null

    fi

fi

[root@localhost ~]# chmod +x /root/nginx_check.sh

脚本写好后,现在到keepalived的配置文件中调用脚本

[root@localhost ~]# cat /etc/keepalived/keepalived.conf

! 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_script check_nginx_status {

    script "/root/nginx_check.sh"

    interval 2

    }

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    mcast_src_ip 192.168.0.111

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    track_script {

        check_nginx_status

    }

    virtual_ipaddress {

        192.168.0.114

    }

}

 

 

Lvs负载均衡

1.在主机器上安装ipvsadm

[root@bogon ~]# yum -y install ipvsadm

2.使用keepalived来管理lvs

[root@bogon ~]# cat  /etc/keepalived/keepalived.conf

! 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_script check_nginx_status {

    script "/root/nginx_check.sh"

    interval 2

    }

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

     track_script {

        check_nginx_status

    }

    virtual_ipaddress {

        192.168.0.114

    }

}

virtual_server 192.168.0.114 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

    real_server 192.168.0.111 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

    }

}

    real_server 192.168.0.112 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

}

3.编写lvs管理脚本

[root@bogon ~]# cat lvs_keepd.sh

#!/bin/sh

#description:start realserver

vip=192.168.0.114

source /etc/rc.d/init.d/functions

case "$1" in

 

start)

    echo "start server"

    /sbin/ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up

    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce

    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore

    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

;;

stop)

    echo "stop server"

    /sbin/ifconfig lo:0 down

    echo "0" > /proc/sys/net/ipv4/conf/lo/arp_ignore

    echo "0" > /proc/sys/net/ipv4/conf/lo/arp_announce

    echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore

    echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce

;;

*)

    echo "Usage: $0 (start | stop)"

  exit 1

esac

exit 0

4.启动脚本

[root@bogon ~]# bash /root/lvs_keepd.sh start

 

5.测试lvs是否启动

[root@bogon ~]# 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.0.114:80 rr persistent 50

  -> 192.168.0.111:80             Local   1      1          0         

  -> 192.168.0.112:80             Route   1      0          0         

备机上操作也是一样



目录
相关文章
|
2月前
|
弹性计算 运维 监控
快速部署 Nginx 社区版
Nginx是一个高性能的HTTP和反向代理服务器。Nginx在计算巢上提供了社区版服务,您无需自行配置云主机,即可在计算巢上快速部署Nginx服务、实现运维监控,从而方便地基于Nginx搭建您自己的应用。本文介绍使用如何通过计算巢快速部署Nginx社区版。
快速部署 Nginx 社区版
|
27天前
|
Ubuntu 应用服务中间件 Linux
Linux下搭建Nginx环境的搭建
Linux下搭建Nginx环境的搭建
|
2月前
|
运维 负载均衡 网络协议
LVS+Keepalived 负载均衡
LVS+Keepalived 负载均衡
73 8
LVS+Keepalived 负载均衡
|
1月前
|
应用服务中间件 Shell PHP
windows系统配置nginx环境运行pbootcms访问首页直接404的问题
windows系统配置nginx环境运行pbootcms访问首页直接404的问题
|
1月前
|
前端开发 JavaScript 应用服务中间件
使用nginx部署网站
使用nginx部署网站
|
1月前
|
JavaScript 应用服务中间件 nginx
nginx部署vue项目
本文介绍了将Vue项目部署到Nginx的步骤,包括构建Vue项目、上传dist文件夹到服务器、安装Nginx、配置Nginx代理静态文件以及重启Nginx,确保了Vue应用可以通过域名或IP地址访问。
108 1
|
1月前
|
前端开发 JavaScript 应用服务中间件
linux安装nginx和前端部署vue项目(实际测试react项目也可以)
本文是一篇详细的教程,介绍了如何在Linux系统上安装和配置nginx,以及如何将打包好的前端项目(如Vue或React)上传和部署到服务器上,包括了常见的错误处理方法。
310 0
linux安装nginx和前端部署vue项目(实际测试react项目也可以)
|
1月前
|
Kubernetes 应用服务中间件 nginx
k8s基础使用--使用k8s部署nginx服务
本文介绍了Kubernetes中核心概念Deployment、Pod与Service的基本原理及应用。Pod作为最小调度单元,用于管理容器及其共享资源;Deployment则负责控制Pod副本数量,确保其符合预期状态;Service通过标签选择器实现Pod服务的负载均衡与暴露。此外,还提供了具体操作步骤,如通过`kubectl`命令创建Deployment和Service,以及如何验证其功能。实验环境包括一台master节点和两台worker节点,均已部署k8s-1.27。
178 1
|
2月前
|
域名解析 运维 负载均衡
LVS+Keepalived 负载均衡(二)28-1
【8月更文挑战第28天】LVS+Keepalived 负载均衡 配置 LVS VIP
63 5
|
2月前
|
应用服务中间件 nginx Docker
docker应用部署---nginx部署的配置
这篇文章介绍了如何使用Docker部署Nginx服务器,包括搜索和拉取Nginx镜像、创建容器并设置端口映射和目录映射,以及如何创建一个测试页面并使用外部机器访问Nginx服务器。