lnmap實戰之負載均衡架構(無高可用)
架構圖如下:
此次實戰軟件,全部yum安裝
1.準備好機器,同步好時間
192.168.42.150 node1 [負載均衡器]
192.168.42.152 node3 [web2]
192.168.42.153 node4 [web1]
192.168.42.151 node2 [memcached session存儲]
192.168.42.154 node5 [nfs 共享存儲]
192.168.42.155 node6 [mariadb]
我們這次實戰從后面的節點開始吧
2.在node6節點上安裝mariadb
安裝mariadb
yum install mariadb-server -y
更改基本配置
vim /etc/my.cnf.d/server.cnf [mysqld] skip_name_resolve=1 log-bin=mysql-bin innodb_file_per_table = 1
啟動mariadb
systemctl start mariadb
更改密碼
mysqladmin -uroot -p password "root"
登錄mysql
mysql -u root -p
添加外部訪問賬號
MariaDB [(none)]> grant all privileges on *.* to 'magedu'@'192.168.42.%' identified by '123456'; MariaDB [(none)]> flush privileges;
登錄測試一下
mysql -u magedu -h 192.168.42.155 -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 7 Server version: 5.5.52-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
3.node5 安裝nfs
安裝nfs
yum install nfs-utils rpcbind -y
添加用戶(用戶id,組id來自node3,node4的apache用戶,我們需要統一)
[root@node5 ~] groupadd -g 48 apache [root@node5 ~] useradd -r -g 48 -u 48 apache [root@node5 ~] id apache uid=48(apache) gid=48(apache) groups=48(apache)
創建共享目錄
mkdir -p /data chown -R apache.apache /data
配置共享目錄
vim /etc/exports /data 192.168.42.0/24(rw,sync,all_squash,anonuid=48,anongid=48)
啟動rpcbind,nfs
systemctl start rpcbind systemctl start nfs
設置開機啟動 因為需要先啟動rpcbind,然后再啟動nfs,因此我們把開機啟動放入rc.local里
chmod +x /etc/rc.local/rc.local vim /etc/rc.d/rc.local systemctl start rpcbind systemctl start nfs
查看此時的端口
ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:9743 *:* LISTEN 0 128 *:111 *:* LISTEN 0 128 *:20048 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 64 *:2049 *:* LISTEN 0 64 *:28518 *:* LISTEN 0 64 :::8812 :::* LISTEN 0 128 :::61836 :::* LISTEN 0 128 :::111 :::* LISTEN 0 128 :::20048 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 64 :::2049 :::*
查看掛載的目錄
showmount -e 127.0.0.1 Export list for 127.0.0.1: /application/data 168.92.168.42.0/24
去node4,node3掛載試一下
yum install nfs-utils rpcbind httpd -y id apache uid=48(apache) gid=48(apache) groups=48(apache) mkdir /test chown -R apache.apache /test mount -t nfs 192.168.42.154:/data /test
4.node4,node3在測試nfs的時候已經安裝過httpd了 所以我們只需要在node4,node3節點上安裝php就行了
yum install php php-mysql -y 此步驟只適合2.2版本,yum安裝的2.4版本/etc/httpd/conf.d/php.conf已經幫我們添加了 配置apache虛擬主機,并讓apache支持php vim /etc/httpd/conf/httpd.conf 找到對應的位置,添加 ServerName localhost:80 找到對應的位置,添加 LoadModule php5_module modules/libphp5.so 找到對應的位置,添加 AddType application/x-httpd-php .php 找到對應的位置,添加 <IfModule dir_module> DirectoryIndex index.php index.html </IfModule>
測試php是否能順利運行
vim /var/www/html/index.php <?php phpinfo(); ?>
測試是否能順利連接mysql
<?php $conn = mysql_connect("192.168.42.155","magedu","123456"); if($conn){ echo "Connection success"; }else{ echo mysql_error(); } ?>
5.在node2上安裝memcached
yum install libevent libevent-devle memcached -y systemctl enable memcached systemctl start memcached [root@node2 ~] ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 1024 *:11211 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 1024 :::11211 :::*
6.在node4,node3上操作,將php的session存儲設置為memcached
(1)安裝memcached擴展
yum install php-pecl-memcache* -y
(2)設置php的session存儲
vim /etc/httpd/conf.d/php.conf php_value session.save_handler "memcache" php_value session.save_path "tcp://192.168.42.151:11211"
(3)用例子測試一下:
代碼如下:
vim /var/www/html/session.php <?php session_start(); if (!isset($_SESSION['TEST'])) { $_SESSION['TEST'] = "set web1 ok"; } echo $_SESSION['TEST']; ?>
(4).用另一個文件查看一下:
代碼如下:
vim /var/www/html/get.php <?php session_start(); echo $_SESSION['TEST']; ?>
7.在node1上安裝nginx 負載均衡器
yum install nginx -y vim /etc/nginx/conf.d/test.conf server { listen 80; server_name www.test.com; location / { proxy_pass http://sshsrvs; add_header X-Via $server_addr; add_header X-Accel $server_name; } }
負載均衡配置
vim /etc/nginx/nginx.conf upstream sshsrvs { server 192.168.42.152:80; server 192.168.42.153:80; least_conn; }
8.在node4,node3添加虛擬主機測試
node3:
創建應用目錄 mkdir -p /application/discuz chown -R apache.apache /application/discuz 測試頁 echo "this is discuz_1 test page." > /application/discuz/index.html 定義discuz虛擬主機 vim /etc/httpd/conf.d/discuz.conf <VirtualHost *:80> ServerName test.discuz.com DocumentRoot "/application/discuz" <Directory "/application/discuz"> Options None AllowOverride None Require all granted </Directory> CustomLog "logs/iounix_access_log" combined </VirtualHost> 域名解析 vim /etc/hosts 192.168.42.152 test.discuz.com 重啟apache systemctl restart httpd
node4:
創建應用目錄 mkdir -p /application/discuz chown -R apache.apache /application/discuz 測試頁 echo "this is discuz_4 test page." > /application/discuz/index.html 定義discuz虛擬主機 vim /etc/httpd/conf.d/discuz.conf <VirtualHost *:80> ServerName test.discuz.com DocumentRoot "/application/discuz" <Directory "/application/discuz"> Options None AllowOverride None Require all granted </Directory> CustomLog "logs/iounix_access_log" combined </VirtualHost> 域名解析 vim /etc/hosts 192.168.42.153 test.discuz.com 重啟apache systemctl restart httpd
9.在負載均衡上添加域名解析并測試
vim /etc/hosts 192.168.42.150 test.discuz.com
添加discuz虛擬主機
vim /etc/nginx/conf.d/discuz.conf server { listen 80; server_name test.discuz.com; location / { proxy_pass http://sshsrvs; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; add_header X-Via $server_addr; add_header X-Accel $server_name; } }
測試test.discuz.com
[root@node1 conf.d] for i in {1..10};do curl test.discuz.com ; done this is discuz_1 test page. this is discuz_4 test page. this is discuz_1 test page. this is discuz_4 test page. this is discuz_1 test page. this is discuz_4 test page. this is discuz_1 test page. this is discuz_4 test page. this is discuz_1 test page. this is discuz_4 test page.
負載均衡效果已經出來了
10.安裝discuz測試
為了避免出錯發生,我們需要把負載均衡,卸掉一個,留一個,因為我們是從物理機的瀏覽器安裝discuz
在node1上注釋掉一個
vim /etc/nginx/nginx.conf upstream sshsrvs { server 192.168.42.152:80; server 192.168.42.153:80; least_conn; }
重啟nginx
systemctl restart nginx
測試一下
[root@node1 nginx] for i in {1..10};do curl test.discuz.com ; done this is discuz_1 test page. this is discuz_1 test page. this is discuz_1 test page. this is discuz_1 test page. this is discuz_1 test page. this is discuz_1 test page. this is discuz_1 test page. this is discuz_1 test page. this is discuz_1 test page. this is discuz_1 test page.
進入node3的/application/discuz目錄下載discuz
wget -c http://download.comsenz.com/DiscuzX/3.3/Discuz_X3.3_SC_UTF8.zip yum install unzip -y unzip Discuz_X3.3_SC_UTF8.zip ls [root@node3 discuz] ls Discuz_X3.3_SC_UTF8.zip index.html readme upload utility
刪掉之前測試的index.html
rm -f index.html
我們可以看到discuz的目錄層次有點深,因此我們需要把網站的根目錄指向upload
vim /etc/httpd/conf.d/discuz.conf DocumentRoot "/application/discuz/upload"
重啟apache
systemctl restart httpd
因為discuz需要檢查目錄權限,因此我們
chown -R apache.apache /application/discuz
添加物理機hosts域名解析
192.168.42.150 test.discuz.com
瀏覽器輸入 test.discuz.com
可以看到以下效果:
按照提示操作即可完成discuz安裝
11.discuz安裝完成以后,我們需要把文件推送到node4節點上
cd /application scp -rp discuz root@192.168.42.153:/application
推送完成以后
node4操作:
cd /application/discuz rm -f index.html chown -R apache.apache /application/discuz
更改虛擬主機discuz的根目錄為upload
vim /etc/httpd/conf.d/discuz.conf DocumentRoot "/application/discuz/upload"
完成上述操作以后,還記得之前在node1上注釋的嗎,我們需要把它打開
vim /etc/nginx/nginx.conf upstream sshsrvs { server 192.168.42.152:80; server 192.168.42.153:80; least_conn; }
重啟nginx
12.在去瀏覽器中訪問,登錄后臺,操作等,一切正常
13.還有最后一個問題需要處理,discuz的數據目錄,共享存儲 在node5上操作:
mkdir -p /data/discuz/
進入node3 把data推送過來
cd /application/discuz/upload scp -rp uc_server root@192.168.42.154:/data/discuz/ scp -rp data root@192.168.42.154:/data/discuz/
在node5上操作:
vim /etc/exports /data/discuz/data 192.168.42.0/24(rw,sync,all_squash,anonuid=48,anongid=48) /data/discuz/uc_server 192.168.42.0/24(rw,sync,all_squash,anonuid=48,anongid=48) exportfs -rv chown -R apache.apache /data
在node3上掛載并加入開機掛載
cd /application/discuz/upload/data rm -rf * umount /test cd ../ mount -t nfs 192.168.42.154:/data/discuz/data /application/discuz/upload/data mount -t nfs 192.168.42.154:/data/discuz/uc_server /application/discuz/upload/uc_server chmod +x /etc/rc.d/rc.local echo "mount -t nfs 192.168.42.154:/data/discuz/data /application/discuz/upload/data" >>/etc/rc.d/rc.local echo "mount -t nfs 192.168.42.154:/data/discuz/uc_server /application/discuz/upload/uc_server" >>/etc/rc.d/rc.local
在node4做同樣的操作:
至此工作已經全部完成
原創文章,作者:srayban,如若轉載,請注明出處:http://www.www58058.com/78403