Understand the world of containers and microservices.
- Only simple-service app is allowed to talk to postgres database. No service from outside can connect to postgres.
- There are no restrictions in terms of CPU, Memory.
- The simple-service webapp will show status as
Running
until progres is up andWell Done
once the postgres is up.
Golang, Docker, Postgres, Minikube, Kubectl, Istio( to manage microservices within kubernetes ), Docker Hub
I’m using kubernetes version v1.18.3 and Istio 1.6.5.
How to build image from Dockerfile of simple-service-webapp and push it to container repository (Docker Hub)
-
Install Docker:
Use https://www.docker.com/products/docker-desktop to install docker on windows.
-
Build the image:
Go to simple-service folder and copy the docker file and issue below command
docker build -t 225517/simple-service-webapp:v1 .
- Verify simple-service-webaap image exist bu running below command
docker images
- Push the image to repository(docker-hub)
docker login
docker tag 225517/simple-service-webapp:v1 225517/simple-service-webapp:v1
docker push 225517/simple-service-webapp:v1
- After pushing the image to docker hub you should see message (not exactly same)
v1: digest: sha256:00fcdfecb03a3e653d2056d3d540af21f6eec5880a3b41609b1a133448e49c15 size: 2616
- Install Minikube ( an opensource tool to use kubernetes locally ).
minikube start --vm-driver hyperv --hyperv-virtual-switch "Minikube Virtual Switch"
- Install Kube Control ( kubectl ).
kubectl version
( to confirm tool is available ).kubectl get nodes
( to check the nodes of your kubernetes cluster ).
We will Istio for secure communication between microservices, Tracing, Monitoring and Logging, authentication and authorizaztion.
-
Download Istio: (We are downloading istio for windows 10):
Go to https://github.com/istio/istio/releases/tag/1.6.5 and download Istio.
-
cd istio-1.6.5
-
The istioctl client binary in the bin/ directory. Add the istioctl client to your path.
istioctl version
- Install Istio
istioctl manifest apply --set profile=demo
kubectl get svc -n istio-system
kubectl get pods -n istio-system
Great! Installation of Istio Done.
- Instruct Istio to automatically inject Envoy sidecar proxies
kubectl label namespace default istio-injection=enabled
- Add the simple-service-webapp image to Minikube cache
minikube cache add 225517/simple-service-webapp:v1
As simple-service webapp depends on postgres. We need to first deploy postgres into our kubernetes cluster. Are you excited? 😉 Let's start!!
- Create Kubernetes Deployments - This creates a container. The container uses postgres image.
- Create Kubernetes Service - Defines logical set of pods and a policy to access them.
- To run this postgres service in your Kubernetes cluster, you will have to issue the following command:
kubectl apply -f postgres-service.yaml
You should see output as
> service/postgresdb created
> deployment.apps/postgresdb-v1 created
- Verify postgres pod and service is running by issuing below commmand:
kubectl get pods
> postgresdb-v1-7dd4b56dfc-tnvcr 2/2 Running 0 20m
Kubectl get svc
> postgresdb ClusterIP 10.108.60.181 <none> 5432/TCP 21m
- ToDo:
Configuring environment variables in postgres deployment to crate user and its database upon startup. Configuring environement variables in simple-service-webapp to connect to database. error prone and quite unsecured 😄
- Configure postgres with a Secrets to handle connection credentials (Decouple configuration from deployment. The values need to be encoded in base64.
- As docker containers are ephemeral in nature. Wondering how to keep the data safe when the pod is rescheduled? :D a. Persistent Volumes b. Persistemt Volume Claims
Access the PgSQL client to create a test database, table, and adding a row.
- Create Kubernetes Deployments - This creates a container. The container uses image we built in first step using Dockerfile.
- Create Kubernetes Service - Defines logical set of pods and a policy to access them.
- To run this simple-service-webapp service in your Kubernetes cluster, you will have to issue the following command:
kubectl apply -f simple-service-webapp-service.yaml
You should see output as
> service/simple-service-webapp-service created
> deployment.apps/simple-service-webapp-v1 created
- Verify simple-service pod and service is running by issuing below commmand:
kubectl get pods
kubectl get svc
- As postgresdb port is 5432 and host is postgresdb let's reflect that in config file Open config.go file in simple service applciation and do the config changes described below
// Config is responsible for holding the application configuration
// variables. Each configuration point is also exported as an environment
// variable.
type Config struct {
Port uint `env:"PORT" envDefault:"8080"`
PostgresURL string `env:"POSTGRES_URL" envDefault:"postgres://user:pass@postgresdb/simple-service"`
}
Let's deploy the new version of the simple service webapp:
docker build -t 225517/simple-service-webapp:v1 .
push the new image to Docker Hub
docker push 225517/simple-service-webapp:v1
kubectl logs ${POD_NAME} ${CONTAINER_NAME}
In my case it is kubectl logs simple-service-webapp-v1-689f6f8f5c-d4sbv simple-service-webapp Below is the output you gonna get on command prompt Listening on port 8080 Mon Jul 27 14:43:37 2020 - error querying database: pq: SSL is not enabled on the server
By default, PostgreSQL comes with SSL support. It listens for both SSL and normal connections on the same port.
Let's establish DB connection without SSL encryption Add
sslmode=disable
in simple-service-webaap-service.yaml
Cool! You just deployed simple-service webapp service into your kubernetes cluster. ☕
HPA will increase or decrease the number of replicas
- In order to use kubernetes feature like horizonal pod autoscaler, we need to use
Metrics Server
. Metrics Server is available as one of the plugins. Execute below command:
minikube addons enable metrics-server
kubectl -n kube-system rollout status deployment metrics-server
kubectl top nodes
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
minikube 174m 8% 2504Mi 43%
- Create Horizontal Pod Autoscaler
kubectl autoscale deployment simple-service-webapp-v1 --cpu-percent=50 --min=1 --max=10
- Check the current status of autoscaler by running:
kubectl get hpa
- Let's add the load send an infinite loop of queries to the simple-service-webapp
kubectl run -it --rm load-generator --image=busybox /bin/sh
Hit enter for command prompt
while true; do wget -q -O- http://{YOUR-CLUSTER-PUBLIC-IP}/live; done
- Wait a minute or so and execute the below command to see the CPU load
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
simple-service-webapp-v1 Deployment/simple-service-webapp-v1 265%/50% 1 10 6 7m27s
you can scale your pods according to your workload
- Wondering How to consume Deployment, right? 😉
5.Issue the following command to create simple-service virtual service in your cluster:
kubectl apply -f simple-service-webapp-virtual-service.yaml
- Issue the following command to confirm that sample-service-webaap virtual service is indeed up and running.
kubectl get svc
- After creating your service, you can finally define an ingress to expose simple-service service to the outside world.
- To deploy the new ingress-gateway in your cluster, you can issue the following command:
kubectl apply -f simple-service-ingress-gateway.yaml
- Find the Public IP address of your kubernetes cluster by issuing below command
minikube tunnel
kubectl get svc -n istio-system istio-ingressgateway -o=jsonpath='{.status.loadBalancer.ingress[0].ip}'
In my case it is: 10.97.72.213
Open the browser and hit http://10.97.72.213/live you should see output as
Well done :)
on browser.
Congratulations !!! We deployed a simple-service-webapp and postgres on kubernetes cluster cheers!! 🍺
Istio uses sidecar proxies as sidecontainers to microservice containers. Since all traffic flows through these proxies, they send telemetry data to Prometheus, which can be stored and visualised using tools such as Grafana.
- Verify that the prometheus service is running in your cluster by issuing below command:
kubectl -n istio-system get svc prometheus
istioctl dashboard prometheus
- Verify that the grafana service is running in your cluster by issuing below command:
kubectl -n istio-system get svc grafana
- open the Istio Dashboard via the Grafana UI
kubectl -n istio-system get pod -l app=grafana
kubectl -n istio-system port-forward grafana-54b54568fc-r6tbx 3000:3000
- Send traffic to mesh by visting http://{YOUR-CLUSTER-PUBLIC-IP}/live
- Setting up a CI/CD pipeline to deploy a containerized application to Kubernetes.
- Automate Kubernetes environment setup.
- Implementing authentication and authorization to microservice architecture using Istio and Auth0.
- Snapshot and backup of postgresql.
- Validate data persistence by deleting the PostgreSQL pod
- Resize my PostgreSQL volume if I am running out of space
- Enable secure PostgreSQL connection Portworx offers a simpler and more cost effective solution to running HA PostgreSQL on Kubernetes.[#ToDo [4, 5, 6]]