www.baidu.com 它是可以分发到不同的站点的,叫做 地域服务器 。keepalived
在北京访问北京的机房。其他的地方,访问其他地方的机房。
流量是无限的。我们需要通过 多机房,地域来解析。
把你的请求转发到离你最近的服务器上。
安装步调
版本,2.0.7 其他版本会有问题。
keepalived 的配置复制代码
- [root@Darian1 software]# tar -zxvf keepalived-2.0.11.tar.gz
- [root@Darian1 software]# mkdir keepalived
- [root@Darian1 software]# cd keepalived-2.0.11/
- [root@Darian1 keepalived-2.0.11]# ./configure --prefix=/software/keepalived --sysconf=/etc
- configure: error:
- !!! OpenSSL is not properly installed on your system. !!!
- !!! Can not include OpenSSL headers files. !!!
- [root@Darian1 keepalived-2.0.11]# yum install openssl-devel
- Is this ok [y/d/N]: y
- *** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
- [root@Darian1 keepalived-2.0.11]# yum install libnl libnl-devel
- [root@Darian1 keepalived-2.0.11]# yum install gcc
- [root@Darian1 keepalived-2.0.11]# ./configure --prefix=/software/keepalived --sysconf=/etc
- configure: error: libnfnetlink headers missing
- [root@Darian3 keepalived-2.0.7]# yum install -y libnfnetlink-devel
- [root@Darian1 keepalived-2.0.7]# make && make install
- [root@Darian3 software]# mkdir keepalived
- [root@Darian1 keepalived]# cd ../keepalived
- [root@Darian1 keepalived]# ln -s sbin/keepalived /sbin
- [root@Darian1 keepalived]# cp /software/keepalived-2.0.7/keepalived/etc/init.d/keepalived /etc/init.d
- [root@Darian1 keepalived]# chkconfig --add keepalived
- [root@Darian1 keepalived]# chkconfig keepalived on
- 注意:正在将请求转发到“systemctl enable keepalived.service”。
- Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
- [root@Darian1 keepalived]# service keepalived start
- [root@Darian1 keepalived]# service keepalived status
请立马配置 keepalived 的日记
配置同一个网段,不是同一个网段,访问会有问题!!!keepalived 日记文件配置
线程在进程之内是共享的。共享资源相互影响。
线程产生的本钱比进程低。
复制代码
- [root@Darian1 keepalived]# vim /etc/sysconfig/keepalived
- [root@Darian1 keepalived]# vim /etc/rsyslog.conf
- # keepalived 的log
- local0.* /var/log/keepalived.log
- [root@Darian1 keepalived]# service rsyslog restart
- [root@Darian1 keepalived]# service keepalived restart
Keepalived 需要配置主备来实现动态的切换。Nginx 挂掉的时候,keepalived 也要制止掉。keepalived 的存在的意义是用来监听 Nginx 的请求。这个监听的过程实际上是通过 LVS 来转发的。需要脚原来触发。
利用步调:
- 编写 keepalived.conf 脚本
复制代码
- [root@Darian1 sbin]# vim /etc/keepalived/keepalived.conf
- global_defs {
- # 运行 keepalived 服务器的标识,在一个网络内应该是唯一的
- router_id LVS_DEVEL
- enable_script_security
- }
- vrrp_script nginx_status_process {
- # 实现当 Nginx 挂掉以后,keepalived 也挂掉
- script "/software/nginx/sbin/nginx_status_check.sh"
- # 用户的隔离,用户的运行权限,防止其他用户对这个脚本的执行
- user root
- # 检查频次
- interval 3
- }
- # vrrp 实例定义部分
- vrrp_instance VI_1 {
- # 设置 lvs 的状态,MASTER 和 BACKUP 两种,必须大写
- state MASTER
- # 设置对外服务的接口,网卡的地址
- interface ens33
- # 设置虚拟路由标示,这个标示是一个数字,同一个 vr rp 实例使用唯一标示
- virtual_router_id 51
- # 定义优先级,数字越大优先级越高,在一个 vrrp——instance 下,master 的优先级必须大于 backup
- priority 100
- # 设定 master 与 backup 负载均衡器之间同步检查的时间间隔,单位是秒
- advert_int 1
- authentication {
- # 设置验证类型和密码
- auth_type PASS
- # 验证密码,同一个 vrrp_instance 下 MASTER 和 BACKUP 密码必须相同
- auth_pass 1111
- }
- virtual_ipaddress {
- #设置虚拟 ip 地址,可以设置多个,每行一个
- 192.168.40.100
- }
- track_script{
- # 对应的 上边的 nginx_status_process
- nginx_status_process
- }
- }
- # 设置虚拟服务器,需要指定虚拟 ip 和服务端口
- virtual_server 192.168.40.100 80 {
- # 健康检查时间间隔
- delay_loop 6
- # 负载均衡调度算法
- lb_algo rr
- # 负载均衡转发规则
- lb_kind NAT
- # 设置会话保持时间protocol TCP #指定转发协议类型,有 TCP 和 UDP 两种
- persistence_timeout 50
- # 配置服务器节点 1,需要指定 real server 的真实 IP 地址和端口
- real_server 192.168.40.128 80 {
- # 设置权重,数字越大权重越高
- weight 1
- # realserver 的状态监测设置部分单位秒,用来检测它的状态
- TCP_CHECK {
- # 超时时间
- connect_timeout 3
- # 重试间隔
- delay_before_retry 3
- # 监测端口
- connect_port 80
- }
- }
- }
- 编写 脚本。Nginx/sbin
复制代码
- [root@Darian1 sbin]# vim nginx-ha-check.sh
- #!bin/sh #! /bin/sh 是指此脚本使用/bin/sh 来执行
- A=`ps -C nginx --no-header |wc -l`
- if [ $A -eq 0 ]
- then
- echo 'nginx server is died'
- service keepalived stop
- fi
- 测试脚本是否可用
复制代码
- [root@Darian1 sbin]# sh nginx_status_check.sh
- nginx server is died
- Stopping keepalived (via systemctl): [ 确定 ]
- 添加生效
复制代码
- [root@Darian1 sbin]# chmod +x nginx_status_check.sh
- [root@Darian1 sbin]# service keepalived restart
- [root@Darian1 sbin]# ./nginx -s stop
复制代码
- [root@Darian1 openresty]# mkdir demo
- [root@Darian1 openresty]# cd demo/
- [root@Darian1 demo]# mkdir conf
- [root@Darian1 demo]# mkdir logs
- [root@Darian1 demo]# cd conf/
- [root@Darian1 conf]# vim nginx.conf
- worker_processes 1;
- error_log logs/error.log;
- events {
- worker_connections 1024;
- }
- http {
- server {
- listen 8888;
- location / {
- default_type text/html;
- content_by_lua_block {
- ngx.say("Hello world")
- }
- }
- }
- }
- [root@Darian1 conf]# cd ../../nginx/sbin/
- [root@Darian1 sbin]# ./nginx -p /software/openresty/demo/
写一个完全做认证的模块库文件利用 在 lua 里边有模块的概念,有包的概念。我可以把公共的代码放到一个文件里边。以 API 的方式去提供。可以把他作为一个方法。复制代码
- [root@Darian1 openresty]# cd demo/conf/
- [root@Darian1 conf]# mkdir lua
- [root@Darian1 conf]# cd lua
- [root@Darian1 lua]# vim add.lua
- local args = ngx.req.get_uri_args();
- ngx.say(args.a + args.b);
- [root@Darian1 lua]# vim params.lua
- local _M = {}
- function _M.is_number(...)
- local arg = {...}
- local num;
- for i,v in ipairs(arg) do
- num=tonumber(v);
- if nil == num then
- return false;
- end
- end
- return true;
- end
- return _M;
- [root@Darian1 lua]# vim check.lua
- local param=require("params");
- local args=ngx.req.get_uri_args();
- if not args.a or not args.b or not param.is_number(args.a, args.b) then
- ngx.exit(ngx.HTTP_BAD_REQUEST);
- return;
- end
- [root@Darian1 lua]# vim ../nginx.conf
- worker_processes 1;
- error_log logs/error.log;
- events {
- worker_connections 1024;
- }
- http {
- lua_package_path '$prefix/lua/?.lua';
- lua_code_cache off;
- server {
- listen 80;
- location ~ ^/api/([-_a-zA-Z0_9]+) {
- access_by_lua_file lua/check.lua;
- content_by_lua_file lua/$1.lua;
- }
- }
- }
- [root@Darian1 lua]# cd ../../../nginx/sbin/
- [root@Darian1 sbin]# history
- [root@Darian1 sbin]# ./nginx -p /software/openresty/demo/
- nginx: [alert] lua_code_cache is off; this will hurt performance in /software/openresty/demo/conf/nginx.conf:11
正常地来说,我们会有一个 lua 文件夹,按照我们的需求去写。
我可以利用 lua 写一些复杂的逻辑。可以写业务代码的。
注意:
lua 目次在 demo 目次下 openresty/demo/lua/sub.lua
- JS 、 shell 都叫做脚本语言。
- history 检察下令的历史。
比如说,支付包地址会有一个网关,做一个统一的路由转发。为什么需要网关?
zuul 的 filter 也是雷同的实现。灰度发布
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |