1. Cluster preparation
Cluster: a group of servers doing the same work
1.1 cluster construction of virtual environment
The web01 virtual machine will be shut down first for cloning. Boot configuration ip hostname
The configuration of the web cluster is as follows:
Host name | ip |
---|---|
web01 | 192.168.246.7 |
web02 | 192.168.246.8 |
web03 | 192.168.246.9 |
Modify host name hostnamectl set-hostname web02 modify ip sed -i 's#\.8#.9#g' /etc/sysconfig/network-scripts/ifcfg-eth[01] sed -i '/UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth[01] Restart the network systemctl restart network
Just reconnect
1.2 deployment of Enterprise Clusters
1. First deploy an lnmp server and upload code information
2. Conduct access test
3. Batch deploy multiple web servers (ansible)
4,. Distribute nginx configuration files
5. Distribute the site directory to all hosts
2. Configuration of load balancing server
2.1 clone a new machine with template machine
Modify ip and host name. The method is the same as above
hostname | ip |
---|---|
lb01 | 192.168.246.5 |
2.2 installation nginx
Configure yum source: https://nginx.org/en/linux_packages.html
vim //etc/yum.repos.d/nginx.repo
[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
Installing nginx:
yum -y install nginx
Start:
systemctl start nginx systemctl enable nginx
2.3 write the configuration file of nginx for load balancing
Two modules
1. Load balancing
ngx_http_upstream_module --upstream
2. Reverse proxy (e.g. intranet – intranet – extranet (from inside to outside))
ngx_http_proxy_module --proxy_pass
2.4 simple load balancing profile
vim /etc/nginx/conf.d/lb.conf
upstream yq{ server 192.168.246.7:80; server 192.168.246.8:80; server 192.168.246.8:80; } server { listen 80; server_name www.yq.com; location / { proxy_pass http://yq; } }
systemctl restart nginx
There is no problem with such a simple configuration when one website is running, but multiple websites will appear:
1. The website page is disordered
2. If the server has a problem, it also displays an error
3. Unable to collect the customer's ip address for statistical analysis
2.5 special attention: upstream yq and proxy_ pass http://yq The back YQ of the must be the same
2.6 realize load balancing function
Three web Operations (all operations):
mkdir /html/{bbs,www} -p
web01 for name in www bbs ;do echo "$name 192.168.246.7">/html/$name/idenx.html;done web02 for name in www bbs ;do echo "$name 192.168.246.8">/html/$name/idenx.html;done web03 for name in www bbs ;do echo "$name 192.168.246.9">/html/$name/idenx.html;done
chown -R www.www /html
nginx profile:
/Create www.conf bbs.conf under / etc/nginx/conf.d /
www.conf:
server{ listen 80; server_name www.yq.com; access_log /var/log/nginx/www_log.log main; error_log /var/log/nginx/www_log.log warn; location /{ root /html/www; index index.html; } }
bbs.conf:
server{ listen 80; server_name bbs.yq.com; access_log /var/log/nginx/www_log.log main; error_log /var/log/nginx/www_log.log warn; location /{ root /html/bbs; index index.html; } }
systemctl reload nginx
Configure windows hosts file
192.168.246.5 blog.yq.com www.yq.com
Browser access:
www.yq.com bbs.yq.com
Load balancing server lb01:
vim /etc/hosts 172.16.1.5 www.yq.com bbs.yq.com
curl www.yq.com curl bbs.yq.com
3. ngx_http_upstream_module details
3.1 different scheduling functions
1. The default is average allocation
2. Self distribution (more capable)
To modify a profile:
upstream yq{ server 192.168.246.7:80 weight=3; server 192.168.246.8:80 weight=2; server 192.168.246.8:80 weight=1; }
Six such visits,
3 times on web01(192.168.246.7)
Twice web02(192.168.246.8)
1 web01(192.168.246.9)
3.2 backup function
configuration file
upstream yq{ server 192.168.246.7:80; server 192.168.246.8:80; server 192.168.246.8:80 backup; }
If the first two servers don't work, the third one will be used
3.3 maximum number of failures. Resend the request after failure (max_failures = 5 fail timeout = 10s)
configuration file
upstream yq{ server 192.168.246.7:80 max_fails=5 fail_time=10s; server 192.168.246.8:80; server 192.168.246.8:80; }
After the web01 connection fails five times, the connection will be sent again in 10s
3.4 different scheduling algorithms
1.rr rotation training scheduling algorithm
2.wrr weight scheduling algorithm
3.ip_ Hash (when repeated login occurs)
When we log in to the website, the card will appear in the login interface. At this time, the request is a different web server every time
Solution: ip_hash
configuration file
upstream yq{ ip_hash; server 192.168.246.7:80; server 192.168.246.8:80; server 192.168.246.8:80 backup; }
In this way, when there is little access, it is based on his MD5 that the server is accessed all the time, and the load balance is uneven
3.2 the cache server can also solve this problem (later)
4.lest_ Conn (allocate resources according to the number of connections to the server)
4. ngx_http_proxy_module module
Problems that can be solved:
4.1 when visiting different websites, problems of different websites cannot be displayed
Edit profile:
upstream yq { server 192.168.246.7:80; server 192.168.246.8:80; server 192.168.246.9:80; } server { listen 80; server_name www.yq.com; location / { proxy_pass http://yq; #Different URLs display different interfaces proxy_set_header Host $host; } }
4.2 the address information of access users cannot be counted
proxy_set_header X-Forwarded-For $remote_addr;
upstream yq { server 192.168.246.7:80; server 192.168.246.8:80; server 192.168.246.9:80; } server { listen 80; server_name www.yq.com; location / { proxy_pass http://yq; #Different URLs display different interfaces proxy_set_header Host $host; # The address information of users accessing the website cannot be analyzed and counted proxy_set_header X-Forwarded-For $remote_addr; } }
Format of log file
cat /etc/nginx/nginx.conf
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; $remote_addr Show user access sources IP Address information $remote_user Displays user name information for authentication [$time_local] Displays the time when the site was visited "$request" Request line information of request message $status User access site status code information $body_bytes_sent Displays data size information for the response $http_referer Record the connection address information of calling website resources(Prevent users from stealing chains) $http_user_agent Record what client software the user uses to access the page (Google Firefox IE Android iphone) $http_x_forwarded_for load balancing ip
$http_x_forwarded_for is the ip address accessed
Visit the website to view the log file:
On web01
cat /var/log/nginx/access_www.log
192.168.246.5 - - [20/Sep/2021:10:52:31 +0800] "GET /wenwen.html HTTP/1.0" 200 6 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36" "192.168.246.1"
192.168.246.5 is the address of the load balancing proxy server, and 192.168.246.1 is the ip address accessed
4.3 the error page will show the problem
upstream yq { server 192.168.246.7:80; server 192.168.246.8:80; server 192.168.246.9:80; } server { listen 80; server_name www.yq.com; location / { proxy_pass http://yq; #Different URLs display different interfaces proxy_set_header Host $host; #The error page is not displayed to the user proxy_next_upstream error timeout http_404 http_502 http_403; # The address information of users accessing the website cannot be analyzed and counted proxy_set_header X-Forwarded-For $remote_addr; } }
systemctl reload nginx
4.4 complete configuration file
upstream yq { server 192.168.246.7:80; server 192.168.246.8:80; server 192.168.246.9:80; } server { listen 80; server_name www.yq.com; location / { proxy_pass http://yq; #Different URLs display different interfaces proxy_set_header Host $host; #The error page is not displayed to the user proxy_next_upstream error timeout http_404 http_502 http_403; # The address information of users accessing the website cannot be analyzed and counted proxy_set_header X-Forwarded-For $remote_addr; } } server { listen 80; server_name bbs.yq.com; location / { proxy_pass http://yq; proxy_set_header Host $host; proxy_next_upstream error timeout http_404 http_502 http_403; } }
systemctl reload nginx
This is the configuration file of the blog website; You can add it or not
server { listen 80; server_name blog.yq.com; location / { proxy_pass http://yq; proxy_set_header Host $host; proxy_next_upstream error timeout http_404 http_502 http_403; } }