Keepaliev+Nginx+http
admin
2023-03-23 20:00:43
0

 

  Ngxin作为一个强大的开源软件是可以先做为高可用集群服务的,这篇博文就介绍一下nginx+Keepalived是如何实现高性能+高可用集群服务的

    Keepaliev+Nginx+http


环境介绍:

   硬件:    4台虚拟服务器

   系统:CentOS 7.3

   软件:KeepalivedApacheNginx

IP及主机名

   Master

       主机名:shiyan1

       IP地址:172.18.17.31

   Backup

       主机名:shiyan2

       IP地址:172.18.17.32

   Httpd1

       主机名:shiyan3

       IP地址:172.18.17.33

   Httpd2

       主机名:shiyan4

       IP地址:172.18.17.34

 

四台服务器初始化配置(四台服务器相同的配置)

   关闭防火墙

     

    [root@shiyan~ ]# systemctl disable firewalld
    [root@shiyan~ ]# systemctl stop firewalld
    [root@shiyan~ ]# iptables –F

   

  关闭Selinux

    [root@shiyan~ ]# vim /etc/selinux/config
       SELINUX=disabled
     #保存重启系统生效


   安装软件

     Master/Backup

     

    [root@shiyan~ ]# yum install keepalived httpd nginx   #(Nginx需要单独配置EPEL源)

     Httpd1/Httpd2

      

        [root@shiyan~ ]# yum install httpd


Httpd1配置

    [root@shiyan3 ~ ]# mkdir -p /app/apache/html/  
    [root@shiyan3 ~ ]# chown -R apache.apache /app/apache/html/
    [root@shiyan3 ~ ]# echo "Apache Server 1" > /app/apache/html/index.html
    [root@shiyan3 ~ ]# vim /etc/httpd/conf/httpd.conf 
          #此处是更改httpd.conf中的内容,并非添加内容
       DocumentRoot "/app/apache/html"    
       #更改为自定义的路径
            
       #
       # Relax access to content within /var/www.
       #
           #更改为自定义的路径
       AllowOverride None
       # Allow open access:
       Require all granted
       
            
       # Further relax access to the default document root:
            #更改为自定义的路径.
     [root@shiyan3 ~ ]# systemctl restart httpd
     
     #测试网站是否正常运行
     [root@yum ~ ]# curl http://172.18.17.33
            Apache Server 1  #测试成功


Httpd2配置

    [root@shiyan4 ~ ]# mkdir -p /app/apache/html/  
    [root@shiyan4 ~ ]# chown -R apache.apache /app/apache/html/
    [root@shiyan4 ~ ]# echo "Apache Server 2" > /app/apache/html/index.html
    [root@shiyan4 ~ ]# vim /etc/httpd/conf/httpd.conf 
          #此处是更改httpd.conf中的内容,并非添加内容
       DocumentRoot "/app/apache/html"    
       #更改为自定义的路径
            
       #
       # Relax access to content within /var/www.
       #
           #更改为自定义的路径
       AllowOverride None
       # Allow open access:
       Require all granted
       
            
       # Further relax access to the default document root:
            #更改为自定义的路径.
    [root@shiyan4 ~ ]# systemctl restart httpd
          #测试网站是否正常运行
        [root@yum ~ ]# curl http://172.18.17.34
            Apache Server 2  #测试成功


Master配置

   配置Sorry-Server
    [root@shiyan1 ~ ]# mkdir -p /app/apache/html/  
    [root@shiyan1 ~ ]# chown -R apache.apache /app/apache/html/
    [root@shiyan1 ~ ]# echo "

Sorry Server 1

" > /app/apache/html/index.html     [root@shiyan1 ~ ]# vim /etc/httpd/conf/httpd.conf         #此处是更改httpd.conf中的内容,并非添加内容        Listen 8080        DocumentRoot "/app/apache/html"            #更改为自定义的路径                     #        # Relax access to content within /var/www.        #            #更改为自定义的路径        AllowOverride None        # Allow open access:        Require all granted                             # Further relax access to the default document root:             #更改为自定义的路径.     [root@shiyan1 ~ ]# systemctl restart http                #测试网站是否正常运行         [root@yum ~ ]# curl http://172.18.17.31:8080               

Sorry Server 1

  #测试成功       配置Keepalived            [root@shiyan1 ~ ]# cp /etc/keepalived/keepalived.conf{,.bak}    #备份文件            [root@shiyan1 ~ ]# vim /etc/keepalived/keepalived.conf               global_defs {                notification_email {                   root   #定义收邮件的用户                }                notification_email_from Alexandre.Cassen@firewall.loc                smtp_server 172.18.17.31   #定义邮件地址                smtp_connect_timeout 30                router_id node1            #定义节点名称             }                          vrrp_instance VI_1 {                 state MASTER              #定义节点为主节点模式                 interface ens33           #定义使用ens33为VIP网卡                 virtual_router_id 51      #定义节点编号                 priority 150              #定义优先级                 advert_int 1                    authentication {                     auth_type PASS                     auth_pass 1111                 }                 virtual_ipaddress {                     172.18.17.30          #定义VIP                 }             } ~      配置Nginx服务             [root@shiyan1 ~ ]# vim /etc/nginx/nginx.conf                 #添加nginx集群                 upstream websrvs {                     server 172.18.17.33:80;                     server 172.18.17.34:80;                     server 127.0.0.1:8080 backup;                 }                 #server 部分的内容需要全部注释掉                              [root@shiyan1 ~ ]# vim /etc/nginx/conf.d/default.conf                      server {                     listen       80;                     location / {                         root  html;                         proxy_pass http://websrvs;                         index  index.html index.htm;                     }                 }             [root@shiyan1 ~ ]# systemctl restart nginx             [root@shiyan1 ~ ]# systemctl restart keepalived             [root@shiyan1 ~ ]# systemctl restart httpd


Backup配置

   配置Sorry-Server
    [root@shiyan2 ~ ]# mkdir -p /app/apache/html/  
    [root@shiyan2 ~ ]# chown -R apache.apache /app/apache/html/
    [root@shiyan2 ~ ]# echo "

Sorry Server 2

" > /app/apache/html/index.html     [root@shiyan2 ~ ]# vim /etc/httpd/conf/httpd.conf         #此处是更改httpd.conf中的内容,并非添加内容        Listen 8080        DocumentRoot "/app/apache/html"            #更改为自定义的路径                     #        # Relax access to content within /var/www.        #            #更改为自定义的路径        AllowOverride None        # Allow open access:        Require all granted                             # Further relax access to the default document root:             #更改为自定义的路径.     [root@shiyan2 ~ ]# systemctl restart http                #测试网站是否正常运行         [root@yum ~ ]# curl http://172.18.17.31:8080               

Sorry Server 2

  #测试成功    配置Keepalived      [root@shiyan2 ~ ]# cp /etc/keepalived/keepalived.conf{,.bak}    #备份文件      [root@shiyan2 ~ ]# vim /etc/keepalived/keepalived.conf               global_defs {                notification_email {                   root   #定义收邮件的用户                }                notification_email_from Alexandre.Cassen@firewall.loc                smtp_server 172.18.17.31   #定义邮件地址                smtp_connect_timeout 30                router_id node1            #定义节点名称             }                          vrrp_instance VI_1 {                 state MASTER              #定义节点为主节点模式                 interface ens33           #定义使用ens33为VIP网卡                 virtual_router_id 51      #定义节点编号                 priority 150              #定义优先级                 advert_int 1                    authentication {                     auth_type PASS                     auth_pass 1111                 }                 virtual_ipaddress {                     172.18.17.30          #定义VIP                 }             } ~  配置Nginx服务       [root@shiyan2 ~ ]# vim /etc/nginx/nginx.conf          #添加nginx集群           upstream websrvs {            server 172.18.17.33:80;            server 172.18.17.34:80;            server 127.0.0.1:8080 backup;            }            #server 部分的内容需要全部注释掉                            [root@shiyan2 ~ ]# vim /etc/nginx/conf.d/default.conf             server {            listen       80;            location / {            root  html;            proxy_pass http://websrvs;            index  index.html index.htm;            }            }        [root@shiyan2 ~ ]# systemctl restart keepalived        [root@shiyan2 ~ ]# systemctl restart nginx        [root@shiyan2 ~ ]# systemctl restart httpd


测试环境    

 #默认使用rr算法依次轮询访问后端httpd服务器
    [root@yum ~ ]# curl http://172.18.17.30    
    Apache Server 1
    [root@yum ~ ]# curl http://172.18.17.30
    Apache Server 2
 
 #关闭后端http1服务,这样只能访问httpd2的服务
     [root@yum ~ ]# curl http://172.18.17.30
      Apache Server 2
     [root@yum ~ ]# curl http://172.18.17.30
      Apache Server 2
      
 #关闭两台后端主机的httpd服务,这样因为没有后端服务器所以Master的sorry-server提供服务
    [root@yum ~ ]# curl http://172.18.17.30
    

Sorry Server 1

 #关闭Master测试     [root@yum ~ ]# curl http://172.18.17.30     

Sorry Server 2



相关内容

热门资讯

女子出租屋凌晨疑遭陌生人闯入,... 极目新闻记者 郭奕据红星新闻报道,居住在杭州的女子小鱼(化名)在社交媒体上发布了一段拍摄于4月28日...
唐山市委书记调整 澎湃新闻记者 岳怀让据河北卫视《河北新闻联播》消息,河北省委常委常斌已任唐山市委书记。公开资料显示,...
视频丨开工忙、消费旺、出口强 ... 国家发展改革委国家信息中心今天发布4月份经济各领域先行指标,显示出经济稳步向好的大趋势。
喷了光触媒的物品在车里可以除甲... 最佳回答 可以的,效果挺好的。光催化剂中的催化剂可以氧化分解各种有机污染物和无机污染物,在空气中与...
家庭装修走暗线还是明线好? 个人认为装修的话选择暗线好些 1、由于走暗线带来的美观效果非常强,因此成为大多数人的首选...
朗逸钥匙装饰盖怎么取下 将遥控钥匙有标志的一面向上,用扁的小起子撬进缝隙的中部,钥匙会分开一个缝,按中缝将钥匙分开即可。既然...
不平整的墙面怎么装饰 不平等的墙可以请专门的画腻子画的大师去进行创作。现在很多的墙面,人家都用刮腻子的方式去做壁画。像那种...
明线安装和暗线安装应该怎么选择... 随着社会发展,人们生活水平提高,大部分农村家庭都能盖上一层或者几层楼房,楼层盖好后接下来就是装修的问...
“华盛顿”号航母驶离日本横须贺... △“乔治·华盛顿”号核动力航空母舰(资料图)当地时间10日上午,美国“乔治·华盛顿”号核动力航母完成...
司机因操作不当引发车祸致2死6... 极目新闻记者 谢茂5月10日13时40分许,重庆黔江区一处十字路口发生一起交通事故。据重庆市公安局黔...