弹性云服务器 ECS-使用虚拟IP和Keepalived搭建高可用Web集群:步骤二:为主备ECS配置Keepalived
步骤二:为主备E CS 配置Keepalived
- 执行以下操作,为ECS-HA1配置Keepalived。
- 将EIP绑定至ECS-HA1。
具体方法请参见绑定弹性公网IP。
- 远程登录ECS-HA1。
ECS有多种登录方法,具体请参见登录弹性云服务器。
- 执行以下命令,安装Nginx、Keepalived软件包及相关依赖包。
yum install nginx keepalived -y
回显类似如下信息,表示安装完成。[root@ecs-ha1 ~]# yum install nginx keepalived -y Loaded plugins: fastestmirror Determining fastest mirrors base | 3.6 kB 00:00:00 epel | 4.3 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/7): epel/x86_64/group | 399 kB 00:00:00 (2/7): epel/x86_64/updateinfo | 1.0 MB 00:00:00 (3/7): base/7/x86_64/primary_db | 6.1 MB 00:00:00 (4/7): base/7/x86_64/group_gz | 153 kB 00:00:00 (5/7): epel/x86_64/primary_db | 8.7 MB 00:00:00 (6/7): extras/7/x86_64/primary_db | 253 kB 00:00:00 (7/7): updates/7/x86_64/primary_db ..... Dependency Installed: centos-indexhtml.noarch 0:7-9.el7.centos gperftools-libs.x86_64 0:2.6.1-1.el7 lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7_9.1 net-snmp-agent-libs.x86_64 1:5.7.2-49.el7_9.4 net-snmp-libs.x86_64 1:5.7.2-49.el7_9.4 nginx-filesystem.noarch 1:1.20.1-10.el7 openssl11-libs.x86_64 1:1.1.1k-7.el7 Complete!
- 执行以下操作,修改Nginx配置文件,添加80端口相关配置。
- 执行以下命令,打开“/etc/nginx/nginx.conf”文件。
vim /etc/nginx/nginx.conf
- 按i进入编辑模式。
- 将文件中原有的内容,全部替换成以下内容。
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; } } }
- 按ESC退出,并输入:wq!保存配置。
- 执行以下命令,打开“/etc/nginx/nginx.conf”文件。
- 执行以下操作,修改index.html文件内容,用来验证网站的访问情况。
- 执行以下命令,打开“/usr/share/nginx/html/index.html”文件。
vim /usr/share/nginx/html/index.html
- 按i进入编辑模式。
- 将文件中原有的内容,全部替换成以下内容。
Welcome to ECS-HA1
- 按ESC退出,并输入:wq!保存配置。
- 执行以下命令,打开“/usr/share/nginx/html/index.html”文件。
- 执行以下命令,设置Nginx服务开机自启动,并启动Nginx服务。
systemctl start nginx.service
回显类似如下信息:[root@ecs-ha1 ~]# systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. [root@ecs-ha1 ~]# systemctl start nginx.service
- 打开浏览器,并输入EIP地址(124.X.X.187),验证Nginx单节点的访问情况。
- 执行以下操作,修改Keepalived配置文件。
- 执行以下命令,打开“/etc/keepalived/keepalived.conf”文件。
vim /etc/keepalived/keepalived.conf
- 按i进入编辑模式。
- 根据实际情况,替换配置文件中的IP参数,并将文件中原有的内容,全部替换成以下内容。
- mcast_src_ip和unicast_src_ip:替换为ECS-HA1的私有IP地址,本示例为192.168.0.195。
- virtual_ipaddress:替换为虚拟IP地址,本示例为192.168.0.177。
! 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.195 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } unicast_src_ip 192.168.0.195 virtual_ipaddress { 192.168.0.177 } track_script { chk_http_port } }
- 按ESC退出,并输入:wq!保存配置。
- 执行以下命令,打开“/etc/keepalived/keepalived.conf”文件。
- 执行以下操作,配置Nginx监控脚本。
- 执行以下命令,打开“/etc/keepalived/chk_nginx.sh”文件。
vim /etc/keepalived/chk_nginx.sh
- 按i进入编辑模式。
- 将文件中原有的内容,全部替换成以下内容。
#!/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
- 按ESC退出,并输入:wq!保存配置。
- 执行以下命令,打开“/etc/keepalived/chk_nginx.sh”文件。
- 执行以下命令,为“chk_nginx.sh”文件添加执行权限。
chmod +x /etc/keepalived/chk_nginx.sh
- 执行以下命令,设置Keepalived服务开机自启动,并启动Keepalived服务。
systemctl start keepalived.service
- 将EIP和ECS-HA1解绑定。
具体方法请参见解绑弹性公网IP。
- 将EIP绑定至ECS-HA1。
- 执行以下操作,为ECS-HA2配置Keepalived。
- 将EIP绑定至ECS-HA2。
具体方法请参见绑定弹性公网IP。
- 远程登录ECS-HA2。
ECS有多种登录方法,具体请参见登录弹性云服务器。
- 执行以下命令,安装Nginx、Keepalived软件包及相关依赖包。
yum install nginx keepalived -y
回显类似如下信息,表示安装完成。[root@ecs-ha2 ~]# yum install nginx keepalived -y Loaded plugins: fastestmirror Determining fastest mirrors base | 3.6 kB 00:00:00 epel | 4.3 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/7): epel/x86_64/group | 399 kB 00:00:00 (2/7): epel/x86_64/updateinfo | 1.0 MB 00:00:00 (3/7): base/7/x86_64/primary_db | 6.1 MB 00:00:00 (4/7): base/7/x86_64/group_gz | 153 kB 00:00:00 (5/7): epel/x86_64/primary_db | 8.7 MB 00:00:00 (6/7): extras/7/x86_64/primary_db | 253 kB 00:00:00 (7/7): updates/7/x86_64/primary_db ..... Dependency Installed: centos-indexhtml.noarch 0:7-9.el7.centos gperftools-libs.x86_64 0:2.6.1-1.el7 lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7_9.1 net-snmp-agent-libs.x86_64 1:5.7.2-49.el7_9.4 net-snmp-libs.x86_64 1:5.7.2-49.el7_9.4 nginx-filesystem.noarch 1:1.20.1-10.el7 openssl11-libs.x86_64 1:1.1.1k-7.el7 Complete!
- 执行以下操作,修改Nginx配置文件,添加80端口相关配置。
- 执行以下命令,打开“/etc/nginx/nginx.conf”文件。
vim /etc/nginx/nginx.conf
- 按i进入编辑模式。
- 将文件中原有的内容,全部替换成以下内容。
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; } } }
- 按ESC退出,并输入:wq!保存配置。
- 执行以下命令,打开“/etc/nginx/nginx.conf”文件。
- 执行以下操作,修改index.html文件内容,用来验证网站的访问情况。
- 执行以下命令,打开“/usr/share/nginx/html/index.html”文件。
vim /usr/share/nginx/html/index.html
- 按i进入编辑模式。
- 将文件中原有的内容,全部替换成以下内容。
Welcome to ECS-HA2
- 按ESC退出,并输入:wq!保存配置。
- 执行以下命令,打开“/usr/share/nginx/html/index.html”文件。
- 执行以下命令,设置Nginx服务开机自启动,并启动Nginx服务。
systemctl start nginx.service
回显类似如下信息:[root@ecs-ha2 ~]# systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. [root@ecs-ha2 ~]# systemctl start nginx.service
- 打开浏览器,并输入EIP地址(124.X.X.187),验证Nginx单节点的访问情况。
- 执行以下操作,修改Keepalived配置文件。
- 执行以下命令,打开“/etc/keepalived/keepalived.conf”文件。
vim /etc/keepalived/keepalived.conf
- 按i进入编辑模式。
- 根据实际情况,替换配置文件中的IP参数,并将文件中原有的内容,全部替换成以下内容。
- mcast_src_ip和unicast_src_ip:替换为ECS-HA2的私有IP地址,本示例为192.168.0.233。
- virtual_ipaddress:替换为虚拟IP地址,本示例为192.168.0.177。
! 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.233 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } unicast_src_ip 192.168.0.233 virtual_ipaddress { 192.168.0.177 } track_script { chk_http_port } }
- 按ESC退出,并输入:wq!保存配置。
- 执行以下命令,打开“/etc/keepalived/keepalived.conf”文件。
- 执行以下操作,配置Nginx监控脚本。
- 执行以下命令,打开“/etc/keepalived/chk_nginx.sh”文件。
vim /etc/keepalived/chk_nginx.sh
- 按i进入编辑模式。
- 将文件中原有的内容,全部替换成以下内容。
#!/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
- 按ESC退出,并输入:wq!保存配置。
- 执行以下命令,打开“/etc/keepalived/chk_nginx.sh”文件。
- 执行以下命令,为“chk_nginx.sh”文件添加执行权限。
chmod +x /etc/keepalived/chk_nginx.sh
- 执行以下命令,设置Keepalived服务开机自启动,并启动Keepalived服务。
systemctl start keepalived.service
- 将EIP和ECS-HA2解绑定。
具体方法请参见解绑弹性公网IP。
- 将EIP绑定至ECS-HA2。