Kubernetes YAML Generator
Generate Kubernetes manifests for Deployments, Services, ConfigMaps, and Namespaces.
Deployment Configuration
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
namespace: default
labels:
app: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: nginx:latest
ports:
- containerPort: 80
env:
- name: ENV
value: "production"
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512MiApply with: kubectl apply -f deployment.yaml
Preguntas Frecuentes
Implementación de Código
import subprocess
import yaml
import tempfile
import os
def generate_deployment(name, image, replicas=2, port=80, namespace="default"):
"""Generate a Kubernetes Deployment manifest dict."""
return {
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {"name": name, "namespace": namespace},
"spec": {
"replicas": replicas,
"selector": {"matchLabels": {"app": name}},
"template": {
"metadata": {"labels": {"app": name}},
"spec": {
"containers": [{
"name": name,
"image": image,
"ports": [{"containerPort": port}]
}]
}
}
}
}
manifest = generate_deployment("my-app", "nginx:latest", replicas=3)
yaml_str = yaml.dump(manifest, default_flow_style=False)
print(yaml_str)
# Apply using kubectl
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
f.write(yaml_str)
tmp = f.name
result = subprocess.run(["kubectl", "apply", "-f", tmp], capture_output=True, text=True)
print(result.stdout)
os.unlink(tmp)Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.