Exposing a public service on Google Kubernetes Engine

To expose a service running on Google Kubernetes Engine to inbound traffic, create a LoadBalancer for the service like this:

kubectl expose deployment exampleservice --type=LoadBalancer --name=exampleservice

Once created, describe the service, and the LoadBalancer Ingress IP is your public IP for the service.

Google’s LoadBalancer service is charged per hour of usage and per GB of traffic. Check the docs for cost.

Deploying a simple SpringBoot app to Docker for Mac Kubernetes

Several months back I spent some time playing with Kubernetes but it’s been a while so I need to retrace my steps to get back up to speed.

I’ve already have my Docker for Mac single Kubernetes node running (see here)

Next, I need to run a local Docker repository where I can push my Docker images for testing. I walked through these steps here.

I have a Docker image called examplespringboot that I want to deploy to my local single node Kubernetes cluster, so first up, tag it:

docker tag examplespringboot [ip of your docker machine]:5000/examplespringboot

And then push it:

docker push [ip of your docker machine]:5000/examplespringboot

With my local Kubernetes cluster up:

$ kubectl cluster-info
Kubernetes master is running at https://localhost:6443
KubeDNS is running at https://localhost:6443/api/v1/namespaces/kube-system/services/kube-dns/proxy

I created a deployment.yml for my Docker container using the example here:

apiVersion: apps/v1
kind: Deployment
metadata:
name: exampleservice
labels:
app: exampleservice
spec:
replicas: 1
selector:
matchLabels:
app: exampleservice
template:
metadata:
labels:
app: exampleservice
spec:
containers:
- name: exampleservice
image: 192.168.0.126:5000/examplespringboot
ports:
- containerPort: 8080

This references the Docker image from my local Docker repo.

To create the deployment:

$ kubectl create -f exampleservice-deployment.yml

Now you can describe the pod and the deployment and watch them come up:

$ kubectl get pods
NAME READY STATUS RESTARTS AGE
exampleservice-7b9b4b7db9-c7hx6 1/1 Running 0 9m

At this point the running pod is not exposed outside of the cluster. Following the steps here to expose as a service and assign an external ip:

$ kubectl expose deployment exampleservice --type=LoadBalancer --name=exampleservice

If you describe the service you now see it has a LoadBalancer Ingress assigned on localhost:

$ kubectl describe service exampleservice
Name: exampleservice
Namespace: default
Labels: app=exampleservice
Annotations:
Selector: app=exampleservice
Type: LoadBalancer
IP: 10.98.115.137
LoadBalancer Ingress: localhost
Port: 8080/TCP
TargetPort: 8080/TCP
NodePort: 30718/TCP
Endpoints: 10.1.0.16:8080
Session Affinity: None
External Traffic Policy: Cluster
Events:

Calling localhost:8080/example/hello on my service, now it’s up and responding:

{"message":"hello!"}

Done!

Enabling a local Kubernetes cluster with Docker for Mac

Docker for Mac includes a single node kubernetes install that you can enable via the Docker Properties dialog:

Using kubectl you can take a look at what contexts you have configured, at some point I was playing with minikube, so now I have two:

$ kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
docker-for-desktop docker-for-desktop-cluster docker-for-desktop
minikube minikube minikube

To switch to the Docker for Mac cluster:

$ kubectl config use-context docker-for-desktop
Switched to context "docker-for-desktop".

Check cluster info:

$ kubectl cluster-info
Kubernetes master is running at https://localhost:6443
KubeDNS is running at https://localhost:6443/api/v1/namespaces/kube-system/services/kube-dns/proxy

Check running nodes:

$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
docker-for-desktop Ready master 11m v1.10.3

Now to deploy some containers!

(For a summary of kubectl commands, I have a summary post here).