实验环境:

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:

[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0

[root@localhost ~]# yum -y install  httpd php php-mysql mariadb-server

[root@localhost ~]# systemctl httpd.service mariadb.service

[root@localhost ~]# echo "RealServer 1" >> /var/www/html/index.html

RealServer2:

[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install httpd php php-mysql mysql-server

[root@localhost ~]# service httpd restart

[root@localhost ~]# service httpd mysql

[root@localhost ~]# echo "RealServer 2" >> /var/www/html/index.html


配置负载均衡:

1.安装NGINX:

[root@pxe94 ~]# iptables -F
[root@pxe94 ~]# setenforce 0

[root@pxe94 ~]# yum -y install nginx-1.10.1-1.el7.ngx.x86_64.rpm

[root@pxe94 ~]# rpm -ql nginx

2.启动服务:

[root@pxe94 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@pxe94 ~]# nginx

[root@pxe94 ~]# ss -tnl
State      Recv-Q Send-Q      Local Address:Port                     Peer Address:Port             
LISTEN     0      128                     *:80                                  *:*  

3.配置upstream:

[root@pxe94nginx]# vim /etc/nginx/nginx.conf

http {

省略部分

upstream websrvs{

        server 192.168.1.20;

        server 192.168.1.30;

    }

省略部分

}

[root@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.测试:(默认轮询算法)

[root@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.配置调度器算法:

[root@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算法:

[root@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.安装:

[root@pxe94 ~]#yum -y install memcached

[root@pxe94 ~]#rpm -ql memcache

[root@pxe94 ~]#cat /etc/sysconfig/memcached

PORT="11211"

USER="memcached"

MAXCONN="1024"

CACHESIZE="64"

OPTIONS=""

[root@pxe94 ~]#ss -tnl

State      Recv-Q Send-Q      Local Address:Port                     Peer Address:Port             

LISTEN     0     128                    *:11211                              *:*  


2.Memcahed简单配置:

[root@station75 ~]# telnet 172.16.253.94 11211

Trying 172.16.253.94...

Connected to pxe94.magelinux.com (172.16.253.94).

Escape character is '^]'.

命令:

统计类:stats、stats items、stats slabs、stats sizes
存储类:set、add、replace、append、prepend
获取数据类:get、delete、incr/decr
清空:flush_all

常用选项:

-m :缓存空间大小,单位为MB,默认64
-c :并发连接数,默认1024
-u USERNAME:程序的运行着
-p:监听的TCP端口
-U:监听的UDP端口
-i:监听的ip地址
-M:缓存空间耗尽时,向请求存储缓存项返回错误信息
-f:chunk大小增长因子(默认1.25倍)

-t:线程数量,默认为4