HashiCorp Certified: Consul Associate Certification
Register Services and Use Service Discovery
Demo Service Definition and Registration
In this lesson, you’ll learn how to register a service in Consul, watch it appear in the UI, test it without a health check, and finally deregister it. Our environment consists of two Consul servers and one web client node (web-server-01
).
Verify Cluster Membership
On one of the Consul servers (consul-node-a
), confirm that all nodes have joined the cluster:
[ec2-user@consul-node-a ~]$ consul members
Node Address Status Type Build Protocol DC Segment
consul-node-a 10.0.101.110:8301 alive server 1.9.3+ent 2 us-east-1 <all>
consul-node-b 10.0.101.248:8301 alive server 1.9.3+ent 2 us-east-1 <all>
web-server-01 10.0.101.177:8301 alive client 1.9.3+ent 2 us-east-1 <default>
Switch to the web client node (web-server-01
) to define and register your service. You can watch these changes propagate instantly in the Consul UI:
Service Definition File
On web-server-01
, create a JSON file to describe your service:
[ec2-user@ip-10-0-101-177 ~]$ vi service.json
Add the following content:
{
"ID": "web-server-01",
"Name": "front-end-eCommerce",
"Tags": ["v7.05", "production"],
"Address": "10.0.101.177",
"Port": 80
}
Note
The service.json
file follows Consul’s service definition schema. You can extend it later with health checks under the "Check"
block.
Service Definition Fields
Field | Description | Example |
---|---|---|
ID | Unique identifier for the service | web-server-01 |
Name | Logical name of the service | front-end-eCommerce |
Tags | Metadata labels | v7.05 , production |
Address | Service IP address | 10.0.101.177 |
Port | Listening port | 80 |
Register the Service
Register your service definition with Consul:
[ec2-user@ip-10-0-101-177 ~]$ consul services register service.json
Registered service: front-end-eCommerce
In the Consul UI, the new service appears under Services with a healthy status:
Meanwhile, Apache is serving its default test page:
Testing Without a Health Check
Stop the Apache HTTP server:
[ec2-user@ip-10-0-101-177 ~]$ sudo systemctl stop httpd
Reloading the test page in your browser times out, but in Consul, the front-end-eCommerce
service still shows as passing
because no application-level health check is defined.
Warning
Without a defined health check, Consul cannot detect application failures. Always configure checks to get accurate health status!
Restore the default page by restarting Apache:
[ec2-user@ip-10-0-101-177 ~]$ sudo systemctl start httpd
Deregister the Service
To remove the service from Consul:
[ec2-user@ip-10-0-101-177 ~]$ consul services deregister service.json
Deregistered service: web-server-01
Note
You can also deregister services via the Consul UI or the HTTP API by specifying the service ID.
The front-end-eCommerce
entry disappears from the Consul UI.
Links and References
Watch Video
Watch video content