HashiCorp Consul

Detailed information on the HashiCorp Consul state store component

Setup a HashiCorp Consul state store

You can run Consul locally using Docker:

  1. docker run -d --name=dev-consul -e CONSUL_BIND_INTERFACE=eth0 consul

You can then interact with the server using localhost:8500.

The easiest way to install Consul on Kubernetes is by using the Helm chart:

  1. helm install consul stable/consul

This will install Consul into the default namespace. To interact with Consul, find the service with: kubectl get svc consul.

For example, if installing using the example above, the Consul host address would be:

consul.default.svc.cluster.local:8500

Create a Dapr component

The next step is to create a Dapr component for Consul.

Create the following YAML file named consul.yaml:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: <NAME>
  5. namespace: <NAMESPACE>
  6. spec:
  7. type: state.consul
  8. version: v1
  9. metadata:
  10. - name: datacenter
  11. value: <REPLACE-WITH-DATA-CENTER> # Required. Example: dc1
  12. - name: httpAddr
  13. value: <REPLACE-WITH-CONSUL-HTTP-ADDRESS> # Required. Example: "consul.default.svc.cluster.local:8500"
  14. - name: aclToken
  15. value: <REPLACE-WITH-ACL-TOKEN> # Optional. default: ""
  16. - name: scheme
  17. value: <REPLACE-WITH-SCHEME> # Optional. default: "http"
  18. - name: keyPrefixPath
  19. value: <REPLACE-WITH-TABLE> # Optional. default: ""

Warning

The above example uses secrets as plain strings. It is recommended to use a secret store for the secrets as described here.

Example

The following example uses the Kubernetes secret store to retrieve the acl token:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: <NAME>
  5. namespace: <NAMESPACE>
  6. spec:
  7. type: state.consul
  8. version: v1
  9. metadata:
  10. - name: datacenter
  11. value: <REPLACE-WITH-DATACENTER>
  12. - name: httpAddr
  13. value: <REPLACE-WITH-HTTP-ADDRESS>
  14. - name: aclToken
  15. secretKeyRef:
  16. name: <KUBERNETES-SECRET-NAME>
  17. key: <KUBERNETES-SECRET-KEY>
  18. ...

Apply the configuration

In Kubernetes

To apply the Consul state store to Kubernetes, use the kubectl CLI:

  1. kubectl apply -f consul.yaml

Running locally

To run locally, create a components dir containing the YAML file and provide the path to the dapr run command with the flag --components-path.

Last modified February 16, 2021: Merge pull request #1235 from dapr/update-v0.11 (b4e9fbb)