🛠️ToolsShed

Kubernetes YAML 생성기

Deployment, Service, ConfigMap, Namespace K8s 매니페스트를 생성합니다.

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: 512Mi

Apply with: kubectl apply -f deployment.yaml

자주 묻는 질문

코드 구현

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.