Hello Kubernetes#

In this lab you’ll use your local machine to deploy a “Hello World!” application using Kubernetes. By the end of the lab you should be able to confirm that the kubernetes on your machine is working.

Create the Apache Container#

To Kubernetes, everything is a resource. Resources can be loaded from a file (like you’re about to do) or created automatically by a program. When resources are loaded from a file the files are YAML format. YAML files can have multiple resource definitions separated by --- on a line by themselves.

To start let’s create a pod with a default Apache server:

apiVersion: v1
kind: Pod
metadata:
  name: hello-pod
  labels:
    function: webserver
spec:
  containers:
  - name: httpd
    image: docker.io/httpd:latest
    ports:
    - containerPort: 80

Copy and paste that YAML into a file called hello-pod.yaml and apply the configuration:

$ kubectl apply -f hello-pod.yaml

Verify that the pod has started:

$ kubectl get all 

Once started inspect the pod:

$ kubectl describe pod/hello-pod

To check the Apache logs:

$ kubectl logs pod/hello-pod

Manually Access the Container#

There’s no way to access the pod from outside our node yet. The kubectl command gives us a way to access a pod for debugging purposes.

$ kubectl port-forward hello-pod 8080:80 

Now browse to localhost:8080. You should see Apache’s “It works!”

Expose the Container Permanently#

In order to maintain external access to the container you need to define a service. Here’s a service definition:

apiVersion: v1
kind: Service
metadata:
  name: hello-svc
spec:
  type: NodePort
  selector:
    function: webserver
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 80

Copy and paste that YAML into a file called hello-svc.yaml and apply the configuration:

$ kubectl apply -f hello-svc.yaml

Verify that the service is present:

$ kubectl get all 

Inspect the service more closely:

$ kubectl describe service/hello-service

The service type NodePort makes a port available on the Kubernetes node. If you’re running on Ubuntu on your local machine or on a VM you should be able to connect to localhost:8080.

Windows and Mac Users

I’m not sure how Kubernetes works in Docker Desktop. In class I’ll be able to help you with the NodePort service.

Delete the Resources#

Let’s clean up:

$ kubectl delete pod/hello-pod svc/hello-svc