使用华为云VPC搭建Keepalived+Nginx高可用WEB负载均衡

网友投稿 1252 2022-05-29

问题描述:

本文档介绍如何使用华为云VPC在CentOS6.8系统环境实现 Keepalived+Nginx高可用WEB负载均衡环境搭建。

1、物料准备

本文档中的操作为实验环境,所有资源均为“按需付费”,如果您自己在生产环境中使用,建议使用包年/包月模式。为确保各资源组件可连通性,一定确保所有资源均在“华东-上海二”购买,实际生产环境按业务实际需要选择区域。

1.1、购买虚拟私有云(VPC)并指定子网

(1)、虚拟私有云vpc-HA和子网subnet-HA

登录华为云官网,按照网站导航——产品à网络à 虚拟私有云 VPC找到虚拟私有云(VPC)产品。

点击“立即使用”购买虚拟私有云 VPC进入“申请虚拟私有云”页面。

(2)、购买完成的虚拟私有云(vpc-HA)和子网(subnet-HA)信息如下图所示

1.2 、 购买弹性云服务器(ECS)

登录华为云官网,按照网站导航——产品à计算à弹性云服务器 ECS找到弹性云服务器 (ECS)产品。

点击“立即购买”进入“购买弹性云服务器”页面

进入购买页面后,购买过程中注意指定“物料准备”中指定的如下相关信息:

(1)、ecs-HA1:

(2)、 ecs-HA2:

(3)、 购买完成的两台弹性云服务器配置信息

我们这里是实验环境,所有一切从简,并未选购数据盘,实际生产环境使用时请按业务需求选购数据盘,并切实考虑HA两个节点之间业务数据一致性(同步)问题。

1.3、购买弹性公网IP(EIP)

登录华为云官网,按照网站导航——产品à网络à 弹性公网IP(Elastic IP),找到弹性公网IP(Elastic IP)购买页。

我们本次购买到的弹性公网IP为XXXXXXXXXXX

1.4、申请虚拟IP地址

登录华为云官网,按照网站导航——控制台→服务列表→网络→虚拟私有云→vpc-HA→子网→subnet-HA→虚拟IP→申请虚拟IP地址。

2 、 操作步骤

2.1、 配置弹性云服务器环境

配置弹性云服务器环境需要使用互联网安装相关软件包,所以我们在配置弹性云服务器环境的过程中会临时使用弹性公网IPXXXXXXXXXX。

(1)、ecs-HA1:

A.  绑定弹性公网IP XXXXXXXXXXXX到ecs-HA1。

登录华为云官网,按照网站导航——控制台à服务列表à计算à弹性云服务器,找到ecs-HA1。

B.  通过ssh连接ecs-HA1安装nginx、keepalived 软件包及相关依赖包。

# yum install nginx   keepalived -y

C.  编辑nginx配置文件。

# vim   /etc/nginx/nginx.conf

user root;

worker_processes 1;

#error_log   logs/error.log;

#error_log   logs/error.log notice;

#error_log   logs/error.log info;

#pid logs/nginx.pid;

events {

worker_connections   1024;

}

http {

include mime.types;

default_type   application/octet-stream;

#log_format main   '$remote_addr - $remote_user [$time_local] "$request" '

# '$status   $body_bytes_sent "$http_referer" '

#   '"$http_user_agent" "$http_x_forwarded_for"';

#access_log   logs/access.log main;

sendfile on;

#tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65;

#gzip on;

server {

listen 80;

server_name localhost;

#charset koi8-r;

#access_log   logs/host.access.log main;

location / {

root html;

index index.html   index.htm;

}

#error_page 404   /404.html;

# redirect server error   pages to the static page /50x.html

error_page 500 502 503   504 /50x.html;

location = /50x.html {

root html;

}

}

}

D.   编辑index.html文件内容以演示访问效果

# vim   /usr/share/nginx/html/index.html

ECS-HA

Welcome   to ECS-HA1

E.  设置nginx服务开机自启动并启动服务

# systemctl   enable nginx

# systemctl   start nginx.service

使用华为云VPC搭建Keepalived+Nginx高可用WEB负载均衡

F.  Nginx单节点访问测试。

G.  编辑keepalived配置文件。

# vim   /etc/keepalived/keepalived.conf

! Configuration File for   keepalived

global_defs {

router_id master-node

}

vrrp_script   chk_http_port {

script   "/etc/keepalived/chk_nginx.sh"

interval 2

weight -5

fall 2

rise 1

}

vrrp_instance VI_1 {

state MASTER

interface eth0

mcast_src_ip   192.168.0.10

virtual_router_id 51

priority 101

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.0.100

}

track_script {

chk_http_port

}

}

H.  编辑nginx监控脚本。

# vim   /etc/keepalived/chk_nginx.sh

#!/bin/bash

counter=$(ps -C nginx   --no-heading|wc -l)

if [ "${counter}"   = "0" ]; then

systemctl start   nginx.service

sleep 2

counter=$(ps -C nginx   --no-heading|wc -l)

if [   "${counter}" = "0" ]; then

systemctl stop   keepalived.service

fi

fi

chmod +x   /etc/keepalived/chk_nginx.sh

I.  设置keepalived服务开机自启动并启动服务

# systemctl enable   keepalived

# systemctl start   keepalived.service

(2)、 ecs-HA2:

先将弹性公网IP XXXXXXXX从ecs-HA1解绑定。

A.   绑定弹性公网IP xxxxxxxx到ecs-HA2。

登录华为云官网,按照网站导航——控制台à服务列表à计算à弹性云服务器,找到ecs-HA2。

B.  通过ssh连接ecs-HA1安装nginx、keepalived 软件包及相关依赖包。

# yum install nginx keepalived   -y

C.   编辑nginx配置文件(也可以从ecs-HA1上复制一份)。

#   vim /etc/nginx/nginx.conf

user root;

worker_processes 1;

#error_log   logs/error.log;

#error_log   logs/error.log notice;

#error_log   logs/error.log info;

#pid logs/nginx.pid;

events {

worker_connections   1024;

}

http {

include mime.types;

default_type   application/octet-stream;

#log_format main   '$remote_addr - $remote_user [$time_local] "$request" '

# '$status   $body_bytes_sent "$http_referer" '

#   '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log   main;

sendfile on;

#tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65;

#gzip on;

server {

listen 80;

server_name localhost;

#charset koi8-r;

#access_log   logs/host.access.log main;

location / {

root html;

index index.html   index.htm;

}

#error_page 404   /404.html;

# redirect server error   pages to the static page /50x.html

error_page 500 502 503   504 /50x.html;

location = /50x.html {

root html;

}

}

}

D.   编辑index.html文件内容以演示访问效果

# vim   /usr/share/nginx/html/index.html

ECS-HA

Welcome   to ECS-HA2

E.   设置nginx服务开机自启动并启动服务

# systemctl   enable nginx

# systemctl   start nginx.service

F.   Nginx单节点访问测试。

G.   编辑keepalived配置文件。

#   vim /etc/keepalived/keepalived.conf

! Configuration File   for keepalived

global_defs {

router_id master-node

}

vrrp_script   chk_http_port {

script   "/etc/keepalived/chk_nginx.sh"

interval 2

weight -5

fall 2

rise 1

}

vrrp_instance VI_1 {

state BACKUP

interface eth0

mcast_src_ip   192.168.0.20

virtual_router_id 51

priority 101

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.0.100

}

track_script {

chk_http_port

}

}

H.   编辑nginx监控脚本并添加执行权限(也可以从ecs-HA1上复制一份)

# vim   /etc/keepalived/chk_nginx.sh

#!/bin/bash

counter=$(ps -C nginx   --no-heading|wc -l)

if [   "${counter}" = "0" ]; then

systemctl start   nginx.service

sleep 2

counter=$(ps -C nginx   --no-heading|wc -l)

if [   "${counter}" = "0" ]; then

systemctl stop   keepalived.service

fi

fi

chmod +x   /etc/keepalived/chk_nginx.sh

设置keepalived服务开机自启动并启动服务

# systemctl enable   keepalived

# systemctl start   keepalived

2.2 、绑定虚拟IP。

先将弹性公网IP xxxxxxxx从ecs-HA2解绑定。

登录华为云官网,按照网站导航——控制台à服务列表à网络à虚拟私有云àvpc-HAà子网àsubnet-HAà虚拟IP。

(1)、绑定虚拟IP到弹性云服务器ecs-HA1。

(2)、 绑定虚拟IP到弹性云服务器ecs-HA2。

(3)、 绑定虚拟IP到弹性公网IP xxxxxxx。

3 、结果验证

分别重启ecs-HA1和ecs-HA2,通过华为云官网用户控制台远程登录到ecs-HA1上按以下操作进行验证。

3.1、查看虚IP是否有绑定到ecs-HA1的eth0网卡上

# ip addr show

3.2 、通过浏览器访问弹性公网IP看是否可以访问到ecs-HA1节点上的WEB测试页。

3.3、手动停止主服务器(ecs-HA1)上的keepalived服务,然后查看从服务器(ecs-HA2)是否有接管VIP;通过浏览器访问弹性公网IP看是否可以访问到ecs-HA2节点上的WEB测试页。

# systemctl stop   keepalived.service

通过以上验证信息可以看出我们的高可用WEB负载均衡功能正常。

虚拟私有云 VPC

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:新计算 新网络 新旗舰:华为云C6实例首测
下一篇:摄影行业华为云解决方案
相关文章