- Implement portainer-kubernetes server with 30 tools for comprehensive K8s management - Namespace, pod, deployment, and service operations - ConfigMap and Secret management with base64 encoding - Storage operations (PV/PVC) - Ingress configuration - Node information and pod logs - Implement portainer-stacks server with 13 tools for stack management - Docker Compose and Kubernetes manifest support - Git repository integration for stack deployments - Stack lifecycle management (create, update, start, stop, delete) - Environment variable management - Stack migration between environments - Add comprehensive README documentation for both servers - Make server files executable
12 KiB
Portainer Kubernetes MCP Server
This MCP server provides comprehensive Kubernetes cluster management capabilities through Portainer's API.
Features
- Namespace Management: List, create, and delete namespaces
- Pod Operations: List, view, delete pods, and access logs
- Deployment Management: Create, scale, update, restart, and delete deployments
- Service Management: List, create, and delete services
- Ingress Configuration: List and create ingress rules
- ConfigMap & Secret Management: Full CRUD operations with base64 encoding
- Storage Management: PersistentVolume and PersistentVolumeClaim operations
- Node Information: View cluster node details
Installation
-
Ensure you have the Portainer MCP servers repository:
git clone https://github.com/yourusername/portainer-mcp.git cd portainer-mcp
-
Install dependencies:
pip install -r requirements.txt
-
Configure environment variables:
cp .env.example .env # Edit .env with your Portainer URL and API key
-
Make the server executable:
chmod +x portainer_kubernetes_server.py
Configuration
Add to your Claude Desktop configuration:
{
"portainer-kubernetes": {
"command": "python",
"args": ["/path/to/portainer-mcp/portainer_kubernetes_server.py"],
"env": {
"PORTAINER_URL": "https://your-portainer-instance.com",
"PORTAINER_API_KEY": "your-api-key"
}
}
}
Available Tools
Namespace Management
list_namespaces
List all namespaces in the cluster.
- Parameters:
environment_id
(required): Target environment ID
create_namespace
Create a new namespace.
- Parameters:
environment_id
(required): Target environment IDname
(required): Namespace name
delete_namespace
Delete a namespace.
- Parameters:
environment_id
(required): Target environment IDname
(required): Namespace name
Pod Management
list_pods
List pods in a namespace.
- Parameters:
environment_id
(required): Target environment IDnamespace
(optional): Namespace (default: "default")label_selector
(optional): Label selector (e.g., "app=nginx")
get_pod
Get detailed information about a specific pod.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): Pod name
delete_pod
Delete a pod.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): Pod namegrace_period
(optional): Grace period in seconds
get_pod_logs
Get logs from a pod.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): Pod namecontainer
(optional): Container nameprevious
(optional): Get previous container logstail_lines
(optional): Number of lines from end
Deployment Management
list_deployments
List deployments in a namespace.
- Parameters:
environment_id
(required): Target environment IDnamespace
(optional): Namespace (default: "default")
get_deployment
Get deployment details.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): Deployment name
create_deployment
Create a new deployment.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): Deployment nameimage
(required): Container imagereplicas
(optional): Number of replicas (default: 1)port
(optional): Container portenv_vars
(optional): Environment variables objectlabels
(optional): Labels object
scale_deployment
Scale a deployment.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): Deployment namereplicas
(required): Desired replica count
update_deployment_image
Update deployment container image.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): Deployment namecontainer
(required): Container nameimage
(required): New image
restart_deployment
Restart a deployment by updating annotation.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): Deployment name
delete_deployment
Delete a deployment.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): Deployment name
Service Management
list_services
List services in a namespace.
- Parameters:
environment_id
(required): Target environment IDnamespace
(optional): Namespace (default: "default")
create_service
Create a service for a deployment.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): Service nameselector
(required): Pod selector objectports
(required): Array of port mappingstype
(optional): Service type (ClusterIP, NodePort, LoadBalancer)
delete_service
Delete a service.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): Service name
Ingress Management
list_ingresses
List ingress rules in a namespace.
- Parameters:
environment_id
(required): Target environment IDnamespace
(optional): Namespace (default: "default")
create_ingress
Create an ingress rule.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): Ingress namerules
(required): Array of ingress rulestls
(optional): TLS configuration array
ConfigMap Management
list_configmaps
List ConfigMaps in a namespace.
- Parameters:
environment_id
(required): Target environment IDnamespace
(optional): Namespace (default: "default")
create_configmap
Create a ConfigMap.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): ConfigMap namedata
(required): Key-value data object
delete_configmap
Delete a ConfigMap.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): ConfigMap name
Secret Management
list_secrets
List secrets in a namespace.
- Parameters:
environment_id
(required): Target environment IDnamespace
(optional): Namespace (default: "default")
create_secret
Create a secret (automatically base64 encodes).
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): Secret namedata
(required): Key-value data objecttype
(optional): Secret type (default: "Opaque")
delete_secret
Delete a secret.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): Secret name
Storage Management
list_persistent_volumes
List PersistentVolumes in the cluster.
- Parameters:
environment_id
(required): Target environment ID
list_persistent_volume_claims
List PersistentVolumeClaims in a namespace.
- Parameters:
environment_id
(required): Target environment IDnamespace
(optional): Namespace (default: "default")
create_persistent_volume_claim
Create a PersistentVolumeClaim.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): PVC namestorage
(required): Storage size (e.g., "1Gi")access_modes
(optional): Array of access modesstorage_class
(optional): Storage class name
delete_persistent_volume_claim
Delete a PersistentVolumeClaim.
- Parameters:
environment_id
(required): Target environment IDnamespace
(required): Namespacename
(required): PVC name
Node Management
list_nodes
List cluster nodes with details.
- Parameters:
environment_id
(required): Target environment ID
Usage Examples
Create and manage a deployment
// Create a deployment
await use_mcp_tool("portainer-kubernetes", "create_deployment", {
environment_id: "2",
namespace: "production",
name: "nginx-app",
image: "nginx:latest",
replicas: 3,
port: 80,
labels: { app: "nginx", tier: "frontend" }
});
// Scale the deployment
await use_mcp_tool("portainer-kubernetes", "scale_deployment", {
environment_id: "2",
namespace: "production",
name: "nginx-app",
replicas: 5
});
// Create a service for the deployment
await use_mcp_tool("portainer-kubernetes", "create_service", {
environment_id: "2",
namespace: "production",
name: "nginx-service",
selector: { app: "nginx" },
ports: [{ port: 80, targetPort: 80 }],
type: "LoadBalancer"
});
Manage ConfigMaps and Secrets
// Create a ConfigMap
await use_mcp_tool("portainer-kubernetes", "create_configmap", {
environment_id: "2",
namespace: "production",
name: "app-config",
data: {
"app.properties": "debug=false\nport=8080",
"database.conf": "host=db.example.com"
}
});
// Create a Secret (automatically base64 encoded)
await use_mcp_tool("portainer-kubernetes", "create_secret", {
environment_id: "2",
namespace: "production",
name: "db-credentials",
data: {
username: "dbuser",
password: "secretpassword"
}
});
Storage operations
// Create a PersistentVolumeClaim
await use_mcp_tool("portainer-kubernetes", "create_persistent_volume_claim", {
environment_id: "2",
namespace: "production",
name: "data-storage",
storage: "10Gi",
access_modes: ["ReadWriteOnce"],
storage_class: "fast-ssd"
});
Pod troubleshooting
// Get pod logs
await use_mcp_tool("portainer-kubernetes", "get_pod_logs", {
environment_id: "2",
namespace: "production",
name: "nginx-app-7d9c5b5b6-abc123",
tail_lines: 100
});
// Get pod details
await use_mcp_tool("portainer-kubernetes", "get_pod", {
environment_id: "2",
namespace: "production",
name: "nginx-app-7d9c5b5b6-abc123"
});
Error Handling
The server includes comprehensive error handling:
- Network timeouts and retries
- Invalid Kubernetes resource specifications
- Authentication failures
- Resource not found errors
- Namespace conflicts
All errors are returned with descriptive messages to help diagnose issues.
Security Notes
- The server automatically handles base64 encoding for Kubernetes secrets
- API tokens are never logged or exposed
- All communications use HTTPS when configured
- Follows Kubernetes RBAC permissions
Troubleshooting
Common Issues
- Authentication failures: Ensure your API key is valid and has appropriate permissions
- Resource not found: Verify the namespace and resource names
- Permission denied: Check RBAC permissions for the service account
- Timeout errors: Increase HTTP_TIMEOUT in environment variables
Debug Mode
Enable debug logging by setting in your environment:
DEBUG=true
LOG_LEVEL=DEBUG
Requirements
- Python 3.8+
- Portainer Business Edition 2.19+ with Kubernetes endpoints
- Valid Portainer API token
- Kubernetes cluster connected to Portainer