华为云:实现高可用的负载均衡web集群

华为云:

  2台云主机做负载均衡调度

    》》申请一个虚拟浮动ip,并绑定一个弹性公网ip

    》》将两台云主机绑定到虚拟浮动ip上

  3台web服务器

  1台云服务器做jumpserver(跳板机,用于批量管理)

  

  #购买增强型负载均衡器(配置监听器、后端服务器组)

跳板机

  下载nginx源码包并打包成rpm,更新自建yum仓库

    > 安装rpm-build            

[root@jumpserver ~]# yum -y install rpm-build

    > 生成rpmbuild目录结构

[root@jumpserver ~]# rpmbuild -ba nginx.spec

    > 下载源码到rpmbuild目录下的SOURCES子目录下

[root@jumpserver ~]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@jumpserver ~]# mv /root/nginx-1.12.2.tar.gz /root/rpmbuild/SOURCES/

    > 创建并修改SPEC配置文件

name: nginx    
Version: 1.12.2      
Release: 10
Summary: Nginx is a web server software.    

#Group:        
License: GRL    
URL: www.cloud.com
Source0: nginx-1.12.2.tar.gz    

#BuildRequires:    
#Requires:    

%description
this is a nginx...

%post
useradd nginx

%prep
%setup -q


%build
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
make %{?_smp_mflags}


%install
make install DESTDIR=%{buildroot}


%files
%doc
/usr/local/nginx/*



%changelog
View Code

    >  使用配置文件创建RPM包

 [root@jumpserver ~]# yum -y install gcc pcre-devel openssl-devel            # 安装依赖

[root@jumpserver ~]#  rpmbuild -ba /root/rpmbuild/SPECS/nginx.spec

 [root@jumpserver ~]# rpm -qpi /root/rpmbuild/RPMS/x86_64/nginx-1.12.2-10.x86_64.rpm         # 测试

    > 将rpm包拷贝到自建的yum 仓库下并更新yum源

[root@jumpserver ~]# cp /root/rpmbuild/RPMS/x86_64/nginx-1.12.2-10.x86_64.rpm /var/ftp/local_repo/
[root@jumpserver local_repo]# createrepo --update .

    > 批量安装nginx

[root@jumpserver ~]# ansible web -m yum -a 'name=nginx'

    > 配置/etc/systemd/system/nginx.service,使nginx支持systemctl控制,并批量下发到所有主机

[Unit]
Description=nginx
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=-/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStio=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
View Code
[root@jumpserver ~]# ansible web -m copy -a 'src=/root/nginx.service dest=/etc/systemd/system/nginx.service'

    > 批量启动nginx服务

[root@jumpserver ~]#  ansible web -m service  -a 'name=nginx state=started enabled=yes'
[root@jumpserver ~]#  ansible web -m shell  -a 'ss -ltnup | grep *:80'

调度器(2台)

  >  安装 keepalived

yum -y install keepalived

  > 修改配置文件 vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from root@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id web-0001
}

vrrp_instance VI_1 {
    state BACKUP               # 另外一台配置为MASTER
    interface eth0
    virtual_router_id 50      # 两台调度器的值相同
    priority 50                        # 优先级,设置不同值
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111        # 密码 必须相同
    }
    virtual_ipaddress {
        192.168.1.100                           # vip 
    }
}

  > 重启服务

systemctl restart keepalived

  > 配置nginx调度  vim /usr/local/nginx/conf/nginx.conf

http {
...
    # web集群
    upstream webserver {
                server 192.168.1.13:80;
                server 192.168.1.14:80;
                server 192.168.1.15:80;
        }


    server {
        listen       80;
        server_name  localhost;
            location / {
            proxy_pass http://webserver;
        }

...
}

web集群

  > 配置 vim /usr/local/nginx/conf/nginx.conf     #  ansible  批量部署 nginx配置文件,方便排错


server { listen 80;
    server_name localhost;
  add_header 'Cluster- id ' {{ansible_hostname}}';
    location / 
      {
        proxy_pass http://webserver;
      }
  }

原文地址:https://www.cnblogs.com/ray-mmss/p/10479131.html