preloader

How to setup a local Kubernetes cluster

blog-image

Introduction

The last post I explained what Kubernetes is and why you should care. Now it’s time to get hands dirty. In this guide we will set up our first local Kubernetes cluster and deploy a first application. This will give you a good insight how Kubernetes works, and you will be able to play around on your own environment.

There are several different approaches for installing local Kubernetes environments. But the most straightforward approach is Docker Desktop. Yes, you heard right. Docker has a built in Kubernetes solution and it’s fairly easy to use. There are some limitations though. You can’t use it to automate your test infrastructure since it’s designed for desktop usage. If you have more advanced use-cases in mind, than just trying it out, minikube or kind might fit better to you. Anyways let’s stick to Docker Desktop for the first.

Prerequisites

This guide assumes you are using an MacOS.

If you haven’t installed brew yet, do so! It’s a great package manager for MacOS and easy to install. Instruction

Installation

Installation is super easy. Just one command:

brew cask install docker

After completion of the installation, start the Docker application (by clicking on the application). This starts the Docker daemon.

Validate that everything worked fine with the following command:

docker --version

Search the Docker logo on the top right bar next to your clock and go to the preferences menu. There is a Kubernetes configuration.

image alt text

Select the first checkmark and restart the Docker daemon. Congratulations. You’ve just installed your first Kubernetes cluster.

Last but not least we need the Kubernetes command line tool kubectl (read: Koob-sea-tea-el or koob-cuttle) to interact our cluster. Check if it’s already available:

kubectl version --client

If not install it with this command:

brew install kubectl

If you needed to install kubectl you need to set the Kubernetes context correctly. Therefore, again find the Docker logo on the top right, go to Kubernetes and select “docker-for-desktop”. This should be the only option anyways :)

Now let’s deploy our first application to verify everything worked fine.

alt text

Yaaay. It works! But wait a second. How did kubectl know to which cluster to talk to? We didn’t login anywhere! Conveniently Docker automatically created the kubeconfig. That is the file that holds all information to connect to a cluster. in case you would have a cluster with 1000 nodes, you still just need that one file that’s provided to you by the cloud provider of your choice.

It’s time to finally deploy something. Create a file called twelve.yaml Kubernetes resources can be defined in JSON or YAML. Let’s define our resources. We need a pod that runs our container and a service to make them accessible.

apiVersion: v1
kind: Pod
metadata:
  name: twelve
  labels:
    app: twelve
spec:
  containers:
  - image: gcr.io/hightowerlabs/twelve
    name: twelve
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: twelve
  name: twelve
spec:
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: twelve

Then in the same folder fire the following command

kubectl apply -f twelve.yaml

The result looks like this

pod/twelve created
service/twelve created

Verify that your deployment has been rolled out

kubectl get pods

NAME     READY   STATUS    RESTARTS   AGE
twelve   1/1     Running   0          5m47s

If you check the services

kubectl get svc

kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP    37h
twelve       ClusterIP   10.110.98.33   <none>        8080/TCP   6m29s

You will see the clusterIPs of your services. You cannot access them like this. These are only internal IPs within your Kubernetes cluster. There are several options to make them accessible. In most cases we would use an Ingress Controller for this. In our testing scenario we can access our demo app just by port-forwarding it to localhost.

kubectl port-forward svc/twelve 8080:8080

Open your browser and navigate to localhost:8080 and you should see a “Hello World” message. You did it! You’ve just deployed your first app on Kubernetes.

I hope you had fun. Kubernetes has much more to offer. I hope we can explore these possibilities together! If you have any suggestions or need some help to deal with Kubernetes feel free to ping me. I’d love your feedback! Hope you are as curious about the whole cloud native universe as I am.

Stay save and see you next time!

comments powered by Disqus