Code examples for my Interactive debugging within a Jenkins container running on Kubernetes blog post.
Several main issues:
- Some pipelines (like Golden Images) could take an hour or more between builds:
- very slow to develop and troubleshoot
- Couldn't debug within pipeline using standard methods:
- debugging individual scripts fine outside of the pipeline
- what if we need the full build environment?
- Packer and Ansible dont allow interactive debugging within build container:
- no "pause on error" functionality
- need terminal session attached
The solution was to follow these steps:
- Install a terminal multiplexer (like
screen
) within the build container, which allowed sessions you can attach to. - Use Packer's new
error-cleanup-provisioner
to pause the build. - Connect to the build container within Kubernetes:
kubectl exec --namespace jenkins -it <pod_name> -- /bin/sh
- Attach to the screen session.
- Use an interactive debugger, like the Ansible playbook debugger.
- VSCode:
F1 > Screencast Mode
- Start Jenkins pipeline:
(This takes about 3 mins to spin up a build container and stop on error) - Discuss the problems / solution, whilst Jenkins job is running.
- Open the Microsoft Kubernetes extension view in VSCode and select
jenkins
namespace ink8s-cluster
AKS cluster. - Expand
Workloads > Pods
and refresh view to see the Jenkins pod nameddebug-k8s-pipeline...
. - Right-click pod, select
Terminal
, then select thebuilder
container from the drop-down list. - Debug using commands within
/notes/debug_jenkins_container.sh
.