How to deploy an application using Consul on Minikube

Reading Time: 3 minutes

Hello folks, In this blog, we’ll learn about how to deploy an application using consul on Minikube

and how to deploy the Consul with the official Helm chart or the Consul K8S CLI. 

if you’re not familiar with HashiCorp consul you can check out my blogs.

let’s begin with prerequisites

  • kubectl
  • helm
  • minikube

Start Minikube

minikube start --memory 4096
minikube status

so minikube had to download the few megabytes of dependencies and container images.

After that start the minikube dashboard.

minikube dashboard

Install Consul with the official Helm chart

Now we’re ready to install the consul to the Kubernetes cluster using the helm chart.

helm repo add hashicorp https://helm.releases.hashicorp.com
kubectl get namespace  

cat > helm-consul-values.yaml <<EOF
global:
  name: consul
  datacenter: dc1
server:
  replicas: 1
  securityContext:
    runAsNonRoot: false
    runAsGroup: 0
    runAsUser: 0
    fsGroup: 0
ui:
  enabled: true
  service:
    type: 'NodePort'
connectInject:
  enabled: true
controller:
  enabled: true
EOF
helm repo add hashicorp https://helm.releases.hashicorp.com
helm install --values helm-consul-values.yaml consul hashicorp/consul --version "0.40.0"
minikube service list
minikube service consul-ui
how-to-deploy-an-application-using-consul-on-minikube

Deploy services with Kubernetes

Now I’m going to deploy a two-tier application made of a backend data service that returns a number (the counting service), and a frontend dashboard that pulls from the counting service over HTTP and displays the number.

first, we’ve deployment, service, and service account for the counting service.

apiVersion: v1
kind: ServiceAccount
metadata:
  name: counting
---
apiVersion: v1
kind: Service
metadata:
  name: counting
spec:
  selector:
    app: counting
  ports:
  - port: 9001
    targetPort: 9001
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: counting
  name: counting
spec:
  replicas: 1
  selector:
    matchLabels:
      app: counting
  template:
    metadata:
      annotations:
        'consul.hashicorp.com/connect-inject': 'true'
      labels:
        app: counting
    spec:
      containers:
      - name: counting
        image: hashicorp/counting-service:0.0.2
        ports:
        - containerPort: 9001

Create a deployment definition, service, and service account for the dashboard service named dashboard.yaml

cat > dashboard.yaml <<EOF
apiVersion: v1
kind: ServiceAccount
metadata:
  name: dashboard
---
apiVersion: v1
kind: Service
metadata:
  name: dashboard
spec:
  selector:
    app: dashboard
  ports:
  - port: 9002
    targetPort: 9002
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: dashboard
  name: dashboard
spec:
  replicas: 1
  selector:
    matchLabels:
      app: dashboard
  template:
    metadata:
      annotations:
        'consul.hashicorp.com/connect-inject': 'true'
      labels:
        app: dashboard
    spec:
      containers:
      - name: dashboard
        image: hashicorp/dashboard-service:0.0.4
        ports:
        - containerPort: 9002
        env:
        - name: COUNTING_SERVICE_URL
          value: 'http://counting:9001'
EOF

After that deploy both on minikube

kubectl apply -f counting.yaml
kubectl apply -f dashboard.yaml
how-to-deploy-an-application-using-consul-on-minikube

So as you can see we’ve deployed the services on the consul.

how-to-deploy-an-application-using-consul-on-minikube

kubectl port-forward deploy/dashboard 9002:9002

how-to-deploy-an-application-using-consul-on-minikube

Conclusion:

In this blog, we’ve learned about how we can deploy services using consul service discovery and mesh on minikube. If you find this blog helpful do share it with your friends.

Written by 

A curious DevOps Intern , love to learn and working on technical skills/tools, know the basics of Linux, Docker, Ansible , Kubernetes and more..