您好,登錄后才能下訂單哦!
下文給大家帶來實驗keepalived+nginx+apache+mysql如何實現高可用負載均衡第二篇,希望能夠給大家在實際運用中帶來一定的幫助,負載均衡涉及的東西比較多,理論也不多,網上有很多書籍,今天我們就用億速云在行業內累計的經驗來做一個解答。
一環境介紹
主機名 | ip | 安裝軟件 |
---|---|---|
nginx_mysql_m | 192.168.255.67 | keepalived+nginx |
nginx_mysql_s | 192.168.255.66 | keepalived+nginx |
mysql1 | 192.168.255.52 | mysql、mysql-server |
mysql2 | 192.168.255.57 | mysql、mysql-server |
二、keepalived配置
keepalived的配置和上一篇的類似
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id nginx_mysql_s vrrp_skip_check_adv_addr #vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" # 檢查nginx狀態的腳本 interval 2 weight 3 } vrrp_instance VI_1 { state BACKUP interface ens160 virtual_router_id 66 priority 100 #backup這里要比100小 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.255.200 } track_script { chk_nginx } }
三、nginx的配置
因為這里nginx監聽的是3306的端口,不需要web服務所以配置文件較簡單,但是需要用到stream模塊
在配置的時候需要添加上--with--stream
/configure --add-module=../ngx_cache_purge-2.3 --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-pcre=../pcre-8.41 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.0.2o
其他和上一篇的安裝方法一樣
修改nginx配置文件
#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;events {
worker_connections 1024;
}stream {
upstream mysql {
server 192.168.255.52:3306 weight=5 max_fails=3 fail_timeout=30s;
server 192.168.255.57:3306 weight=5 max_fails=3 fail_timeout=30s;
}
server {
listen 3306;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass mysql;
}
}
啟動nginx
/usr/local/nginx/sbin/nginx
netstat -napt
發現nginx已經被成功監聽
backup的nginx配置相同
四、mysql數據庫配置
接下來是重頭戲,mysql主主配置
1 mysql安裝
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
mv http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm /etc/yum.repo/
rpm -ivh http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
yum install -y mysql&&yum install mysql-server -y
systemctl start mysql
首先我們配置mysql雙主模型,讓其數據同步
vim /etc.my.cnf
server-id=1
log-bin=mysql-bin
binlog_format=mixed
relay-log=relay-bin
relay-log-index=slave-relay-bin.index
auto-increment-increment=2
auto-increment-offset=1
log-slave-updates
其中紅框歷史需要添加的內容
粉框中是兩個mysql不同的地方
mysql2的粉框處是2
然后重啟mysql
systemctl restart mysql
mysql
mysql> grant all privileges on . to zhangsan@192.168.255.53 identified by '123456';
在mysql2上把ip改成mysql1的
剩下的步驟在上一篇博客mysql數據不同步里面有,這里不多說了
我們給web云服務器授權,讓其可以進行所有操作
mysql
mysql> create database db_jd;
mysql> create user web@localhost identified by '123456';
mysql> grant all privileges on *.* to web@localhost;
mysql> create user web@192.168.255.50 identified by '123456';
mysql> create user web@192.168.255.53 identified by '123456';
mysql> grant all privileges on db_jd.* to web@192.168.255.50;
mysql> grant all privileges on db_jd.* to web@192.168.255.53;
mysql> flush privileges;
然后在web服務器上用php連接一下mysql看是否成功
>cd /var/www/html
vim con.php
<?php
// mysql_connect("數據庫地址","數據庫賬號","數據庫密碼","連接數據庫");
$con = mysql_connect("192.168.255.200","web","123456","db_jd");
//測試是否連接數據庫
if($con){
echo "連接成功";
}else{
echo "連接失敗 ";
}
瀏覽器訪問wen的ip/con.php
mysql2同理
然后在數據庫db_jd里創建表
mysql > create table tb_goods (id int(11) null, tltle VarChar(40), price Decimal(10), market_price Decimal(10));
然后在web服務器編寫接口程序連接mysql,并寫一個能插入數據到數據庫的表單
vim coon.php
<?php
//1、連接數據庫
mysql_connect('192.168.255.200','web','123456');
//2、選擇數據庫
mysql_select_db('db_jd');
//3、指定數據庫的編碼格式
mysql_query('set names utf8');
?>
vim index.php
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>產品添加-JD產品管理系統</title>
</head>
<body>
<h5>JD產品管理</h5>
<form action="deal.php" method="post" enctype="multipart/form-data">
圖書名稱:<input type='text' name='title' />
<hr />
銷售價格:<input type="text" name='price' />
<hr />
市場價格:<input type="text" name='market_price' />
<hr />
<input type="submit" name="submit" value="添加" />
<input type="reset" name="reset" value="重置" />
</form>
</body>
</html>
vim deal.php
<?php
//1、設置響應頭信息
header('Content-type:text/html; charset=utf-8');
//2、submit安全判斷
if(isset($_POST['submit'])) {
//3、接收數據
$title = $_POST['title'];
$price = $_POST['price'];
$market_price = $_POST['market_price'];
include 'coon.php';
//9、定義SQL語句
$sql = "insert into tb_goods values (null,'$title','$price','$market_price','$thumb')";
//10、執行SQL語句
$result = mysql_query($sql);
if($result) {
echo '添加成功';
} else {
echo '添加失敗';
}
}
?>
然后訪問webvip的地址
插入數據
添加成功
接下來我們給web和mysql的keepalived主機還有兩臺mysql添加時間同步,這里舉例兩臺MySQL主機。
yum -y install ntp
修改npt配置文件,添加下面兩行
vim /etc/ntp.conf
server 127.127.1.0 iburst local clock #添加使用本地時間 restrict 192.168.255.52 mask 255.255.255.0 nomodify #允許更新的IP地址段
啟動ntp服務,并加入開機啟動
systemctl start ntpd
systemctl enable ntpd
添加防火墻策略
只允許192.168.255.57訪問ntp服務。
打開防火墻
systemctl start firewalld
firewalld-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.255.57" port protocol="udp" port="123" accept"
firewalld-cmd --reload
配置 keepalived虛擬路由協議vrrp通過防火墻
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ens192 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
配置web80端口通過防火墻
firewall-cmd --zone=public --add-port=80/tcp
firewall-cmd --zone=public --add-interface=ens160
firewall-cmd --reload
配置3306端口通過防火墻
firewall-cmd --zone=public --add-port=3306/tcp
firewall-cmd --zone=public --add-interface=ens160看了以上關于,如果大家還有什么地方需要了解的可以在億速云行業資訊里查找自己感興趣的或者找我們的專業技術工程師解答的,億速云技術工程師在行業內擁有十幾年的經驗了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。