Skip to content

Commit 983015f

Browse files
committed
pod by pid
1 parent 42da2c8 commit 983015f

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

Kubernetes/pod-by-pid/pod-by-pid.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
3+
# Script to find the Kubernetes pod associated with a process (PID)
4+
5+
# Check if a PID is provided
6+
if [ -z "$1" ]; then
7+
echo "Usage: $0 <PID>"
8+
exit 1
9+
fi
10+
11+
# Get the PID from the first argument
12+
PID=$1
13+
14+
# Verify the process exists
15+
if [[ ! -d "/proc/$PID" ]]; then
16+
echo "Error: Process with PID $PID does not exist."
17+
exit 1
18+
fi
19+
20+
# Extract the cgroup information for the process
21+
CGROUP_FILE="/proc/$PID/cgroup"
22+
if [[ ! -f $CGROUP_FILE ]]; then
23+
echo "Error: Unable to access cgroup information for PID $PID."
24+
exit 1
25+
fi
26+
27+
# Extract the container ID (last segment of the cgroup path)
28+
CONTAINER_ID=$(cat "$CGROUP_FILE" | awk -F '/' '{print $NF}' | tr -d '\n')
29+
30+
# Check if we got a valid container ID
31+
if [[ -z "$CONTAINER_ID" || ${#CONTAINER_ID} -lt 64 ]]; then
32+
echo "Error: Unable to determine container ID for PID $PID."
33+
exit 1
34+
fi
35+
36+
echo "Container ID: $CONTAINER_ID"
37+
38+
# Use kubectl to find the pod corresponding to the container ID
39+
POD_INFO=$(kubectl get pods --all-namespaces -o json | jq -r \
40+
--arg CONTAINER_ID "$CONTAINER_ID" \
41+
'.items[]
42+
| select(.status.containerStatuses != null)
43+
| select(.status.containerStatuses[].containerID? != null and (.status.containerStatuses[].containerID | endswith($CONTAINER_ID)))
44+
| "\(.metadata.namespace) \(.metadata.name)"')
45+
46+
# Output the result
47+
if [[ -z "$POD_INFO" ]]; then
48+
echo "Error: No pod found for container ID $CONTAINER_ID."
49+
exit 1
50+
else
51+
echo "Pod Information:"
52+
echo "Namespace: $(echo $POD_INFO | awk '{print $1}')"
53+
echo "Name: $(echo $POD_INFO | awk '{print $2}')"
54+
fi

Kubernetes/pod-by-pid/readme.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# `pod_by_pid_4microk8s`
2+
3+
### A Script to Find the Kubernetes Pod Associated with a Process (PID) on MicroK8s
4+
5+
`pod_by_pid.sh` is a Bash script designed specifically for environments running MicroK8s. It helps you determine which Kubernetes pod a specific process (identified by its PID) belongs to. This script extracts the container ID from the Linux process cgroup file and maps it to the corresponding pod using the `kubectl` command.
6+
7+
---
8+
9+
## ✨ Features
10+
11+
- **Works exclusively with MicroK8s**:
12+
- The script is tailored for MicroK8s, a lightweight Kubernetes distribution.
13+
- **Maps processes to pods**:
14+
- Given a Linux process ID (PID), this script efficiently locates the associated pod's name and namespace.
15+
- **Handles Edge Cases**:
16+
- Includes robust handling for processes without container IDs or when certain fields are null or incomplete.
17+
- **Error Handling**:
18+
- Clearly notifies users of invalid PIDs, missing container IDs, or unmatched pods.
19+
20+
---
21+
22+
## ⚙️ How It Works
23+
24+
1. **Provide a Process ID (PID)** as input to the script.
25+
2. The script inspects the process’s `/proc/<PID>/cgroup` file to extract the Kubernetes container ID.
26+
3. It uses the container ID to query all running Kubernetes pods via the MicroK8s `kubectl` tool.
27+
4. If a match is found, it outputs the namespace and name of the pod associated with the process.
28+
29+
This script relies on the fact that MicroK8s organizes containers via `containerd` or similar runtime, and the cgroup paths include critical container metadata.
30+
31+
---
32+
33+
## 📜 Prerequisites
34+
35+
1. **MicroK8s**:
36+
- Ensure MicroK8s is installed and running on your system.
37+
- The script assumes the `kubectl` command provided by MicroK8s is installed and available or properly aliased (e.g., `microk8s kubectl` may be aliased to `kubectl`).
38+
39+
2. **jq**:
40+
- The script requires `jq` to parse JSON output from Kubernetes API queries.
41+
- Install it using:
42+
```bash
43+
# On Debian/Ubuntu
44+
sudo apt-get install jq
45+
46+
# On RHEL/CentOS
47+
sudo yum install jq
48+
49+
# On macOS
50+
brew install jq
51+
```
52+
53+
3. **Run the Script with Sufficient Permissions**:
54+
- Run the script as a user with access to the `/proc` filesystem and Kubernetes cluster configurations in MicroK8s (requires access to `kubectl`).
55+
56+
---
57+
58+
## 🔧 Installation
59+
60+
1. Save the script to a file:
61+
```bash
62+
nano pod_by_pid.sh

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [**sp500-prometheus-exporter**](https://github.com/koss822/misc/blob/master/Kubernetes/sp500exporter) - (created July/20/2023) SP500 exporter for Prometheus
1010
- [**slack-alarm**](https://github.com/koss822/misc/blob/master/Kubernetes/slack-alarm) - (created Feb/2024) Notification of IKEA Parasoll door/window sensor to slack
1111
- [**sops**](https://github.com/koss822/misc/blob/master/Kubernetes/sops) - (created Apr/2024) SOPS Git Hooks for Kubernetes Secrets Management
12+
- [**pod-by-pid**](https://github.com/koss822/misc/blob/master/Kubernetes/pod-by-pid) - (created Dec/2024) A Script to Find the Kubernetes Pod Associated with a Process (PID) on MicroK8s
1213

1314
## Python
1415
<img src="https://raw.githubusercontent.com/koss822/misc/master/imgs/logos-svg/python.svg" alt="logo" width="100" height="100"/><br />

0 commit comments

Comments
 (0)