Shaun Abram
Technology and Leadership Blog
Kubernetes HelloWorld
This tutorial covers how to deploy a simple HelloWorld app on Kubernetes, and expose it externally (as you might for a UI service, for example).
I found setting up the Kube cluster deployment very straightforward, but making it externally accessible much trickier, however it boils down to just two commands:
$ kubectl create -f boothello-deployment.yaml $ kubectl expose -f boothello-service.yaml
Background
See my previous post on how to do HelloWorld with Docker first. The following examples rely on the boothello docker image from that post, which I have uploaded to docker hub at https://hub.docker.com/r/sabram/boothello/
Deployment
Create deployment
To create a deployment, I am using this yaml definition: boothello-deployment.yaml (this is based on this example: https://kubernetes.io/docs/concepts/workloads/controllers/deployment)
Create the deployment as follows:
$ kubectl create -f boothello-deployment.yaml
Alternative approaches
Feel free to skip this section, it is more of a side note, but it seems there are many ways to skin a cat in Kubernetes and that we could have used a number of other commands here to achieve a similar approach. e.g.
$ kubectl run boothello-deployment --image=sabram/boothello $ kubectl create deployment boothello-deployment --image=sabram/boothello
I used the yaml file approach since I think it is a little more explicit and I wanted to be clear on exactly what I was doing! I think my (kubectl create -f) approach is the “Imperative object configuration”, described here https://kubernetes.io/docs/concepts/overview/object-management-kubectl/overview.
One way to see how the approaches subtly differ is to run each and then compare the out of
$ kubectl describe deployments boothello-deployment
Check deployment status
Check on the rollout status:
$ kubectl get deployments
And if the available pods still show as zero, check the rollout status with:
$ kubectl rollout status deployment/boothello-deployment
If you see something like “Waiting for rollout to finish: 0 of X updated replicas are available…” with no other updates, try checking for issues with the individual pods by doing
$ kubectl get pods
If all is working, then great! We have now deployed our app to a Kubernetes cluster.
Make deployment accessible
The cluster is not externally visible by default. There are a few ways to expose it, including using kubectl proxy, creating an Ingress, creating a LoadBalance Service or, as we will show here, creating a NodePort Service.
In this specific example (inspired by https://kubernetes.io/docs/tutorials/stateless-application/expose-external-ip-address/), we create a Service object of type NodePort that exposes the deployment, using this file boothello-service.yaml
$ kubectl expose -f boothello-service.yaml
(to see another example of using NodePort, see
- https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/#creating-a-service
- Method 2: NodePort here https://kb.brightcomputing.com/faq/index.php?action=artikel&cat=26&id=357&artlang=en
)
When complete, take a look at the service we just deployed:
$ kubectl describe service boothello-service Name: boothello-service Namespace: default Labels: app=boothello Annotations: <none> Selector: app=boothello Type: NodePort IP: 10.106.141.127 LoadBalancer Ingress: localhost Port: <unset> 8080/TCP TargetPort: 8080/TCP NodePort: <unset> 31170/TCP Endpoints: 10.1.0.135:8080,10.1.0.136:8080,10.1.0.137:8080 Session Affinity: None External Traffic Policy: Cluster Events: <none>
You should now be able to access the app by using the Node Port:
curl localhost:31170
Other Options for exposing a cluster
For other ways to expose a cluster, see some notes here:
Having problems?
This https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ may help (although I have never been able to get nslookup working on a pod! I guess I need to figure out how to have it included in the docker image?)
Also see https://kukulinski.com/10-most-common-reasons-kubernetes-deployments-fail-part-1/
Tags: docker, helloworld, k8s, kube, kubernetes