当前位置: 首页 > news >正文

lvs+keepalived+dns高可用

在这里插入图片描述

1.配置dns相关服务

1.1修改ip地址主机名
dns-master:

hostnamectl hostname lvs-master
nmcli c modify ens160 ipv4.method manual ipv4.addresses 10.10.10.107/24 ipv4.gateway 10.10.10.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
nmcli c up ens160

dns-salve:

hostnamectl hostname lvs-slave
nmcli c modify ens160 ipv4.method manual ipv4.addresses 10.10.10.108/24 ipv4.gateway 10.10.10.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
nmcli c up ens160

1.2配置仓库,下载bind服务

vim /etc/yum.repos.d/yum.repo
[BaseOS]
name=BaseOS
baseurl=/mnt/BaseOS
gpgcheck=0
[AppStream]
name=AppStream
baseurl=/mnt/AppStream
gpgcheck=0

挂载

mount /dev/sr0 /mnt

下载

dnf install bind -y

关闭防火墙和selinux服务

systemctl stop firewalld
setenforce 0

1.3配置dns相关文件
dns-master:

vim /etc/named.conf
options {
	listen-on port 53 { 10.10.10.107;10.10.10.200; };
	directory 	"/var/named";
};


zone "example.com" IN {
	type master;
	file "example.com";
};
vim /var/named/example.com
$TTL    1D
@       IN      SOA  @   amdin.example.com. (0 1D 1H 2H 2D)
        IN      NS      ns1
        IN      NS      ns2
ns1     IN      A       10.10.10.107
ns2     IN      A       10.10.10.108
www     IN      A       10.10.10.200

启动服务并测试:

systemctl start named
dig -t A www.example.com @10.10.10.107
dig -t ns example.com @10.10.10.107

[root@dns-master ~]# dig -t ns example.com @10.10.10.107

; <<>> DiG 9.16.23-RH <<>> -t ns example.com @10.10.10.107 ;; global
options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status:
NOERROR, id: 14914 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2,
AUTHORITY: 0, ADDITIONAL: 3

;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 1232 ; COOKIE:
f8f87f0c77258da70100000067fa038f37c0baa6b48666c1 (good) ;; QUESTION
SECTION: ;example.com. IN NS

;; ANSWER SECTION: example.com. 86400 IN NS ns1.example.com.
example.com. 86400 IN NS ns2.example.com.

;; ADDITIONAL SECTION: ns1.example.com. 86400 IN A 10.10.10.107
ns2.example.com. 86400 IN A 10.10.10.108

;; Query time: 0 msec ;; SERVER: 10.10.10.107#53(10.10.10.107) ;;
WHEN: Sat Apr 12 14:09:19 CST 2025 ;; MSG SIZE rcvd: 136
[root@dns-master ~]# dig -t A www.example.com @10.10.10.107

; <<>> DiG 9.16.23-RH <<>> -t A www.example.com @10.10.10.107 ;;
global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY,
status: NOERROR, id: 41021 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1,
AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 1232 ; COOKIE:
915f2990163d16460100000067fa03b5ec98879ed99ed361 (good) ;; QUESTION
SECTION: ;www.example.com. IN A

;; ANSWER SECTION: www.example.com. 86400 IN A 10.10.10.200

;; Query time: 0 msec ;; SERVER: 10.10.10.107#53(10.10.10.107) ;;
WHEN: Sat Apr 12 14:09:57 CST 2025 ;; MSG SIZE rcvd: 88

dns-slave:

vim /etc/named.conf
options {
	listen-on port 53 { 10.10.10.108 ;10.10.10.200 ; };
	directory 	"/var/named";
};


zone "example.com" IN {
	type slave;
	file "slaves/example.com";
	masters {10.10.10.107 ;};
};

2.配置web相关服务

2.1修改ip地址和主机名

web01:

hostnamectl hostname web01
nmcli c modify ens160 ipv4.method manual ipv4.addresses 10.10.10.201/24 ipv4.gateway 10.10.10.2 ipv4.dns "10.10.10.107 10.10.10.108" connection.autoconnect yes
nmcli c up ens160

web02:

hostnamectl hostname web02
nmcli c modify ens160 ipv4.method manual ipv4.addresses 10.10.10.202/24 ipv4.gateway 10.10.10.2 ipv4.dns "10.10.10.107 10.10.10.108" connection.autoconnect yes
nmcli c up ens160

web03:

hostnamectl hostname web03
nmcli c modify ens160 ipv4.method manual ipv4.addresses 10.10.10.203/24 ipv4.gateway 10.10.10.2 ipv4.dns "10.10.10.107 10.10.10.108" connection.autoconnect yes
nmcli c up ens160

2.2配置仓库,下载nginx服务

vim /etc/yum.repos.d/yum.repo
[BaseOS]
name=BaseOS
baseurl=/mnt/BaseOS
gpgcheck=0
[AppStream]
name=AppStream
baseurl=/mnt/AppStream
gpgcheck=0

挂载

mount /dev/sr0 /mnt

下载

dnf install nginx -y

关闭防火墙和selinux服务

systemctl stop firewalld
setenforce 0

1.3配置dns相关文件
web01:

vim /etc/nginx/conf.d/web.conf
server{
	server_name www.example.com;
	listen 80;
	root /usr/share/nginx/html/index.html;
}
echo $(hostname) - $(hostname -I) > /usr/share/nginx/html/index.html

web02:

vim /etc/nginx/conf.d/web.conf
server{
	server_name www.example.com;
	listen 80;
	root /usr/share/nginx/html/index.html;
}
echo $(hostname) - $(hostname -I) > /usr/share/nginx/html/index.html

web03:

vim /etc/nginx/conf.d/web.conf
server{
	server_name www.example.com;
	listen 80;
	root /usr/share/nginx/html/index.html;
}
echo $(hostname) - $(hostname -I) > /usr/share/nginx/html/index.html

修改dns

nmcli c modify ens160 ipv4.dns 10.10.10.100

开启和测试nginx服务

systemctl start nginx
curl localhost

[root@web01 ~]# curl localhost web01 - 10.10.10.201 [root@web02 ~]#
curl localhost web01 - 10.10.10.202 [root@web03 ~]# curl localhost
web01 - 10.10.10.203

3.配置lvs相关服务

3.1修改ip地址和主机名

lb-master:

hostnamectl hostname lb-master
nmcli c modify ens160 ipv4.method manual ipv4.addresses 10.10.10.105/24 ipv4.gateway 10.10.10.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
nmcli c up ens160

lb-salve:

hostnamectl hostname lb-salve
nmcli c modify ens160 ipv4.method manual ipv4.addresses 10.10.10.106/24 ipv4.gateway 10.10.10.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
nmcli c up ens160

3.2配置仓库,下载keepalived ipvsadm服务

vim /etc/yum.repos.d/yum.repo
[BaseOS]
name=BaseOS
baseurl=/mnt/BaseOS
gpgcheck=0
[AppStream]
name=AppStream
baseurl=/mnt/AppStream
gpgcheck=0

挂载

mount /dev/sr0 /mnt

下载

dnf install keepalived ipvsadm -y

关闭防火墙和selinux服务

systemctl stop firewalld
setenforce 0

3.3配置keepalived相关文件
lb-master:

vim /etc/keeailved;keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id LVS_MASTER
}

vrrp_instance VI_1 {
    state MASTER
    interface ens160
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.10.10.100
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface ens160
    virtual_router_id 50
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.10.10.200
    }
}
virtual_server 10.10.10.100 53 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 10
    protocol UDP

    real_server 10.10.10.107 53 {
        weight 1
        MISC_CHECK {
            connect_timeout 3
	      misc_path "/etc/keepalived/checkdns.sh -h 10.10.10.107 txt.example.com"
        }
    }
    real_server 10.10.10.108 53 {
        weight 1
        MISC_CHECK {
            connect_timeout 3
	      misc_path "/etc/keepalived/checkdns.sh -h 10.10.10.108 txt.example.com"
        }
    }
}

virtual_server 10.10.10.200 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 10
    protocol TCP

    real_server 10.10.10.201 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
    real_server 10.10.10.202 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
    real_server 10.10.10.203 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
}

lb-slave:

vim  /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id LVS_BACKUP
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.10.10.100
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface ens160
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.10.10.200
    }
}
virtual_server 10.10.10.100 53 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 10
    protocol UDP

    real_server 10.10.10.107 53 {
        weight 1
        MISC_CHECK {
            connect_timeout 3
	      misc_path "/etc/keepalived/checkdns.sh -h 10.10.10.107 txt.example.com"
        }
    }
    real_server 10.10.10.108 53 {
        weight 1
        MISC_CHECK {
            connect_timeout 3
	      misc_path "/etc/keepalived/checkdns.sh -h 10.10.10.108 txt.example.com"
        }
    }
}

virtual_server 10.10.10.200 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 10
    protocol TCP

    real_server 10.10.10.201 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
    real_server 10.10.10.202 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
    real_server 10.10.10.203 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
}

3.4编写执行的脚本

vim /etc/keepalived/checkdns.sh
#!/bin/bash
[ $# -le 2 ] && { echo "usage: $0 -h <ip>"; exit 2; }
domain=$3
while getopts "h:" OPT; do
      case $OPT in
              h)
                       host=$OPTARG
                      ;;
              *)
                       echo "usage: $0 -h <ip>" && exit 1
                      ;;
       esac
done
dig @${host} txt ${domain} +time=1 | grep "\<AaBbCcDdEeFf\>" >
/dev/null
exit $?
chmod a+x /etc/keepalived/checkdns.sh

3.5启动服务

systemctl start keepalived ipvsadm

查看lvs规则

ipvsadm -ln

4.修改web的dns增加ip

nmcli c modify ens160 ipv4.dns 10.10.10.100
nmcli c modify ens160 up
ifconfig lo:0 10.10.10.200 netmask 255.255.255.255
route add -host 10.10.10.200 dev lo:0

5.增加dns路由

ifconfig lo:0 10.10.10.100 netmask 255.255.255.255
route add -host 10.10.10.100 dev lo:0

6.测试
在这里插入图片描述
在这里插入图片描述

相关文章:

  • 使用nuxt3+tailwindcss4+@nuxt/content3在页面渲染 markdown 文档
  • 红宝书第四十讲:React 核心概念:组件化 虚拟 DOM 简单教程
  • forms+windows添加激活水印
  • 塔能科技解节能密码,工厂成本“效益方程式”精准破题
  • AF3 ProteinDataset类的_process方法解读
  • 操作系统之进程同步
  • python的flask框架连接数据库
  • 区块链从专家到小白
  • GAS:车载体验的智能革新力量
  • Rag实现流程
  • 软件界面设计:打造用户喜爱的交互体验
  • 新版雷电模拟器过应用检测技术详解
  • leetcode 264. Ugly Number II
  • ReactNative 使用腾讯的MMKV持久化存储
  • 深入浅出:信号灯与系统V信号灯的实现与应用
  • 区块链点燃游戏行业新未来——技术变革与实践指南
  • 【Linux网络与网络编程】08.传输层协议 UDP
  • 互联网三高-数据库高并发之分库分表
  • PostgreSQL 的统计信息
  • 0x02.Redis 集群的实现原理是什么?
  • 一级a做爰片免费网站偷拍厕所/网站排名seo软件
  • 互联网公司十大排名/seo关键词是什么
  • 广告网站建设网站排名优化/推广业务
  • 做网站老师/怎样建立一个网站
  • 中国建筑有限公司官网/搜索引擎优化是指什么意思
  • 英文响应式网站建设/什么平台可以免费发广告