Using Kubernetes from GoLand
Updated and validated on November 17, 2022. You can find more tutorials on how to use containers here. You may also refer to the Docker and Kubernetes sections of our Help documentation.
- Go development with Docker Containers
- Debugging a Go application inside a Docker container
- Running Go applications using Docker Compose in GoLand
- Using Kubernetes from GoLand (this post)
This is the last part of our series on running a Go service using Docker, Docker Compose, or Kubernetes. In this part, we’ll focus exclusively on running and debugging when using Kubernetes clusters.
While we won’t cover how to install and configure a Kubernetes cluster, a variety of tools, such as kubeadm, minikube, and microk8s, can all be used to achieve this. If you are on Windows, then Docker Desktop for Windows comes with built-in Kubernetes support. If you instead plan to use a platform that is based on ARM chips, like the Raspberry Pi 4, then you can use something like k3s to get started.
Before we start, the Kubernetes plugin should also be installed, as it is not bundled with the IDE. You can install it by going to Settings/Preferences | Plugins | Marketplace and searching for Kubernetes.
Running a service in Kubernetes from the IDE
If you’ve been following this series so far, you know that all the code has been available to download and use, and this is the case for using Kubernetes as well. We can start with the kubernetes branch.
Here we can find two different files: db.yaml
and web.yaml
. These files contain all the definitions needed for us to get started with our application in a Kubernetes cluster. For convenience, they also assume that Kubernetes is running on the same machine as the IDE.
Let’s open db.yaml
:
apiVersion: v1 kind: ConfigMap metadata: name: db-config labels: dockerdev: db data: POSTGRES_DB: goland POSTGRES_USER: goland POSTGRES_PASSWORD: goland --- apiVersion: apps/v1 kind: StatefulSet metadata: labels: dockerdev: db name: db spec: serviceName: dockerdev-db selector: matchLabels: dockerdev: db template: metadata: labels: dockerdev: db spec: containers: - name: db image: postgres:12.2-alpine imagePullPolicy: IfNotPresent envFrom: - configMapRef: name: db-config ports: - containerPort: 5432 volumeMounts: - name: db-init mountPath: /docker-entrypoint-initdb.d/init.sql subPath: init.sql volumes: - name: db-init hostPath: path: /d/GoLandProjects/dockerdev --- apiVersion: v1 kind: Service metadata: labels: dockerdev: db name: dockerdev-db-exported spec: type: NodePort ports: - name: 5432-tcp port: 5432 targetPort: 5432 nodePort: 30432 selector: dockerdev: db
Note: Before launching this example, we need to make sure to change where the init.sql
file is on our host. You can control this by replacing path: /d/GoLandProjects/dockerdev
with the path that corresponds to where you previously cloned the project.
Once this is done, we can deploy the database in Kubernetes using the green arrow on the editor gutter at the top of the file.
Deploying the database will create a StatefulSet and run the database inside a pod. The Services Tool Window will appear and show the command used to create our resources and the output of that command.
Kubernetes cluster overview
In the Kubernetes cluster, we’ll see the Workloads information, such as which Pods, Deployments, Stateful Sets, Daemon Sets, Jobs, Cron Jobs, Replica Sets, and Replication Controllers are running.
We can also see the Network information about the Services and Ingress points in the cluster.
The Configuration section contains all the configuration information for our current namespace or cluster, such as information about running Namespaces, Nodes, Cluster Roles, Roles, Config Maps, and Secrets.
Finally, the Storage section will show us the Persistent Volumes, Persistent Volume Claims, and Storage Classes in our current configuration.
Run a Go application in a Kubernetes cluster from the IDE
Let’s run our Go application in the same Kubernetes cluster and see how it works.
Before we can run the application, though, we first need to build the Docker container that it’s in.
As you’ve probably guessed, after that, we can finally run our Go application inside Kubernetes using the same green arrow that we previously used with the db.yaml
file, only this time we’ll use it in the web.yaml
file.
apiVersion: apps/v1 kind: Deployment metadata: labels: dockerdev: web name: web spec: selector: matchLabels: dockerdev: web template: metadata: labels: dockerdev: web spec: containers: - name: dockerdev-web image: dockerdev-web:latest imagePullPolicy: Never env: - name: DD_DB_HOST value: "dockerdev-db-exported" ports: - containerPort: 8000 --- apiVersion: v1 kind: Service metadata: labels: dockerdev: web name: dockerdev-web-exported spec: type: NodePort ports: - name: 8000-tcp port: 8000 targetPort: 8000 nodePort: 30800 selector: dockerdev: web
Pro tip: We can use the HTTP Request file approach and run a request from the IDE to check whether the service is up and running.
Debugging a service from Kubernetes
We need to make a few changes to our web.yaml
file before we can use GoLand to debug a Kubernetes service. These changes can be seen in the kubernetes-debug branch of our repository.
We need to make some changes that are very similar to those we made to debug a regular Docker Container.
First, we need to adjust our Dockerfile, and then we have to build it using the Run | Run … | ‘build Dockerfile’ configuration.
Then, we can use Run | Debug … | Kubernetes Service, which will launch the Go Remote debug configuration.
The debugger will work just like any form of debugging we are used to.
This concludes our series on running and debugging a Go microservice using Docker, Docker Compose, and Kubernetes.
In this article, we discussed how to use the Kubernetes plugin to edit a Deployment file so it will, instead of launching a service normally, launch it so it can be debugged.
Please share your feedback with us, either in the comments section below, on our issue tracker, or by tweeting to us @GoLandIDE.