catalogue
4, Health check for load balancing
5, Brief introduction of nginx for load balancing
3. Configure the load balancing function in nginx
1, What is load balancing?
Load balancing is to distribute the user's access requests to the machines that really provide servers at the back end.
Load balancer is a machine that realizes the function of load balancing
2, Why load balancing?
Load balancing can evenly distribute a large number of service requests to the back end, which will not lead to excessive traffic on some machines and no traffic on some machines.
3, Load balancing algorithm
1.round-robin
Polling: requests to the application server are distributed in a circular manner. Weighted polling can be used. By default, the weight value of all servers is 1. The greater the weight value, the higher the priority
2.ip-hash
A hash function is used to determine which server should be selected for the next request. Load balancing is done based on the ip address of the client, and the same ip address is forwarded to the same server
3.least-connected
Minimum number of connections. The next request is assigned to the server with the least number of active connections
4, Health check for load balancing
Health check is divided into active and passive. Active is how often the load balancer actively detects whether the server is still working normally. Passive is that the load balancer allocates requests to the server and does not get a response.
5, Brief introduction of nginx for load balancing
http://nginx.org/en/docs/http/load_balancing.html
The next experiment is to use nginx to realize the load balancing function. Nginx is a web server that can realize the http function, but nginx can also balance the load of http access.
nginx and lvs are classic open source and free load balancing software.
6, Compile and install nginx
1. Preparation
Prepare a new centos machine as load balancer
IP:192.168.10.227 host name: load balancer1
#Turn off firewall [root@load-balancer1 ~]# service firewalld stop #Set startup not to start firewall [root@load-balancer1 ~]# systemctl disable firewalld #Close selinux [root@load-balancer1 ~]# vim /etc/selinux/config [root@load-balancer1 ~]# vim /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of these three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted #To modify selinux, you need to restart the service [root@manager ~]# reboot
2. Compile and install nginx
[root@load-balancer1 ~]# vim onekey_install_nginx_v10.sh [root@load-balancer1 ~]# cat onekey_install_nginx_v10.sh #To solve the software dependency, install the software package yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make psmisc net-tools lsof vim wget #New users and groups id sanchuang || useradd sanchuang -s /sbin/nologin #Download nginx software mkdir /sanchuang99 -p cd /sanchuang99 wget http://nginx.org/download/nginx-1.21.1.tar.gz #Decompression software tar xf nginx-1.21.1.tar.gz #Enter the extracted folder cd nginx-1.21.1 #Pre compilation configuration ./configure --prefix=/usr/local/scsanchuang99 --user=sanchuang --group=sanchuang --with-http_ssl_module --with-threads --with-http_v2_module --with-http_stub_status_module --with-stream #If the above pre compilation configuration fails, exit the script directly if (( $? != 0));then exit fi #compile make -j 2 #Compile and install make install #Modify PATH variable echo "PATH=$PATH:/usr/local/scsanchuang99/sbin" >>/root/.bashrc #Execute a script that modifies the environment variables source /root/.bashrc #firewalld and selinux #stop firewall and set firewalld not to be started next time service firewalld stop systemctl disable firewalld #Temporarily stop selinux and permanently stop selinux setenforce 0 sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config #Power on chmod +x /etc/rc.d/rc.local echo "/usr/local/scsanchuang99/sbin/nginx" >>/etc/rc.local
#Run the installed script [root@load-balancer1 ~]# bash onekey_install_nginx_v10.sh #Switch users and load the modified PATH variable [root@load-balancer1 ~]# su - root #View nginx commands [root@load-balancer1 ~]# which nginx /usr/local/scsanchuang99/sbin/nginx #start nginx [root@load-balancer1 ~]# nginx #View nginx process [root@load-balancer1 ~]# ps aux|grep nginx root 952 0.0 0.1 44580 820 ? Ss 15:57 0:00 nginx: master process /usr/local/scsanchuang99/sbin/nginx sanchua+ 954 0.0 0.2 77128 2028 ? S 15:57 0:00 nginx: worker process root 7258 0.0 0.1 12344 1108 pts/0 S+ 16:37 0:00 grep --color=auto nginx #View the port of nginx [root@load-balancer1 ~]# ss -anplut|grep nginx tcp LISTEN 0 128 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=954,fd=8),("nginx",pid=952,fd=8))
3. Configure the load balancing function in nginx
#Enter the installation directory of nginx [root@load-balancer1 conf]# cd /usr/local/scsanchuang99/ [root@load-balancer1 scsanchuang99]# ls client_body_temp fastcgi_temp logs sbin uwsgi_temp conf html proxy_temp scgi_temp #Enter the configuration file directory [root@load-balancer1 scsanchuang99]# cd conf/ [root@load-balancer1 conf]# ls fastcgi.conf koi-win scgi_params fastcgi.conf.default mime.types scgi_params.default fastcgi_params mime.types.default uwsgi_params fastcgi_params.default nginx.conf uwsgi_params.default koi-utf nginx.conf.default win-utf #Modify the configuration file of nginx #nginx.conf There is more than that in the file, only the corresponding location of the content to be added is copied,#They are all comment lines and can not be used [root@load-balancer1 conf]# vim nginx.conf http { #keepalive_timeout 0; keepalive_timeout 65; #gzip on; #Define a load balancer called scweb upstream scweb { #Back end server server 192.168.10.230:8889; server 192.168.10.231:8889; server 192.168.10.232:8889; } server { #Listen to port 80 listen 80; #Domain name service. If you need to use domain name access, you need to modify the hosts file server_name www.sc.com; location / { #Call load balancer proxy_pass http://scweb; } #Reload profile [root@load-balancer1 conf]# nginx -s reload
4. Test
Now it's time to access the IP address of the load balancer. The default is to listen to port 80, so there is no need to connect terminals