您好,登錄后才能下訂單哦!
ps:實現(xiàn)是正常情況數(shù)據(jù)流向,虛線是異常情況下的數(shù)據(jù)流向
主機(jī)名 | IP地址 | 系統(tǒng) | 用途 |
---|---|---|---|
master | 192.168.217.128 | CentOS 7.3 | 反向代理機(jī)、redis主緩存、mysql主數(shù)據(jù)庫 |
backup | 192.168.217.129 | CentOS 7.3 | 反向代理機(jī)、redis從緩存、mysql從數(shù)據(jù)庫 |
tomcat-node 1 | 192.168.217.130 | CentOS 7.3 | web |
tomcat-node 2 | 192.168.217.131 | CentOS 7.3 | web |
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/\
nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 安裝帶有nginx rpm軟件包的源 主從都要做
yum install -y nginx
vi /etc/nginx/nginx.conf # 在 http 添加
http {
upstream tomcat_pool {
server 192.168.217.130:8080; #真實節(jié)點 web
server 192.168.217.131:8080;
ip_hash; #會話穩(wěn)固功能,否則無法通過vip地址登陸
}
server {
listen 80;
server_name 192.168.175.188; #虛擬出的IP
location / {
proxy_pass http://tomcat_pool;
proxy_set_header X-Real-IP $remote_addr;
}
}
include /etc/nginx/mime.types;
.......
nginx -t -c /etc/nginx/nginx.conf //測試配置文件語法
yum install -y keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
route_id NGINX_HA
}
vrrp_script nginx { #添加腳本路徑
script "/opt/shell/nginx.sh"
interval 2 #每隔兩秒執(zhí)行一次
}
vrrp_instance VI_1 {
state MASTER #活躍服務(wù)器
interface ens33 #網(wǎng)卡
virtual_router_id 51
priority 100 #優(yōu)先級
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script { #調(diào)用腳本
nginx
}
virtual_ipaddress {
192.168.217.188 #虛擬IP
}
}
mkdir /opt/shell
vim /opt/shell/nginx.sh
#!/bin/bash
k=`ps -ef | grep keepalived | grep -v grep | wc -l`
if [ $k -gt 0 ];then
/bin/systemctl start nginx.service
else
/bin/systemctl stop nginx.service
fi
# 以上意思為開啟 keepalived 會自動開啟 Nginx
chmod +x /opt/shell/nginx.sh
systemctl start keepalived.service #開啟服務(wù)
vim /etc/keepalived/keepalived.conf
.......
vrrp_instance VI_1 {
state BACKUP #修改備份模式
interface ens33
virtual_router_id 51
priority 90 #確定主從
......
systemctl start keepalived.service #開啟服務(wù)
在keepalived服務(wù)開啟狀態(tài)下,關(guān)閉nginx做測試,發(fā)現(xiàn)每隔2秒nginx又會自動啟用
關(guān)閉 主keepalived ,測試主從漂移地址切換,使用 ip addr 查看 VIP 綁定位置
tar xf jdk-8u144-linux-x64.tar.gz -C /opt
cp -rv /opt/jdk1.8.0_144/ /usr/local/java #移動重命名
vim /etc/profile #添加java環(huán)境
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
source /etc/profile #刷新環(huán)境
java -version #證明java環(huán)境安裝成功
java version "1.8.0_144"
tar xf apache-tomcat-8.5.23.tar.gz -C /opt
cp -r /opt/apache-tomcat-8.5.23 /usr/local/tomcat8 #移動重命名
ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup #開啟tomcat
ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown #關(guān)閉tomcat
tomcatup #開啟
netstat -anpt | grep 8080 #查看服務(wù)是否開啟
http://192.168.217.130:8080/ #測試默認(rèn)測試頁是否正常顯示
http://192.168.217.131:8080/
訪問 http://192.168.217.188 #修改web不同首頁,測試keepalived和反向代理是否成功
vim /usr/local/tomcat8/webapps/ROOT/index.jsp #首頁位置
yum install -y mariadb-server mariadb
systemctl start mariadb.service
systemctl enable mariadb.service
netstat -anpt | grep 3306
mysql_secure_installation #常規(guī)安全設(shè)置,測試可以省略。
mysql -uroot -p < slsaledb-2014-4-10.sql #導(dǎo)入SL商城數(shù)據(jù)
mysql -uroot -p
show databases; #查看有沒有 slsaledb 數(shù)據(jù)庫
GRANT all ON slsaledb.* TO 'root'@'%' IDENTIFIED BY 'abc123'; #授權(quán)
flush privileges; # 刷新
cd /usr/local/tomcat8/conf/
vim server.xml #跳到148行尾,在Host name下新增
<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context>
#日志調(diào)試信息debug為0表示信息越少,docBase指定訪問目錄
# SLSaleSystem 為webapps目錄下的源碼包
tar xf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/ #解壓商城的源碼包
cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes #移動到 webapps下
vim jdbc.properties #修改數(shù)據(jù)庫IP地址是VRRP的虛擬IP,以及授權(quán)的用戶名root和密碼abc123。
http://192.168.217.130:8080/ #默認(rèn)的用戶名admin 密碼:123456
http://192.168.217.131:8080/
http://192.168.217.188 #輸入虛擬地址測試登錄,并且關(guān)閉主再測試登錄
yum install -y epel-release #安裝epel源
yum install redis -y # 安裝 redis 緩存服務(wù)
vim /etc/redis.conf #修改配置文件
bind 0.0.0.0
systemctl start redis.service #開啟服務(wù)
netstat -anpt | grep 6379
redis-cli -h 192.168.217.128 -p 6379 #本地測試連接
192.168.217.128:6379> set name test #創(chuàng)建name 值是test
192.168.217.128:6379> get name #查看name值
yum install -y epel-release #安裝epel源
yum install redis -y # 安裝 redis 緩存服務(wù)
vim /etc/redis.conf #修改配置文件
bind 0.0.0.0
slaveof 192.168.217.128 6379 #主服務(wù)器的IP不是虛擬IP 第266行
systemctl start redis.service #開啟服務(wù)
redis-cli -h 192.168.217.129 -p 6379 //登錄從,獲取值,成功說明主從同步成功
192.168.217.129:6379> get name #如果能查看到 ,說明同步成功
vim /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/applicationContext-mybatis.xml
47 行 <constructor-arg value="192.168.217.188"/> #修改虛擬IP
48 <constructor-arg value="6379"/>
redis-cli -h 192.168.217.188 -p 6379
192.168.175.188:6379> info
keyspace_hits:1 或者 keyspace_misses:0 #關(guān)注這個值,命中數(shù)和未命中數(shù)
#登錄商城,然后反復(fù)點擊需要數(shù)據(jù)庫參與的操作頁面,再回來使用info 檢查keyspace_hits或者keyspace_misses: 值變化。
redis-cli -h 192.168.217.128 info Replication //獲取當(dāng)前服務(wù)器的角色
vim /etc/redis-sentinel.conf
17 protected-mode no #開啟
68 sentinel monitor mymaster 192.168.175.128 6379 1 #1表示1臺從 注意:修改
98 sentinel down-after-milliseconds mymaster 3000 #故障切換時間單位是毫秒
systemctl restart redis-sentinel.service //啟動集群
netstat -anpt | grep 26379
redis-cli -h 192.168.217.128 -p 26379 info Sentinel //查看集群信息
systemctl stop redis.service #關(guān)閉主緩存服務(wù)
redis-cli -h 192.168.175.128 -p 26379 info Sentinel #發(fā)現(xiàn)主變成了129 稍微等下 在master查看
redis-cli -h 192.168.217.129 -p 6379 #進(jìn)入從緩存服務(wù)器
192.168.217.129:6379> set name2 test2 #創(chuàng)建
OK
192.168.217.129:6379> get name2
"test2"
systemctl start redis #把主緩存服務(wù)啟動
redis-cli -h 192.168.217.128 -p 6379 #進(jìn)入主緩存服務(wù)器
192.168.217.128:6379> get name2 #查看
"test2"
vim /etc/my.cnf #[mysqld]下添加
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1 #主從id不同
log_slave_updates=true
sync_binlog=1
vim /etc/my.cnf # mysql從服務(wù)器 #在[mysqld]下添加
server_id=2
systemctl restart mariadb.service #重啟數(shù)據(jù)庫
mysql -u root -p
show master status; #記錄日志文件名稱和偏移量位置值
grant replication slave on *.* to 'rep'@'192.168.175.%' identified by '123456'; #授權(quán)
flush privileges; #刷新
mysql -u root -p
change master to master_host='192.168.217.128',
master_user='rep',master_password='123456',
master_log_file='mysql_bin.000001',master_log_pos=245;
start slave; #開啟
show slave status; #查看
Slave_IO_Running: Yes #成功
Slave_SQL_Running: Yes
在主數(shù)據(jù)庫創(chuàng)建數(shù)據(jù)庫,在從數(shù)據(jù)庫查看。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。