nginx keepalived 本地二进制部署

news/2024/11/20 23:24:51/

在这里插入图片描述

文章目录

    • 安装 nginx
    • 安装 keepalived
    • 卸载 nginx
    • 卸载 keepalived

安装 nginx

wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -xf nginx-1.24.0.tar.gz
cd nginx-1.24.0/
./configure --with-stream --prefix=/usr/local/nginx
make && make install

修改nginx配置文件
创建nginx日志存放目录

$ mkdir /var/log/nginx$ vi /usr/local/nginx/conf/nginx.conf
worker_processes  2;error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;events {worker_connections  10240;
}stream {upstream apiserver {server 192.168.1.11:6443 weight=5 max_fails=3 fail_timeout=30s;server 192.168.1.12:6443 weight=5 max_fails=3 fail_timeout=30s;server 192.168.1.13:6443 weight=5 max_fails=3 fail_timeout=30s;}server {listen 16443;proxy_connect_timeout 15s;proxy_timeout 15s;proxy_pass apiserver;}log_format proxy    '$remote_addr [$time_local] ''$protocol $status $bytes_sent $bytes_received ''$session_time "$upstream_addr" ''"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';access_log /var/log/nginx/access.log proxy;
}

配置nginx启动服务文件

$ vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true[Install]
WantedBy=multi-user.target

启动并检查nginx

systemctl daemon-reload;systemctl enable nginx.service;systemctl restart nginx.service;systemctl status nginx

安装 keepalived

安装keepalived软件,二进制安装

wget https://www.keepalived.org/software/keepalived-2.2.8.tar.gz
tar -xf keepalived-2.2.8.tar.gz
cd keepalived-2.2.8
./configure --prefix=/usr/local/keepalived-2.2.8
make && make installln -s /usr/local/keepalived-2.2.8 /usr/local/keepalived
mkdir /etc/keepalived/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived-2.2.8/etc/sysconfig/keepalived /etc/sysconfig/
cp /root/keepalived-2.2.8/keepalived/keepalived.service /etc/systemd/system/
ln -s /usr/local/keepalived-2.2.8/sbin/keepalived /usr/sbin/
cp /root/keepalived-2.2.8/keepalived/etc/init.d/keepalived /etc/init.d/ 
chmod 755 /etc/init.d/keepalived
systemctl enable keepalived.service

修改配置文件,配置文件略有不同,因为这个采用了非抢占模式
1master1节点配置

$ vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 127.0.0.1smtp_connect_timeout 30router_id 192.168.1.11
}
vrrp_script check_nginx {script "/etc/keepalived/check_nginx.sh"interval 5weight -60fall 2rise 2
}
vrrp_instance VI_1 {state BACKUPnopreemptinterface ens160virtual_router_id 56 # VRRP 路由 ID实例,每个实例是唯一的priority 80   # 优先级,备服务器设置 90advert_int 1    # 指定VRRP 心跳包通告间隔时间,默认1秒authentication {auth_type PASSauth_pass 1111}# 虚拟IPvirtual_ipaddress {192.168.1.10/24}track_script {check_nginx}
}

其余节点需要修改priority优先级即可,其他不用动

master2节点配置

$ vi /etc/keepalived/keepalived.conf
global_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 127.0.0.1smtp_connect_timeout 30router_id 192.168.1.12
}
vrrp_script check_nginx {script "/etc/keepalived/check_nginx.sh"interval 5weight -60fall 2rise 2
}
vrrp_instance VI_1 {state BACKUPnopreemptinterface ens160virtual_router_id 56 # VRRP 路由 ID实例,每个实例是唯一的priority 90   # 优先级,备服务器设置 90advert_int 1    # 指定VRRP 心跳包通告间隔时间,默认1秒authentication {auth_type PASSauth_pass 1111}# 虚拟IPvirtual_ipaddress {192.168.1.10/24}track_script {check_nginx}
}

master3节点配置

$ vi /etc/keepalived/keepalived.conf
global_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 127.0.0.1smtp_connect_timeout 30router_id 192.168.1.13
}
vrrp_script check_nginx {script "/etc/keepalived/check_nginx.sh"interval 5weight -60fall 2rise 2
}
vrrp_instance VI_1 {state BACKUPnopreemptinterface ens160virtual_router_id 56 # VRRP 路由 ID实例,每个实例是唯一的priority 100   # 优先级,备服务器设置 90advert_int 1    # 指定VRRP 心跳包通告间隔时间,默认1秒authentication {auth_type PASSauth_pass 1111}# 虚拟IPvirtual_ipaddress {192.168.1.10/24}track_script {check_nginx}
}

配置检测脚本

$ vi /etc/keepalived/check_nginx.sh
#!/bin/bash# if check error then repeat check for 12 times, else exit
# 检测次数可以适当调整
err=0
for k in $(seq 1 2)
docheck_code=$(curl -k http://localhost:16443)if [[ $check_code == "" ]]; thenerr=$(expr $err + 1)sleep 5continueelseerr=0breakfi
doneif [[ $err != "0" ]]; then# if apiserver is down send SIG=1echo 'nginx error!'systemctl stop   keepalivedexit 1
else# if apiserver is up send SIG=0echo 'nginx ok'
fichmod +x /etc/keepalived/check_nginx.sh

启动并验证keepalived

systemctl enable keepalived ; systemctl restart keepalived

卸载 nginx

systemctl stop nginx
rm -rf /var/log/nginx/
rm -rf /usr/local/nginx/
rm -rf /usr/lib/systemd/system/nginx.service

卸载 keepalived

ls /usr/local/keepalived-2.2.8 && rm -rf /usr/local/keepalived-2.2.8
ls /usr/local/keepalived && rm -rf /usr/local/keepalived
ls /etc/keepalived/ && rm -rf /etc/keepalived/
ls /etc/sysconfig/keepalived && rm -rf  /etc/sysconfig/keepalived
ls /etc/systemd/system/keepalived.service && rm -rf /etc/systemd/system/keepalived.service 
ls /etc/init.d/keepalived && rm -rf  /etc/init.d/keepalived

http://www.ppmy.cn/news/1044137.html

相关文章

c++小游戏制作技巧(1)——使用的windows.h中的指令

有很多使用的指令,它们可以使得游戏体验感更佳,内容更丰富。 实用的指令 system("cls")——清除屏幕 这个指令需引用windows.h头文件。 这个指令应该是最实用的指令之一了。如果在游戏中输出了一大串字符,但是不清除而保留在屏幕…

C/C++中const关键字详解

为什么使用const?采用符号常量写出的代码更容易维护;指针常常是边读边移动,而不是边写边移动;许多函数参数是只读不写的。const最常见用途是作为数组的界和switch分情况标号(也可以用枚举符代替),分类如下:…

JVM——StringTable面试案例+垃圾回收+性能调优

JVM——引言JVM内存结构_北岭山脚鼠鼠的博客-CSDN博客 书接上回内存结构——方法区。 这里常量池是运行时常量池。 方法区 面试题 intern()方法 intern() 方法用于在运行时将字符串添加到内部的字符串池stringtable中,并返回字符串池stringtable中的引用。 返…

一文读懂结构型模式----组合模式!

一、组合模式 1.组合模式的概述 1.1 组合模式是什么 组合模式是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。 三要素: 根节点(root):在一棵非空的树中只有一个根节点&#xff0…

【rust/egui】(三)看看template的app.rs:序列化、持久化存储

说在前面 rust新手,egui没啥找到啥教程,这里自己记录下学习过程环境:windows11 22H2rust版本:rustc 1.71.1egui版本:0.22.0eframe版本:0.22.0上一篇:这里 serde app.rs中首先定义了我们的Templ…

36.SpringMVC视图

SpringMVC视图 SpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户 SpringMVC视图的种类很多,默认有转发视图(InternalResourceView)和重定向视图(RedirectView) 配置视图: 当工程引入jstl的依赖&a…

Java 计算两个字符的相似度

在Java中,要计算两个字符的相似度,可以借助一些字符串相似度算法。以下是几种常见的字符串相似度算法: Levenshtein距离:也称为编辑距离,用于计算两个字符串之间的最小编辑操作次数(插入、删除、替换&…

Unity进阶–通过PhotonServer实现人物移动和攻击–PhotonServer(五)

文章目录 Unity进阶–通过PhotonServer实现人物移动和攻击–PhotonServer(五)DLc: 消息类和通信类服务器客户端 Unity进阶–通过PhotonServer实现人物移动和攻击–PhotonServer(五) DLc: 消息类和通信类 Message namespace Net {public class Message{p…