linux集群-高可用集群
Ⅰ:安装keepalived
-
两节点配置yum源
cd /etc/yum.repos.d rm -f C* cat local.repo [gpmall] name=gpmall baseurl=file:///opt/gpmall-repo gpgcheck=0 enabled=1 yum clean all yum repolist
-
安装
yum install -y keepalived
-
配置文件
cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { #全局配置标识,表明这个区域{}是全局配置 notification_email { 131917381@qq.com #表示发送通知邮件时邮件源地址是谁 } notification_email_from root@aaaaa.com #表示keepalived在发生诸如切换操作时需要发送email通知,以及email发送给哪些邮件地址,邮件地址可以多个,每行一个 smtp_server 127.0.0.1 #表示发送email时使用的smtp服务器地址,这里可以用本地的sendmail来实现 smtp_connect_timeout 30 #连接smtp连接超时时间 router_id LVS_DEVEL #机器标识 } vrrp_script chk_nginx { script "/usr/local/sbin/check_ng.sh" #检查服务是否正常,通过写脚本实现,脚本检查服务健康状态 interval 3 #检查时间间断是3秒 } vrrp_instance VI_1 { #VRRP配置标识 VI_1是实例名称 state MASTER #定义master相关 interface ens33 #通过vrrp协议去通信、去发广播。此为网卡名 virtual_router_id 51 #定义路由器ID ,配置的时候和从机器一致 priority 100 #权重,主角色和从角色的权重是不同的,一般主比从大 advert_int 1 #设定MASTER与BACKUP主机质检同步检查的时间间隔,单位为秒 authentication { #认证相关信息 auth_type PASS #认证类i auth_pass 5201314>g #密码的形式是一个字符串 } virtual_ipaddress { #设置虚拟IP地址 (VIP),又叫做漂移IP地址 192.168.200.100 } track_script { #加载脚本 chk_nginx } }
-
编辑脚本并给予权限
cat /usr/local/sbin/check_ng.sh #!/bin/bash #时间变量,用于记录日志 d=`date --date today +%Y%m%d_%H:%M:%S` #计算nginx进程数量 n=`ps -C nginx --no-heading|wc -l` #如果进程为0,则启动nginx,并且再次检测nginx进程数量 if [ $n -eq "0" ]; then /etc/init.d/nginx start n2=`ps -C nginx --no-heading|wc -l` #如果还为0,说明nginx无法启动,此时需要关闭keepalived if [ $n2 -eq "0" ]; then echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log systemctl stop keepalived fi fi chmod a+x /usr/local/sbin/check_ng.sh
-
Backup
yum install -y epel-release yum install -y nginx systemctl start keepalived
-
验证
curl -I 192.168.200.146 HTTP/1.1 200 OK Server: nginx/1.20.1 Date: Mon, 19 Sep 2022 07:34:29 GMT Content-Type: text/html Content-Length: 4833 Last-Modified: Fri, 16 May 2014 15:12:48 GMT Connection: keep-alive ETag: "53762af0-12e1" Accept-Ranges: bytes curl -I 192.168.200.145 HTTP/1.1 200 OK Server: nginx/1.20.1 Date: Mon, 19 Sep 2022 07:34:53 GMT Content-Type: text/html Content-Length: 4833 Last-Modified: Fri, 16 May 2014 15:12:48 GMT Connection: keep-alive ETag: "53762af0-12e1" Accept-Ranges: bytes curl -I 192.168.200.100 HTTP/1.1 200 OK Server: nginx/1.20.1 Date: Mon, 19 Sep 2022 07:35:04 GMT Content-Type: text/html Content-Length: 4833 Last-Modified: Fri, 16 May 2014 15:12:48 GMT Connection: keep-alive ETag: "53762af0-12e1" Accept-Ranges: bytes
-
测试脚本
systemctl stop nginx netstat -nltp |grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17655/nginx: master tcp6 0 0 :::80 :::* LISTEN 17655/nginx: master
-
给与权限,开启keepalived,查看ip
chmod a+x /usr/local/sbin/check_ng.sh [root@master ~]# systemctl start keepalived ip add 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:c4:90:9b brd ff:ff:ff:ff:ff:ff inet 192.168.200.146/24 brd 192.168.200.255 scope global noprefixroute dynamic ens33 valid_lft 1652sec preferred_lft 1652sec inet 192.168.200.100/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::14f3:d1b4:207a:b91/64 scope link noprefixroute valid_lft forever preferred_lft forever ps -aux |grep nginx root 2071 0.0 0.0 39304 932 ? Ss 15:20 0:00 nginx: master process /usr/sbin/nginx nginx 2072 0.0 0.1 39692 1892 ? S 15:20 0:00 nginx: worker process nginx 2073 0.0 0.1 39692 1892 ? S 15:20 0:00 nginx: worker process root 2082 0.0 0.0 112720 984 pts/0 S+ 15:20 0:00 grep --color=auto nginx
-
-
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)