In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
The following brings you, hope to give you some help in practical application, load balancing involves more things, there are not many theories, there are many books on the Internet, today we use the accumulated experience in the industry to do an answer.
Environment introduction
Configure nginx load balancer because multiple CVMs will be used, so I will use docker below. For specific docker, please move to docker.
System environment: root@ubuntu:~# lsb_release-a # View the system version No LSB modules are available.Distributor ID: UbuntuDescription: Ubuntu 19.10Release: 19.10Codename: eoanroot@ubuntu:~# uname-a # to see how many bits the system is Linux ubuntu 5.3.0-18-generic # 19-Ubuntu SMP Tue Oct 8 20:14:06 UTC 2019 x86 "64 GNU/Linux
Docker version and related operating system version docker version: root@ubuntu:~# docker-- version # View docker version Docker version 19.03.3 Build a872fc2f86 operating system version: [root@57b669db1de1 /] # cat / etc/redhat-release # View system version CentOS Linux release 8.0.1905 (Core) [root@57b669db1de1 /] # uname-aLinux 57b669db1de1 5.3.0-18-generic # 19-Ubuntu SMP Tue Oct 8 20:14:06 UTC 2019 x86 "64 GNU/Linux Software version: nginx version: root@ubuntu:~# nginx-v View nginx version Nginx version: nginx version in nginx/1.16.1 (Ubuntu) docker: [root@57b669db1de1 /] # nginx-v # View nginx version nginx version: nginx/1.14.1 installation nginx
For specific nginx installation, please refer to nginx installation and deployment.
Install dependent software update package: root@ubuntu:~# apt-get update install dependent software: root@ubuntu:~# apt- y install openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev make gcc compile install nginx download nginx:root@ubuntu:~# wget http:#nginx.org/download/nginx-1.17.6.tar.gz extract: root@ubuntu:/opt# tar zxf nginx-1.17.6.tar.gz root@ubuntu:/opt# cd nginx -1.17.6/root@ubuntu:/opt/nginx-1.17.6# ls # lists the directory auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README srcroot@ubuntu:/opt/nginx-1.17.6# configuration: root@ubuntu:/opt/nginx-1.17.6#. / configure-- prefix=/usr/local/nginx compilation and deployment: root@ubuntu:/opt/nginx-1.17.6# make & & make installroot@ubuntu: / opt/nginx-1.17.6# cd / usr/local/nginx/root@ubuntu:/usr/local/nginx# lsconf html logs sbin launch: root@ubuntu:/usr/local/nginx# ln sbin/nginx / usr/local/sbin/ # create nginx soft connection Set to command root@ubuntu:/usr/local/nginx# nginx# to start nginxroot@ubuntu:/usr/local/nginx# netstat-anpl | grep nginx# to check whether the nginx port is open root@ubuntu:/usr/local/nginx# lsof-ianpl 80 # check whether port 80 is enabled docker installation nginx create container download image: root@ubuntu:~# docker pull registry.cn-beijing.aliyuncs.com/blxt/centos creation Create the container and launch: root@ubuntu:~# root@ubuntu:~# docker run-itd-- name nginx1-- privileged registry.cn-beijing.aliyuncs.com/blxt/centos / sbin/initroot@ubuntu:~# docker ps-a # check whether the container is created and start CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAME6801d55682a3 registry.cn-beijing.aliyuncs.com/blxt/centos "/ sbin/init" 5 minutes ago Up 5 minutes nginx1 creates a second container in the same way: root@ubuntu:~# docker run-itd-- name nginx2-- privileged registry.cn-beijing.aliyuncs.com/blxt/centos / sbin/initroot@ubuntu:~# docker ps-a root@ Ubuntu:~# root@ubuntu:~# docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1d31d3fc0ec1 registry.cn-beijing.aliyuncs.com/blxt/centos "/ sbin/init" 4 minutes ago Up 4 minutes Nginx26801d55682a3 registry.cn-beijing.aliyuncs.com/blxt/centos "/ sbin/init" 5 minutes ago Up 5 minutes nginx1 connects to the container: root@ubuntu:~# docker exec-it nginx1 / bin/bashroot@ubuntu:~# docker exec-it nginx2 / bin/bash [root@6801d55682a3 /] # hostname nginx1 # change the host name [root@6801d55682a3 /] # bash # to make the changed name effective [root@1d31d3fc0ec1 /] # hostname nginx2 [root@1d31d3fc0ec1 /] # bash install nginx update package: [root@nginx1 /] # yum clean all # clear cache [root@nginx1 /] # yum makecache # update package install nginx: [root @ nginx1 /] # yum-y install nginx [root@nginx1 /] # rpm-qa | grep nginx # check whether it is installed Start nginx: [root @ nginx1 /] # systemctl start nginx [root@nginx1 /] # netstat-anpl | grep nginx # check whether the nginxnn port is open [root@nginx1 /] # lsof-isystemctl start nginx 80 # check whether port 80 is open and install the second nginx using the same method! Omitted here! Visit nginx: [root@nginx1 /] # curl 127.0.0.1 modify web page content to view web page existence directory: [root@nginx1 nginx] # more nginx.conf # View configuration file find the following root line in the configuration file, and the following directory is the web page file storage directory root / usr/share/nginx/html [root@nginx1 html] # cd / usr/share/nginx/html/ [root@nginx1 html] # ls # list directory contents 404.html 50x.html index.html nginx-logo.png poweredby.png [root@nginx1 html] # echo nginx1 > index.html # clear the contents of the nginx home page file and rewrite the contents as nginx1nginx2 Be careful not to set the same homepage content to access nginx: [root@nginx1 html] # curl 127.0.0.1nginx1 [root@nginx2 ~] # curl 127.0.0.1nginx2 modify configuration file nginx1 modify content as follows: server {listen 80 Server_name www.nginx1.com; / / set domain name # charset koi8-r;#access_log logs/host.access.log main;location / {root html; index index.html index.htm;}} nginx2 is modified as follows: server {listen 80th serverSecretname www.nginx2.com; / / set domain name # charset koi8-r;#access_log logs/host.access.log main;location / {root html Index index.html index.htm;}} configure http load balancer
To start load balancing HTTP traffic for a group of servers using NGINX Plus or NGINX open source, define the group using the upstream directive, which is placed in the http context, and the proxy_pass directive is used to forward requests to the backend, which is generally specified in the loction context.
Basic configuration method:
Http {upstream nginx {# test is the group name server www.nginx1.com; # server is used to specify the access address of the backend server. Generally, you can specify the domain name, ip, port, domain name and IP. The port can ignore server www.nginx2.com weight=5; # weight to specify the weight value server www.nginx3.com down. # down is used to stop forwarding to this server. Omit other configurations} server {location / {proxy_pass http://nginx; # http://nginx is the back-end server that forwards that group. Omit other configurations}. Omit other configurations}}
Nginx officially provides four methods for load balancing. These four methods are described one by one:
Round Robin
Requests are distributed evenly among servers, taking into account server weights. This method is used by default (there is no instruction to enable it)
The default configuration is Round Robin, and the configuration method:
Http {upstream nginx {# test is the group name server www.nginx1.com; # server is used to specify the access address of the backend server server www.nginx2.com; # weight specifies the weight value. Omit other configurations} server {location / {proxy_pass http://nginx; # http://nginx is the back-end server that forwards that group. Omit other configurations}. Omit other configurations}} minimum connections
The method of sending requests with the least active connections to the server is also set up taking into account the weight of the server
Configuration method:
Http {upstream nginx {# test is the group name less_conn; server www.nginx1.com; # server is used to specify the access address of the backend server server www.nginx2.com; # weight specifies the weight value. Omit other configurations} server {location / {proxy_pass http://nginx; # http:#nginx is the back-end server that forwards that group. Omit other configurations}. Omit other configurations}} ip_hash
Determine the server to which the request is sent from the client IP address. In this case, the hash value can be calculated using the first three octets of the IPv4 address or the entire IPv6 address. This method ensures that requests from the same address will arrive at the same server unless it is unavailable.
Configuration method:
Http {upstream nginx {# test is the group name ip_hash; server www.nginx1.com; # server is used to specify the access address of the backend server server www.nginx2.com; # weight specifies the weight value. Omit other configurations} server {location / {proxy_pass http://nginx; # http:#nginx is the back-end server that forwards that group. Omit other configurations}. Omit other configurations}} hash
The server to which the request is sent is based on a user-defined key, which can be a text string, variable, or combination.
Configuration method:
Http {upstream nginx {# test is the group name hash $request_uri consistent; server www.nginx1.com; # server is used to specify the access address of the backend server server www.nginx2.com; # weight specifies the weight value. Omit other configurations} server {location / {proxy_pass http://nginx; # http://nginx is the back-end server that forwards that group. Omit other configurations}. Omit other configurations}}
After reading the above introduction of nginx load balancing across multiple application instances, if there is anything else you need to know, you can find out what you are interested in in the industry information or find our professional and technical engineers for answers. Technical engineers have more than ten years of experience in the industry.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.