Microservice - Deploy with K8s (2)

MicroService Deployment with Docker + Kubernetes + Skaffold

This one is pretty long but very helpful to understand what is going on with Kubernetes and how different pods get connected with each other!

The banner image is Dao Cheng (稻城), which is a famous natural reserve in Sichuan province. I have never been there but always heard it. I have to say that Sichuan province is gifted as it has so many natural sceneries that cannot be missed. Really wish to visit there after I back there! btw, really missing Sichuan hometown food~~

Microservice - Deployment

Kubernetes + Docker

Untitled

We will have a common message channel that routes events to each node separately!

Untitled

Windows:

minikube start --vm=true

minikube start --driver=hyperv

minikube start --driver=virtualbox

Untitled

Service is a general API that handle access to a running container

1
2
3
4
5
6
7
8
apiVersion: v1
kind: Pod
metadata:
name: posts
spec:
containers:
- name: posts
image: hughqing/posts:0.0.1

kubectl apply -f posts.yaml

Untitled

Untitled

Untitled

Deployment

Deployment controls a set of same-type pods for updating, re-creating new pods…etc

1
2
kubectl delete deployment <name>
kubectl apply -f deployment ./posts-depl.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: apps/v1
kind: Deployment
metadata:
name: posts-depl
spec:
replicas: 1
selector:
matchLabels:
app: posts
template:
metadata:
labels:
app: posts
spec:
containers:
- name: posts
image: hughqing/posts:0.0.1

Update - Make Change to Deployment

use latest in posts-depl.yaml

1
kubectl rollout restart deployment [depl_name]

Untitled

Service

Between each pod, Service is used to communicate between each other

Service is used as a general API of the pods.

Untitled

  • Within each pod, we use Cluster IP
  • In the Dev stage, we can use the port to connect with the web browser
  • In the Prod stage, Load Balancer is used

For now, we use Node Port for fun

Untitled

nodePort is a randomly assigned port to access the service outside of the Node

1
2
kubectl apply -f posts-srv.yaml
kubectl get services

One type of pod will get one service

To communicate from Event-Bus to another pod, we are basically communicating from posts-clusterip-srv and event-bus-srv

Here a cluster is a set of same-type service

Untitled

A general procedure of creating a deployment+service

  1. Build an image for the Service ( event bus, in this step)
  2. Push the image to Docker Hub
  3. Create a deployment for Event Bus
  4. Create a cluster IP service for event bus and posts
  5. Wire them up

We can write deployment config and service config in one config file

I type: ClusterIP in spec will be the default

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
apiVersion: apps/v1
kind: Deployment
metadata:
name: event-bus-depl
spec:
replicas: 1
selector:
matchLabels:
app: event-bus
template:
metadata:
labels:
app: event-bus
spec:
containers:
- name: event-bus
image: hughqing/event-bus
---
apiVersion: v1
kind: Service
metadata:
name: event-bus-srv
spec:
type: ClusterIP #defualt
selector:
app: event-bus
ports:
- name: event-bus
protocol: TCP
port: 4005
targetPort: 4005

Untitled

Once ClusterIp service is setted up, it can directly communicate within cluster with

1
http://<service_name>:<port>

This finishes our last step of wiring up.

We then do the similar steps from

  • Build container image
  • Push
  • Create deployment file
  • kubectl apply -f .
  • wire them in event-bus
  • kubectl rollout restart deployment <name> if need

Load Balancer - Nginx/ Ingress

Load Balancer Service: A config file for a LoadBalancer Service is to let Cloud platform provision a Load Balancer outside of a cluster so that traffic can be routed into cluster.

Ingress: A pod with a set of routing rules to distribute traffic to other services

https://kubernetes.github.io/ingress-nginx/deploy/#quick-start

Untitled

Nginx ingress controller uses LoadBalancer type service actually as entrypoint to the cluster. Then is checks ingress rules and distributes the load.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# ingress-srv.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-srv
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- host: posts.com
http:
paths:
- path: /posts
pathType: Prefix
backend:
service:
name: posts-clusterip-srv # service name
port:
number: 4000

Untitled

Skaffold

As the process of deploying app so annoying, we can use this tool to expedite our development.

这里具体看代码

终于搞完这个了,学到了几点:

  1. The way of interactions between micro-services is by HTTP requests (or we can use gRPC in future.
  2. Each service will send event to Event Bus , which is pretty similar to how REDUX works.
  3. Each service will have a event listener as well.
  4. Each service will need Deployment , ClusterIP Service to communicate within a cluster.
  5. A ingress-nginx pod is created to match different routes to specific ports based on route rules. (through nginx)
  6. A LoadBalancer type Service is automatically initialized with ingress-nginx. It is solely responsible to communicate with ingress-nginx pod.
  7. Skaffold is able to detech changes made inside source files by connecting depl.yml, Dockerfile, skaffold.ymlNote that we need to use nodemon, star:dev to allow for hot reload.