##临时关闭selinux~]# setenforce 0
图片服务器centos 1:192.168.150.149 (须要单独安装一个lrzsz来导入图片使用)文档服务器centos 3:192.168.150.152
centos 2:内网:192.168.150.154外网:172.16.251.68centos 4:内网:192.168.150.155外网:172.16.251.162
~]#cd /var/www/html(拖一个图片文件到这个目录)##查看目录下内容文件已经存在~l]# lsup.png##启动服务~]# systemctl start httpd##查看80端口已经启用~]# ss -tanlState Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0 128 *:22 *:*LISTEN 0 100 127.0.0.1:25 *:*LISTEN 0 128 :::80 :::*LISTEN 0 128 :::22 :::*LISTEN 0 100 ::1:25 :::*
##这里使用默认目录以及默认文件。vim /var/www/html/index.html<h1>centos7.3</h1>##启动服务~]# systemctl start httpd##查看80端口已经启用~]# ss -tanlState Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0 128 *:22 *:*LISTEN 0 100 127.0.0.1:25 *:*ISTEN 0 128 :::80 :::*LISTEN 0 128 :::22 :::*LISTEN 0 100 ::1:25 :::*
##文件是本身建立的,没有修改原配置文件,可是建立的文件必须在conf.d目录下以.conf结尾,由于在nginx.conf文件中有设定;vim /etc/nginx/conf.d/ap.conf##给服务器配置缓存功能;三级目录,定义缓存关键字以及大小,磁盘上用于缓存数据的上限;proxy_cache_path /data/web levels=1:1:1 keys_zone=pxycache:20m max_size=1g;##配置一个server;server {##监听在本机全部IP 的80端口;listen 80;##给反代的web服务器配置的域名;server_name www.myue.com;##设置网站根目录;root /var/www/html;##指明要调用的缓存;proxy_cache pxycache;##设置缓存的key;proxy_cache_key $request_uri;##定义状态码20三、30二、304缓存多久;proxy_cache_valid 200 302 304 1h;##定义除了以上的状态码另外的状态缓存多久;proxy_cache_valid any 1m;##文档访问IP ;location / {proxy_pass http://192.168.150.149:80;}##图片访问IP,用正则匹配。由于我只配置了一个图片因此只匹配.png的图片,能够根据本身须要进行配置;location ~* \.png$ {proxy_pass http://192.168.150.152:80;}}##手动建立缓存目录;~]#mkdir /data/web -pv##测试配置文件语法;~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfulhtml##注意keepalived重启不稳定,若是须要重启须要stop 而后在start,双主模式下只能启动不能中止;~]# systemctl stop keepalived~]# systemctl start keepalived
~]# vim /etc/keepalived/keepalived.conf##默认起始行! Configuration File for keepalived##全局配置;global_defs {##故障出现是发送的邮箱地址,这里设置为本主机;notification_email {root@localhost}##表示发送邮件的源地址;notification_email_from keepalived@localhost##发送有限的smtp服务器地址;smtp_server 127.0.0.1##超时时长smtp_connect_timeout 30##组广播地址vrrp_mcast_group4 224.1.100.100}##keepalived从服务器定义;vrrp_instance VI_1 {##定义为从,可选项MASTER|BACKUP;state BACKUP##使用的网卡;interface ens33##定义组ID;virtual_router_id 51##定义优先级,从服务器的优先级要比主服务器低;priority 96##多久在组里广播一下本身的心跳信息,默认单位为秒s;advert_int 1##通讯时认证;authentication {##使用简单字符串方式,不能超过8个字符,超过部分不予理会;auth_type PASS##主从两个服务器的字符串要设定为相同的;auth_pass YN9jd93d}##检测网卡状态;track_interface {ens33}##给keepalived配置的虚拟IP;virtual_ipaddress {172.16.100.100/16}}vrrp_instance VI_2 {##主服务器,由于是双主模式,因此一个服务器能够即为主又为从,可在网站PV量较大的时候 两个服务器同时工做提升效率;state MASTERinterface ens33##组ID和上面配置不能相同,可是和须要相同组内的ID要保持一致;virtual_router_id 41##优先级定义100,本组内的从服务器优先级要低于这个数字;priority 100advert_int 1authentication {auth_type PASS##这里要和本组内的字符串保持一致;auth_pass YN98d93d}track_interface {ens33}virtual_ipaddress {172.16.100.101/16}}##启动nginx 以及keepalived;~]# systemctl start nginx~]# systemctl stop keepalived~]# systemctl start keepalived
##大体配置相同,只有几处地方不一样,上文已经指出,而且下面已经用红颜色标注,这里不过多介绍;! Configuration File for keepalivedglobal_defs {notification_email {root@localhost}notification_email_from keepalived@localhostsmtp_server 127.0.0.1smtp_connect_timeout 30vrrp_mcast_group4 224.1.100.100}vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass YN9jd93d}track_interface {ens33}virtual_ipaddress {172.16.100.100/16}}vrrp_instance VI_2 {state BACKUPinterface ens33virtual_router_id 41priority 96advert_int 1authentication {auth_type PASSauth_pass YN98d93d}track_interface {ens33}virtual_ipaddress {172.16.100.101/16}}##启动nginx 以及keepalived~]# systemctl start nginx~]# systemctl stop keepalived~]# systemctl start keepalived
##抓包组IP试一下看到两个服务器都为主而且都每隔1s在组内发送一下本身的心跳~]# tcpdump -i ens33 -nn host 224.1.100.100tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on ens33, link-type EN10MB (Ethernet), capture size 65535 bytes10:17:27.566889 IP 172.16.251.68 > 224.1.100.100: VRRPv2, Advertisement, vrid 41, prio 100, authtype simple, intvl 1s, length 2010:17:27.951391 IP 172.27.35.8 > 224.1.100.100: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 2010:17:28.567182 IP 172.16.251.68 > 224.1.100.100: VRRPv2, Advertisement, vrid 41, prio 100, authtype simple, intvl 1s, length 2010:17:28.952235 IP 172.27.35.8 > 224.1.100.100: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20^C13 packets captured13 packets received by filter0 packets dropped by kernel
C:\Windows\System32\drivers\etc\HOSTS ##文件最下面添加两行解析linux
##把两个IP解析为相同域名,实现简单的轮询效果, 保存退出nginx
172.16.100.100 www.myue.comweb
172.16.100.101 www.myue.comvim
##中止一个nginx 或者中止一个keepalived一样能够访问centos
~]# systemctl stop nginx 缓存
~]# systemctl stop keepalived服务器
##而且缓存目录中已经有内容,而且三级目录。负载均衡
~]# ls /data/web/9/d/7/6666cd76f96956469e7be39d750cc7d9
/data/web/9/d/7/6666cd76f96956469e7be39d750cc7d框架