实验环境:
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 :
[[email protected] ~]# iptables -F
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum -y install httpd php php-mysql mysql-server
[[email protected] ~]# service httpd restart
[[email protected] ~]# service httpd mysql
[[email protected] ~]# echo "RealServer1 " >> /var/www/html/index.html
RealServer2:
[[email protected] ~]# iptables -F
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum -y install httpd php php-mysql mariadb-server
[[email protected] ~]# systemctl start httpd.service mariadb.service
[[email protected] ~]# echo "RealServer2 " >> /var/www/html/index.html
负载均衡httpd:
1.安装NGINX:
[[email protected] ~]# iptables -F
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum -y install nginx-1.10.1-1.el7.ngx.x86_64.rpm
[[email protected] ~]# rpm -ql nginx
2.启动服务:
[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] ~]# nginx
[[email protected] ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
3.定义upstream负载均衡:
[[email protected]]# vim /etc/nginx/nginx.conf
http {
省略部分…
upstream websrvs{
server 192.168.1.20;
server 192.168.1.30;
}
省略部分…
}
4.调用upstream模块:
[[email protected]]# vim /etc/nginx/conf.d/default.conf
server {
省略部分…
location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://websrvs;
}
省略部分…
}
5.客户端测试:(默认轮询算法)
[[email protected]
~]# 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.配置调度器算法:
[[email protected]]# 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算法:
[[email protected]~]# 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服务:
[[email protected]]# 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;
}
}
[[email protected]]#nginx -s stop
[[email protected]]#nginx
客户端远程登陆:
ssh -P 22202 [email protected]
Memcached缓存服务:
[[email protected] ~]#yum -y install memcached
[[email protected] ~]#rpm -ql memcache
[[email protected] ~]#cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
[[email protected] ~]#ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:11211 *:*
Memcahed简单配置:
[[email protected]
~]# 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