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 your GKE cluster 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 8000:80 

Now browse to localhost:8000. 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: LoadBalancer
  selector:
    function: webserver
  ports:
    - protocol: TCP
      port: 80
      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-svc

The service type LoadBalancer will cause Google to allocate an IP address for your service. The process may take a few minutes. Once you see an IP address navigate there with your browser.

Delete the Resources#

Let’s clean up:

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