赞
踩
目录
1、使用keepalived做nginx和haproxy高可用。
keepalived的安装可通过包安装或编译安装
例:编译安装
#部署编译环境
root@ubuntu2004:~# apt update && apt -y install make gcc ipvsadm build-essential pkg-config automake autoconf libipset-dev libnl-3-dev libnl-genl-3-dev libssl-dev libxtables-dev libip4tc-dev libip6tc-dev libmagic-dev libsnmp-dev libglib2.0-dev libpcre2-dev libnftnl-dev libmnl-dev libsystemd-dev
#离线或在线下载tar文件并解压缩
root@ubuntu2004:~# wget https://keepalived.org/software/keepalived-2.0.20.tar.gzroot@ubuntu2004:~# tar xvf keepalived-2.0.20.tar.gz -C /usr/local/srcroot@ubuntu2004:~# cd /usr/local/src/keepalived-2.0.20/#编译root@ubuntu2004:~# ./configure --prefix=/usr/local/keepalivedroot@ubuntu2004:~# make && make install#查看版本root@ubuntu2004:~# /usr/local/keepalived/sbin/keepalived -v
Keepalived v2.0.20 (01/22,2020)
Copyright(C) 2001-2020 Alexandre Cassen, <acassen@gmail.com>
Built with kernel headers for Linux 5.4.255
Running on Linux 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023configure options: --prefix=/usr/local/keepalived
Config options: NFTABLES LVS VRRP VRRP_AUTH OLD_CHKSUM_COMPAT FIB_ROUTING
#服务配置文件#下默认源码目录会自动生成unit文件root@ubuntu2004:~# cp ./keepalived/keepalived.service/lib/systemd/system/root@ubuntu2004:~# cat /usr/lib/systemd/system/keepalived.service[Unit]Description=LVS and VRRP High Availability MonitorAfter=network-online.target syslog.targetWants=network-online.target[Service]Type=forkingPIDFile=/run/keepalived.pidKillMode=processEnvironmentFile=-/usr/local/keepalived/etc/sysconfig/keepalivedExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONSExecReload=/bin/kill -HUP $MAINPID[Install]WantedBy=multi-user.target#配置文件root@ubuntu2004:~# mkdir /etc/keepalivedroot@ubuntu2004:~# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf#启动服务root@ubuntu2004:~# systemctl enable --now keepalived.service
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /lib/systemd/system/keepalived.service.
#Nginx配置文件root@ubuntu2004:~# vim /etc/nginx/conf.d/www.huang.org.confupstream webservers {
server 10.0.0.202:80;
server 10.0.0.203:80;
}server {
listen 10.0.0.199:80;
server_name www.huang.org;
location / {
proxy_pass http://webservers;
}
}#两个节点都需要配置
root@ubuntu2004:~# scp /etc/nginx/conf.d/www.huang.org.conf 10.0.0.201:/etc/nginx/conf.d/www.huang.org.conf
#Keepalived 配置文件
root@ubuntu2004:~# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {
router_id ka1.huang.org #另一个节点为ka2.huang.org
vrrp_mcast_group 230.0.0.0
}vrrp_script check_down {
fall 3
rise 2
timeout 2
}vrrp_script check_nginx{
script "/usr/bin/killall -0 nginx"
interval 1
weight -30
fall 3
rise 2
timeout 2
}
include /etc/keepalived/conf.d/*.confroot@ubuntu2004:~# cat /etc/keepalived/conf.d/www.huang.org.conf
vrrp_instance VI_1 {
state MASTER #另一个节点为backup
interface eth1
virtual_router_id 51
priority 100 #另一个节点为80
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.0.0.199/24 dev eth0 label eth0:1
}
track_interface {
eth0
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup
notify_fault "/etc/keepalived/notify.sh fault
track_script {
check_nginx
}
}root@ubuntu2004:~# systemctl restart keepalived.service
#haproxy配置
[root@ka1 ~]#cat /etc/haproxy/haproxy.cfglisten web_httpbind 10.0.0.199:80server web1 10.0.0.202:80 checkserver web2 10.0.0.203:80 checklisten statsmode httpbind 10.0.0.8:9999stats enablelog globalstats uri /haproxy-statusstats auth haadmin:123456#在两个ka1和ka2两个节点启用内核参数[root@ka1,2 ~]#vim /etc/sysctl.confnet.ipv4.ip_nonlocal_bind = 1[root@ka1,2 ~]#sysctl -p#keepalived配置root@ubuntu2004:~# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {
router_id ka1.huang.org #另一个节点为ka2.huang.org
vrrp_mcast_group 230.0.0.0
}vrrp_script check_down {
fall 3
rise 2
timeout 2
}vrrp_script check_haproxy{
script "/usr/bin/killall -0 haproxy"
interval 1
weight -30
fall 3
rise 2
timeout 2
}
include /etc/keepalived/conf.d/*.confroot@ubuntu2004:~# cat /etc/keepalived/conf.d/www.huang.org.conf
vrrp_instance VI_1 {
state MASTER #另一个节点为backup
interface eth1
virtual_router_id 51
priority 100 #另一个节点为80
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.0.0.199/24 dev eth0 label eth0:1
}
track_interface {
eth0
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup
notify_fault "/etc/keepalived/notify.sh fault
track_script {
check_haproxy
}
}root@ubuntu2004:~# systemctl restart keepalived.service
准备6台主机
主: 10.0.0.100 10.0.0.200 10.0.0.201
从: 10.0.0.201 10.0.0.202 10.0.0.203
例:编译安装
#编译环境
[root@ubuntu2004 ~] #apt update & apt -y install make gcc libjemalloc-dev libsystemd-dev#下载源码[root@ubuntu2004 ~] #wget http://download.redis.io/releases/redis-6.2.4.tar.gz#解压安装[root@ubuntu2004 ~]tar xvf redis-6.2.4.tar.gz#编译安装[root@ubuntu2004 ~]#cd redis-6.2.4/[root@ubuntu2004redis-6.2.4]#make -j 2 PREFIX=/apps/redis install #指定redis安装目录#如果支持systemd,需要执行下面[root@ubuntu2004 redis-6.2.4]#make -j 2 USE_SYSTEMD=yes PREFIX=/apps/redisinstall#配置环境变量[root@ubuntu2004 ~]#echo 'PATH=/apps/redis/bin:$PATH' > /etc/profile.d/redis.sh[root@ubuntu2004 ~]#. /etc/profile.d/redis.sh#准备相关目录和配置文件[root@ubuntu2004 ~]#mkdir /apps/redis/{etc,log,data,run} #创建配置文件、日志、数据等目录[root@ubuntu2004 ~]#cp redis.conf /apps/redis/etc/
每个节点都要修改redis配置
[root@redis-node1 ~]vim /etc/redis.confbind 0.0.0.0masterauth 123456 #建议配置,否则后期的master和slave主从复制无法成功,还需再配置requirepass 123456cluster-enabled yes #取消此行注释,必须开启集群,开启后 redis 进程会有cluster标识cluster-config-file nodes-6379.conf #取消此行注释,此为集群状态数据文件,记录主从关系及slot范围信息,由redis cluster 集群自动创建和维护cluster-require-full-coverage no #默认值为yes,设为no可以防止一个节点不可用导致整个cluster不可用#或者执行下面命令,批量修改[root@redis-node1 ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e'/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-filenodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-requirefull-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf#如果是编译安装可以执行下面操作[root@redis-node1 ~]#sed -i.bak -e '/masterauth/a masterauth 123456' -e'/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-filenodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-requirefull-coverage yes/a cluster-require-full-coverage no'/apps/redis/etc/redis.conf
#命令redis-cli的选项 --cluster-replicas 1 表示每个master对应一个slave节点,注意:所有节点数据必须清空[root@redis-node1 ~]#redis-cli -a 123456 --cluster create 10.0.0.100:6379 10.0.0.200:6379 10.0.0.201:6379 10.0.0.202:6379 10.0.0.203:6379 10.0.0.204:6379 --cluster-replicas 1# 观察以上结果,可以看到 3 组 master/slavemaster:10.0.0.100---slave:10.0.0.202master:10.0.0.200---slave:10.0.0.203master:10.0.0.201---slave:10.0.0.204# 如果节点少于 3 个会出下面提示错误[root@node1 ~]#redis-cli -a 123456 --cluster create 10.0.0.100:637910.0.0.200:6379Warning: Using a password with '-a' or '-u' option on the command line interfacemay not be safe.*** ERROR: Invalid configuration for cluster creation.*** Redis Cluster requires at least 3 master nodes.# 使用选项 -c 以集群模式连接[root@centos8 ~]#redis-cli -c -h 10.0.0.100 -a 123456 --no-auth-warning
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。