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!