您好,登錄后才能下訂單哦!
億速云負載均衡(Cloud Load Balancer)是對多臺云服務器進行流量分發的服務。億速云負載均衡可以通過流量分發擴展應用系統對外的服務能力,通過消除單點故障提升應用系統的可用性。億速云負載均衡服務通過設置虛擬服務地址(VIP),將位于同一地域的多臺云服務器資源虛擬成一個高性能、高可用的應用服務池。根據應用指定的方式,將來自客戶端的網絡請求分發到云服務器池中。億速云負載均衡服務會檢查云服務器池中云服務器實例的健康狀態,自動隔離異常狀態的實例,從而解決了云服務器的單點問題,同時提高了應用的整體服務能力。今天億速云給大家介紹下實現Web服務器之間使用同一個MYSQL和相同的網頁配置文件的方法
前言:
用NFS、LAMP、BIND結合的方法,應對網絡中訪問量增大而導致云服務器負載運行的情況。以實現Web服務器之間使用同一個MYSQL和相同的網頁配置文件。
實驗環境:
HostName | IP | 規劃 |
nfs.preferred.com | 192.168.1.6 | NFS |
ns.preferred.com | 192.168.1.5 | DNS |
mysql.preferred.com | 192.168.1.4 | MYSQL |
www.preferred.com | 192.168.1.3 | WEB |
www.preferred.com | 192.168.1.2 | WEB |
實驗拓撲:
實驗步驟:
一、搭建NFS
以下操作在nfs.preferred.com服務器上實現
[root@localhost ~]# yum install nfs-utils -y <---這里需要安裝nfs這個包(基本的NFS命令與監控程序) [root@localhost ~]# rpm -qa | grep nfs-utils --color nfs-utils-1.2.3-39.el6.x86_64
創建共享目錄并配置nfs文件
[root@localhost~]# mkdir /mydata [root@localhost~]# mkdir /webdata [root@localhost~]# cd /webdata/ [root@localhost webdata]# unzip wordpress-3.2.1-zh_CN.zip &> /dev/null [root@localhost webdata]# chmod -R 777 wordpress [root@localhost webdata]# ls -l drwxrwxrwx 5 root root 4096 Oct 11 2016 wordpress -rw-r--r-- 1 root root 4372680 Oct 11 2016 wordpress-3.2.1-zh_CN.zip [root@localhost webdata]# vim /etc/exports /mydata 192.168.1.4(rw,no_root_squash) /webdata 192.168.1.2(rw,no_root_squash) 192.168.1.3(rw,no_root_squash) [root@localhost ~]# service rpcbind start 注:rpcbind是一個RPC服務,主要是在nfs共享的時候負責通知客戶端、服務器nfs端口號。 [root@localhost ~]# service nfs start Starting NFS services: [ OK ] Starting NFS quotas: [ OK ] Starting NFS mountd: [ OK ] Starting NFS daemon: [ OK ] Starting RPC idmapd: [ OK ] [root@localhost ~]# showmount -e 192.168.1.6 <---查看當前共享的目錄 Export list for 192.168.1.6: /webdata 192.168.1.3,192.168.1.2 /mydata 192.168.1.4
二、搭建DNS
以下操作在ns.preferred.com服務器上實現
[root@localhost ~]# yum install bind -y | tail -n 5 Installed: bind.x86_64 32:9.8.2-0.47.rc1.el6_8.1 Complete! [root@localhost ~]# vim /etc/named.rfc1912.zones <---添加zone(正向與反向的) zone "preferred.com" IN { type master; file "preferred.com.zone"; }; zone "1.168.192.in-addr.arpa" IN { type master; file "192.168.1.5.zone"; }; [root@localhost ~]# vim /var/named/preferred.com.zone <---配置DNS正向區域解析 $TTL 86400 $ORIGIN preferred.com. @ IN SOA ns.preferred.com admin.preferred.com ( 2016101001 1D 5M 7D 1D ) IN NS ns ns IN A 192.168.1.5 www IN A 192.168.1.2 www IN A 192.168.1.3 nfs IN A 192.168.1.6 [root@localhost ~]# vim /var/named/192.168.1.5.zone <---配置DNS反向區域解析 $TTL 86400 @ IN SOA ns.preferred.com. admin.preferred.com ( 2016101001 1H 5M 7D 1D ) IN NS ns.preferred.com. 5 IN PTR ns.preferred.com. 2 IN PTR www.preferred.com. 3 IN PTR www.preferred.com. 6 IN PTR nfs.preferred.com. [root@localhost ~]# systemctl restart named [root@localhost ~]# dig -t A www.preferred.com @192.168.1.5 <---測試DNS能否正向解析 ; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.4 <<>> -t A www.preferred.com @192.168.1.5 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23764 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 2 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;www.preferred.com. IN A ;; ANSWER SECTION: www.preferred.com. 86400 IN A 192.168.1.2 www.preferred.com. 86400 IN A 192.168.1.3 ;; AUTHORITY SECTION: preferred.com. 86400 IN NS ns.preferred.com. ;; ADDITIONAL SECTION: ns.preferred.com. 86400 IN A 192.168.1.5 ;; Query time: 0 msec ;; SERVER: 192.168.1.5#53(192.168.1.5) ;; WHEN: Tue Oct 11 04:22:36 EDT 2016 ;; MSG SIZE rcvd: 111 [root@localhost ~]# dig -x 192.168.1.2 @192.168.1.5 <---測試DNS能否反向解析 ; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.4 <<>> -x 192.168.1.2 @192.168.1.5 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21950 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;2.1.168.192.in-addr.arpa. IN PTR ;; ANSWER SECTION: 2.1.168.192.in-addr.arpa. 86400 IN PTR www.preferred.com. ;; AUTHORITY SECTION: 1.168.192.in-addr.arpa. 86400 IN NS ns.preferred.com. ;; ADDITIONAL SECTION: ns.preferred.com. 86400 IN A 192.168.1.5 ;; Query time: 0 msec ;; SERVER: 192.168.1.5#53(192.168.1.5) ;; WHEN: Tue Oct 11 04:23:57 EDT 2016 ;; MSG SIZE rcvd: 117
三、搭建MYSQL
以下操作在mysql.preferred.com服務器上實現
[root@localhost ~]# mkdir /mydata/ [root@localhost ~]# mount -t nfs 192.168.1.6:/mydata/ /mydata/ <---掛載共享目錄 [root@localhost ~]# groupadd -r mysql <---創建mysql組和用戶 [root@localhost ~]# useradd -r -g mysql -s /sbin/nologin mysql [root@localhost ~]# cd /usr/local/ [root@localhost local]# ln -sv /root/mariadb-5.5.43-linux-x86_64 mysql "mysql" -> "/root/mariadb-5.5.43-linux-x86_64" [root@localhost local]# cd mysql/ [root@localhost mysql]# ls bin COPYING.LESSER EXCEPTIONS-CLIENT INSTALL-BINARY man README share support-files COPYING data include lib mysql-test scripts sql-bench [root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/mydata/ <---初始化數據庫 Installing MariaDB/MySQL system tables in '/mydata' ... 161010 13:33:59 [Note] ./bin/mysqld (mysqld 5.5.43-MariaDB) starting as process 2183 ... OK Filling help tables... 161010 13:33:59 [Note] ./bin/mysqld (mysqld 5.5.43-MariaDB) starting as process 2191 ... OK ... [root@localhost mysql]# ls /mydata/ <--查看初始后生成的數據 aria_log.00000001 aria_log_control mysql performance_schema test [root@localhost mysql]# mkdir /etc/mysql/ [root@localhost mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf [root@localhost mysql]# vim /etc/mysql/my.cnf <---修改配置文件,添加以下三行 datadir = /mydata innodb_file_per_table = on skip_name_resolve = on [root@localhost mysql]# service mysqld start Starting MySQL... [確定] [root@localhost mysql]# ss -tnl | grep 3306 <---服務端口已經開啟 LISTEN 0 50 *:3306 *:* *:* [root@localhost mysql]# mysql -bash: mysql: command not found 注:嘗試登陸MYSQL卻報錯,不要緊張。這是因為/usr/local/bin目錄下確實mysql導致,只需要建立一個軟連接即可解決。 [root@localhost ~]# ln -sv /usr/local/mysql/bin/mysql /usr/bin/ "/usr/bin/mysql" -> "/usr/local/mysql/bin/mysql" [root@localhost ~]# mysql <---連接數據庫創建用戶密碼與授權 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 115 Server version: 5.5.43-MariaDB-log MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> CREATE DATABASE pre; Query OK, 1 rows affected (0.00 sec) MariaDB [(none)]> GRANT ALL ON pre.* TO 'jds'@'%' IDENTIFIED BY 'redhat'; <---創建用戶并賦予權限 Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
四、搭建WEB
以下操作在www.preferred.com(192.168.1.2)服務器上實現
[root@localhost ~]# yum install httpd php-mysql php -y | tail -n 10 warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Importing GPG key 0xC105B9DE: Userid: CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org> Package: centos-release-6-5.el6.centos.11.1.x86_64 (@anaconda-CentOS-201311272149.x86_64/6.5) From: /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 php-cli.x86_64 0:5.3.3-48.el6_8 php-common.x86_64 0:5.3.3-48.el6_8 php-pdo.x86_64 0:5.3.3-48.el6_8 Updated: httpd.x86_64 0:2.2.15-54.el6.centos Dependency Updated: httpd-tools.x86_64 0:2.2.15-54.el6.centos Complete! [root@localhost ~]# mkdir /webdata [root@localhost ~]# vim /etc/httpd/conf/httpd.conf <---添加以下幾行 ServerName www.preferred.com:80 <VirtualHost 192.168.1.2:80> ServerName www.preferred.com DocumentRoot /webdata/wordpress </VirtualHost> [root@localhost ~]# service rpcbind start [root@localhost ~]# mount -t nfs 192.168.1.6:/webdata /webdata [root@localhost ~]# ls /webdata/ wordpress wordpress-3.2.1-zh_CN.zip [root@localhost ~]# service httpd start Starting httpd: [ OK ]
接下來安裝wordpress,沒有的話可以自己去下載一個https://wordpress.org/download/
[root@localhost ~]# cd /webdata/wordpress [root@localhost wordpress]# ls index.php wp-app.php wp-config.php wp-includes wp-pass.php wp-settings.php license.txt wp-atom.php wp-config-sample.php wp-links-opml.php wp-rdf.php wp-signup.php readme.html wp-blog-header.php wp-content wp-load.php wp-register.php wp-trackback.php wp-activate.php wp-comments-post.php wp-cron.php wp-login.php wp-rss2.php xmlrpc.php wp-admin wp-commentsrss2.php wp-feed.php wp-mail.php wp-rss.php [root@localhost wordpress]# cp wp-config-sample.php wp-config.php <---這是一個示例配置文件,更名并配置 [root@localhost wordpress]# vim wp-config.php // ** MySQL 設置 - 具體信息來自您正在使用的主機 ** // /** WordPress 數據庫的名稱 */ define('DB_NAME', 'pre'); <---連接數據庫所使用的,與剛剛在mysq中創建的一致,以下幾個都是。 /** MySQL 數據庫用戶名 */ define('DB_USER', 'jds'); /** MySQL 數據庫密碼 */ define('DB_PASSWORD', 'redhat'); /** MySQL 主機 */ define('DB_HOST', '192.168.1.4'); /** 創建數據表時默認的文字編碼 */ define('DB_CHARSET', 'utf8'); /** 數據庫整理類型。如不確定請勿更改 */ define('DB_COLLATE', ''); [root@localhost ~]# mysql -ujds -predhat -h 192.168.1.4 <---嘗試登陸 ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.4' (113) 注:遇到這個問題怎么個人都不好了,在MYSQL服務器上取消防火墻也不行。所以嘗試在MYSQL服務器的防火墻上加上端口的允許。 [root@localhost ~]# iptables -L -n | grep 3306 [root@localhost ~]# iptables -I INPUT -s 0/0 -p tcp --dport 3306 -j ACCEPT [root@localhost mysql]# iptables -L -n | grep 3306 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 [root@localhost ~]# mysql -ujds -predhat -h 192.168.1.4 <---嘗試再次登陸 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 116 Server version: 5.5.43-MariaDB-log MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | pre | <---之前創建的數據庫可以訪問 | test | +--------------------+ 5 rows in set (0.00 sec)
測試訪問正常!!!
以下操作在www.preferred.com(192.168.1.3)服務器上實現
[root@localhost ~]# yum install httpd php-mysql php -y [root@localhost ~]# mkdir /webdata [root@localhost ~]# vim /etc/httpd/conf/httpd.conf <---添加以下幾行 ServerName www.preferred.com:80 <VirtualHost 192.168.1.3:80> ServerName www.preferred.com DocumentRoot /webdata/wordpress </VirtualHost> [root@localhost ~]# service rpcbind start [root@localhost ~]# mount -t nfs 192.168.1.6:/webdata /webdata [root@localhost ~]# ls /webdata/ wordpress wordpress-3.2.1-zh_CN.zip [root@localhost ~]# service httpd start Starting httpd: [ OK ]
先用WEB(192.168.1.3)進行測試。發布一條文章
用WEB(192.168.1.2)進行測試。現在訪問看看
看到了在WEB(192.168.1.3)上發布的文章,我們在WEB(192.168.1.2)上也發布一篇
最后我們在WEB(192.168.1.3)上查看
至此,我們實驗的目的已經達到。
如果大家還有什么地方需要了解的可以在億速云官網找我們的專業技術工程師的,億速云技術工程師在行業內擁有十幾年的經驗了,所以會比小編回答的更加詳細專業。億速云官網鏈接www.mlszssj.com
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。