实验-CentOS 使用Keepalived搭建高可用集群

发布时间:2024-11-14 点击:67
建站服务器
配置keepalivedha集群
一)实验环境:
服务器系统:centos7.3
服务:nginx,keepalived
客户端系统:windows 10,centos7.3
二)初始化操作:master/backup,童颜的操作
#安装keepalived软件
[root@shiyan7 ~ ]# yum install keepalived
#安装nginx(需要配置epel源)
[root@shiyan7 ~ ]# yum installnginx
#关闭清空防火墙
[root@shiyan7 ~ ]# systemctl disable firewall.d
[root@shiyan7 ~ ]# systemctl stop firewall.d
[root@shiyan7 ~ ]# iptables -f
#添加主机解析
[root@shiyan7 ~ ]# vim /etc/hosts
127.0.0.1localhost localhost.localdomain localhost4 localhost4.localdomain4
::1localhost localhost.localdomain localhost6 localhost6.localdomain6
172.18.17.34 shiyan.fore
172.18.17.31 shiyan.one
#关闭selinux(配置后需要重启)
[root@shiyan7 ~ ]# vim /etc/selinux/config
selinux=disabled
三)master配置
nginx配置
[root@shiyan7 ~ ]# mkdir /app/nginx/html
[root@shiyan7 ~ ]# echo sorry server-1 >/app/nginx/html/index.html
[root@shiyan7 ~ ]# cp /etc/nginx/nginx.conf{,.bak}
[root@shiyan7 ~ ]# vim /etc/nginx/nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /app/nginx/html;
# loadconfiguration files for the default server block.
include/etc/nginx/default.d/*.conf;
location /{
}
error_page404 /404.html;
location = /40x.html {
}
error_page500 502 503 504 /50x.html;
location = /50x.html {
}
}
[root@shiyan7 ~ ]# systemctl start nginx
使用浏览器测试nginx
keepalived配置
[root@shiyan7 ~ ]# cp /etc/keepalived/keepalived.conf{,.bak}
[root@shiyan7 ~ ]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from alexandre.cassen@firewall.loc
smtp_server192.168.200.1
smtp_connect_timeout 30
router_id node1
}
vrrp_instance ha_1 {
state backup
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type pass
auth_pass 1111
}
virtual_ipaddress {
172.18.17.30/16 dev ens33
}
}
四)backup配置
nginx配置
[root@shiyan7 ~ ]# mkdir /app/nginx/html
[root@shiyan7 ~ ]# echo sorry server-2 >/app/nginx/html/index.html
[root@shiyan7 ~ ]# cp /etc/nginx/nginx.conf{,.bak}
[root@shiyan7 ~ ]# vim /etc/nginx/nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /app/nginx/html;
# loadconfiguration files for the default server block.
include/etc/nginx/default.d/*.conf;
location /{
}
error_page404 /404.html;
location = /40x.html {
}
error_page500 502 503 504 /50x.html;
location = /50x.html {
}
}
[root@shiyan7 ~ ]# systemctl start nginx
使用浏览器测试nginx
keepalived配置
[root@shiyan7 ~ ]# cp /etc/keepalived/keepalived.conf{,.bak}
[root@shiyan7 ~ ]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email{
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from alexandre.cassen@firewall.loc
smtp_server192.168.200.1
smtp_connect_timeout 30
router_id node2
}
vrrp_instance ha_1 {
state backup
interface ens33
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type pass
auth_pass 1111
}
virtual_ipaddress {
172.18.17.30/16 dev ens33
}
}
五)测试:
1)开启主被服务器的keepalived服务,并查看vip/ip
master
[root@shiyan7 ~ ]# systemctl start keepalived
[root@shiyan7 ~ ]# ip a l
backup
[root@shiyan7 ~ ]# systemctl start keepalived
[root@shiyan7 ~ ]# ip a l
测试测试master环境上的nginx
2)关闭master测试backup
master
[root@shiyan7 ~ ]# systemctl stop keepalived
[root@shiyan7 ~ ]# ip a l
backup
[root@shiyan7 ~ ]# ip a l
测试backup环境上的nginx
3)开启master
master
[root@shiyan7 ~ ]# systemctl start keepalived
root@shiyan7 ~ ]# ip a l
backup
[root@shiyan7 ~ ]# ip a l
测试测试master环境上的nginx
更多关于云服务器,域名注册,虚拟

云服务器怎么查看日志的步骤
上海华为云erp服务器租赁
我十六岁是末成年人能购买域名吗
云服务器哪家好 哪些云服务器好用
三大关键词重构数据库走向,腾讯云王义成详解数据库最新布局
互联网域名管理的喜和忧
我提交服务器白名单的时候显示已经加入白名单
买好了腾讯云的云服务器怎么用啊