实验环境:

NGINX                         CentOS 7.2x86_64            IP:172.16.253.94    192.168.1.10

RealServer1                 CentOS 6.7x86_64            IP:192.168.1.20

RealServer2                 CentOS 7.2x86_64            IP:192.168.1.30

client                           RHEL5.5                            IP:172.16.251.75


RealServer1 :

[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 "RealServer1 " >> /var/www/html/index.html


RealServer2:

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

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

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


负载均衡httpd:

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;

    }

省略部分…

}

4.调用upstream模块:

[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;

    }

省略部分…

}

5.客户端测试:(默认轮询算法)

[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

6.配置调度器算法:

[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

  }

省略部分…

}

7.测试一致性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



负载均衡ssh服务:

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

stream{
 upstream sshsrvs{
  server 192.168.1.20:22;
  server 192.168.1.30:22;
 }

 server {
  listen 172.16.253.94:22202;
  proxy_pass sshsrvs;
 }
}

[root@pxe94nginx]#nginx -s stop

[root@pxe94nginx]#nginx

客户端远程登陆:

ssh -P 22202 root@172.16.253.94



Memcached缓存服务:

[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                              *:*  


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<num>:缓存空间大小,单位为MB,默认64
-c<num>:并发连接,默认为1024
-u USERNAME:程序的运行者
-p PORT:监听的TCP端口
-U PORT:监听的UDP端口
-l <ip_addr>:监听的Ip地址
-M:缓存耗尽时,向请求存储缓存项返回错误信息
-f<factor>:chunk大小增长因子
-t<num>:线程数量,默认为4