No BS guide to Kubernetes
Tags: development, kubernetes, tutorial, docker
Published: 2021-12-30 23:49:56

Recently due to the huge presence and dominance of [Kubernetes](https://kubernetes.io/docs/home/) as an orchestration tool I decided to take another look at it. I have worked a little bit with Kubernetes back in 2017 but having mostly focused on Development after that I didn't do much with it since then. 

Going back to it was a bit of a pain initially as there's so much material around and so much information that it took me longer than I wanted to have a simple initial example setup project with Kubernetes running in my local environment. To mitigate that for others I decided to do this quick article about it.

The focus of this article is running a minimal Kubernetes local setup so you can get a first hands on experience with it. I do not explain the concepts of Kubernetes or Docker in this post. 

I plan in the near future to migrate some of my local docker-compose setups to Kubernetes for fun and learning pourposes, so stay tuned, enough intro for now, let's go for it.

## Pre-requisites:

Install: 

- [Docker](https://docs.docker.com/get-docker/)

- [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl)

- [kind](https://kubernetes.io/docs/tasks/tools/#kind)

## Create kind configuration file and start kind

Create a cofiguration file for kind: `kind-config.yml`

```
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 31111
    hostPort: 31111
    listenAddress: "0.0.0.0" # Optional, defaults to "0.0.0.0"
    protocol: tcp # Optional, defaults to tcp
- role: worker
```

Now start kind using the config file: `kind create cluster --config=kind-config.yml` this will start a local docker container containing the Kind kubernetes control plane we will be using, it also exposes a NodePort to your host so you can reach the nginx service we will run in kubernetes next.

>> Note: This file(kind-config.yml) is required to run local kubernetes Nodeport as [described here](https://kind.sigs.k8s.io/docs/user/quick-start/#mapping-ports-to-the-host-machine)

## Kubernetes configuration files

Create the kubernetes declarative files:

A simple app running in a docker container wrapped in a pod.

Let's use [nginx hello](https://hub.docker.com/r/nginxdemos/nginx-hello) demo image. Create a file called: 

hello-pod.yml

```
apiVersion: v1
kind: Pod
metadata:
  name: hello-pod
  labels:
    app: nginx
spec:
  containers:
    - name: web-nginx
      image: nginxdemos/hello:latest
      ports:
        - containerPort: 80
```

---

3. A kubernetes nodeport service, create a file called:

svc-config.yml

```
apiVersion: v1
kind: Service
metadata:
  name: nginx-nodeport
spec:
  type: NodePort
  ports:
  - port: 8000
    targetPort: 80
    nodePort: 31111
    protocol: TCP
  selector:
    app: nginx
```

---

Apply configurations: 

`kubectl apply -f hello-pod.yml`

`kubectl apply -f svc-config.yml`

You can optionally apply multiple files pointing to a directory so instead of the two commands above you could place those 2 files in a folder for example `k8s` and then run: 

`kubectl apply -f k8s` 

---

Access your running kubernetes setup application, on browser navigate to : 

[http://localhost](http://localhost) You should see it: 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddet67nm7ei73d8ho0w5.png)
 


### Some useful commands: 

`docker ps` -> show running docker containers used by kind to create the kubernetes control plane and worker.

`kubectl cluster-info` -> display kind cluster info.

`kubectl describe svc $svc_name` -> describes service

`kubectl get pods` -> show pod information

`kubectl get pods --show-labels`

`kubectl version --short`

`kind get clusters` -> display kluster names


### Delete commands: 

`kubectl delete pod $pod_name`

`kubectl delete svc $service_name`

`kind delete cluster $cluster_name`


**This is it, no BS.... it's done! **


AS usual the code and sketch for this article can be found on [Github repo](https://github.com/mmaia/kubernetes-quick-start) 


Photo by <a href="https://unsplash.com/@carrier_lost?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Ian Taylor</a> on <a href="https://unsplash.com/s/photos/docker?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>