创作不易,来了的客官点点关注,收藏,订阅一键三连❤😜
前言
运维之基础——Linux。我是一个即将毕业的大学生,超超。如果你也在学习Linux,不妨跟着萌新超超一起学习Linux,拿下Linux,一起加油,共同努力,拿到理想offer!
系列文章
Linux进阶 | ❤Docker+NFS+Volume实现数据一致性❤
Linux进阶 | 超详细全方面的Docker Swarm Web集群介绍与部署!
Linux进阶 | docker compose的安装与使用详解,超详细!
概述
本期内容为Docker第七期,通过本期内容将会掌握Nginx的负载均衡、keepalived实现高可用等,同时将负载均衡+高可用(LB+HA)实现双VIP。
目录
负载均衡
什么是负载均衡?
负载均衡:load balancing,将用户的访问请求均衡的分散到真正提供服务的机器上
负载均衡器:load balancer,实现负载均衡功能的一个机器
为什么要实现负载均衡?
1.能够将大量的请求比较均匀的分散到后端,不会导致某台服务器访问量过大,某个服务又没有访问量
2.高可用(对后端的服务器进行健康检测,如果后端那台服务器出现问题,就不会再将请求转发给它,从而避免用户访问不了服务器,启动一个容错的功能)
负载均衡的算法
1.轮询(roundrobin):默认下为轮询算法,默认情况下所有的服务器的权重值都是1 ,值越大优先级越好(加权轮询)
2.ip_hash:基于客户端的ip地址做负载均衡,相同的ip地址转发到同一个服务器 --》用户的会话信息需要保存的,尽量让这个客户机每次都访问相同的一台
3.least-connected:最小连接数首选遍历后端集群,比较每个后端的conns/weight,选取该值最小的后端。如果有多个后端的conns/weight值同为最小的,那么对它们采用加权轮询算法。
负载均衡的实现
1.准备一台客户机作为负载均衡器
IP:192.168.232.136 主机名:load-balancer担任角色:负载均衡器(需要配置)
IP:192.168.232.132 主机名:docker-manager-1 担任角色:swarm manager
IP:192.168.232.133 主机名:docker-2 担任角色:swarm node1
IP:192.168.232.134 主机名:docker-3 担任角色:swarm node2
IP:192.168.232.131 主机(ubuntu)名:chaochao 担任角色:swarm node3
IP:192.168.232.135 主机名:nfs-server 担任角色:nfs服务器
2.编译脚本
[root@load-balancer ~]# vim onekey_install_lizhichao_nginx_v10.sh
[root@load-balancer ~]# cat onekey_install_lizhichao_nginx_v10.sh
#!/bin/bash
#解决软件的依赖关系,需要安装的软件包
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make psmisc net-tools lsof vim wget
#新建chaochao用户和组
id chaochao || useradd chaochao -s /sbin/nologin
#下载nginx软件
mkdir /lzc_load_balancing -p
cd /lzc_load_balancing
wget http://nginx.org/download/nginx-1.21.1.tar.gz
#解压软件
tar xf nginx-1.21.1.tar.gz
#进入解压后的文件夹
cd nginx-1.21.1
#编译前的配置
./configure --prefix=/usr/local/lzc_load_balancing --user=chaochao --group=chaochao --with-http_ssl_module --with-threads --with-http_v2_module --with-http_stub_status_module --with-stream
#如果上面的编译前的配置失败,直接退出脚本
if (( $? != 0));then
exit
fi
#编译
make -j 2
#编译安装
make install
#修改PATH变量
echo "PATH=$PATH:/usr/local/lzc_load_balancing/sbin" >>/root/.bashrc
#执行修改了环境变量的脚本
source /root/.bashrc
#firewalld and selinux
#stop firewall和设置下次开机不启动firewalld
service firewalld stop
systemctl disable firewalld
#临时停止selinux和永久停止selinux
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config
#开机启动
chmod +x /etc/rc.d/rc.local
echo "/usr/local/lzc_load_balancing/sbin/nginx" >>/etc/rc.local
[root@load-balancer ~]#
3.安装运行脚本
[root@load-balacer ~]# bash onekey_install_lizhichao_nginx_v10.sh
……
test -d '/usr/local/lzc_load_balancing/logs' \
|| mkdir -p '/usr/local/lzc_load_balancing/logs'
make[1]: 离开目录“/lzc_load_balancing/nginx-1.21.1”
Redirecting to /bin/systemctl stop firewalld.service
[root@load-balancer ~]#
4.启动nginx
命令:
nginx 启动nginx
nginx -s stop 关闭nginx
[root@load-balancer nginx-1.21.1]# nginx
[root@load-balancer nginx-1.21.1]#
[root@load-balancer nginx-1.21.1]# ps aux|grep nginx
root 9301 0.0 0.2 119148 2176 ? Ss 18:20 0:00 nginx: master process nginx
nginx 9302 0.0 0.9 151824 7912 ? S 18:20 0:00 nginx: worker process
root 9315 0.0 0.1 12344 1108 pts/0 S+ 18:21 0:00 grep --color=auto nginx
[root@load-banlancer nginx-1.21.1]# ss -anplut|grep nginx
tcp LISTEN 0 128 0