Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After the consul_service resource is created using Terrafrom, it will be automatically destroyed #422

Open
vitaaaaa1 opened this issue Dec 11, 2024 · 1 comment

Comments

@vitaaaaa1
Copy link

Can someone please help me take a look at this issue? I'm trying to create a Consul service resource using Terraform, but I don't know why a direct call using curl can create the resource normally, but when created using Terraform, the resource is automatically deleted with the prompt "This service has been deregistered and no longer exists in the catalog." Here is the content of my Terraform execution file

terraform {
  required_providers {
    consul = {
      source = "hashicorp/consul"
      version = "2.21.0"
    }
  }
}

provider "consul" {
  address    = "http://10.8.0.84:8500"
#   datacenter = "nyc1"
}
resource "consul_service" "node_exporter" {
  name    = "node-exporter"
  service_id = "node-exporter_89"
  node    = "consul-01"
  address = "10.8.0.89"
  port    = 9100
  tags = ["node-exporter"]

  meta = {
    app     = "linux-node"
    project = "consule_test"
  }

  check {
    name     = "Endpoint health check"
    http     = "http://10.8.0.89:9100/metrics"
    interval = "15s"
    check_id = "service:node-exporter_89"
    timeout  = "15s"
#     deregister_critical_service_after = 0
  }
}

Consul is installed using a binary installation, and here is the content of my systemd startup file

ExecStart=/usr/local/bin/consul agent -server -ui -bootstrap\
            -client=0.0.0.0 \
            -bind=10.8.0.84 \
            -node=10.8.0.84 \
            -data-dir=/var/lib/consul \
            -node=consul-01 \
            -config-dir=/etc/consul.d

image
image

I can assure you that the registered service is accessible.
image

I tried to delete the check module, which did skip the health check, but the resource was automatically deleted after a while

@blake
Copy link
Contributor

blake commented Dec 11, 2024

Hi @vitaaaaa1, the first part of #187 (comment) explains what is going on here.

…you are trying to register a service on a node where a Consul agent is running (an internal service). The consul_service resource was created to register external services and adds the service to the Consul catalog but not to the local catalog of the agent. When the agent perform the anti-entropy syncs, it finds a service in the catalog it knows nothing about and removes it

The solution is to register the service to a node that is not running a Consul agent.

terraform {
  required_providers {
    consul = {
      source = "hashicorp/consul"
      version = "2.21.0"
    }
  }
}

provider "consul" {
  address    = "http://10.8.0.84:8500"
#   datacenter = "nyc1"
}

resource "consul_node" "external-node-placeholder" {
  address = "192.0.2.10"
  name    = "external-node-placeholder"
}

resource "consul_service" "node_exporter" {
  name    = "node-exporter"
  service_id = "node-exporter_89"
  node    = consul_node.external-node-placeholder.name
  address = "10.8.0.89"
  port    = 9100
  tags = ["node-exporter"]

  meta = {
    app     = "linux-node"
    project = "consule_test"
  }

  check {
    name     = "Endpoint health check"
    http     = "http://10.8.0.89:9100/metrics"
    interval = "15s"
    check_id = "service:node-exporter_89"
    timeout  = "15s"
    #deregister_critical_service_after = 0
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants