1.环境
准备4台机器
ip地址:192.168.5.135(note01 主)
ip地址:192.168.5.136(note02 备)
ip地址:192.168.5.137(note03 nginx)
ip地址:192.168.5.138(note04 nginx)
2.主机安装环境192.168.5.135(note01 主)
2.主机安装环境192.168.5.135(note01 主)
(1)安装阿里云源
家里网络,这里为了方便安装,配置一下阿里云的yum源和epel源
#yum install -y wget
#wget http://mirrors.aliyun.com/repo/Centos-7.repo #下载阿里的云源
#wget http://mirrors.aliyun.com/repo/epel-7.repo #下载阿里云的源
#cd /etc/yum.repos.d/ #替换源
# ll
# mkdir repo_bak #系统自带的源,备份一下
# mv *.repo repo_bak/
# mv /root/*.repo ./ #移入阿里源
# ll
(2).安装keppalived
#yum -y install keepalived
2.配置
#vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { router_id note01 #节点标识字条串,通常为hostname } #配置 nginx 状态检测 vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" #脚本存放路径 interval 2 #检测时间间隔 weight 2 #配置节点权重 } ## 定义虚拟路由, VI_1 为虚拟路由的标示符 vrrp_instance VI_1 { state MASTER # 主节点为 MASTER, 对应的备份节点为 BACKUP interface ens33 #绑定虚拟 IP 的网络接口,与本机 IP 地址接口相同 virtual_router_id 51 #虚拟路由的 ID 号, 两个节点设置必须一样 mcast_src_ip 192.168.5.135 #本机 IP 地址 priority 100 # 节点优先级,值范围 0-254,MASTER要比BACKUP高 nopreempt #配置MASTER异常恢复后再次抢占 advert_int 1 # 组播信息发送间隔,两个节点设置必须一样, 默认 1s #设置验证信息,两个节点必须一致 authentication { auth_type PASS auth_pass 1111 #自定义配置 } #将 track_script 块加入 instance 配置块 track_script { chk_nginx #执行Nginx状态检测的服务 } # 设置虚拟 IP 池, 两个节点设置必须一样 virtual_ipaddress { 192.168.5.100 #虚拟 ip,可以定义多个 } } #vim /etc/keepalived/check_nginx.sh #!/bin/bash a=$(ps -C nginx --no-header | wc -l) if [ $a -eq 0 ];then systemctl restart nginx if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then systemctl stop keepalived fi fi |
#chmod 755 /etc/keepalived/check_nginx.sh
(3).安装nginx
#yum -y install nginx #vim /etc/nginx/nginx.conf
在原配置文件上增加代码:
#server外 upstream webs { #添加地址池 server 192.168.5.137:80; server 192.168.5.138:80; } #server内 location / { proxy_pass http://webs/; #进行匹配 }
完整的nginx.conf
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { 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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; upstream webs { #添加地址池 server 192.168.5.137:80; server 192.168.5.138:80; } server { listen 80; listen [::]:80; server_name 192.168.5.100; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { proxy_pass http://webs/; #进行匹配 } error_page 404 /404.html; location = /404.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } # Settings for a TLS enabled server. # # server { # listen 443 ssl http2; # listen [::]:443 ssl http2; # server_name _; # root /usr/share/nginx/html; # # ssl_certificate "/etc/pki/nginx/server.crt"; # ssl_certificate_key "/etc/pki/nginx/private/server.key"; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # # # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; # # error_page 404 /404.html; # location = /40x.html { # } # # error_page 500 502 503 504 /50x.html; # location = /50x.html { # } # } } |
退出保存
重启nginx
#systemctl restart nginx #systemctl enable nginx #systemctl restart keepalived #systemctl enable keepalived #sudo iptables -F #关下防火墙
3.备机安装环境192.168.5.136(note06 备)
(1)执行2的步骤
(2)keepalived配置修改
router_id note02
state BACKUP
mcast_src_ip 192.168.5.136
priority 50
4.分别在web服务器安装nignx服务
(1)ip地址:192.168.5.137(note03 nginx)
#yum -y install nginx
配置nginx.conf
#vim /etc/nginx/nginx.conf
只改下这行:server_name 192.168.5.137;
完整的
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { 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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 80; listen [::]:80; server_name 192.168.5.137; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; error_page 404 /404.html; location = /404.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } # Settings for a TLS enabled server. # # server { # listen 443 ssl http2; # listen [::]:443 ssl http2; # server_name _; # root /usr/share/nginx/html; # # ssl_certificate "/etc/pki/nginx/server.crt"; # ssl_certificate_key "/etc/pki/nginx/private/server.key"; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # # # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; # # error_page 404 /404.html; # location = /40x.html { # } # # error_page 500 502 503 504 /50x.html; # location = /50x.html { # } # } } |
#sudo iptables -F #关下防火墙
#service nginx restart
浏览器访问:http://192.168.5.137
修改下index.html内容
#vim /usr/share/nginx/html/index.html
this is note03 web server 192.168.5.137
(2)ip地址:192.168.5.138(note04 nginx)
#yum -y install nginx
配置nginx.conf
#vim /etc/nginx/nginx.conf
只改下这行:server_name 192.168.5.138;
#sudo iptables -F #关下防火墙
#service nginx restart
浏览器访问:http://192.168.5.138
#vim /usr/share/nginx/html/index.html
this is note04 web server 192.168.5.138
(3)检验结果:访问http://192.168.5.100/
把主机停掉再试
访问正常
5.如果自动ip分配变化,可以设置静态ip
1.编辑网卡
#ip addr
# cd /etc/sysconfig/network-scripts/
#vim ifcfg-ens33
或者
#vim /etc/sysconfig/network-scripts/ifcfg-ens33
各项说明:
TYPE=Ethernet # 网卡类型:为以太网 PROXY_METHOD=none # 代理方式:关闭状态 BROWSER_ONLY=no # 只是浏览器:否 BOOTPROTO=dhcp # 网卡的引导协议:DHCP[中文名称: 动态主机配置协议] DEFROUTE=yes # 默认路由:是, 不明白的可以百度关键词 `默认路由` IPV4_FAILURE_FATAL=no # 是不开启IPV4致命错误检测:否 IPV6INIT=yes # IPV6是否自动初始化: 是[不会有任何影响, 现在还没用到IPV6] IPV6_AUTOCONF=yes # IPV6是否自动配置:是[不会有任何影响, 现在还没用到IPV6] IPV6_DEFROUTE=yes # IPV6是否可以为默认路由:是[不会有任何影响, 现在还没用到IPV6] IPV6_FAILURE_FATAL=no # 是不开启IPV6致命错误检测:否 IPV6_ADDR_GEN_MODE=stable-privacy # IPV6地址生成模型:stable-privacy [这只一种生成IPV6的策略] NAME=ens33 # 网卡物理设备名称 UUID=f47bde51-fa78-4f79-b68f-d5dd90cfc698 # 通用唯一识别码, 每一个网卡都会有, 不能重复, 否两台linux只有一台网卡可用 DEVICE=ens33 # 网卡设备名称, 必须和 `NAME` 值一样 ONBOOT=yes # 是否开机启动, 要想网卡开机就启动或通过 `systemctl restart network`控制网卡,必须设置为 `yes`
这里我们修改配置:
TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no #BOOTPROTO=dhcp DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=c500a478-a621-42d4-b50e-1bd710003939 DEVICE=ens33 #ONBOOT=no IPV6_PRIVACY=no #static assignment NM_CONTROLLED=no #表示该接口将通过该配置文件进行设置,而不是通过网络管理器进行管理 ONBOOT=yes #开机启动 BOOTPROTO=static #静态IP IPADDR=192.168.5.138 #本机地址 NETMASK=255.255.255.0 #子网掩码 GATEWAY=192.168.5.2 #默认网关 DNS1=10.14.8.154 DNS2=10.14.8.155
保存退出
#systemctl restart network
#ping www.baidu.com
如果能ping通ip,但是ping 不同baidu.com,应该是dns的问题,查看电脑的dns
在配置文件上面增加两行
DNS1=10.14.8.154
DNS2=10.14.8.155
到这里负载均衡实验完成。