案例环境:
必须注意软件版本号,版本号不同可能会导致实验失败
系统类型 |
IP地址 |
主机名 |
所需软件 |
Centos 7.4 1708 64bit |
192.168.100.101 |
Nginx |
nginx-1.12.2.tar.gz |
Centos 7.4 1708 64bit |
192.168.100.102 |
Tomcat1 |
apache-tomcat-7.0.54.tar.gz jdk-7u51-linux-x64.gz nfs-utils、rpcbind |
Centos 7.4 1708 64bit |
192.168.100.103 |
Tomcat2 |
apache-tomcat-7.0.54.tar.gz jdk-7u51-linux-x64.gz nfs-utils、rpcbind |
Centos 7.4 1708 64bit |
192.168.100.104 |
NFS |
nfs-utils、rpcbind、JAVA项目 |
Centos 7.4 1708 64bit |
192.168.100.105 |
MySQL |
MySQL 、项目smbms_db.sql |
Centos 7.4 1708 64bit |
192.168.100.106 |
Mem1 |
libevent-release-1.4.15-stable.tar.gz magent-0.5.tar.gz memcached-1.5.10.tar.gz keepalived |
Centos 7.4 1708 64bit |
192.168.100.107 |
Mem2 |
libevent-release-1.4.15-stable.tar.gz magent-0.5.tar.gz memcached-1.5.10.tar.gz keepalived |
Nginx反向代理服务器的配置192.168.100.101
[root@nginx ~]#yum -y install pcre-devel zlib-devel
[root@nginx ~]#useradd -M -s /sbin/nologin nginx
[root@nginx ~]#tar zxvf nginx-1.12.2.tar.gz -C /usr/src/
[root@nginx ~]# cd /usr/src/nginx-1.12.2/
[root@nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@nginx nginx-1.12.2]# make && make install
[root@nginx nginx-1.12.2]# cd
[root@nginx ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx ~]#vim /usr/lib/systemd/system/nginx.service #nginx的启动脚本
[Unit]
Description=nginxapi
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=kill -s HUP $(cat /usr/local/nginx/logs/nginx.pid)
ExecStop=kill -s QUIT $(cat /usr/local/nginx/logs/nginx.pid)
PrivateTmp=Flase
[Install]
WantedBy=multi-user.target
[root@nginx ~]#yum -y remove httpd
[root@nginx ~]#systemctl start nginx
[root@nginx ~]#systemctl enable nginx
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
upstream tomcat{
server 192.168.100.102:8080;
server 192.168.100.103:8080;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location ~ \.(js|asp|aspx|php|jsp|do|css|jpg|png) {
proxy_pass http://tomcat;
}
[root@nginx ~]# systemctl stop nginx
[root@nginx ~]#systemctl start nginx
[root@nginx ~]# netstat -tunlp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4747/nginx: master
NFS服务器的部署192.168.100.104
[root@nfs ~]#yum -y install rpcbind nfs-utils
[root@nfs ~]# for I in rpcbind nfs; do systemctl enable $I; done #加入开机自启
[root@nfs ~]#vim /etc/exports
/opt/webapp 192.168.100.0/24(rw,sync,no_root_squash) #配置文件自己编写
[root@nfs ~]# mkdir /opt/webapp
[root@nfs ~]#chmod 777 /opt/webapp
[root@nfs ~]# cp -rf WebRoot/ /opt/webapp/
[root@nfs ~]#for I in rpcbind nfs; do systemctl start $I; done
[root@nfs ~]# netstat -tunlp | grep rpc
tcp 0 0 0.0.0.0:42315 0.0.0.0:* LISTEN 2998/rpc.statd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 3296/rpcbind
tcp 0 0 0.0.0.0:20048 0.0.0.0:* LISTEN 3314/rpc.mountd
tcp6 0 0 :::111 :::* LISTEN 3296/rpcbind
tcp6 0 0 :::20048 :::* LISTEN 3314/rpc.mountd
tcp6 0 0 :::35578 :::* LISTEN 2998/rpc.statd
udp 0 0 0.0.0.0:49042 0.0.0.0:* 2998/rpc.statd
udp 0 0 127.0.0.1:659 0.0.0.0:* 2998/rpc.statd
udp 0 0 0.0.0.0:927 0.0.0.0:* 3296/rpcbind
udp 0 0 0.0.0.0:20048 0.0.0.0:* 3314/rpc.mountd
udp 0 0 0.0.0.0:111 0.0.0.0:* 3296/rpcbind
udp6 0 0 :::927 :::* 3296/rpcbind
udp6 0 0 :::49330 :::* 2998/rpc.statd
udp6 0 0 :::20048 :::* 3314/rpc.mountd
udp6 0 0 :::111 :::* 3296/rpcbind
[root@nfs ~]# showmount -e 192.168.100.104
Export list for 192.168.100.104:
/opt/webapp 192.168.100.0/24
[root@nfs ~]# vim /opt/webapp/WebRoot/WEB-INF/classes/database.properties
driver=com.mysql.jdbc.Driver
#在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8
url=jdbc:mysql://192.168.100.105:3306/smbms?useUnicode=true&characterEncoding=utf-8
user=linux
password=123123
Memcached1服务器的部署 192.168.100.106
[root@mem1 ~]# tar zxvf libevent-release-1.4.15-stable.tar.gz -C /usr/src/
[root@mem1 ~]# cd /usr/src/libevent-release-1.4.15-stable/
[root@mem1 libevent-release-1.4.15-stable]# ./autogen.sh
[root@mem1 libevent-release-1.4.15-stable]# ./configure --prefix=/usr/local/libevent
[root@mem1 libevent-release-1.4.15-stable]# make && make install
[root@mem1 libevent-release-1.4.15-stable]# cd
[root@mem1 ~]# ln -s /usr/local/libevent/lib/libevent* /usr/lib64/
[root@mem1 ~]# tar zxvf memcached-1.5.10.tar.gz -C /usr/src/
[root@mem1 ~]# cd /usr/src/memcached-1.5.10/
[root@mem1 memcached-1.5.10 ~]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[root@mem1 memcached-1.5.10 ~]# make && make install
[root@mem1 memcached-1.5.10 ~]# cd
[root@mem1 ~]# ln -s /usr/local/memcached/bin/* /usr/local/bin/
部署master节点的magent程序,部署完成传送给slave节点;
[root@mem1 ~]# ls magent-0.5.tar.gz
magent-0.5.tar.gz
[root@mem1 ~]# mkdir magent
[root@mem1 ~]# tar zxvf magent-0.5.tar.gz -C magent/
ketama.c
magent.c
ketama.h
Makefile
[root@mem1 ~]# cd magent
[root@mem1 magent]# ls
ketama.c ketama.h magent.c Makefile
[root@mem1 magent]# vi ketama.h ##在文件开头添加
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
[root@mem1 magent]# vi Makefile
1 LIBS = -levent -lm -L /usr/local/libevent/lib ##选项为小-L
2 INCLUDE= -I /usr/local/libevent/include ##选项为大-i
[root@mem1 magent]# make
gcc -Wall -O2 -g -I /usr/local/libevent/include -c -o magent.o magent.c
gcc -Wall -O2 -g -I /usr/local/libevent/include -c -o ketama.o ketama.c
gcc -Wall -O2 -g -o magent magent.o ketama.o -levent -lm -L /usr/local/libevent/lib
[root@mem1 magent]# ls
ketama.c ketama.h ketama.o magent magent.c magent.o Makefile
[root@mem1 magent]# cp magent /usr/bin/
[root@mem1 magent]# scp magent 192.168.100.107:/usr/bin/
[root@mem1 magent]# cd
配置master节点的keepalived服务;
[root@mem1 ~]# yum -y install keepalived
[root@mem1 ~]# vi /etc/keepalived/keepalived.conf
global_defs {
router_id R1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.250
}
}
[root@mem1 ~]# vi /opt/check.sh
#!/bin/bash
while true;do
K=$(ip a|grep 192.168.100.250|wc -l)
if [ $K -ne 0 ];then
magent -u root -n 51200 -l 192.168.100.250 -p 12000 -s 192.168.100.106:11211 -b 192.168.100.107:11211
else
pkill -9 magent
fi
M=$(netstat -utpln |grep mem|wc -l)
if [ $M -eq 0 ];then
systemctl stop keepalived
fi
done
注解:magent参数详解:
-u ##指定运行用户
-n ##最大的连接数,默认为4096
-l ##小写L,magent监听的ip地址
-p ##magent监听的端口
-s ##设置memcached主缓存的ip地址和端口
-b ##设置memcached备缓存的ip地址和端口
[root@mem1 ~]# chmod +x /opt/check.sh
[root@mem1 ~]# memcached -u root -d -m 128m
[root@mem1 ~]# netstat -utpln |grep memcached
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 7117/memcached
[root@mem1 ~]# systemctl start keepalived
[root@mem1 ~]# /opt/check.sh &> /dev/null &
[root@mem1 ~]# jobs -l
[1]+ 16273 运行中 /opt/check.sh &
[root@mem1 ~]# ip a|grep 192.168.100.250
inet 192.168.100.250/32 scope global eth0
[root@mem1 ~]# netstat -utpln |grep magent
tcp 0 0 192.168.100.250:12000 0.0.0.0:* LISTEN 22834/magent
Memcached2服务器的部署 192.168.100.107
[root@mem1 ~]# tar zxvf libevent-release-1.4.15-stable.tar.gz -C /usr/src/
[root@mem1 ~]# cd /usr/src/libevent-release-1.4.15-stable/
[root@mem2 libevent-release-1.4.15-stable]# ./autogen.sh
[root@mem2 libevent-release-1.4.15-stable]# ./configure --prefix=/usr/local/libevent
[root@mem2 libevent-release-1.4.15-stable]# make && make install
[root@mem2 libevent-release-1.4.15-stable]# cd
[root@mem2 ~]# ln -s /usr/local/libevent/lib/libevent* /usr/lib64/
[root@mem2 ~]# tar zxvf memcached-1.5.10.tar.gz -C /usr/src/
[root@mem2 ~]# cd /usr/src/memcached-1.5.10/
[root@mem2 memcached-1.5.10 ~]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[root@mem2 memcached-1.5.10 ~]# make && make install
[root@mem2 memcached-1.5.10 ~]# cd
[root@mem2 ~]# ln -s /usr/local/memcached/bin/* /usr/local/bin/
配置slave节点的keepalived服务;
[root@mem2 ~]# yum -y install keepalived
[root@mem2 ~]# vi /etc/keepalived/keepalived.conf
global_defs {
router_id R2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 1
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.250
}
}
[root@mem2 ~]# vi /opt/check.sh
#!/bin/bash
while true;do
K=$(ip a|grep 192.168.100.250|wc -l)
if [ $K -ne 0 ];then
memcached -u root -d -m 128m
magent -u root -n 51200 -l 192.168.100.250 -p 12000 -s 192.168.100.107:11211 -b 192.168.100.106:11211
else
pkill magent
fi
done
[root@mem2 ~]# chmod +x /opt/check.sh
[root@mem2 ~]# /opt/check.sh &
[1] 18066
[root@mem2 ~]# jobs -l
[1]+ 18066 完成 /opt/check.sh
[root@mem2 ~]# memcached -u root -d -m 128m
[root@mem2 ~]# netstat -utpln |grep mem
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 17920/memcached
[root@mem2 ~]# systemctl start keepalived
Tomcat1服务器的部署192.168.100.102
[root@tomcat1 ~]#tar jdk-7u51-linux-x64.gz
[root@tomcat1 ~]#mv jdk1.7.0_51/ /usr/local/java
[root@tomcat1 ~]#cat <
export JAVA_HOME=/usr/local/java
export PATH=$PATH:/usr/local/java/bin
END
[root@tomcat1 ~]#source /etc/profile
[root@tomcat1 ~]# java -version
[root@tomcat1 ~]#tar zxvf apache-tomcat-7.0.54.tar.gz
[root@tomcat1 ~]# mv apache-tomcat-7.0.54 /usr/local/tomcat
kryo序列化tomcat配置
[root@tomcat1 ~]# vim /usr/local/tomcat/conf/context.xml #在Context区域添加红色部分内容
memcachedNodes="n1:192.168.100.250:12000"
sticky="false"
requestUriIgnorePattern= ".*\.(png|gif|jpg|css|js)$"
sessionBackupAsync= "true"
sessionBackupTimeout= "1800000"
copyCollectionsForSerialization="false"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
在目录/usr/local/tomcat/lib/下添加
[root@tomcat1 ~]#/usr/local/tomcat/bin/startup.sh
[root@tomcat1 ~]#vim /usr/local/tomcat/conf/server.xml
unpackWARs="true" autoDeploy="true">
[root@tomcat1 ~]#/usr/local/tomcat/bin/shutdown.sh
[root@tomcat1 ~]#/usr/local/tomcat/bin/startup.sh
[root@tomcat1 ~]# netstat -tunlp | grep java
tcp6 0 0 :::8080 :::* LISTEN 2056/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 2056/java
tcp6 0 0 :::8009 :::* LISTEN 2056/java
[root@tomcat1 ~]#yum -y install rpcbind nfs-utils
[root@tomcat1 ~]#systemctl enable rpcbind
[root@tomcat1 ~]# systemctl enable nfs
[root@tomcat1 ~]# systemctl start rpcbind
[root@tomcat1 ~]# systemctl start nfs
[root@tomcat1 ~]#echo 3 >/proc/sys/vm/drop_caches
[root@tomcat1 ~]# showmount -e 192.168.100.104
Export list for 192.168.100.104:
/opt/webapp 192.168.100.0/24
[root@tomcat1 ~]# cat /etc/fstab | tail -1
192.168.100.104:/opt/webapp /usr/local/tomcat/wbeapps nfs defaults,_netdev 0 0 #进行挂载
[root@tomcat1 ~]# mount -a
[root@tomcat1 ~]# df -hT | tail -1
192.168.100.104:/opt/webapp nfs4 17G 5.7G 12G 34% /usr/local/tomcat/weapps
Tomcat2服务器的部署192.168.100.103
[root@tomcat2 ~]#tar zxvf jdk-7u51-linux-x64.gz
[root@tomcat2 ~]#mv jdk1.7.0_51/ /usr/local/java
[root@tomcat2 ~]#cat <
export JAVA_HOME=/usr/local/java
export PATH=$PATH:/usr/local/java/bin
END
[root@tomcat2 ~]#source /etc/profile
[root@tomcat2 ~]# java -version
[root@tomcat2 ~]#tar zxvf apache-tomcat-7.0.54.tar.gz
[root@tomcat2 ~]#mv apache-tomcat-7.0.54 /usr/local/tomcat
kryo序列化tomcat配置
[root@tomcat2 ~]# vim /usr/local/tomcat/conf/context.xml #在Context区域添加红色部分内容
memcachedNodes="n1:192.168.100.250:12000"
sticky="false"
requestUriIgnorePattern= ".*\.(png|gif|jpg|css|js)$"
sessionBackupAsync= "true"
sessionBackupTimeout= "1800000"
copyCollectionsForSerialization="false"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
在目录/usr/local/tomcat/lib/下添加 同tomcat1
[root@tomcat2 ~]#/usr/local/tomcat/bin/startup.sh
[root@tomcat2 ~]#vim /usr/local/tomcat/conf/server.xml
unpackWARs="true" autoDeploy="true">
[root@tomcat2 ~]#/usr/local/tomcat/bin/shutdown.sh
[root@tomcat2 ~]#/usr/local/tomcat/bin/startup.sh
[root@tomcat2 ~]# netstat -tunlp | grep java
tcp6 0 0 :::8080 :::* LISTEN 2056/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 2056/java
tcp6 0 0 :::8009 :::* LISTEN 2056/java
[root@tomcat2 ~]#yum -y install rpcbind nfs-utils
[root@tomcat2 ~]#systemctl enable rpcbind
[root@tomcat2 ~]# systemctl enable nfs
[root@tomcat2 ~]# systemctl start rpcbind
[root@tomcat2 ~]# systemctl start nfs
[root@tomcat2 ~]#echo 3 >/proc/sys/vm/drop_caches
[root@tomcat2 ~]# showmount -e 192.168.100.104
Export list for 192.168.100.104:
/opt/webapp 192.168.100.0/24
[root@tomcat2 ~]# cat /etc/fstab | tail -1
192.168.100.104:/opt/webapp /usr/local/tomcat/webapps nfs defaults,_netdev 0 0 #进行挂载
[root@tomcat2 ~]# mount -a
[root@tomcat2 ~]# df -hT | tail -1
192.168.100.104:/opt/webapp nfs4 17G 5.7G 12G 34% /usr/local/tomcat/weapps
MySQL服务器的部署
[root@mysql ~]# yum -y install mariadb-server mysql
[root@mysql ~]# systemctl start mariadb
[root@mysql ~]# mysqladmin -uroot password 123123
[root@mysql ~]# mysql -uroot -p123123 < smbms_db.sql
[root@mysql ~]# mysql -uroot -p123123
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| smbms |
| test |
+--------------------+
MariaDB [(none)]> grant all on smbms.* to 'linux'@'192.168.100.102' identified by '123123';
MariaDB [(none)]> grant all on smbms.* to 'linux'@'192.168.100.103' identified by '123123';
访问测试