Skip to content

Instantly share code, notes, and snippets.

@brianjbayer
Last active August 13, 2021 13:10
Show Gist options
  • Save brianjbayer/be9d64687a105d1f8ddc5fcd7c6f294b to your computer and use it in GitHub Desktop.
Save brianjbayer/be9d64687a105d1f8ddc5fcd7c6f294b to your computer and use it in GitHub Desktop.
Kubernetes in Docker Desktop for Mac

Kubernetes in Docker Desktop for Mac

Lake Erie Lighthouse at Fairport Harbor- Wendy Bayer


What this is (the simplest thing that might possibly work)

Probably the simplest way you can get started with Kubernetes (k8s) on a Mac (or run a local development environment) is with the single-node Kubernetes cluster built into Docker Desktop.

This post provides the basic recipes for...

  1. Enabling Kubernetes in Docker Desktop
  2. Using Local Images
  3. Configuring Mounted (Persistent) Volumes
  4. Adding an Ingress Controller (ingress-nginx)

Enabling Kubernetes in Docker Desktop

You enable the single-node Kubernetes cluster built into Docker Desktop through the Dashboard settings. Docker Desktop will need to restart to enable k8s.

Enabling K8s in Docker Desktop

To enable Kubernetes in Docker Desktop...

STEP ACTION
1 Start Docker Desktop (in terminal... % open /Applications/Docker.app)
2 Open Dashboard in Docker Desktop
3 Click the Settings Button
4 Select Kubernetes in the left panel
5 Select the Enable Kubernetes checkbox
6 When Enabled, Click the Apply & Restart button
7 Wait for Docker Desktop to restart and Kubernetes to be running

Using Local Images

Use imagePullPolicy: Never in your Kubernetes container configuration to use your local (locally built) images.

If you want to use locally built images with the Docker Desktop k8s...
Set the imagePullPolicy to Never in the Container using the local image.

For example in your app's deploy manifest (kubernetes configuration file)...

  spec:
    containers:
      - name: yourapp
        image: yourlocalimage
        imagePullPolicy: Never
       ...

Like most, I learned this from stackoverflow.


Configuring Mounted (Persistent) Volumes

Use the hostPath Kubernetes Volume Type to mount your local files and directories. This is particularly useful for mounting in your source code while developing.

Be aware that you are allowing access to your computer and that this type of Volume Mount is discouraged for security reasons. Be sure to allow minimum permissions and access needed.

Before you get started, check your File Sharing...

If you have permissions issues (especially if not running as root, which is a bad practice), check your Mac System Preferences > Sharing > File Sharing...

 MacOs System Preferences Sharing File Sharing

Kubernetes Configuration

To configure your locally mounted volume, you will need to...

  1. Configure your pod(s) to use the mounted volume
  2. Configure the actual local hostPath mounted volume

Configure your pod(s) to use the mounted volume

In the (Pod) Container volumeMounts: specification, specify...

  • The Location and name for the volume in the container (mountPath: /app )
  • The name of the Kubernetes Volume Mount for the mounted volume (name: app-source)

For example in your pod specification...

 spec:
    containers:
      - name: yourapp
        image: yourimage
       ... 
        volumeMounts:
          - mountPath: /app
            name: app-source

Configure the actual local hostPath mounted volume

In the volumes: specification, specify...

  • The name of the Kubernetes Volume Mount for the mounted volume (name: app-source)
  • hostPath: Kubernetes Volume Type
  • The Fully Qualified Path of the local file or directory to be mounted (path: /Users/yourmacuser/.../yourappsource)
  • Type of local mounted volume (type: Directory, type: File )

For example in your deployment specification...

    volumes:
      - name: app-source
        hostPath:
          path: /Users/yourmacuser/yourappsource
          type: Directory

I learned this from docker forum.


Adding an Ingress Controller (ingress-nginx)

If you need an ingress-controller for your ingresses in Kubernetes in Docker Desktop, you will need to use the basic ingress-nginx NGINX Ingress Controller.

Once you have added the ingress-nginx controller to the Docker Desktop k8s cluster, you can then configure your ingresses to use it.

Adding the NGINX Ingress Controller to Docker Desktop Kubernetes Cluster

Use the official Kubernetes NGINX Ingress Controller deployment manifest to add ingress-nginx to your k8s cluster.

STEP ACTION
1 Ensure Kubernetes in Docker Desktop is running
2 Get the current ingress-nginx deployment at https://kubernetes.github.io/ingress-nginx/deploy/
3 In a terminal window, use kubectl to apply the ingress-nginx deployment (for example: % kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.48.1/deploy/static/provider/cloud/deploy.yaml)
4 In a terminal window, run the command to watch the deployment to verify it is running % kubectl get pods -n ingress-nginx -l app.kubernetes.io/name=ingress-nginx --watch
5 Type Ctrl + C to exit watching the ingress

Kubernetes Configuration

To use the ingress-nginx controller in your ingress, you must configure it.

Here is an example ingress using ingress-nginx localhost routing ( http://localhost/)...

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: yourapp-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
        - path: /
          backend:
            service:
              name: app
              port:
                number: 3000

I learned this from Matthew Palmer's Kubernetes Ingress with Nginx Example and referencing the ingress-nginx README.


Brian J. Bayer - Recovering Developer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment