从 keepalived 通知脚本执行 docker 命令

从 keepalived 通知脚本执行 docker 命令

我已经为这个问题苦苦挣扎了几天,非常感谢大家的帮助。基本上,我想在运行 docker-compose 的两台服务器之间使用 keepalived,因此当其中一台服务器更改为主服务器时,我想执行 docker-compose up,然后在备份服务器上执行 docker-compose down。在尝试设置它时,我能够使用正确的用户执行通知脚本(我在 docker 命令之前回显了它),但尝试执行时,docker run --name hello hello-world权限被拒绝。

另一个问题是除了服务状态之外我如何才能看到keepalived日志。

这是 keepalived 配置

global_defs {
   notification_email_from [email protected]
   smtp_server localhost
   smtp_connect_timeout 30
   script_user sky
   enable_script_security
}

vrrp_instance VI_1 {
    state MASTER
    interface enp0s9
    virtual_router_id 101
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.1.10.30
    }
    notify "/home/sky/keepchange.sh"
}

这是 keepchange.sh 脚本

#!/bin/bash
echo $1 $2 $3 $4 $5 >> /home/sky/keepalived.txt
echo $(whoami) >> /home/sky/keepalived.txt
/bin/bash -c '/usr/bin/docker run --name hello -d hello-world' &>> /home/sky/keepalived.txt

这是 keepalived.txt 中的输出

INSTANCE VI_1 FAULT 101
sky
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/create?name=hello: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.
INSTANCE VI_1 BACKUP 101
sky
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/create?name=hello: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.
INSTANCE VI_1 MASTER 101
sky
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/create?name=hello: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.

任何帮助和建议都将不胜感激

相关内容