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...
- Enabling Kubernetes in Docker Desktop
- Using Local Images
- Configuring Mounted (Persistent) Volumes
- Adding an Ingress Controller (ingress-nginx)
You enable the single-node Kubernetes cluster built into Docker Desktop through the Dashboard settings. Docker Desktop will need to restart to enable k8s.
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 |
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.
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.
If you have permissions issues (especially if not running as root, which is a bad practice), check your Mac System Preferences > Sharing > File Sharing...
To configure your locally mounted volume, you will need to...
- Configure your pod(s) to use the mounted volume
- Configure the actual local
hostPath
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
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.
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.
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 |
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