Keepalived+Nginx 高可用集群(主从模式)

news/2024/7/7 20:23:37

一、架构(主从模式)

https://images2018.cnblogs.com/blog/892002/201805/892002-20180529184944548-572610911.png

二、nginx安装

(1)修改yum源,源地址:http://nginx.org/en/linux_packages.html#RHEL-CentOS

(a)安装yum-utils

yum install yum-utils

(b)查询并修改源地址

vi /etc/yum.repos.d/nginx.repo

内容如下:

[nginx-stable]

name=nginx stable repo

baseurl=http://nginx.org/packages/centos/$releasever/$basearch/

gpgcheck=1

enabled=1

gpgkey=https://nginx.org/keys/nginx_signing.key

module_hotfixes=true

[nginx-mainline]

name=nginx mainline repo

baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/

gpgcheck=1

enabled=0

gpgkey=https://nginx.org/keys/nginx_signing.key

module_hotfixes=true

(c)切换为主线分支

yum-config-manager --enable nginx-mainline

(2)安装

yum install nginx

三、nginx配置

主备服务器配置一样,基本配置如下:

worker_processes  1;

events {

    worker_connections  1024;

}

http {

   upstream  test-server {

       server    192.168.232.200:8080;

       server    192.168.232.204:8080;

   }

   server {

       listen       8080;

       server_name  localhost;

       location / {

        proxy_pass http://test-server;

        proxy_redirect default;

      }

    }

}

说明:upstream的默认分配方式为轮询,还可以通过设备状态分配:

down 表示单前的server暂时不参与负载.

weight 默认为1.weight越大,负载的权重就越大。

max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误.

fail_timeout : max_fails次失败后,暂停的时间。

backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

四、nginx常用操作命令

#启动

systemctl start nginx

#停止

systemctl stop nginx

#重启

systemctl restart nginx

#重载文件

systemctl reload nginx

#查看状态

systemctl status nginx

#检查配置文件语法

nginx -t

五、keepalived安装

yum install keepalived -y

六、keepalived配置

(1)主节点

vim /etc/keepalived/keepalived.conf

global_defs {

   notification_email {

    123@qq.com

   }

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL_01

}

vrrp_instance VI_1 {

    state MASTER

    interface ens33

    virtual_router_id 51

    priority 150

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

    192.168.232.203/24

    }

}

(2)备节点

vim /etc/keepalived/keepalived.conf

global_defs {

   notification_email {

    123@qq.com

   }

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL_02  #不能与主节点一样

}

vrrp_instance VI_1 {

    state BACKUP             #备节点为BACKUP

    interface ens33

    virtual_router_id 51  #与主接点一致

    priority 99          #比主节点小

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

    192.168.232.203/24  #与主节点一致

    }

}

七、keepalived常用操作命令

#启动keepalived

systemctl start keepalived

#停止keepalived

systemctl stop keepalived

#查看IP

ip addr

八、注意事项

(1)时间同步

(a)安装ntpdate工具

yum -y install ntp ntpdate

(b)设置系统时间与网络时间同步

可以选择time.nist.gov、time.nuri.net、0.asia.pool.ntp.org、1.asia.pool.ntp.org、2.asia.pool.ntp.org、3.asia.pool.ntp.org中任意一个。

ntpdate 0.asia.pool.ntp.org

(c)将系统时间写入硬件时间

hwclock --systohc

(d)核对时间

date

(2)关闭selinux

#设置为宽容模式,临时

setenforce 0

#设置为宽容模式,永久

sed -i 's/=enforcing/=disabled/g' /etc/sysconfig/selinux 

(3)防火墙开放VRRP

#添加规则

firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0  --protocol vrrp -j ACCEPT

#重启防火墙

firewall-cmd --reload


http://www.niftyadmin.cn/n/4021773.html

相关文章

nginx只允许域名访问的配置方法(ngnix禁止IP访问的配置方法)

1.只允许域名访问的配置方法一 修改配置文件,判断是否用域名访问 server { listen 8081; server_name www.tq.com; if ( $host ! www.tq.com ){ return 403; } } 2.只允许域名访问的配置方法二 #修改配置文件,配置2个server,一个配置域名…

新闻网站项目django--一些必要的配置

settings.py: INSTALLED_APPS [django.contrib.admin,django.contrib.auth,django.contrib.contenttypes,django.contrib.sessions,django.contrib.messages,django.contrib.staticfiles,zhidaily, #记得在这里添加你的app ] TEM…

Spring获取properties中同一个key对应的多条value的方法

如下方式使用Spring EL Value("#{${my.list.of.strings}.split(,)}") private List<String> myList; 然后在.properties中这样配置 my.list.of.stringsA , B , C , D转载于:https://www.cnblogs.com/taojintianxia/p/6698923.html

linux系统查看文件大小及使用空间大小

1.查看/data/test文件夹下所有文件大小之和命令&#xff1a; du -h --max-depth1 /data/test 2.查看/data/test文件夹下所有文件大小明细&#xff08;每个文件大小&#xff09;命令&#xff1a; du -h --max-depth1 /data/test/* 3.查看当前服务器空间使用情况命令&#xf…

FileZilla可以连接但是传输文件失败

在linux本地创建文件夹后&#xff0c;用FileZilla传输文件失败了。 如果用的是普通用户&#xff0c;创建的文件夹是属于root用户。所以不能传输&#xff0c;没有权限。 修改权限&#xff1a;sudo chown -R lishengnan:lishengnan /opt/data01 转载于:https://www.cnblogs.com/l…

mysql复制表create table like 和 create table as select

create table table_name_b like table_name_a 将表table_name_a字段结构复制到table_name_b&#xff0c;同时将表table_name_a的索引也复制过来。 create table table_name_b as select * from table_name_a where 12 将表table_name_a字段结构复制到table_name_b&#xff…

linux 查看服务器资源(cpu型号、cpu数量、内存大小、磁盘空间)

1.查看cpu信息cat /proc/cpuinfo 查看CPU型号cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c 查看物理CPU个数cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l 查看每个物理CPU中core的个数(即核数)cat /proc/cpuinfo| grep "cpu cores&…

框架之 hibernate简单入门

hibernate框架的搭建 Hibernate框架的概述 1. Hibernate框架的概述* Hibernate称为* Hibernate是一个开放源代码的对象关系映射&#xff08;ORM&#xff09;框架&#xff0c;它对JDBC进行了非常轻量级的对象封装&#xff0c;使得Java程序员可以随心所欲的使用对象编程思维来操纵…