實驗環境:
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 <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