Pod-level traffic control for Kubernetes using labels
Project description
k8s-trafficctl
Pod-level traffic control for Kubernetes using labels โ no service mesh required.
๐ Quick Start
# Install from PyPI
pip install k8s-trafficctl
# Enable traffic to 3 pods
trafficctl apply --namespace demo --deployment web-server --count 3
# Watch pods in real-time
trafficctl watch --namespace demo --deployment web-server
# Interactive mode
trafficctl interactive --namespace demo --deployment web-server
๐ Overview
k8s-trafficctl is a lightweight CLI that enables dynamic traffic routing at the pod level by manipulating labels used by Kubernetes Service selectors.
Instead of relying on:
- Service mesh (Istio / Linkerd)
- Complex ingress rules
- Deployment-level rollouts
You can directly control:
Which pods receive traffic โ in real time
โจ Features
- ๐ฏ Enhanced Status Display - View pod IP, node, readiness, and labels in rich tables
- ๐ Watch Mode - Real-time monitoring with auto-refresh
- ๐ฑ๏ธ Interactive TUI - Menu-driven interface for easy pod selection
- ๐ Reconciliation - Maintain desired pod count with traffic labels
- ๐ก๏ธ Safety First - Only targets ready pods, no restarts required
- ๐ฆ Zero Dependencies - No service mesh or additional infrastructure needed
๐ฏ Problem
Kubernetes Services route traffic using label selectors:
selector:
role: philos-traffic-ingress
However:
- You cannot dynamically control how many pods receive traffic
- Canary rollouts require additional tooling
- Debugging individual pods requires manual intervention
- No native mechanism exists for fine-grained traffic control
๐ก Solution
k8s-trafficctl introduces:
Label-driven traffic control at pod level
It works by:
- Adding a label โ pod starts receiving traffic
- Removing a label โ pod stops receiving traffic
All routing changes are handled natively by Kubernetes.
๐ฆ Installation
From PyPI (Recommended)
pip install k8s-trafficctl
From Source
git clone https://github.com/Manikandan-t/k8s-trafficctl.git
cd k8s-trafficctl
pip install -e .
Requirements
- Python 3.8+
- Kubernetes cluster access (via kubeconfig or in-cluster config)
- Dependencies:
kubernetes,click,rich,questionary
๐งช Complete Usage Guide
Setting Up Your Service
First, ensure your Kubernetes Service uses label selectors for traffic routing:
apiVersion: v1
kind: Service
metadata:
name: philos-service
spec:
selector:
app: philos-server
role: philos-traffic-ingress # This is the traffic control label or you can change based on what you set as label
ports:
- port: 80
targetPort: 8080
Command Reference
1. Status - View Pod Information
Show labeled pods with detailed information:
trafficctl status \
--namespace bronze \
--deployment philos-server
Show all pods (not just labeled ones):
trafficctl status \
--namespace bronze \
--deployment philos-server \
--show-all
Output includes:
- Pod name
- Status (Running/Pending/Failed)
- Readiness indicator (โ/โ)
- IP address
- Node name
- Label values
2. Apply - Enable Traffic to N Pods
Enable traffic to 3 pods:
trafficctl apply \
--namespace bronze \
--deployment philos-server \
--count 3
Custom label key/value:
trafficctl apply \
--namespace bronze \
--deployment philos-server \
--count 2 \
--label-key "traffic" \
--label-value "enabled"
3. Remove - Disable Traffic from N Pods
Disable traffic from 1 pod:
trafficctl remove \
--namespace bronze \
--deployment philos-server \
--count 1
4. Reconcile - Maintain Desired State
Ensure exactly 2 pods have traffic enabled:
trafficctl reconcile \
--namespace bronze \
--deployment philos-server \
--desired 2
This will:
- Add labels if current count < desired
- Remove labels if current count > desired
- Do nothing if already at desired state
5. Watch - Real-time Monitoring
Monitor pods with auto-refresh every 5 seconds:
trafficctl watch \
--namespace bronze \
--deployment philos-server
Custom refresh interval (2 seconds):
trafficctl watch \
--namespace bronze \
--deployment philos-server \
--interval 2
Watch all pods in deployment:
trafficctl watch \
--namespace bronze \
--deployment philos-server \
--show-all
Press Ctrl+C to exit watch mode
6. Interactive - TUI Mode
Launch interactive menu for easy pod management:
trafficctl interactive \
--namespace bronze \
--deployment philos-server
Interactive features:
- View all pods or labeled pods only
- Select specific pods to label/unlabel (with checkboxes)
- Reconcile to target count
- Visual pod selection with IP/Node/Ready status
- Confirmation prompts for safety
๐ Using Custom Kubeconfig
All commands support custom kubeconfig:
trafficctl --kubeconfig /path/to/config \
status \
--namespace default \
--deployment my-app
For role-based access setup, see: NameSpace Access
๐ผ Use Cases
1. Canary Deployment (Without Service Mesh)
Gradually increase traffic to new version:
# Start with 1 pod
trafficctl reconcile --namespace prod --deployment api-v2 --desired 1
# Monitor performance
trafficctl watch --namespace prod --deployment api-v2
# Gradually increase
trafficctl reconcile --namespace prod --deployment api-v2 --desired 3
trafficctl reconcile --namespace prod --deployment api-v2 --desired 5
2. Debugging Production Pods
Isolate a problematic pod without downtime:
# View all pods
trafficctl status --namespace prod --deployment web-server --show-all
# Remove traffic from specific pod using interactive mode
trafficctl interactive --namespace prod --deployment web-server
# Select the problematic pod and unlabel it
# Debug the pod without affecting traffic
kubectl logs <pod-name> -n prod
kubectl exec -it <pod-name> -n prod -- /bin/bash
3. Load Shedding During High Traffic
Limit active pods to conserve resources:
# During traffic spike, reduce to essential pods
trafficctl reconcile --namespace prod --deployment api --desired 3
# Monitor with watch mode
trafficctl watch --namespace prod --deployment api
4. Blue-Green Deployment
Switch traffic between versions:
# Green (new version) deployment
kubectl apply -f deployment-v2.yaml
# Wait for pods to be ready
kubectl rollout status deployment/app-v2 -n prod
# Enable traffic to green deployment
trafficctl apply --namespace prod --deployment app-v2 --count 5
# Disable traffic to blue deployment
trafficctl remove --namespace prod --deployment app-v1 --count 5
๐ง Architecture
How It Works
User CLI
โ
k8s-trafficctl
โ
Kubernetes API Server
โ
Pod Labels Updated
โ
Endpoints Controller
โ
Service Endpoints Updated
โ
kube-proxy
โ
Traffic redistributed
Key Insight
This tool leverages:
Kubernetes' native label-selector and endpoint reconciliation mechanism
No sidecars. No proxies. No service mesh.
Kubernetes Integration
Interacts directly with the Kubernetes API Server using the official Python client.
Supports:
- โ
Local kubeconfig (
~/.kube/config) - โ
Custom kubeconfig (
--kubeconfig) - โ In-cluster config (when running inside Kubernetes)
๐ก๏ธ Safety Features
- Ready Pods Only - Only targets pods in Ready state
- No Restarts - Labels are updated without pod restarts
- No Deployment Changes - Deployment specs remain unchanged
- Fully Reversible - All operations can be undone
- Confirmation Prompts - Interactive mode asks before operations
- Non-Destructive - Never deletes or modifies pod configuration
โ ๏ธ Limitations
- Count-Based Control - Traffic control is pod-count based, not exact percentage
- No Metrics - No metric-based routing (CPU, latency, error rate) yet
- CLI-Driven - No continuous reconciliation loop (must be triggered manually)
- Label Dependency - Requires service to use label selectors for routing
Development Setup
# Clone repository
git clone https://github.com/Manikandan-t/k8s-trafficctl.git
cd k8s-trafficctl
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install in editable mode
pip install -e .
# Install development dependencies
pip install -r requirements.txt
Running Locally
# Run commands directly
python -m trafficctl.cli --help
# Or use the installed command
trafficctl --help
Project Structure
k8s-trafficctl/
โโโ trafficctl/
โ โโโ __init__.py
โ โโโ cli.py # CLI commands
โ โโโ k8s.py # Kubernetes API interactions
โ โโโ utils.py # Utility functions (logging, tables)
โ โโโ selector.py # Pod selection logic
โ โโโ labeling.py # Label operations
โ โโโ reconcile.py # Reconciliation logic
โ โโโ interactive.py # Interactive TUI mode
โโโ examples/
โ โโโ deployment.yaml # Sample deployment
โ โโโ quickstart.sh # Quick start script
โโโ setup.py
โโโ requirements.txt
โโโ README.md
Credits
Inspired by the need for lightweight, mesh-free traffic control in Kubernetes environments.
๐ Related Projects
- initbox - Kubernetes installation and configuration guides
- Namespace Access Guide - Role-based kubeconfig generation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file k8s_trafficctl-0.1.0.tar.gz.
File metadata
- Download URL: k8s_trafficctl-0.1.0.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02d59c35231cbbc28ff39904d9a1250c74ae0cf7812f095e27b2242fea571c9d
|
|
| MD5 |
451f6230c9a0d8e240dd206f0f80736b
|
|
| BLAKE2b-256 |
4474bbba69f8aee5c7ce6f92fbe0a262679472aad3fea8e225c48ce4389d60e1
|
File details
Details for the file k8s_trafficctl-0.1.0-py3-none-any.whl.
File metadata
- Download URL: k8s_trafficctl-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
120ba7de0671a8a2019ef71f8d370c348ee243b249525dc4f2ed3ccc8d2ed1f9
|
|
| MD5 |
b1285caf843b611cf8d0de7b4ca7df59
|
|
| BLAKE2b-256 |
15accadf4787db186cf48ce1072ab766b43e75b73352fad6d9fae4b71a2541ae
|