Deploying your docker image to Minikube
In this tutorial I’ll be showing you how you can quickly set up minikube on your local windows machine, and then deploy a docker image successfully in it. This tutorial continues from my previous post Containerize a .NET Core Web Api App with Docker
Enabling Hyper-V
Before you install Minikube, ensure Hyper-v is enabled on your windows machine. You can verify this by clicking on the start menu and searching for Turn Windows features on or off
If you are using windows Home edition, this Hyper-V feature will not be available for you in the list. If that is the case then do the following :
Create a new .bat file with the following content :
pushd "%~dp0"
dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hyper-v.txt
for /f %%i in ('findstr /i . hyper-v.txt 2^>nul') do dism /online /norestart /add-package:"%SystemRoot%\servicing\Packages\%%i"
del hyper-v.txt
Dism /online /enable-feature /featurename:Microsoft-Hyper-V -All /LimitAccess /ALL
pause
Save the .bat script, then right-click and Run as Administrator. Once the script completes execution, you might have to restart your PC, after which you will see the Hyper-v option available under the Turn Windows features on or off screen.
- Issue on github : https://github.com/MicrosoftDocs/Virtualization-Documentation/issues/915
Installing Minikube
To install minikube you can follow the installation steps in https://minikube.sigs.k8s.io/docs/start/
In my case I’ve used the chocolatey command :
choco install minikube
Once the installation is complete, you can run the following command to start minikube
minikube start
NOTE : Kubectl should already be installed on your windows machine, if you had enabled Kubernetes in docker desktop. Otherwise install Kubectl https://kubernetes.io/docs/tasks/tools/install-kubectl-windows/
Deploying our Docker image
We will be using the docker image we created in our pervious tutorial to minikube.
Lets first create a simple deployment.yml file and save it. If you are new to kubernetes and need to understand the contents of this deployment.yml file, then check out the following link for the detailed explaination: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapi-deployment
spec:
replicas: 3
selector:
matchLabels:
app: webapi
template:
metadata:
labels:
app: webapi
spec:
containers:
- name: webapi
image: docker.io/rimaz523/clean-learn-architecture-solution:latest
ports:
- containerPort: 8080
Lets now apply this deployment using kubectl
kubectl apply -f ./deployment.yml
We can now see our deployment, replicasets and pods using the commands above.
To expose our service run the following command :
kubectl expose deployment webapi-deployment --name=webapi --type=LoadBalancer --target-port=8080
To get the exposed service url run :
minikube service webapi --url
You can now access your webapi running in minikube by going to the URL http://127.0.0.1:59267/swagger/index.html
Congratulations, you now have a web api running on minikube!
To tear down your deployment and service run
$ kubectl delete deployment webapi-deployment
$ kubectl delete service webapi
To stop minikube : $ minikube stop
To delete your local minikube cluster : $ minikube delete