DEV Community

Cover image for No BS guide to Kubernetes
Marcos Maia
Marcos Maia

Posted on • Edited on

3

No BS guide to Kubernetes

Recently due to the huge presence and dominance of Kubernetes 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:

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
Enter fullscreen mode Exit fullscreen mode

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

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 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
Enter fullscreen mode Exit fullscreen mode

  1. 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
Enter fullscreen mode Exit fullscreen mode

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 You should see it:

Image description

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

Photo by Ian Taylor on Unsplash

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay