您好,登錄后才能下訂單哦!
下文給大家帶來掌握Nginx +keepalived+varnish+lamp實現高可用、負載均衡集群的方法,希望能夠給大家在實際運用中帶來一定的幫助,負載均衡涉及的東西比較多,理論也不多,網上有很多書籍,今天我們就用億速云在行業內累計的經驗來做一個解答。
描述:
1、前端兩臺NGINX,通過keepalived虛擬IP漂移,實現前端兩臺NGINX高可用;
2、利用NGINX反向代理功能對后端varnish實現高可用集群,
3、再通過varnish實現動靜分離
注:1、先裝Nginx +keepalived
2、裝varnish
3、裝lamp
需要6臺虛擬機(100-101裝Nginx +keepalived:100主,101備)需要聯網(102-103裝varnish)需要聯網(104-105裝lamp)需要聯網
所有主機必做的步驟
systemctl stop firewalld //關閉防火墻
setenforce 0 //關閉監控
1、裝Nginx +keepalived(兩臺機子都要做的)(100主101備)
systemctl stop firewalld //關閉防火墻
setenforce 0 //關閉監控
cd /etc/yum.repos.d/
mv back/* ./
yum install -y epel-release
yum install -y nginx
yum install keepalived -y
vi /etc/keepalived/keepalived.conf(把里面內容全刪了添加以下內容)
! Configuration File for keepalived
global_defs {
route_id NGINX-01
}
vrrp_script nginx {
script "/opt/nginx.sh"
interval 2
weight -10
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
nginx
}
virtual_ipaddress {
192.168.80.188
}
}
scp /etc/keepalived/keepalived.conf root@192.168.80.101:/etc/keepalived/keepalived.conf
vi /etc/keepalived/keepalived.conf(修改以下畫圈部分)
vi /opt/nginx.sh(添加以下內容)
#!/bin/bash
A=$(ps -ef | grep keepalived | grep -v grep | wc -l)
if [ $A -gt 0 ];then
systemctl start nginx
else
systemctl sop nginx
fi
chmod +x /opt/nginx.sh
ll /opt/nginx.sh
netstat -anpt | grep nginx
systemctl start keepalived
netstat -anpt | grep nginx
ip addr show ens32
vi /opt/nginx.sh(添加以下內容)
#!/bin/bash
A=$(ip addr | grep 192.168.80.188/32 | grep -v grep | wc -l)
if [ $A -gt 0 ];then
systemctl start nginx
else
systemctl stop nginx
fi
chmod +x /opt/nginx.sh
systemctl start keepalived
cat /var/log/messages
cat /var/log/messages
vi /etc/nginx/nginx.conf(修改以下內容)
upstream varnish_pool {
server 192.168.80.102:80;
server 192.168.80.103:80;
}
proxy_pass http://varnish_pool;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
nginx -t
scp /etc/nginx/nginx.conf 192.168.80.101:/etc/nginx/nginx.conf
systemctl restart nginx
2、裝varnish(80.102,80.103)(兩臺機子都需要做)
systemctl stop firewalld //關閉防火墻
setenforce 0 //關閉監控
cd /etc/yum.repos.d/
mv back/* ./
yum install epel-release -y //需要聯網
yum install -y varnish
vi /etc/varnish/varnish.params(修改環圈部分)
vi /etc/varnish/default.vcl(修改添加以下部分)
backend web1 {
.host = "192.168.80.104";
.port = "80";
}
backend web2 {
.host = "192.168.80.105";
.port = "80";
}
sub vcl_recv {
if (req.url ~ "(?i)\.php$"){
set req.backend_hint = web1;
}else{
set req.backend_hint = web2;
}
systemctl start varnish
netstat -anpt | grep varnish
vi /etc/varnish/varnish.params(修改以下內容)
scp /etc/varnish/default.vcl 192.168.80.103:/etc/varnish/default.vcl
systemctl start varnish
netstat -anpt | grep varnish
3、裝lamp(兩臺機子都要做的)(100主101備)
systemctl stop firewalld //關閉防火墻
setenforce 0 //關閉監控
cd /etc/yum.repos.d/
mv back/* ./
安裝wget
yum install -y wget
yum install -y httpd
systemctl start httpd
systemctl enable httpd
獲取rpm軟件包:
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
安裝rpm包
rpm -ivh mysql-community-release-el7-5.noarch.rpm
安裝mysql
yum install -y mysql-community-server
啟動
systemctl start mysql
systemctl enable mysql
修改root密碼并設置允許遠程連接
進入mysql
mysql -uroot
設置root密碼為123456
set password for 'root'@'localhost' =password('123456');
設置允許用root賬戶進行遠程連接,并設置其密碼為123456
grant all privileges on *.* to root@'%'identified by '123456';
修改的配置立即生效
flush privileges;
退出:
exit
部署php
yum install -y php
安裝組件是php支持mysql
yum install -y \
php-mysql \
php-gd \
libjpeg* \
php-ldap \
php-odbc \
php-pear \
php-xml \
php-xmlrpc \
php-mbstring \
php-bcmath \
php-mhash
vi /etc/httpd/conf/httpd.conf(修改以下內容)
ServerName www.example.com:80去掉#號
systemctl start httpd
echo "<h3>192.168.80.101</h3>" > /var/www/html/index.php
vi /etc/httpd/conf/httpd.conf(修改以下內容)
ServerName www.example.com:80去掉#號
systemctl start httpd
echo "<h3>192.168.80.102</h3>" > /var/www/html/index.html
看了以上關于掌握Nginx +keepalived+varnish+lamp實現高可用、負載均衡集群的方法,如果大家還有什么地方需要了解的可以在億速云行業資訊里查找自己感興趣的或者找我們的專業技術工程師解答的,億速云技術工程師在行業內擁有十幾年的經驗了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。