Java最新keepalived实现httpd负载均衡机高可用(2),java开发岗高频面试题

最后

无论是哪家公司,都很重视基础,大厂更加重视技术的深度和广度,面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。

针对以上面试技术点,我在这里也做一些分享,希望能更好的帮助到大家。

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

//注意:至少有一个VRRP实例调用它并且优先级不能为0.优先级范围是1-254.

vrrp_script <SCRIPT_NAME> {

}

//选项说明:

script “/path/to/somewhere” //指定要执行的脚本的路径。

interval //指定脚本执行的间隔。单位是秒。默认为1s。

timeout //指定在多少秒后,脚本被认为执行失败。

weight <-254 — 254> //调整优先级。默认为2.

rise //执行成功多少次才认为是成功。

fall //执行失败多少次才认为失败。

user [GROUPNAME] //运行脚本的用户和组。

init_fail //假设脚本初始状态是失败状态。

//weight说明:

  1. 如果脚本执行成功(退出状态码为0),weight大于0,则priority增加。

  2. 如果脚本执行失败(退出状态码为非0),weight小于0,则priority减少。

  3. 其他情况下,priority不变。

real_server段配置

weight //给服务器指定权重。默认是1

inhibit_on_failure //当服务器健康检查失败时,将其weight设置为0, \

//而不是从Virtual Server中移除

notify_up //当服务器健康检查成功时,执行的脚本

notify_down //当服务器健康检查失败时,执行的脚本

uthreshold //到这台服务器的最大连接数

lthreshold //到这台服务器的最小连接数

tcp_check段配置

connect_ip //连接的IP地址。默认是real server的ip地址

connect_port //连接的端口。默认是real server的端口

bindto //发起连接的接口的地址。

bind_port //发起连接的源端口。

connect_timeout //连接超时时间。默认是5s。

fwmark //使用fwmark对所有出去的检查数据包进行标记。

warmup //指定一个随机延迟,最大为N秒。可防止网络阻塞。如果为0,则关闭该功能。

retry //重试次数。默认是1次。

delay_before_retry //默认是1秒。在重试之前延迟多少秒。

keepalived实现httpd负载均衡机高可用

========================================================================================

环境说明


| 系统信息 | 主机名 | IP |

| — | — | — |

| redhat8 | master | 192.168.129.134 |

| redhat8 | slave | 192.168.129.135 |

本次高可用虚拟IP(VIP)地址暂定为 192.168.129.250

keepalived安装


配置主keepalived

//关闭防火墙与SELINUX

[root@master ~]# systemctl disable --now firewalld

[root@master ~]# sed -ri ‘s/^(SELINUX=).*/\1disabled/g’ /etc/selinux/config

[root@master ~]# setenforce 0

[root@master ~]# reboot

//配置网络源

[root@master ~]# curl -o /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo

[root@master ~]# sed -i ‘s/$releasever/7/g’ /etc/yum.repos.d/CentOS7-Base-163.repo

[root@master ~]# sed -i ‘s/^enabled=.*/enabled=1/g’ /etc/yum.repos.d/CentOS7-Base-163.repo

[root@master ~]# yum -y install epel-release vim

//安装keepalived

[root@master ~]# yum -y install keepalived

//查看安装生成的文件

[root@master ~]# rpm -ql keepalived

/etc/keepalived #配置目录

/etc/keepalived/keepalived.conf #此为主配置文件

/etc/sysconfig/keepalived

/usr/bin/genhash

/usr/lib/.build-id

/usr/lib/.build-id/0a

/usr/lib/.build-id/0a/410997e11c666114ca6d785e58ff0cc248744e

/usr/lib/.build-id/6f

/usr/lib/.build-id/6f/ba0d6bad6cb5ff7b074e703849ed93bebf4a0f

/usr/lib/systemd/system/keepalived.service #此为服务控制文件

/usr/libexec/keepalived

/usr/sbin/keepalived

…此处省略N行

在备服务器上安装keepalived

//关闭防火墙与SELINUX

[root@slave ~]# systemctl disable --now firewalld

[root@slave ~]# sed -ri ‘s/^(SELINUX=).*/\1disabled/g’ /etc/selinux/config

[root@slave ~]# setenforce 0

[root@slave ~]# reboot

//配置网络源

[root@slave ~]# curl -o /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo

[root@slave ~]# sed -i ‘s/$releasever/7/g’ /etc/yum.repos.d/CentOS7-Base-163.repo

[root@slave ~]# sed -i ‘s/^enabled=.*/enabled=1/g’ /etc/yum.repos.d/CentOS7-Base-163.repo

[root@slave ~]# yum -y install epel-release vim

//安装keepalived

[root@slave ~]# yum -y install keepalived

在主备机上分别安装httpd


在master上安装httpd

[root@master ~]# yum -y install httpd

[root@master ~]# echo “This is MASTER” > /var/www/html/index.html

[root@master ~]# systemctl restart httpd

[root@master ~]# systemctl enable --now httpd

[root@master ~]# curl 192.168.129.134

This is MASTER

在这里插入图片描述

在slave 上安装httpd

[root@slave ~]# yum -y install httpd

[root@slave ~]# echo “This is BACKUP” > /var/www/html/index.html

[root@slave ~]# systemctl restart httpd

[root@slave ~]# systemctl enable --now httpd

[root@slave ~]# curl 192.168.129.135

This is BACKUP

在这里插入图片描述

keepalived配置


配置主keepalived

[root@master ~]# vim /etc/keepalived/keepalived.conf

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

! Configuration File for keepalived

global_defs {

router_id lb01

}

vrrp_instance VI_1 {

state MASTER

interface ens160

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 12345678

}

virtual_ipaddress {

192.168.129.250

}

}

virtual_server 192.168.129.250 80 {

delay_loop 6

lb_algo rr

lb_kind DR

persistence_timeout 50

protocol TCP

real_server 192.168.129.134 80 {

weight 1

TCP_CHECK {

connect_port 80

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.129.135 80 {

weight 1

TCP_CHECK {

connect_port 80

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

[root@master ~]# systemctl enable --now keepalived

Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.

配置备keepalived

[root@slave ~]# vim /etc/keepalived/keepalived.conf

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

! Configuration File for keepalived

global_defs {

router_id lb02

}

vrrp_instance VI_1 {

state BACKUP

interface ens160

virtual_router_id 51

priority 90

advert_int 1

authentication {

auth_type PASS

auth_pass 12345678

}

virtual_ipaddress {

192.168.129.250

}

}

virtual_server 192.168.129.250 80 {

delay_loop 6

lb_algo rr

lb_kind DR

persistence_timeout 50

protocol TCP

real_server 192.168.129.134 80 {

weight 1

TCP_CHECK {

connect_port 80

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.129.135 80 {

weight 1

TCP_CHECK {

connect_port 80

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

[root@slave ~]# systemctl enable --now keepalived

Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.

查看VIP在哪里


在MASTER上查看

[root@master ~]# ip a

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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000

link/ether 00:0c:29:04:e2:3a brd ff:ff:ff:ff:ff:ff

inet 192.168.129.134/24 brd 192.168.129.255 scope global noprefixroute ens160

valid_lft forever preferred_lft forever

inet 192.168.129.250/32 scope global ens160

valid_lft forever preferred_lft forever

inet6 fe80::5f9d:bb13:b9cf:e4a2/64 scope link noprefixroute

valid_lft forever preferred_lft forever

SLAVE上查看

[root@slave ~]# ip a

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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000

link/ether 00:0c:29:aa:87:a6 brd ff:ff:ff:ff:ff:ff

inet 192.168.129.135/24 brd 192.168.129.255 scope global secondary noprefixroute ens160

valid_lft forever preferred_lft forever

inet6 fe80::830b:1a3a:1104:10b5/64 scope link noprefixroute

valid_lft forever preferred_lft forever

修改内核参数,开启监听VIP功能


此步可做可不做,该功能可用于仅监听VIP的时候

在master上修改内核参数

[root@master ~]# echo ‘net.ipv4.ip_nonlocal_bind = 1’ >>/etc/sysctl.conf

[root@master ~]# sysctl -p

net.ipv4.ip_nonlocal_bind = 1

[root@master ~]# cat /proc/sys/net/ipv4/ip_nonlocal_bind

1

在slave上修改内核参数

[root@slave ~]# echo ‘net.ipv4.ip_nonlocal_bind = 1’ >>/etc/sysctl.conf

[root@slave ~]# sysctl -p

net.ipv4.ip_nonlocal_bind = 1

[root@slave ~]# cat /proc/sys/net/ipv4/ip_nonlocal_bind

1

让keepalived监控httpd负载均衡机


在master上编写脚本

[root@master ~]# mkdir /scripts

[root@master ~]# cd /scripts/

[root@master scripts]# vim check_n.sh

[root@master scripts]# cat check_n.sh

#!/bin/bash

httpd_status=$(ps -ef|grep -Ev “grep|$0”|grep ‘\bhttpd\b’|wc -l)

if [ $httpd_status -lt 1 ];then

systemctl stop keepalived

fi

[root@master scripts]# chmod +x check_n.sh

[root@master scripts]# ll

总用量 4

-rwxr-xr-x 1 root root 130 10月 21 19:17 check_n.sh

[root@master scripts]# vim notify.sh

[root@master scripts]# cat notify.sh

#!/bin/bash

VIP=$2

sendmail (){

subject=“${VIP}'s server keepalived state is translate”

content=“date +'%F %T': hostname’s state change to master”

echo c o n t e n t ∣ m a i l − s " content | mail -s " contentmails"subject" 2464146612@qq.com

}

case “$1” in

master)

httpd_status=$(ps -ef|grep -Ev “grep|$0”|grep ‘\bhttpd\b’|wc -l)

if [ $httpd_status -lt 1 ];then

systemctl start httpd

fi

sendmail

;;

backup)

httpd_status=$(ps -ef|grep -Ev “grep|$0”|grep ‘\bhttpd\b’|wc -l)

if [ $httpd_status -gt 0 ];then

systemctl stop httpd

fi

;;

*)

echo “Usage:$0 master|backup VIP”

;;

esac

[root@master scripts]# chmod +x notify.sh

[root@master scripts]# ll

总用量 8

-rwxr-xr-x 1 root root 130 10月 21 19:17 check_n.sh

-rwxr-xr-x 1 root root 444 10月 21 19:18 notify.sh

在slave上编写脚本

[root@slave ~]# mkdir /scripts

[root@slave ~]# cd /scripts/

[root@slave scripts]# vim notify.sh

[root@slave scripts]# cat notify.sh

#!/bin/bash

VIP=$2

sendmail (){

subject=“${VIP}'s server keepalived state is translate”

content=“date +'%F %T': hostname’s state change to master”

echo c o n t e n t ∣ m a i l − s " content | mail -s " contentmails"subject" 2464146612@qq.com

}

case “$1” in

master)

httpd_status=$(ps -ef|grep -Ev “grep|$0”|grep ‘\bhttpd\b’|wc -l)

if [ $httpd_status -lt 1 ];then

systemctl start httpd

fi

sendmail

;;

backup)

httpd_status=$(ps -ef|grep -Ev “grep|$0”|grep ‘\bhttpd\b’|wc -l)

if [ $httpd_status -gt 0 ];then

systemctl stop httpd

fi

;;

*)

echo “Usage:$0 master|backup VIP”

;;

esac

[root@slave scripts]# chmod +x notify.sh

[root@slave scripts]# ll

总用量 4

-rwxr-xr-x 1 root root 445 10月 21 19:19 notify.sh

配置keepalived加入监控脚本的配置


配置主keepalived

[root@master ~]# vim /etc/keepalived/keepalived.conf

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

! Configuration File for keepalived

global_defs {

router_id lb01

}

#添加一下五行内容

vrrp_script httpd_check {

script “/scripts/check_n.sh”

interval 1

weight -20 #权重减20

}

vrrp_instance VI_1 {

state MASTER

interface ens160

virtual_router_id 51

priority 100

nopreempt

advert_int 1

authentication {

auth_type PASS

auth_pass 12345678

}

virtual_ipaddress {

192.168.129.250

}

track_script {

httpd_check

}

}

virtual_server 192.168.129.250 80 {

delay_loop 6

lb_algo rr

lb_kind DR

persistence_timeout 50

protocol TCP

real_server 192.168.129.134 80 {

weight 1

TCP_CHECK {

connect_port 80

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.129.135 80 {

weight 1

TCP_CHECK {

connect_port 80

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

[root@master ~]# systemctl restart keepalived

最后

无论是哪家公司,都很重视基础,大厂更加重视技术的深度和广度,面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。

针对以上面试技术点,我在这里也做一些分享,希望能更好的帮助到大家。

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

t {

httpd_check

}

}

virtual_server 192.168.129.250 80 {

delay_loop 6

lb_algo rr

lb_kind DR

persistence_timeout 50

protocol TCP

real_server 192.168.129.134 80 {

weight 1

TCP_CHECK {

connect_port 80

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.129.135 80 {

weight 1

TCP_CHECK {

connect_port 80

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

[root@master ~]# systemctl restart keepalived

最后

无论是哪家公司,都很重视基础,大厂更加重视技术的深度和广度,面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。

针对以上面试技术点,我在这里也做一些分享,希望能更好的帮助到大家。

[外链图片转存中…(img-flxj9J5D-1715406987764)]

[外链图片转存中…(img-AqKXdiB5-1715406987764)]

[外链图片转存中…(img-SVBgT1UC-1715406987765)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值