I have a problem when using consul;
I mistakenly registered a service with the id \n192.168.1.1,Now I want to delete this service via API。
when i using http://{ip}:8500/v1/agent/service/deregister/{service_id} to deregister this service,i got a problem,the request return “method GET not allowed”,I tried escaping \n as %5C to make the request again,it return “Unknown service ID “\\n192.168.1.1”. Ensure that the service ID is passed, not the service name.”。
In this case, how do I need to remove this service from my consul?
Please refer to the documentation for this API, where you will find it is specified to use PUT requests, not GET requests: Service - Agent - HTTP API | Consul | HashiCorp Developer
You have said
which casts some doubt on that.
But perhaps you have since corrected that.
It seems you are passing the incorrect service ID. You haven’t shown exact API output that would confirm what the correct service ID is, so I can only speculate that perhaps \n
is a representation of a newline character which should be encoded as %0A
in an URL.
This way I also tried。
Use %0A encoding instead of %5C,There is no problem with this request,but the corresponding service on consul has not been canceled
So I want to know is how to deregister server if it service id contain \n
here is one of them,the id contains \n,this is due to my wrong registration,I want to delete these wrong registrations without changing the original data
Please show the exact request you made and the response, as this should be the correct way to do it.
After doing a quick bit of local testing, I have come to the conclusion that this is a bug in the Consul API. It allows you to register services containing characters that it is impossible to pass to the agent deregister API.
The only workaround I can think of would be to shut down the relevant Consul agent, and then manually look through the contents of its data directory, to find files in the checks
and services
directories relevant to this service, and delete them from disk.
That would presumably still leave the service in the Consul servers’ catalog - it may be necessary to also use the https://developer.hashicorp.com/consul/api-docs/catalog#deregister-entity to remove it from there. Happily, that API does not take the service ID as an URL-path parameter, so should not be affected by the same bug.
thank you very much, you are right。
I successfully deleted this node using the API you mentioned。
The following are the commands:
curl -X PUT -H 'application/json' -d '{"Node":"{your node}","Datacenter": "{your datacenter}","ServiceID": "${your service_id}"}' http://127.0.0.1:8500/v1/catalog/deregister
The ${your service_id} here can be used directly \n without escaping
Using this way I found a problem.
When I use the /v1/catalog/deregister interface, I can delete the service at that moment, but it will appear on my service list after a while, and after the deletion is completed, I use /v1/agent/services The interface is able to query the data of this service. It looks like it just doesn’t show up on my service list for a while after I call the /v1/catalog/deregister interface, but it shows up again after a while
Yes, I already addressed this in my earlier reply, but you skipped that part:
Can you tell me the steps, I don’t quite understand .
I try to delete the file corresponding to a service in the /data/consul/data/services directory, and then execute the /v1/catalog/deregister interface, but this service will still appear in my service list.
Here is my start command:
nohup ./consul agent -data-dir=/data/consul/data -node=client -bind=10.1.252.248 -enable-script-checks=true -client 0.0.0.0 -ui > consul-server.log 2>&1 &
Do I need to stop my consul service and then delete it
I tested it out, thanks again for your answer
we need to stop consul service first, then go to my persistent storage, delete the files corresponding to the nodes with abnormal service_id, and then start my consul. At this time, those nodes will not appear in the service list