This is the companion code to https://robrich.org/slides/debug-kubernetes-devspaces/#/
Want to debug a microservice running in Kubernetes without running all the services locally too?
-
Debug the
backend
solution in Visual Studio. -
Startup
middletier
in VS Code:cd middletier npm install npm run start
-
Debug
frontend
solution in Visual Studio. -
Browse to http://localhost:5000/
This builds and runs all 3 containers locally inside Docker (not Kubernetes).
-
Start it up:
docker-compose build docker-compose up
-
Browse to http://localhost:5000/
-
When you're done, stop things:
cntrl-c docker-compose down
-
Build containers:
cd frontend docker build -t frontend:v0.1 . cd ../middletier docker build -t middletier:v0.1 . cd ../backend docker build -t backend:v0.1 . cd ..
-
Start everything in Kubernetes:
kubectl apply -f k8s/backend.yaml kubectl apply -f k8s/middletier.yaml kubectl apply -f k8s/frontend.yaml
Applying them in this order ensures the services exist before the pods that will use them, ensuring the DNS entries exist by the time the pod launches.
-
Get status and launch browser:
kubectl get all,ing
If ingress is not configured on your local kubernetes cluster, browse to the 30000's port for the frontend service. If ingress is configured, you may need to add hosts file entries to
frontend.docker.internal
. -
When you're done, stop things:
kubectl delete -f k8s
-
Create an Azure Kubernetes Service (AKS) and an Azure Container Registry (ACR). In AKS, turn on HTTP Application Routing.
-
Adjust k8s files:
Inside the
k8s
folder, adjust all the entries referencingACRURL
andAKSURL
to match ACR'sLogin server
and AKS'sAPI server address
respectively. -
Build containers:
cd frontend docker build -t frontend:v0.1 . cd ../middletier docker build -t middletier:v0.1 . cd ../backend docker build -t backend:v0.1 . cd ..
-
Switch context to AKS cluster:
a. Get the credentials to the cluster
b. Switch contexts
kubectl config get-contexts kubectl config use-context <your_aks_name>
-
Start everything in Kubernetes:
kubectl apply -f k8s/backend.yaml kubectl apply -f k8s/middletier.yaml kubectl apply -f k8s/frontend.yaml
Applying them in this order ensures the services exist before the pods that will use them, ensuring the DNS entries exist by the time the pod launches.
-
Get status and launch browser:
kubectl get all,ing
It may take some time for the ingress address to appear and the new DNS entries to propigate. Until then, you can proxy into the frontend service:
kubectl port-forward service/frontend 5000:80
and browse to http://localhost:5000/.Once everything is ready, launch the browser to http://frontend.AKSURL swapping in your AKSURL as we did above.
-
When you're done, stop things:
kubectl delete -f k8s
-
Install Visual Studio prerequisites.
-
Run all content in AKS (see above).
-
Open
backend
orfrontend
in Visual Studio 2019 16.7 or better. -
Set some breakpoints in interesting code such as the HomeController.
-
In the Debug menu choose Local Process with Kubernetes.
-
Click the green play button.
The first time you run, it'll prompt you to choose the Azure subscription, AKS cluster, k8s namespace, and service to debug. Chose the service that matches the project you're running.
It'll prompt you for admin access to update your hosts file and launch kubectl proxy commands. See https://docs.microsoft.com/en-us/visualstudio/containers/overview-local-process-kubernetes?#using-local-process-with-kubernetes
-
Launch the public url in AKS for the frontend service: http://frontend.AKSURL swapping in your AKSURL url.
-
It'll hit your breakpoint.
Once you stop debugging, it'll automatically detach the debugger and put the original service back into place.
-
Install VS Code prerequisites.
-
Run all content in AKS (see above) and switch kubectl contexts (see above).
-
Open
middletier
in VS Code. -
Set some breakpoints in interesting code such as routes/index.js or lib/shoot.js.
-
Configure the link to AKS:
-
Hit cntrl+shift+p
-
Choose
Local Process with Kubernetes: Configure Local Process with Kubernetes
-
Choose the
middletier
service, and forward to port 3000.
-
-
Switch to the Debug tab, and launch
debug with aks
.It'll prompt you for admin access to update your hosts file and launch kubectl proxy commands. See https://docs.microsoft.com/en-us/visualstudio/containers/overview-local-process-kubernetes?#using-local-process-with-kubernetes
-
Launch the public url in AKS for the frontend service: http://frontend.AKSURL swapping in your AKSURL url.
-
It'll hit your breakpoint.
Once you stop debugging, it'll automatically detach the debugger and put the original service back into place.
MIT, Copyright Richardson & Sons, LLC