Setting Up Kubernetes Locally with K3d

December 11, 2023  2 minute read  

๐ŸŒŸ Welcome to the Ultimate Guide on Setting Up Kubernetes Locally with Docker Desktop and NGINX Ingress! ๐Ÿš€

Are you ready to dive into the exciting world of Kubernetes without leaving the comfort of your local machine? Letโ€™s get started!

๐Ÿ“ฅ Step 1: Install Docker Desktop

First things first, you need Docker Desktop on your machine. Itโ€™s your gateway to running Kubernetes locally.

๐Ÿ”„ Step 2: Enable Kubernetes in Docker Desktop

Once Docker is cozy on your machine, itโ€™s time to awaken the Kubernetes beast within it.

  • โœ… Enable Kubernetes: Open Docker Desktop, find your way to Preferences > Kubernetes, and tick the โ€œEnable Kubernetesโ€ box.
  • ๐Ÿ” Apply & Restart: Hit โ€œApply & Restartโ€ and watch the magic happen!

๐Ÿšฆ Step 3: Install NGINX Ingress Controller

Kubernetes is cool, but it needs a trusty Ingress controller to manage traffic. And NGINX is our hero here.

  • ๐Ÿ—บ๏ธ Install via Helm: Helm is like your Kubernetes treasure map. Follow the instructions on the Helm website to get it. Then run these commands:
    helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
    helm repo update
    helm install nginx-ingress ingress-nginx/ingress-nginx
    

๐Ÿ–Š๏ธ Step 4: Configure DNS Using Hosts File

Time to play with the hosts file to make your local DNS play nice with Kubernetes.

  • ๐Ÿ“ Edit Hosts File: This file is hiding at /etc/hosts on Linux and macOS, and C:\Windows\System32\Drivers\etc\hosts on Windows. Add lines like these:
    127.0.0.1   myservice.local
    127.0.0.1   anotherservice.local
    

๐Ÿš€ Step 5: Deploy Your Application

Letโ€™s get your app into the Kubernetes playground!

  • ๐Ÿ“„ Create Kubernetes Manifests: Whip up some YAML files for your deployment and service. Hereโ€™s a sample to get you started:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  selector:
    matchLabels:
      app: my-app
  replicas: 2
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

๐Ÿ›ฃ๏ธ Step 6: Create Ingress Resource

We need some rules to tell NGINX how to route that traffic.

  • ๐Ÿ“ƒ Define Ingress Rules: Craft an Ingress YAML that looks something like this:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - host: myservice.local
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-app-service
            port:
              number: 80

โœ… Step 7: Apply Your Configuration

Almost there! Letโ€™s make Kubernetes aware of your grand plans.

  • ๐Ÿ”ง Apply with kubectl: Run these
  kubectl apply -f <your-deployment.yaml>
  kubectl apply -f <your-ingress.yaml>

๐ŸŽ‰ Step 8: Test Your Setup

Moment of truth! Open your browser and go to http://myservice.local. Fingers crossed! ๐Ÿคž


๐Ÿ” Troubleshooting Tips: If things go sideways, peek into the NGINX Ingress controller logs and your podsโ€™ logs.

Remember, this is your local Kubernetes playground. In the real world, youโ€™d have an external IP or a proper domain name instead of localhost.

Thatโ€™s it! Youโ€™ve just set up a Kubernetes environment locally with Docker Desktop and NGINX Ingress. Happy coding! ๐Ÿ’ป๐ŸŒ

Leave a comment