Kubernetes local deployment


What's inside this article ⌄
  • Kubernetes local development setup
  • Docker desktop Kubernetes enable
  • Kubectl deployment nginx example
  • Kubernetes port forwarding local

For those who uninitiated, Kubernetes is often shortened to k8s. The thing is that sometimes you want to play around with k8s configuration and other stuff but don’t want to spin up a complete cluster in the cloud.

The easiest way to spin up a local single-noded k8s installation is to use Docker Desktop. Go to Preferences → Kubernetes and check the box next to “Enable Kubernetes” and click “Apply & Restart”.

After enabling Kubernetes in Docker Desktop, you can verify the installation by opening a terminal and running the following command:

kubectl version

Let’s test it now. Create a file named deployment.yaml with the basic deployment configuration:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest

You see replicas set to 3. It means that we eventually end up with three pods or three workloads having each other back.

Now, deploy it:

kubectl apply -f deployment.yaml

You can check k8s deployments or pods using:

kubectl get deployments
kubectl get pods

This is the command to use in case you want to view the logs:

kubectl logs <pod-name>

It’s time to access the nginx server now. There are two ways of achieving it:

  • Through port forwarding to deployment
  • through port forwarding to a specific pod.

When using the first approach, a round-robin algorithm will be used to choose a pod you’ll be connected to:

kubectl port-forward deployment/nginx-deployment 8080:80

Finally, open localhost:8080 in your browser.