華為雲:
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
> 使用配置文件創建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
[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;
}
}
