Hashicorp Consul Watches & Snapshot 问题(使用 Docker 容器)

Hashicorp Consul Watches & Snapshot problem (with Docker container)

我正在尝试做一些防灾工作。备份流程如图:

Watches 处理程序:“K/V 检测到更改”=> 调用 shell“执行快照保存”

观看配置:

{
  "watches": [
    {
        "type": "keyprefix",
        "prefix": "www/",
        "args": ["/consul/run.sh"]
    }
  ]
}

shell (/consul/run.sh):

#!/bin/sh

echo "start backup snapshot" >> /consul/trigger.log
consul snapshot save /consul/keyvalue.bak
echo "end backup snapshot" >> /consul/trigger.log

当 K/V 发生变化时调用 shell 确实没有任何错误日志(甚至 echo 消息已存储在 /consul/trigger.log 中),但我找不到我的备份任何目录中的文件。

有人可以向我解释为什么备份文件丢失了吗?

通过执行以下步骤,我能够在 Docker Compose 下运行它。按照这些说明操作后,您可能会看到成功的快照。

  1. 创建一个 docker-compose.yaml 文件。

      ---
    version: "3.8"
    services:
      consul:
        image: consul:1.10.2
        ports:
          - 8500:8500
        environment:
          CONSUL_LOCAL_CONFIG: |
            {
              "watches": [
                {
                    "type": "keyprefix",
                    "prefix": "www/",
                    "args": ["/consul/run.sh"]
                }
              ]
            }
        volumes:
          - ./consul_data/:/consul/
    
  2. 创建 consul_data 包含 Consul 所需的几个子目录。

    $ mkdir -p consul_data/{config,data}
    
  3. consul_data/ 目录中创建 run.sh

    #!/bin/sh
    
    echo "start backup snapshot" >> /consul/trigger.log
    consul snapshot save /consul/keyvalue.bak
    echo "end backup snapshot" >> /consul/trigger.log
    
  4. 确保run.sh可执行。

    chmod +x consul_data/run.sh
    
  5. 启动 Consul。

    $ docker-compose up
    
  6. 在另一个终端中,使用 consul kv putwww/ 前缀下创建一个密钥。

    $ consul kv put www/test
    Success! Data written to: www/test
    
  7. 在之前的终端中,我可以看到配置的 watch 正在触发,并且正在保存快照。

    consul_1  | 2021-09-07T05:48:11.741Z [DEBUG] agent.http: Request finished: method=PUT url=/v1/kv/www/test from=192.168.32.1:65036 latency=657.891µs
    consul_1  | 2021-09-07T05:48:11.741Z [DEBUG] agent.http: Request finished: method=GET url=/v1/kv/www/?index=21&recurse= from=127.0.0.1:44946 latency=24.915165128s
    consul_1  | 2021-09-07T05:48:11.927Z [INFO]  agent.fsm: snapshot created: duration=261.695µs
    consul_1  | 2021-09-07T05:48:11.927Z [INFO]  agent.server.raft: starting snapshot up to: index=26
    consul_1  | 2021-09-07T05:48:11.928Z [INFO]  agent.server.raft: snapshot complete up to: index=26
    consul_1  | 2021-09-07T05:48:11.979Z [DEBUG] agent.http: Request finished: method=GET url=/v1/snapshot from=127.0.0.1:44974 latency=52.717138ms
    consul_1  | 2021-09-07T05:48:12.023Z [DEBUG] agent: watch handler output: watch_handler=[/consul/run.sh] output="Saved and verified snapshot to index 26
    consul_1  | "
    
  8. 最后,确认快照存在。

    $ file consul_data/keyvalue.bak
    consul_data/keyvalue.bak: gzip compressed data, original size modulo 2^32 9728