目錄
一、準(zhǔn)備
二、在作為haproxy的服務(wù)器上導(dǎo)入以下shell執(zhí)行haproxy安裝
三、由于nginx服務(wù)需要用的nfs共享目錄,先部署nfs
四、nginx服務(wù)器1部署
五、nginx服務(wù)器2部署同上
六、測試
一、準(zhǔn)備
四臺服務(wù)器
系統(tǒng) | IP | 搭建服務(wù)器 |
centos7 | 192.168.1.12 | haproxy |
centos7 | 192.168.1.132 | nfs(rpcbind) |
centos7 | 192.168.1.133 | nginx |
centos7 | 192.168.1.134 | nginx |
四臺服務(wù)器關(guān)閉防火墻和selinux
[root@localhost ~] systemctl stop firewalld.service #關(guān)閉防火墻
[root@localhost ~] systemctl disable firewalld.service #防火墻開機(jī)不自啟
[root@localhost ~] sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux
#將selinux的配置文件改為不啟動
[root@localhost ~] reboot #重啟服務(wù)器,修改selinux配置后重啟生效
二、在作為haproxy的服務(wù)器上導(dǎo)入以下shell執(zhí)行haproxy安裝
[root@localhost ~] vim haproxy_install.sh
#!/bin/bash
#安裝haproxy修改配置文件并添加兩臺web
yum install haproxy -y #yum安裝haproxy
echo 請輸入nginx服務(wù)器1ip
read a #彈出輸入框并將輸入內(nèi)容作為變量a
echo 請輸入nginx服務(wù)器2ip
read b #彈出輸入框并將輸入內(nèi)容作為變量a
sed -i "82s/127.0.0.1:5001/$a:80/" /etc/haproxy/haproxy.cfg #替換haproxy的配置文件82行為nginx1的ip
sed -i "83s/127.0.0.1:5002/$b:80/" /etc/haproxy/haproxy.cfg #替換haproxy的配置文件83行為nginx2的ip
sed -i '63s/5000/80/' /etc/haproxy/haproxy.cfg #將haproxy的默認(rèn)5000端口改為80端口
echo "listen admin_stats" >> /etc/haproxy/haproxy.cfg #設(shè)置haproxy的web管理用戶
echo "stats enable" >> /etc/haproxy/haproxy.cfg #開啟haproxy程序web服務(wù)
echo "bind *:8080" >> /etc/haproxy/haproxy.cfg #haproxy管理頁面端口為8080
echo "mode http" >> /etc/haproxy/haproxy.cfg #以下為haproxy系統(tǒng)配置
echo "option httplog" >> /etc/haproxy/haproxy.cfg
echo "log global" >> /etc/haproxy/haproxy.cfg
echo "maxconn 10" >> /etc/haproxy/haproxy.cfg
echo "stats refresh 30s" >> /etc/haproxy/haproxy.cfg
echo "stats uri /admin" >> /etc/haproxy/haproxy.cfg
echo "stats realm haproxy" >> /etc/haproxy/haproxy.cfg
echo "stats auth admin:admin" >> /etc/haproxy/haproxy.cfg
echo "stats hide-version" >> /etc/haproxy/haproxy.cfg
echo "stats admin if TRUE" >> /etc/haproxy/haproxy.cfg #以上為haproxy系統(tǒng)配置
systemctl start haproxy.service #開啟haproxy程序
c=$(ip a | grep "inet "|grep ens33| awk '{print $2}'|awk -F/ '{print $1}') #變量c等于本機(jī)ip
echo haproxy部署完成
echo 訪問$c將自動輪詢$a和$b的web頁面
echo 訪問$c:8080/admin為haproxy程序的管理頁面
echo 管理頁面登錄賬戶為admin密碼為admin
#執(zhí)行結(jié)果如下,在彈出的提示語后分別輸入兩臺nginx服務(wù)器ip地址(根據(jù)實際環(huán)境輸入)
[root@localhost ~] chmod 755 haproxy_install.sh #添加執(zhí)行權(quán)限
[root@localhost ~] ./haproxy_install.sh #執(zhí)行腳本
完畢!
請輸入nginx服務(wù)器1ip
192.168.1.133
請輸入nginx服務(wù)器2ip
192.168.1.134
haproxy部署完成
訪問192.168.1.12將自動輪詢192.168.1.133和192.168.1.134的web頁面
訪問192.168.1.12:8080/admin為haproxy程序的管理頁面
管理頁面登錄賬戶為admin密碼為admin
[root@localhost ~] systemctl status haproxy.service #查看狀態(tài)
Active: active (running) since 五 2023-06-16 14:22:43 CST; 15min ago #運(yùn)行中
三、由于nginx服務(wù)需要用的nfs共享目錄,先部署nfs
[root@localhost ~] vim nfs_install.sh
#!/bin/bash
#function:安裝nfs 創(chuàng)建/app/file作為共享文件
yum install nfs-utils rpcbind -y #yum安裝nfs和rpc服務(wù)
touch /etc/exports #創(chuàng)建nfc的配置文件
mkdir -p /app/file #創(chuàng)建nfc共享文件目錄
chown -R nfsnobody.nfsnobody /app/file/ #賦予共享目錄nfs權(quán)限
echo "/app/file *(rw,sync)" >> /etc/exports #允許所有IP訪問nfs共享目錄并有可讀寫權(quán)限
exportfs –rv #載入配置
systemctl enable nfs #開機(jī)自啟動nfs
systemctl enable rpcbind #開機(jī)自啟動rps
systemctl start nfs #啟動nfs程序
systemctl start rpcbind #啟動rps程序
touch /app/file/index.html #在共享目錄下創(chuàng)建網(wǎng)頁文件
echo "<h1>skl6666666</h1>" >> /app/file/index.html #在網(wǎng)頁文件中寫入要顯示的數(shù)據(jù)
echo nfs服務(wù)部署完成
[root@localhost ~] chmod 755 nfs_install.sh
[root@localhost ~] ./nfs_install.sh
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
nfs服務(wù)部署完成
[root@localhost ~] systemctl status nfs
四、nginx服務(wù)器1部署
[root@localhost ~] vim nginx_install.sh
# Centos7一鍵安裝nginux
yum -y install gcc gcc-c++ autoconf automake libtool make openssl openssl-devel pcre pcre-devel #安裝nginx所需環(huán)境
cd /usr/local/src/ #切換到安裝目錄
wget http://nginx.org/download/nginx-1.8.1.tar.gz #下載nginx到當(dāng)前目錄
tar -zxvf nginx-1.8.1.tar.gz #解壓nginx安裝包
cd nginx-1.8.1 #進(jìn)入解壓后目錄
./configure \--prefix=/usr/local/nginx \--with-http_ssl_module \--with-http_flv_module \--with-http_stub_status_module \--with-http_gzip_static_module \--with-pcre #編譯文件
make && make install #編譯安裝
cd /usr/local/nginx #進(jìn)入nginx安裝目錄
sed -i '14s/nginx/BDQN/' /usr/local/nginx/html/index.html #方便測試區(qū)分將網(wǎng)頁中nginx改為BDQN
/usr/local/nginx/sbin/nginx #啟動nginx服務(wù)
echo nginx部署完成,請測試web頁面 #屏幕輸出內(nèi)容提醒用戶
echo 接下來將部署nfs并映射html,請確認(rèn)是否繼續(xù) #屏幕輸出內(nèi)容提醒用戶
echo 鍵入任意值將繼續(xù)操作,如需中斷請按Ctrl+c #屏幕輸出內(nèi)容提醒用戶
read a #彈出框暫停執(zhí)行,用戶鍵入任意值繼續(xù)執(zhí)行,鍵入值作為變量a但后續(xù)并不調(diào)用這個變量
yum install nfs-utils rpcbind -y #安裝nfs和rpc服務(wù)
systemctl enable nfs #開機(jī)自啟動nfs服務(wù)
systemctl enable rpcbind #開機(jī)自啟動rps服務(wù)
systemctl start nfs #啟動nfs服務(wù)
systemctl start rpcbind #啟動rpc服務(wù)
echo nfs服務(wù)安裝完成,接下來將進(jìn)行掛載操作 #屏幕輸出內(nèi)容提醒用戶
echo 鍵入任意值將繼續(xù)操作,如需中斷請按Ctrl+c #屏幕輸出內(nèi)容提醒用戶
echo 請輸入nfs服務(wù)器ip #屏幕輸出內(nèi)容提醒用戶
read b #將鍵入值作為變量b
mount -t nfs $b:/app/file /usr/local/nginx/html/ #掛載nfs上的目錄到nginx的html目錄下
[root@localhost ~] chomd 755 nginx_install.sh
[root@localhost ~] ./nginx_install.sh
請輸入nfs服務(wù)器ip
192.168.1.132
[root@localhost ~] df -h
192.168.1.132:/app/file 17G 1.7G 16G 10% /usr/local/nginx/html #成功掛載
五、nginx服務(wù)器2部署同上
六、測試
關(guān)閉nginx 1?
?啟動中
?關(guān)閉掉
?發(fā)現(xiàn)還能登錄192.168.1.133
?關(guān)閉nginx 2
測試完成
?文章來源:http://www.zghlxwxcb.cn/news/detail-492194.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-492194.html
到了這里,關(guān)于haproxy服務(wù)器對nginx服務(wù)器web服務(wù)調(diào)度負(fù)載均衡、用nfs做共享目錄(腳本部署)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!