Micro on Kubernetes is kubernetes native micro.
Micro is a microservice toolkit. Kubernetes is a container orchestrator.
Together they provide the foundations for a microservice platform.
- No external dependencies
- Client side discovery caching
- Optional k8s service load balancing
- gRPC transport protocol
- Pre-initialised toolkit
- Installing Micro
- Writing a Service
- Deploying a Service
- Healthchecking Sidecar
- K8s Load Balancing
- Contribute
go get github.com/micro/kubernetes/cmd/micro
or
docker pull microhq/micro:kubernetes
For go-micro
import "github.com/micro/kubernetes/go/micro"
Write a service as you would any other go-micro service.
import (
"github.com/micro/go-micro"
k8s "github.com/micro/kubernetes/go/micro"
)
func main() {
service := k8s.NewService(
micro.Name("greeter")
)
service.Init()
service.Run()
}
Here's an example k8s deployment for a micro service
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
namespace: default
name: greeter
spec:
replicas: 1
template:
metadata:
labels:
app: greeter-srv
spec:
containers:
- name: greeter
command: [
"/greeter-srv",
"--server_address=0.0.0.0:8080",
"--broker_address=0.0.0.0:10001"
]
image: microhq/greeter-srv:kubernetes
imagePullPolicy: Always
ports:
- containerPort: 8080
name: greeter-port
Deploy with kubectl
kubectl create -f greeter.yaml
The healthchecking sidecar exposes /health
as a http endpoint and calls the rpc endpoint Debug.Health
on a service.
Every go-micro service has a built in Debug.Health endpoint.
go get github.com/micro/health
or
docker pull microhq/health:kubernetes
Run e.g healthcheck greeter service with address localhost:9091
health --server_name=greeter --server_address=localhost:9091
Call the healthchecker on localhost:8080
curl http://localhost:8080/health
Add to a kubernetes deployment
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
namespace: default
name: greeter
spec:
replicas: 1
template:
metadata:
labels:
app: greeter-srv
spec:
containers:
- name: greeter
command: [
"/greeter-srv",
"--server_address=0.0.0.0:8080",
"--broker_address=0.0.0.0:10001"
]
image: microhq/greeter-srv:kubernetes
imagePullPolicy: Always
ports:
- containerPort: 8080
name: greeter-port
- name: health
command: [
"/health",
"--health_address=0.0.0.0:8081",
"--server_name=greeter",
"--server_address=0.0.0.0:8080"
]
image: microhq/health:kubernetes
livenessProbe:
httpGet:
path: /health
port: 8081
initialDelaySeconds: 3
periodSeconds: 3
Micro includes client side load balancing by default but kubernetes also provides Service load balancing strategies. We can offload load balancing to k8s by using the static selector and k8s services.
Rather than doing address resolution, the static selector returns the service name plus a fixed port e.g greeter returns greeter:8080. Read about the static selector.
To use the static selector when running your service specify the flag or env var
MICRO_SELECTOR=static ./service
or
./service --selector=static
An example deployment
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
namespace: default
name: greeter
spec:
replicas: 1
template:
metadata:
labels:
app: greeter-srv
spec:
containers:
- name: greeter
command: [
"/greeter-srv",
"--selector=static",
"--server_address=0.0.0.0:8080",
"--broker_address=0.0.0.0:10001"
]
image: microhq/greeter-srv:kubernetes
imagePullPolicy: Always
ports:
- containerPort: 8080
name: greeter-port
The static selector offloads load balancing to k8s services. So ensure you create a k8s Service for each micro service.
Here's a sample service
apiVersion: v1
kind: Service
metadata:
name: greeter
labels:
app: greeter
spec:
ports:
- port: 8080
protocol: TCP
selector:
app: greeter
Deploy with kubectl
kubectl create -f service.yaml
Calling micro service "greeter" from your service will route to the k8s service greeter:8080.
We're looking for contributions from the community to help guide the development of Micro on Kubernetes
- Integrate service mesh - provide config to inject istio, conduit, etc
- Integrate metaparticle - the ability to create self deploying microservices
- Example multi-service app - provide a microservice app example
- K8s api extenstons - provide config to setup the micro api as a k8s extension
- Support for micro functions
- Easy deployment scripts