DEV Community

Cover image for Setup local Kubernetes cluster with Docker Desktop
Sayed Naweed Rizvi
Sayed Naweed Rizvi

Posted on

Setup local Kubernetes cluster with Docker Desktop

Cloud is in the air, literally !!

Kubernetes has been a game changer, when it comes to scaling your applications and the ease with which you can deploy containers on a cluster.

Learn how easy it is to deploy a container on your very own local kubernetes cluster, no need to worry about getting access to a Azure/AWS/GCP subscription. Just follow along.

Here are the Topics we will cover

  1. Enable Kubernetes on DockerDesktop
  2. Deploy a “Hello World” app on your local cluster
  3. Deploy a NGINX app & access it from local browser using
  4. Port Forwarding

Pre-requisites:
DockerDesktop

Docker Desktop: The #1 Containerization Tool for Developers | Docker

Docker Desktop is collaborative containerization software for developers. Get started and download Docker Desktop today on Mac, Windows, or Linux.

favicon docker.com

NOTE: Docker Desktop Stable releases require the Hyper-V feature which is not available in the Windows 10 Home edition.


Enable Kubernetes Cluster on DockerDesktop
You can use other tools such as Minikube or Kind which are also suited for running a single Node Cluster locally.

First and foremost, Enable Kubernetes & Restart Docker Desktop

Docker Desktop

We now have a single-node cluster running on our systems, you can run kubectl commands and play around a bit before getting started.

On checking cluster-info shown below, we find that a master is running, now do remember that this is a single-node cluster i.e. master & worker are are on the same node. (A more practical use-case would have a master and many worker nodes to achieve High Availability-HA).

kubectl cluster info

kubectl get nodes

Check namespaces, not that it’s relevant in this context but remember we will be working in the default namespace.

kubectl get namespaces

Also, you will find a default ‘kubernetes’ service already running on clusterIP.



Deploy “Hello World” App on the Cluster
Lets start out by deploying a simple “Hello World” application. For this we will create a Deployment and a Service — which connects to the deployment.

Do remember that you can either run kubectl commands directly from a Power shell window or create manifest Yaml file(s) as shown below.

1. Deployment Creation
By creating a manifest file with kind: Deployment, we are essentially creating containers, pods, replicaSets in the background.

deployment.yaml



apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
spec:
  selector:
    matchLabels:
      app: hello
      version: 2.0.0
  replicas: 2
  template:
    metadata:
      labels:
        app: hello
        version: 2.0.0
    spec:
      containers:
      - name: hello
        image: "gcr.io/google-samples/hello-app:2.0"
        env:
        - name: "PORT"
          value: "50001"


Enter fullscreen mode Exit fullscreen mode

_Translation:

Pull a container from gcr.io/google-samples/hello-app:2.0 and

replicas: 2 means that there will be 2 pods running any point of time._

2. Services — Access the deployment
service.yaml



apiVersion: v1
kind: Service
metadata:
  name: hello-service
spec:
  type: NodePort
  selector:
    app: hello
    version: 2.0.0
  ports:
  - name: http
    protocol: TCP
    port: 8081
    targetPort: 50001
    nodePort: 32000


Enter fullscreen mode Exit fullscreen mode

Here the ‘hello-service’ connects to all the pods created above having name ‘hello’ as mentioned by the selector attribute at targetPort=50001

Remember, we are creating a NodePort service to enable access from outside the cluster. From within the cluster, all the pods are accessible through ClusterIP.

Hello World is deployed and is accessible through nodePort, http://localhost:32000/



Hello, world!
Version: 2.0.0
Hostname: hello-fdf67c7-b4fs4



Enter fullscreen mode Exit fullscreen mode

Deploying Nginx in 3 steps

We will be deploying an nginx:alpine image (from the public docker hub registry) onto a local kubernetes cluster running on our DockerDesktop.

1. Create Deployment

Lets create nginx deployment, this will create a Deployment and a Pod



kubectl create deploy nginx —image=nginx:alpine


Enter fullscreen mode Exit fullscreen mode

2. Create Service to expose deployment

We want our nginx app to be accessible from any pod within cluster, so we expose it.

kubectl expose deploy nginx --port=80 --target-port=8000

Verify if this nginx app is accessible from any other pod within the cluster, add a ubuntu test pod and run curl on the Pod IP



# lets connect to the nginx app using clusterIP
$ kubectl get service nginx
NAME    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
nginx   ClusterIP   10.106.163.0   <none>        80/TCP    25h
# spin up a test ubuntu pod
$ kubectl run -i --tty ubuntu --image=ubuntu --restart=Never -- sh
# check connectivity through PodIP and Port
$ curl http://10.1.0.13:80


Enter fullscreen mode Exit fullscreen mode

BINGO !! we can reach the nginx deployment from within cluster

3. Access the deployment from outside the cluster (locally)

Now we want to make this nginx deployment accessible from outside the cluster.

Do remember that each Pod within a Cluster has a unique IP address, which can be accessed from outside the cluster through a Service, but if you are running cluster locally this can be easily achieved through Port Forwarding.

Port Forwarding will allow you to connect to a Pod on a forwarded(different) port from you local machine, without exposing the Services.

Use Port Forwarding to Access Applications in a Cluster | KubernetesUse Port Forwarding to Access Applications in a Cluster | Kubernetes

This page shows how to use kubectl port-forward to connect to a MongoDB server running in a Kubernetes cluster. This type of connection can be useful for database debugging. Before you begin You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts.

favicon kubernetes.io

Lets port-forward our nginx deployment to a random port.



$ kubectl port-forward deployment/nginx :80
Forwarding from 127.0.0.1:56271 -> 80
Forwarding from [::1]:56271 -> 80
Handling connection for 56271
Handling connection for 56271
Handling connection for 56271
Handling connection for 56271


Enter fullscreen mode Exit fullscreen mode

Open up your browser and point to the localhost:56271 (forwarded port)

Nginx Welcome Page

NOTE: Port-Forwarding need to be running all the time for this to work

Hope this blog helps you in getting started with kubernetes.

It’s an open ground, Play around !!

Top comments (0)