centos上安装的是离线版keepalived如何做开机自启动脚本如何编写如何配置
时间: 2024-12-08 08:24:23 浏览: 19
在 CentOS 系统上安装并设置离线版本的 Keepalived 实现高可用服务,并使其开机自动启动,你需要按照以下步骤操作:
1. **安装 Keepalived**:
- 首先,由于是离线版本,需要手动下载 Keepalived 的 tarball 包。访问 Keepalived 官网 (<http://www.keepalived.org/downloads.html>) 下载适用于你的系统的二进制包。
- 解压并移动到合适的目录,例如 `/opt`:
```
tar xvf keepalived-X.X.X.tar.gz
sudo mv keepalived-X.X.X /opt
```
2. **创建配置文件**:
- 在 `/etc/keepalived/` 目录下创建 `vrrp.conf` 文件,这是 Keepalived 的主配置文件。你可以编辑它来配置虚拟路由器组 (VRRP)、IP 地址等信息。
3. **编写开机启动脚本**:
- 创建一个新的 shell 脚本,比如 `/etc/init.d/keepalived`,内容如下(假设 Keepalived 运行命令是 `sbin/keepalived`):
```bash
#!/bin/bash
# chkconfig: 2345 90 10
# description: Keepalived high availability clustering setup
case "$1" in
start)
sbin/keepalived &
;;
stop)
killall keepalived
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit 0
```
- 授权执行权限:
```bash
chmod +x /etc/init.d/keepalived
```
4. **添加到系统启动**:
- 使用 `chkconfig` 添加脚本到系统启动管理:
```bash
chkconfig --add keepalived
chkconfig keepalived on
```
5. **配置自动启动**:
更新运行级别,让 Keepalived 在启动时运行:
```bash
update-rc.d keepalived defaults
```
6. **配置测试**:
- 启动 Keepalived 并检查其状态:
```bash
service keepalived start
service keepalived status
```
7. **验证配置**:
- 检查 `vrrp.conf` 文件是否正确配置了 VRRP 集群,如 VIP 地址、备份延迟时间等。重启 Keepalived 观察状态变化确认配置生效。
阅读全文