I try to get familiar with Kubernetes (in short k8s) and the best way to start is with a hello world.

But first some overview:

  • We use docker images to scale pods.
  • Pods are part of the deployment.
  • Deployments define scaling, services such as routes and pods.
  • Deployments can be used on k8s cluster.
  • A cluster have at least one master node.
  • But usually multiple child nodes across multiple servers either bare metal or cloud based.
  • A cluster can be extended with plugins.


We can start locally with Docker (Docker for Mac in my case) or with Minikube. I don’t see any difference in usage. So I sticked to Docker for Mac because I already using it. If you hit the checkbox for Kubernetes in the Docker settings. Your system will be updated after a short while and you are ready to go.

kubectl describe nodes # output your current cluster

Check your home folder. There should be a ~/.kube folder with the config file. This file authorizes you with your current cluster. This can be extended for multiple clusters.


Now you can hit the following commands for a quick hello world:

kubectl create deployment hello-node --image=gcr.io/hello-minikube-zero-install/hello-node
kubectl expose deployment hello-node --type=NodePort --port=80 --target-port=8080

kubectl get deployments
### output
NAME         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hello-node   1         1         1            1           2d

kubectl get services
### output
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
hello-node   NodePort    10.103.27.97   <none>        80:32005/TCP   2d
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        2d

The first command creates a basic deployment for us. This deployment will pull the Docker image and instantiates the container. But we cannot access it directly from localhost. For that we create service via kubectl expose command. The easiest way to access our deployment is via NodePort (for me at least at the moment). NodePort maps our defined port to a public port between 30000 and 32767. The target port is the exposed Docker port. With the kubectl get services command you will see which one. But you can define this port as well. Now it should be possible to access the Docker instance via localhost:[port] and you will see “Hello World!”.

You can also edit the deployment and service.

kubectl edit service hello-node
kubectl edit deployment hello-node

Any changes will be validated and applied. If you want to get rid of them:

kubectl delete deployment hello-node
kubectl delete service hello-node

But if your project is getting bigger you don’t want to handle a lot of commands. You want to define yaml files for your project.

# ------------------- Hello Node Deployment ------------------- #
kind: Deployment
apiVersion: apps/v1
metadata:
  name: hello-node
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-node
  template:
    metadata:
      labels:
        app: hello-node
    spec:
      containers:
      - name: hello-node
        image: gcr.io/hello-minikube-zero-install/hello-node
        ports:
        - containerPort: 8080

---
# ------------------- Hello Node Service ------------------- #
kind: Service
apiVersion: v1
metadata:
  name: hello-node
spec:
  ports:
    - port: 80
      targetPort: 8080
  type: NodePort
  selector:
    app: hello-node
kubectl create -f helloworld.yaml

The result is the same.

Sources

  • https://kubernetes.io/docs/tutorials/hello-minikube/