You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28-30Lines changed: 28 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,43 +1,40 @@
1
-
###Preface
1
+
# Preface
2
2
3
-
In this article you are going to learn, how to remotely debug a go service in kubernetes. If you are a developer with an unusual power, your services have no dependencies to other services or all those dependencies are mocked in unit tests, you don't need to debug anything in kubernetes. All other developers need to setup debugging and they want to do it in the kubernetes environment as well. Let us share our exciting and painful experience with you now.
3
+
In this article we want to show you how it´s possible to remote debug a go service in a kubernetes cluster.
2. kind (Kubernetes in Docker): <https://kind.sigs.k8s.io.> (used version: v0.7.0) We decided to use kind instead of minikube, since it is a very good tool for testing kubernetes locally.
4. Visual Studio Code: <https://code.visualstudio.com/download> (used version: 1.32.3)
8
11
9
-
Our version: 19.03.8
10
-
2. Kind (Kubernetes in Docker): https://kind.sigs.k8s.io. We decided to use kind instead of minikube, since it is a very good tool for testing kubernetes locally.
4. Visual Studio Code: https://code.visualstudio.com/download
16
+
* you need a docker container with delve (<https://github.com/go-delve/delve>) as main process
17
+
* delve needs access to the path with the project data. This is done by mounting `$GOPATH/src` on the pod which is running in the kubernetes cluster
18
+
* we start the delve container on port 30123 and bind this port to localhost, so that only our local debugger can communicate with delve
19
+
* to debug an API with delve, it´s necessary to set up an ingress network. For this we use port 8090.
17
20
18
-
Our version: 1.32.3
21
+
A picture serves to illustrate the communication:
19
22
20
-
### Big Picture
21
-
22
-
First, we are going to briefly explain, how it works:
23
-
* you need a docker container with delve started as a main process in it
24
-
* delve (Go debugger) must have an access to the folder with project files. That is done by mounting $GOPATH/src into the pod running in the kubernetes environment
25
-
* we start the delve server on the port 30123 and mount this port to the localhost, so that debugger can communicate with the server through it
26
-
* in order to trigger API functions we want to debug it is necessary to establish an ingress network. We use the port 8090 for that
27
-
28
-
All-in-all it will look like this picture demonstrates:
29
-
30
23

31
24
32
-
### Creating the Kubernetes cluster
33
-
34
-
#### Start the cluster
25
+
### Creating a Kubernetes cluster
35
26
36
-
Before starting we need to adjust the cluster config file to your environment. Unfortunately, `kind`does not use the environment variables and we have to inject them into the config file with `sed`:
27
+
`kind`unfortunately doesn´t use the environment variable `GOPATH`, so we have to update this in [cluster/config.yaml#L21](config.yaml):
You can also open `cluster/config.yaml` and replace {GOPATH} with the absolute path manually:
35
+
36
+
37
+
You can also open [cluster/config.yaml#L21](config.yaml) and replace `{GOPATH}` with the absolute path manually:
41
38
42
39
extraMounts:
43
40
- hostPath: {GOPATH}/src
@@ -48,7 +45,8 @@ Assuming you already have installed kind (Kubernetes in Docker) on your local ma
48
45
49
46
The cluster has the name `local-debug-k8s` and is created with the custom configuration (parameter `--config cluster/config.yaml`). Let us take a look at `cluster/config.yaml` and explain it:
50
47
51
-
```kind: Cluster
48
+
```yml
49
+
kind: Cluster
52
50
apiVersion: kind.x-k8s.io/v1alpha4
53
51
nodes:
54
52
- role: control-plane
@@ -97,7 +95,7 @@ Activate the kube-context, so that _kubectl_ can communicate with the newly crea
In order to make both port mounts working (8090 and 30123), it is necessary to deploy the nginx controller as well.
103
101
Run the following command for it:
@@ -120,7 +118,7 @@ So, we label a worker node with _debug=true_:
120
118
121
119
Our service has only one `/hello` endpoint and writes just a few logs. The interesting part is in the Dockerfile:
122
120
123
-
```
121
+
```Dockerfile
124
122
FROM golang:1.13-alpine
125
123
126
124
ENV CGO_ENABLED=0 # compile gcc statically
@@ -212,7 +210,7 @@ Let's go through the deployment.
212
210
213
211
If you did all steps correctly, your pod should be up and running. Check it with `kubectl get pod`. You should see the output with the pod status _Running_ and two additional services _debug-k8s_ and _service-debug_:
0 commit comments