본문으로 건너뛰기
🛠️ToolsShed

Kubernetes YAML 생성기

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

배포 구성

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

적용 방법: kubectl apply -f deployment.yaml

이 도구 소개

Kubernetes YAML 생성기는 수동으로 Kubernetes 매니페스트 정의를 적절하게 형식화된 YAML 구성 파일로 변환하는 개발자 도구입니다. 컨테이너화된 애플리케이션을 배포하거나, 마이크로서비스를 관리하거나, 인프라를 코드로 구성하는 경우, 이 도구는 배포, 서비스, 설정 맵, 네임스페이스에 대해 깔끔하고 구문적으로 유효한 매니페스트를 생성하여 Kubernetes YAML을 손으로 작성하는 번거로운 작업을 제거합니다. 설정이 Kubernetes API 사양 및 모범 사례를 따르도록 보장합니다.

도구를 사용하려면 설정 양식에 리소스 세부 정보를 입력합니다. 배포 메타데이터, 컨테이너 이미지, 서비스 포트, 환경 변수 및 기타 매개변수를 지정합니다. 생성기는 자동으로 kubectl 또는 CI/CD 파이프라인을 사용하여 Kubernetes 클러스터에 직접 적용할 준비가 된 적절하게 들여쓰기되고 잘 구조화된 YAML을 생성합니다. 생성 전에 리소스 이름, 레이블, 선택자 및 기타 모든 필드를 사용자 정의할 수 있습니다.

이 도구는 Kubernetes를 사용하는 DevOps 엔지니어, 애플리케이션 개발자, 인프라 팀에게 특히 유용합니다. 포트 매핑, volumeMounts, securityContext, 리소스 제한 등 일반적인 엣지 케이스를 처리하여 시간을 절약하고 성공적인 배포를 방지할 수 있는 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.