Installing a Consul demo application

To explore the service mesh capabilities of Consul through a Kubernetes environment, we will install a demo application that is available from Hashicorp. This demo uses two simple services: a counting service and a frontend web UI service (connects to the counting service to show the results). Let's get started:

  1. Let's take a look at the counting pod definition, which shows counting and a counting-init container:
# Counting pod

apiVersion: v1
kind: Pod
metadata:
name: counting
annotations:
"consul.hashicorp.com/connect-inject": "true"
spec:
containers:
- name: counting
image: hashicorp/counting-service:0.0.2
ports:
- containerPort: 9001
name: http
initContainers:
- name: counting-init
image: hashicorp/counting-init:0.0.9
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: HOST_IP
valueFrom:
fieldRef:
fieldPath: status.hostIP

The consul.hashicorp.com/connect-inject annotation, when set to true, will inject a sidecar proxy into the pod through the admission webhook controller. The counting service endpoint's REST URL is at port 9001

  1. Now, let's take a look at the front end dashboard service using a dashboard container and its init container:
# Dashboard pod

apiVersion: v1
kind: Pod
metadata:
name: dashboard
labels:
app: dashboard
annotations:
"consul.hashicorp.com/connect-inject": "true"
"consul.hashicorp.com/connect-service-upstreams": "counting:9001"
spec:
containers:
- name: dashboard
image: hashicorp/dashboard-service:0.0.4
ports:
- containerPort: 9002
name: http
env:
- name: COUNTING_SERVICE_URL
value: "http://localhost:9001"

The preceding code is for the frontend dashboard pod. The following snippet shows the init container:


initContainers:
- name: dashboard-init
image: hashicorp/dashboard-init:0.0.4
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: HOST_IP
valueFrom:
fieldRef:
fieldPath: status.hostIP

The consul.hashicorp.com/connect-inject annotation, when set to true, will inject a sidecar proxy into this frontend GUI. The new annotation, consul.hashicorp.com/connect-service-upstreams, in the dashboard pod defines the upstream counting microservice that provides a REST API endpoint at port 9001. For the dashboard service to connect to counting, it is necessary for the Consul DNS to be connected to the Kubernetes DNS.

The dashboard service provides a hook for the counting service through the COUNTING_SERVICE_URL environment variable. The dashboard service web UI is exposed at port 9002.

  1. The following Kubernetes service will provide an endpoint to the dashboard microservice at its internal port, that is, 9002:
# Define service

apiVersion: v1
kind: Service
metadata:
name: dashboard-service
labels:
app: dashboard
spec:
ports:
- protocol: "TCP"
port: 80
targetPort: 9002
selector:
app: dashboard
type: NodePort
  1. Let's create backend counting and frontend dashboard services:
$ kubectl -n consul apply -f 04-counting-demo.yaml
pod/counting created
pod/dashboard created
service/dashboard-service created
  1. Check the counting and dashboard pods and take a look at the injected sidecar proxies in them:
$ kubectl -n consul get pods
NAME READY ---
consul-9tkg9 1/1 ---
consul-connect-injector-webhook-deployment-699976587d-n9qmp 1/1 ---
consul-server-0 1/1 ---
consul-server-1 1/1 ---
consul-server-2 1/1 ---
consul-sync-catalog-8444f97fc6-ptfwg 1/1 ---
counting 2/2 ---
dashboard 2/2 ---

--- STATUS RESTARTS AGE
--- Running 1 19m
--- Running 0 19m
--- Running 0 19m
--- Running 0 19m
--- Running 0 19m
--- Running 1 19m
--- Running 0 10s
--- Running 0 10s
  1. Describe one of the microservices and check the injected sidecar proxy:
$ kubectl -n consul describe pod counting
...
Containers:
counting:
...
Image: hashicorp/counting-service:0.0.2
...
Port: 9001/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 26 Aug 2019 10:21:40 -0400
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-bq5xd (ro)
consul-connect-envoy-sidecar:
...
Image: envoyproxy/envoy-alpine:v1.9.1
...
Port: <none>
Host Port: <none>
Command:
envoy
--config-path
/consul/connect-inject/envoy-bootstrap.yaml
State: Running
Started: Mon, 26 Aug 2019 10:21:40 -0400
Ready: True
Restart Count: 0
Environment:
HOST_IP: (v1:status.hostIP)
...

By doing this, we have deployed the counting and dashboard services from HashiCorp to explain the service discovery features of Consul. Next, we will create an Ingress entry so that we can access the Consul dashboard.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.93.136