实验环境:
NGINX CentOS 7.2x86_64 IP:172.16.253.94 192.168.1.10
RealServer1 CentOS 7.2x86_64 IP:192.168.1.20
RealServer2 CentOS 6.7x86_64 IP:192.168.1.30
client rhel-5.5 IP:172.16.251.75
RealServer1:
[iyunv@localhost ~]# iptables -F
[iyunv@localhost ~]# setenforce 0
[iyunv@localhost ~]# yum -y install httpd php php-mysql mariadb-server [iyunv@localhost ~]# systemctl httpd.service mariadb.service [iyunv@localhost ~]# echo "RealServer 1" >> /var/www/html/index.html RealServer2:
[iyunv@localhost ~]# iptables -F
[iyunv@localhost ~]# setenforce 0
[iyunv@localhost ~]# yum -y install httpd php php-mysql mysql-server [iyunv@localhost ~]# service httpd restart [iyunv@localhost ~]# service httpd mysql [iyunv@localhost ~]# echo "RealServer 2" >> /var/www/html/index.html
配置负载均衡:
1.安装NGINX:
[iyunv@pxe94 ~]# iptables -F
[iyunv@pxe94 ~]# setenforce 0
[iyunv@pxe94 ~]# yum -y install nginx-1.10.1-1.el7.ngx.x86_64.rpm [iyunv@pxe94 ~]# rpm -ql nginx 2.启动服务: [iyunv@pxe94 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[iyunv@pxe94 ~]# nginx [iyunv@pxe94 ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:* 3.配置upstream:
[iyunv@pxe94nginx]# vim /etc/nginx/nginx.conf http { 省略部分… upstream websrvs{ server 192.168.1.20; server 192.168.1.30; } 省略部分… } [iyunv@pxe94nginx]# vim /etc/nginx/conf.d/default.conf server { 省略部分… location / { root /usr/share/nginx/html; index index.html index.htm; proxy_pass http://websrvs; } 省略部分… } 4.测试:(默认轮询算法)
[iyunv@station75~]# for i in {1..10}; do curl http://172.16.253.94; done
RealServer 1
RealServer 2
RealServer 1
RealServer 2
RealServer 1
RealServer 2
RealServer 1
RealServer 2
RealServer 1
RealServer 2
5.配置调度器算法:
[iyunv@pxe94nginx]# vim /etc/nginx/nginx.conf
http {
省略部分…
upstream websrvs{ server 192.168.1.20; server 192.168.1.30; hash $request_uri consistent; //一致性hash算法:将同一个url请求发往同一个RealServer } 省略部分... }
6.测试hash算法:
[iyunv@station75~]# for i in {1..10}; do curl http://172.16.253.94/; done
RealServer 1
RealServer 1
RealServer 1
RealServer 1
RealServer 1
RealServer 1
RealServer 1
RealServer 1
RealServer 1
RealServer 1
Memcached缓存服务:
1.安装:
[iyunv@pxe94 ~]#yum -y install memcached
[iyunv@pxe94 ~]#rpm -ql memcache
[iyunv@pxe94 ~]#cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
[iyunv@pxe94 ~]#ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:11211 *:*
2.Memcahed简单配置:
[iyunv@station75~]# telnet 172.16.253.94 11211
Trying172.16.253.94...
Connected topxe94.magelinux.com (172.16.253.94).
Escape characteris '^]'.
命令:
统计类:stats、stats items、stats slabs、stats sizes
存储类:set、add、replace、append、prepend
获取数据类:get、delete、incr/decr
清空:flush_all
常用选项:
-m <num>:缓存空间大小,单位为MB,默认64
-c <num>:并发连接数,默认1024
-u USERNAME:程序的运行着
-p:监听的TCP端口
-U:监听的UDP端口
-i<ip_addr>:监听的ip地址
-M:缓存空间耗尽时,向请求存储缓存项返回错误信息
-f<factor>:chunk大小增长因子(默认1.25倍)
-t<num>:线程数量,默认为4
|