Keepalived+nginx

IT
IT
IT
335
文章
1
评论
2020年3月22日20:03:21 评论 1,708 2494字阅读8分18秒

两台机器一起操作

  • 192.168.31.61   192.168.31.62
  • 安装
yum install -y keepalived

备份配置文件

cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.confback

清除配置文件内容

> /etc/keepalived/keepalived.conf

加入新的主配置文件内容

[root@localhost ~]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
luck@qq.com
}
notification_email_from root@lucky.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
  
vrrp_script chk_nginx {
script "/usr/local/sbin/check_ng.sh"
interval 3
}
  
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass lucky>com
}
virtual_ipaddress {
192.168.31.60/24
}
  
track_script {
chk_nginx
}
  
}
  • 编写nginx监听脚本
vim /usr/local/sbin/check_ng.sh
#!/bin/bash
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
service nginx  start
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
systemctl stop keepalived
fi
fi

添加执行权限

chmod 755 /usr/local/sbin/check_ng.sh

配置从的主配置文件

vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
luck@qq.com
}
notification_email_from root@lucky.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
  
vrrp_script chk_nginx {
script "/usr/local/sbin/check_ng.sh"
interval 3
}
  
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass lucky>com
}
virtual_ipaddress {
192.168.31.60/24
}
  
track_script {
chk_nginx
}
  
}

同样配置上面的nginx监听启动脚本

nginx

安装nginx

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx -y

配置nginx

[root@localhost conf.d]# pwd
/etc/nginx/conf.d
[root@localhost conf.d]# rm -rf default.conf
[root@localhost nginx]# pwd
/etc/nginx
[root@localhost nginx]# vim nginx.conf
stream {
    log_format  main  '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
    access_log  /var/log/nginx/k8s-access.log  main;
    upstream k8s-apiserver {
                server 192.168.31.63:6443;
                server 192.168.31.64:6443;
           }
  
    server {
  
       listen 6443;
  
       proxy_pass k8s-apiserver;
  
    }
  
	}

Keepalived+nginx

启动服务

[root@localhost nginx]# service nginx start
Redirecting to /bin/systemctl start nginx.service
[root@localhost nginx]# systemctl start keepalived
[root@localhost nginx]# systemctl enable keepalived
[root@localhost nginx]# ps aux|grep keep
root      8948  0.0  0.0 123008  1408 ?        Ss   14:48   0:00 /usr/sbin/keepalived -D
root      8949  0.0  0.1 125132  3148 ?        S    14:48   0:00 /usr/sbin/keepalived -D
root      8950  0.0  0.1 125132  2580 ?        S    14:48   0:00 /usr/sbin/keepalived -D
root      9022  0.0  0.0 112728   968 pts/0    R+   14:49   0:00 grep --color=auto keep
继续阅读
  • 我的QQ
  • QQ扫一扫
  • weinxin
  • 我的头条
  • 头条扫一扫
  • weinxin
IT
  • 本文由 发表于 2020年3月22日20:03:21
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接