Heavily inspired by the official grpc helloworld
example but with a fully working Kubernetes deployment example.
Will only work on your machine.
The echo-service works as follows: The server will answer pong
on any ping
request sent using the gRPC client. The pong
response and ping
message are hardcoded in the server and client.
- Docker
kind
protoc
and Go plugins for the protocol compiler- Go
- See the prerequisites here to install
protoc
and the Go plugins needed. - In the root of this repository run:
make gen-proto
- Start the
kind
cluster:
kind create cluster --name k8s-1.19.7 --config kind/k8s-1.19.7.yaml
- Install
metallb
to be able to create aService
of typeLoadBalancer
in Kubernetes:
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/master/manifests/namespace.yaml
kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/master/manifests/metallb.yaml
- Identify the CIDR network used by Docker:
docker network inspect -f '{{.IPAM.Config}}' kind
- Create and apply a
ConfigMap
using a subset of the addresses in the CIDR from the output above, you should replace the IP addressesaddresses
field below:
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
namespace: metallb-system
name: config
data:
config: |
address-pools:
- name: default
protocol: layer2
addresses:
- 172.18.255.200-172.18.255.250
EOF
- Build the grpc-echo-service Docker image:
docker build . -t grpc-echo-service:v0.1.0
- Push the built grpc-echo-service Docker image to a registry.
- Change the image in
manifests/deployment.yaml
pointing to the one you've just pushed. - Apply the
deployment.yaml
andservice.yaml
files.
kubectl apply -f manifests/deployment.yaml
kubectl apply -f manifests/service.yaml
- Take a note of the Load Balancer IP the
Service
have received from MetalLB:
kubectl get svc grpc-echo-service -o=jsonpath='{.status.loadBalancer.ingress[0].ip}'
- Add the Load Balancer IP to the
cert/echo_client-ext.cnf
andcert/echo_server-ext.cnf
files. - Re-generate the certificates:
make gen-cert
- Re-build and re-push the Docker image.
- Restart the Deployment of grpc-echo-service server component:
kubectl deployment rollout restart grpc-echo-service
- Run the gRPC client:
go run echo_client/*.go -addr "172.18.255.200:8443" -cert cert/client-cert.pem -key cert/client-key.pem
you should see an output similar to this one:
2021/05/02 22:40:35 Starting gRPC echo service client..
2021/05/02 22:40:35 Echoes service replied: pong