quick debug program running in the k8s pod

quick-debug

English | 中文

What Problem To Solve

As the k8s becomes more and more popular, most projects are deployed in k8s, and so is the development environment.

But debugging is a little more troublesome, at least you need to replace the docker image (at this time you also need to commit your code, CICD build image, automatic or manual deployment, which needs much more time).

Timely feedback debugging (similar to front-end local debugging or local development environment) is more friendly to developers and will save a lot of time.

Application Scenario

  • The development environment cannot be built locally, or the local development environment is not real enough
  • Quickly debug the program by printing log
  • Experiment with unfamiliar lib and check its running results
  • Quickly understand the operation of a program
  • Develop, debug and self-test at the same time

Principle

schematic diagram

The principle is relatively simple. The image of the development environment needs to contain the quick-debug executable file, and the actual service is started by quick-debug.

After the program is compiled locally (for example, add some debug logs), upload it to the corresponding pod through the exposed node port, restart the service, and achieve the purpose of fast debugging.

Tool Command

quick-debug --help

Usage: quick-debug [Options] exec-args [real exec args]

Examples:
  quick-debug --exec-port 60006 --exec-path /your/exec/file/path exec-args --arg1 val1 --arg2 val2

Options:
  -disable-exec-log-file
    	disable exec log to file (when disable, you can't using TailLog API)
  -exec-path string
    	exec file path (absolute path)
  -exec-port int
    	exec file server port (Cannot be duplicated with real exec service) (default 60006)

quick-debug-client --help

NAME:
   quick-debug - quick debug program running in the k8s pod

USAGE:
   quick-debug-client [global options] command [command options] [arguments...]

COMMANDS:
   upload   uploads a file
   taillog  tail log program
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --help, -h  show help (default: false)

Effect

Before Use

Code modification, git submission, CICD generation image (or manual build image), automatic or manual replacement of remote k8s container image, use kubectl or container platform to view logs.

After Use

You can directly modify the code on the local development machine, compile, run the program on the remote k8s, and view the log on the local machine.

after use

Steps For Usage

Take https://github.com/chinaran/go-httpbin as an example, you can refer to example

Note: Your local network needs to be able to access the development k8s cluster, this tool can only be used in the development environment.

0. Install quick-debug and quick-debug-client

git clone https://github.com/chinaran/quick-debug.git $GOPATH/src/github.com/chinaran/quick-debug/
cd $GOPATH/src/github.com/chinaran/quick-debug/
make install

1. Docker image contains /usr/local/bin/quick-debug

The following methods can be used:

  • After local compilation, copy to the image when building
  • Take ghcr.io/chinaran/quick-debug:0.2-alpine3.13 as the base image
  • Similar to docker build example, copy quick-debug to the image

2. Deployment update command and args

        command:
          - quick-debug
          - --exec-path=/bin/go-httpbin # your exec path in docker
          - exec-args
        args: # orginal args
          - -port
          - "80"
          - -response-delay
          - 10ms

The final execution command of go-httpbin pod: quick-debug --exec-path=/bin/go-httpbin exec-args -port 80 -response-delay 10ms

The original command before go-httpbin pod modification: /bin/go-httpbin -port 80 -response-delay 10ms

3. Deploy NodePort Service (default port 60006)

Among them, nodePort is best fixed (random ones are fine)

kubectl apply -f - <
   
apiVersion: v1
kind: Service
metadata:
  name: {your-name}-nodeport
  namespace: {your-namespace}
spec:
  ports:
  - name: quick-deubg
    nodePort: {32101}
    port: 60006
    protocol: TCP
    targetPort: 60006
  selector:
    {your-selector-key}: {your-selector-value}
  type: NodePort
EOF

4. Compile the executable program locally, use quick-debug-client to upload the program and view its running log

# Compile the executable file
CGO_ENABLED=0 GOOS=linux go build -o /tmp/go-httpbin ./cmd/go-httpbin
# upx compression
upx -1 /tmp/go-httpbin
# Upload to remote k8s pod
quick-debug-client upload --addr {your-node-ip}:{your-node-port} --file {your-program-path}
# View remote k8s pod running logs locally
quick-debug-client taillog --addr {your-node-ip}:{your-node-port}

Golang can use shell fucntion to quickly execute corresponding commands (binaries generated in other languages ​​can also refer to this script)

Owner
Similar Resources

Buildg: A tool to interactively debug Dockerfile

buildg: A tool to interactively debug Dockerfile buildg is a tool to interactively debug Dockerfile based on BuildKit. Source-level inspection Breakpo

Dec 29, 2022

A long-running Go program that watches a Youtube playlist for new videos, and downloads them using yt-dlp or other preferred tool.

ytdlwatch A long-running Go program that watches a Youtube playlist for new videos, and downloads them using yt-dlp or other preferred tool. Ideal for

Jul 25, 2022

Turn your Go program(s) into an appliance running on the Raspberry Pi 3 or 4 (or on amd64 PCs!)

Overview gokrazy packs your Go application(s) into an SD card image for the Raspberry Pi 3 or 4 which — aside from the Linux kernel and proprietary Ra

Jan 3, 2023

Translate Prometheus Alerts into Kubernetes pod readiness

prometheus-alert-readiness Translates firing Prometheus alerts into a Kubernetes readiness path. Why? By running this container in a singleton deploym

Oct 31, 2022

⎈ Multi pod and container log tailing for Kubernetes

stern Stern allows you to tail multiple pods on Kubernetes and multiple containers within the pod. Each result is color coded for quicker debugging. T

Nov 7, 2022

A Kubernetes Mutating Webhook to automatically re-point pod images to mirrors

kubernetes-mimic Kubernetes Mimic is a Mutating Webhook that will watch for pod creation and update events in a Kubernetes cluster and automatically a

Nov 22, 2022

An example of Kubernetes' Horizontal Pod Autoscaler using costume metrics.

An example of Kubernetes' Horizontal Pod Autoscaler using costume metrics.

Kubernetes Autoscaling Example In this project, I try to implement Horizontal Pod AutoscalerHPA provided by Kubernetes. The Horizontal Pod Autoscaler

Dec 1, 2022

A pod scaler golang app that can scale replicas either inside of cluster or out of the cluster

pod-scaler A simple pod scaler golang application that can scale replicas via manipulating the deployment Technologies The project has been created us

Oct 24, 2021

A docker container that can be deployed as a sidecar on any kubernetes pod to monitor PSI metrics

CgroupV2 PSI Sidecar CgroupV2 PSI Sidecar can be deployed on any kubernetes pod with access to cgroupv2 PSI metrics. About This is a docker container

Nov 23, 2021
General Pod Autoscaler(GPA) is a extension for K8s HPA, which can be used not only for serving, also for game.
General Pod Autoscaler(GPA) is a extension for K8s HPA, which can be used not only for serving, also for game.

Introduction General Pod Autoscaler(GPA) is a extension for K8s HPA, which can be used not only for serving, also for game. Features Compatible with a

Aug 19, 2022
A kubernetes plugin which enables dynamically add or remove GPU resources for a running Pod
A kubernetes plugin which enables dynamically add or remove GPU resources for a running Pod

GPU Mounter GPU Mounter is a kubernetes plugin which enables add or remove GPU resources for running Pods. This Introduction(In Chinese) is recommende

Jan 5, 2023
A very simple, silly little kubectl plugin / utility that guesses which language an application running in a kubernetes pod was written in.

A very simple, silly little kubectl plugin / utility that guesses which language an application running in a kubernetes pod was written in.

Mar 9, 2022
Display (Namespace, Pod, Container, Primary PID) from a host PID, fails if the target process is running on host

Display (Namespace, Pod, Container, Primary PID) from a host PID, fails if the target process is running on host

Oct 17, 2022
Write controller-runtime based k8s controllers that read/write to git, not k8s

Git Backed Controller The basic idea is to write a k8s controller that runs against git and not k8s apiserver. So the controller is reading and writin

Dec 10, 2021
K8s-ingress-health-bot - A K8s Ingress Health Bot is a lightweight application to check the health of the ingress endpoints for a given kubernetes namespace.

k8s-ingress-health-bot A K8s Ingress Health Bot is a lightweight application to check the health of qualified ingress endpoints for a given kubernetes

Jan 2, 2022
K8s-go-structs - All k8s API Go structs

k8s-api go types Why? Its nice to have it all in a single package. . |-- pkg |

Jul 17, 2022
Running OpenFaas Pro on Linode K8s (feat. Aiven and Pulumi)

Running OpenFaas Pro on Linode K8s (feat. Aiven and Pulumi) Alex Ellis did a great job, when he wrote a tutorial about Event-driven OpenFaaS with Mana

Apr 26, 2022
Small skeleton app running on k8s

k8s-go-app Small skeleton app running on k8s Usage NOTE Please replace ${repositry} in below and k8s/*.yml to point your private/public repositry. doc

Jul 30, 2022
This script search print debug from PHP code.

go-php-print-debug This script search print debug from PHP code. Checking "print", "print_r", "var_dump", "var_export", "echo" as print debug. Exclude

Jan 15, 2022