Consul
Discover Services External to Pivotal Application Service
Pivotal Application Service (PAS) is a cloud-agnostic platform for running applications. It abstracts away the complexity of running your own infrastructure and networking for an application and gives developers the ability to focus on writing code. It has many features, including its own internal service discovery. In hybrid environments, where workloads can run on multiple platforms or clouds, applications running on PAS may need to discover services external to the platform.
Consul’s DNS interface is one of its primary querying interfaces for discovering external services. This introductory tutorial describes the basics on how to configure your PAS installation to delegate DNS lookups for Consul-specific domains to the Consul servers.
Prerequisites
This tutorial assumes basic familiarity with Consul, PAS, as well as its tools, like BOSH and Ops Manager. You will be able complete this tutorial without production experience.
Here is what you will need for this tutorial:
- A running installation of PAS
- Ability to make and apply changes in Pivotal Ops Manager
- Ability to push applications to PAS
- IPs or DNS name(s) of the Consul servers that you will be connecting to
You will also need to ensure that the container host VMs (aka Diego VMs) have network connectivity to the Consul servers.
Configure PAS to use Consul DNS
Log in to your Pivotal Ops Manager instance, use Pivotal documentation for instructions specific to your installation.
From your installation dashboard navigate to the “BOSH DNS Config Page” within the BOSH Director tile.
Add the following configuration to the Handlers
field, setting recursors
to
the IPs or DNS names of your Consul servers.
[
{
"cache": {
"enabled": true
},
"domain": "consul.",
"source": {
"recursors": ["10.52.0.63:8600", "10.52.0.55:8600", "10.52.0.43:8600"],
"type": "dns"
}
}
]
This configuration sets the Consul server running on 10.52.0.63
and the
default DNS port 8600
.
If the Consul server is configured with a different
domain, make sure to
change the domain
property above to reflect that.
Your resulting page should include the configuration in the Handlers
field.
When ready, click "Save". Note that you will need to review and apply pending changes in Ops Manager to apply the configuration.
Discover a service with Consul DNS
Now that you have configured Consul’s DNS as a query interface, you can test that it works by deploying a simple demo application. From the demo application container, you will be able to query Consul DNS.
First, log in to PAS.
$ cf login -a api.sys.example.com
Create an organization.
$ cf create-org hashicorp
Create a space.
$ cf create-space -o hashicorp consul-demo
Target an organization and a space.
$ cf target -o hashicorp -s consul-demo
Next, download the demo application.
$ git clone https://github.com/cloudfoundry-samples/cf-sample-app-spring.git
Move into the directory for the downloaded app.
$ cd cf-sample-app-spring
Deploy the demo application.
$ cf push
The demo application is maintained by Cloud Foundry.
Finally, SSH into the demo application container and use dig
to discover all
running instances of the Consul service.
$ cf ssh cf-demo -c 'dig consul.service.consul'
; <<>> DiG 9.11.3-1ubuntu1.11-Ubuntu <<>> consul.service.consul
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62872
;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 4
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;consul.service.consul. IN A
;; ANSWER SECTION:
consul.service.consul. 5 IN A 10.52.0.63
consul.service.consul. 5 IN A 10.52.0.55
consul.service.consul. 5 IN A 10.52.0.43
;; ADDITIONAL SECTION:
consul.service.consul. 5 IN TXT "consul-network-segment="
consul.service.consul. 5 IN TXT "consul-network-segment="
consul.service.consul. 5 IN TXT "consul-network-segment="
;; Query time: 2 msec
;; SERVER: 169.254.0.2#53(169.254.0.2)
;; WHEN: Thu Dec 05 07:04:34 UTC 2019
;; MSG SIZE rcvd: 332
Next steps
In this tutorial you learned how to configure Pivotal Application Service (PAS) to use Consul’s DNS interface for service discovery of external services. If you have services deployed in hybrid environments, such as VMs or Kubernetes, you will now be able to discover them using Consul’s DNS interface.
Learn more about Consul service discovery and registration and Consul’s DNS interface.