Kubernetes is a powerful container orchestration platform that automates the deployment, scaling, and management of containerized applications. Docker Desktop provides a convenient way to set up a Kubernetes cluster locally, making it an excellent tool for learning and development.
Here’s a step-by-step guide to getting started with Kubernetes using Docker Desktop:
Prerequisites
- Install Docker Desktop:
- Download and install Docker Desktop from the official website for your operating system (Windows, macOS, or Linux).
- Ensure your system meets the minimum requirements.
- Enable Kubernetes:
- Docker Desktop includes a built-in Kubernetes cluster that can be enabled from the settings.
Step 1: Enable Kubernetes in Docker Desktop
- Open Docker Desktop.
- Go to Settings (⚙️) → Kubernetes.
- Check the box to enable Kubernetes:
- “Enable Kubernetes”.
- Click Apply & Restart.
- Docker Desktop will download and configure the Kubernetes components.
- Verify the Kubernetes setup:
- Once Kubernetes is enabled, you’ll see the Kubernetes is running indicator in Docker Desktop.
Step 2: Verify Kubernetes Installation
- Open a terminal.
- Check the Kubernetes version:
kubectl version --client
- This should display the
kubectl
client version, indicating that Kubernetes CLI is available.
3. Verify cluster information:
kubectl cluster-info
4. Check the nodes in the cluster:
kubectl get nodes
- You should see a single node (e.g.,
docker-desktop
).
Step 3: Deploy a Sample Application
Let’s deploy a simple Nginx web server to the Kubernetes cluster.
- Create a deployment: kubectl create deployment nginx –image=nginx
- Expose the deployment as a service:
kubectl expose deployment nginx --type=LoadBalancer --port=80
- Check the deployment and service:
- Deployments:
kubectl get deployments
- Services:
kubectl get services
- Deployments:
- Access the application:
- For Docker Desktop, Kubernetes assigns a local cluster IP.
- Use the following command to find the service IP:
kubectl get service nginx
- Open the browser and navigate to the service’s
EXTERNAL-IP
orlocalhost
(if port-forwarded).
Step 4: Use Kubernetes Dashboard
Docker Desktop includes a Kubernetes dashboard for visual management.
- Enable the Kubernetes dashboard:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
- Access the dashboard:
- Start a proxy to access the dashboard:
kubectl proxy
- Open your browser and navigate to:
http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
- Start a proxy to access the dashboard:
- Generate a token for access:
- List service accounts:
kubectl -n kubernetes-dashboard get serviceaccounts
- Retrieve the token:
kubectl -n kubernetes-dashboard get secret | grep dashboard kubectl -n kubernetes-dashboard describe secret <your-secret-name>
- Copy the token and use it to log in to the dashboard.
- List service accounts:
Step 5: Clean Up Resources
When done, you can clean up the created resources to free up memory and storage.
- Delete the service and deployment:
kubectl delete service nginx kubectl delete deployment nginx
- Stop the Kubernetes proxy (if running):
Ctrl + C
Tips for Using Kubernetes with Docker Desktop
- Resource Allocation:
- Go to Docker Desktop settings and allocate sufficient CPU, memory, and disk space for Kubernetes under Resources.
- Use
kubectl
Autocompletion:- Enable command autocompletion for
kubectl
to make working with Kubernetes easier:bashCopy codesource <(kubectl completion bash)
- Enable command autocompletion for
- YAML Manifests:
- Instead of using
kubectl create
commands, you can define applications in YAML manifests for greater control.
- Instead of using
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
Conclusion
Using Docker Desktop to learn Kubernetes is a simple and effective way to get started. Its built-in Kubernetes cluster eliminates the need for complex setups, enabling you to focus on understanding core concepts and practicing deployments. As you become more comfortable, you can move to more advanced setups with managed Kubernetes services like GKE, AKS, or EKS.