首页 > 其他分享 >ngix+keepalived+k8s

ngix+keepalived+k8s

时间:2023-12-29 10:45:26浏览次数:39  
标签:loc script firewall keepalived nginx vrrp ngix k8s

一.nginx的安装

1.nginx安装包下载

在官网 https://nginx.org/en/download.html下载linux的tar包选择合适的版本如https://nginx.org/download/nginx-1.24.0.tar.gz

2.安装依赖

yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

3.安装nginx

tar -xvf nginx-1.24.0.tar.gz
cd nginx-1.24.0 2
./configure --prefix=/data/nginx  --with-http_stub_status_module --with-http_ssl_module  --with-stream
make && make install 

4.修改index.html

方便后面keepalived的测试

nginx1
vim /data/nginx/html/index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>My Website</title>
</head>
<body>
    <h1>Welcome to nginx1!</h1>
    <p>Current time: <span id="current-time"></span></p>

    <script>
        // 获取当前时间并更新页面
        function updateTime() {
            var currentTime = new Date();
            var currentDateString = currentTime.toLocaleString();
            document.getElementById("current-time").innerHTML = currentDateString;
        }

        // 每秒钟更新一次时间
        setInterval(updateTime, 1000);
    </script>
</body>
</html>


nginx2
vim /data/nginx/html/index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>My Website</title>
</head>
<body>
    <h1>Welcome to nginx2!</h1>
    <p>Current time: <span id="current-time"></span></p>

    <script>
        // 获取当前时间并更新页面
        function updateTime() {
            var currentTime = new Date();
            var currentDateString = currentTime.toLocaleString();
            document.getElementById("current-time").innerHTML = currentDateString;
        }

        // 每秒钟更新一次时间
        setInterval(updateTime, 1000);
    </script>
</body>
</html>

显示效果如下 server名+时间的显示

 二.keepalive的安装与配置

1.使用yum安装

yum install keepalived -y 

2.修改配置文件

1)需要在global_defs 中添加

script_user root
enable_script_security
否则会报警告:WARNING -default user ‘keepalived_script’ for script execution does not exist -please create. Mar 26 11:37:09 localhost.localdomain Keepalived_vrrp[4587]: SECURITY VIOLATION - scripts are being executed but script_security not enabled.

2)需要将vrrp_strict注释掉,否则会ping不通vip

3)需要将原配置文件中所有的virtual_server都删除,否则vip指不到nginx,因为virtual_server中的lb_kind NAT模式不支持域内访问。

keepalived1
vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   script_user root
   enable_script_security
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id NGINX
   vrrp_skip_check_adv_addr
  # vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script nginx_check {
        script "/etc/keepalived/nginx_health.sh"
        interval 2
        weight -20
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.38.0.144
    }
   track_script {
        nginx_check
    }
}

keepalived
vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   script_user root
   enable_script_security
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id NGINX
   vrrp_skip_check_adv_addr
  # vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script nginx_check {
        script "/etc/keepalived/nginx_health.sh"
        interval 2
        weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.38.0.144
    }
     track_script {
        nginx_check
    }
}

 3.测试

1)将nginx1 停掉,由于/etc/keepalived/nginx_health.sh文件,nginx会马上自动重启

systemctl stop nginx
systemctl status nginx

2)将keepalived1停掉,vip会飘到nginx2

 keepalived测试完成。

 

标签:loc,script,firewall,keepalived,nginx,vrrp,ngix,k8s
From: https://www.cnblogs.com/xiaoxiaomuyuyu/p/17934258.html

相关文章

  • k8s限速队列不通过Get方法判断队列是否关闭
    go.modmoduleuse-k8s-queuego1.19requirek8s.io/client-gov0.28.2require( github.com/go-logr/logrv1.2.4//indirect golang.org/x/timev0.3.0//indirect k8s.io/apimachineryv0.28.2//indirect k8s.io/klog/v2v2.100.1//indirect k8s.io/utils......
  • 关于 K8s 的一些基础概念整理
    〇、前言Kubernetes,将中间八个字母用数字8替换掉简称k8s,是一个开源的容器集群管理系统,由谷歌开发并维护。它为跨主机的容器化应用提供资源调度、服务发现、高可用管理和弹性伸缩等功能。下面简单列一下k8s的几个特性:自动化部署:Kubernetes可以根据应用程序计算资源需求自......
  • keepalived故障
    背景说明三个keepalived服务组成一个高可用环境。风和日丽的下午突然通知某个生产环境vip不通,使用承载vip的主机测试服务是正常。进而推断keepalived导致的问题。。。三个主机IP地址分别是:x.x.3.17、x.x.3.18、x.x.3.19。VIP地址是:x.x.3.20,该地址绑定在x.x.3.18......
  • k8s学习
    容器化技术优点:1.自我修复2.弹性伸缩3.服务发现4.负载均衡5.版本回退6.存储编排k8s构成:1.控制节点:集群的控制平面,负责集群的决策1>ApiServer2>Schedule3>ControllerManager4>Etcd 2.工作节点......
  • K8s中下线Hadoop节点(节点下线,调整副本数)
    K8s中下线Hadoop节点(节点下线,调整副本数)将Hadoop从三副本修改为双副本,同时修改datanode和nodemanager节点数为2修改hadoop节点副本数和datanode以及yarnnodemanager节点数:hadoop.hdfs.replication=2hadoop.hdfs.datanode=2hadoop.yarn.nodemanager=2重启hadoopyarn所有pod......
  • k8s 安装kubesphere3.4.1 多次安装报错 Error from server (InternalError): Internal
    failed:[localhost](item={'ns':'kubesphere-system','kind':'users.iam.kubesphere.io','resource':'admin','release':'ks-core'})=>{"ansible_loop_var":"......
  • 有了这篇 Kubectl 命令总结,5分钟优雅入门 K8s!
    kubectl常用命令指南Kubectl命令是操作kubernetes集群的最直接的方式,特别是运维人员,需要对这些命令有一个详细的掌握Kubectl自动补全#setupautocompleteinbash,bash-completionpackageshouldbeinstalledfirst.$source<(kubectlcompletionbash)#setupautoc......
  • 【k8s问题定位】k8s中的pod不停的重启,定位问题原因与解决方法
    现象:running的pod,短时间内重启次数太多 定位问题方法:查看pod日志kubectlgetevent#查看当前环境一个小时内的日志kubectldescribepodpod_name#查看当前pod的日志kubectllogs-fpod_name--previous#查看重启之前的那一次pod的日志,从那......
  • 【K8S系列】Pod重启策略及重启可能原因
    简介: 【K8S系列】Pod重启策略及重启可能原因1重启策略1.1AlwaysPod中的容器,不管因为什么原因停止,都会自动重启。该为默认策略,没有定义重启策略时,默认的就是always1.2 OnFailurePod中的容器,非正常停止/异常退出时,会自动重启容器,如果是正常停止,则不会1.3Ne......
  • K8S发布策略,无损发布
    大家好,相信大部分公司都已经使用K8S进行容器管理和编排了,但是关于K8S的发布策略,还有很多同学不太清楚,通过这篇文章的介绍,相信大家对目前K8S的发布情况有一个概括的认识。总结下来,共有如下几种:重建(recreate):即停止一个原有的容器,然后进行容器的新建。滚动更新(rollingUpdate):停......