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
We will have a common message channel that routes events to each node separately!
Windows:
minikube start --vm=true
minikube start --driver=hyperv
minikube start --driver=virtualbox
Service is a general API that handle access to a running container
1 |
|
kubectl apply -f posts.yaml
¶Deployment
Deployment controls a set of same-type pods for updating, re-creating new pods…etc
1 |
|
1 |
|
¶Update - Make Change to Deployment
use latest
in posts-depl.yaml
1 |
|
¶Service
Between each pod, Service is used to communicate between each other
Service is used as a general API of the pods.
- 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
nodePort
is a randomly assigned port to access the service outside of the Node
1 |
|
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
¶A general procedure of creating a deployment+service
- Build an image for the Service ( event bus, in this step)
- Push the image to Docker Hub
- Create a deployment for Event Bus
- Create a cluster IP service for event bus and posts
- 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 |
|
Once ClusterIp
service is setted up, it can directly communicate within cluster with
1 |
|
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
Nginx ingress controller uses LoadBalancer type service actually as entrypoint to the cluster. Then is checks ingress rules and distributes the load.
1 |
|
¶Skaffold
As the process of deploying app so annoying, we can use this tool to expedite our development.
这里具体看代码
终于搞完这个了,学到了几点:
- The way of interactions between micro-services is by HTTP requests (or we can use gRPC in future.
- Each service will send event to
Event Bus
, which is pretty similar to how REDUX works. - Each service will have a event listener as well.
- Each service will need
Deployment
,ClusterIP Service
to communicate within a cluster. - A
ingress-nginx
pod is created to match different routes to specific ports based on route rules. (through nginx) - A
LoadBalancer type
Service is automatically initialized withingress-nginx
. It is solely responsible to communicate withingress-nginx
pod. - Skaffold is able to detech changes made inside source files by connecting
depl.yml, Dockerfile, skaffold.yml
Note that we need to usenodemon, star:dev
to allow for hot reload.
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!