Score:0

Using SSH in check of Consul

ru flag

I have a server with a dockerized Consul Agent (CA). In this CA, I want to run a script from the host who checks how many disk space left in a service check.

For this, I created a service in the CA :

{
  "service": {
    "name": "disk-usage-var-re7", 
    "tags": [
      "type:check",
      "env:re7"
    ],
    "checks": [
      {
        "id": "disk-usage-var-re7",
        "name": "Disk Usage /var RE7",
        "args": [
          "sh",
          "-c",
          "ssh consul@hostserver 'disk_usage.sh /dev/mapper/centos-var'"
        ],
        "interval": "5s",
        "timeout": "30s"
      }
    ]
  }
}

But, when Consul runs him, I got this error : Host key verification failed.

I am using the official Docker image from Consul (Alpine), version 1.6.1, and I executed these commands in the container :

docker exec consul apk add openssh
docker exec -it consul ssh-keygen -t rsa

docker cp consul:/root/.ssh/id_rsa.pub /tmp

su consul
umask 077 && mkdir ~/.ssh
umask 077 && touch ~/.ssh/authorized_keys
cat /tmp/id_rsa.pub > /home/consul/.ssh/authorized_keys
rm -f /tmp/id_rsa.pub

When I try

docker exec -it consul ssh consul@hostserver 'disk_usage.sh /dev/mapper/centos-var'

The first time, SSH asks me :

The authenticity of host 'hostserver (::1)' can't be established.
ECDSA key fingerprint is SHA256:MlsT6sDr9xXuOurqBJu4e+a8m2De3Lu4ctJSB+5RNmk.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'hostserver' (ECDSA) to the list of known hosts.

And after that, the script still works. But not with the Consul's check.

What am I doing wrong?

Many thanks.

Score:0
ru flag

Ok, I found what happened. The default user in the container is root, but the user who starts Consul is consul.

So you have to run the container as the consul user specifying -u consul and use ssh-keygen with the correct user.

Now take a look at the correct commands :

docker exec -it -u consul consul ssh-keygen -t rsa

docker cp consul:/home/consul/.ssh/id_rsa.pub /tmp

su consul
cat /tmp/id_rsa.pub > /home/consul/.ssh/authorized_keys
rm -f /tmp/id_rsa.pub

And the script works !

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.