mTLS Batch Processing
mTLS mutual TLS Service Mesh Istio Batch Processing Certificate Envoy Sidecar Zero Trust SPIFFE Authorization Kubernetes
| Component | Role | mTLS Function | Config |
|---|---|---|---|
| Istio Control Plane | CA + Policy | ออก Certificate จัดการ Policy | PeerAuthentication |
| Envoy Sidecar | Proxy | Terminate/Originate mTLS | Auto-injected |
| Batch Job Pod | Workload | ใช้ mTLS ผ่าน Sidecar | Annotation |
| Database | Data Store | mTLS Connection | DestinationRule |
| External API | 3rd Party | ServiceEntry + mTLS | ServiceEntry |
Configuration
# === Istio mTLS Configuration for Batch Jobs ===
# PeerAuthentication - Enforce STRICT mTLS
# apiVersion: security.istio.io/v1beta1
# kind: PeerAuthentication
# metadata:
# name: default
# namespace: batch-jobs
# spec:
# mtls:
# mode: STRICT
# AuthorizationPolicy - Only batch-runner can access database
# apiVersion: security.istio.io/v1beta1
# kind: AuthorizationPolicy
# metadata:
# name: allow-batch-to-db
# namespace: database
# spec:
# action: ALLOW
# rules:
# - from:
# - source:
# principals: ["cluster.local/ns/batch-jobs/sa/batch-runner"]
# to:
# - operation:
# ports: ["5432"]
# Batch Job Pod with Sidecar Lifecycle
# apiVersion: batch/v1
# kind: Job
# metadata:
# name: etl-daily
# namespace: batch-jobs
# spec:
# template:
# metadata:
# annotations:
# proxy.istio.io/config: |
# holdApplicationUntilProxyStarts: true
# sidecar.istio.io/inject: "true"
from dataclasses import dataclass
@dataclass
class MtlsConfig:
resource: str
purpose: str
key_settings: str
apply_to: str
configs = [
MtlsConfig("PeerAuthentication",
"บังคับ mTLS STRICT Mode",
"mtls.mode: STRICT",
"Namespace หรือ Mesh-wide"),
MtlsConfig("AuthorizationPolicy",
"กำหนด Service-to-Service Access",
"source.principals + operation.ports",
"Target Namespace/Service"),
MtlsConfig("DestinationRule",
"ตั้งค่า mTLS สำหรับ Destination",
"trafficPolicy.tls.mode: ISTIO_MUTUAL",
"Database External Service"),
MtlsConfig("ServiceEntry",
"Register External Service เข้า Mesh",
"hosts endpoints ports resolution",
"External API Database"),
MtlsConfig("Sidecar Annotation",
"ควบคุม Sidecar Lifecycle สำหรับ Job",
"holdApplicationUntilProxyStarts: true",
"Batch Job Pod"),
]
print("=== mTLS Configurations ===")
for c in configs:
print(f" [{c.resource}] {c.purpose}")
print(f" Settings: {c.key_settings}")
print(f" Apply to: {c.apply_to}")
Batch Job Lifecycle
# === Sidecar Lifecycle for Batch Jobs ===
@dataclass
class LifecycleIssue:
issue: str
symptom: str
cause: str
solution: str
config: str
issues = [
LifecycleIssue("Pod ค้างหลัง Job จบ",
"Job status Complete แต่ Pod ยัง Running",
"Sidecar (Envoy) ยังทำงานอยู่ไม่จบตาม App",
"ใช้ Istio 1.12+ EXIT_ON_ZERO_ACTIVE_CONNECTIONS",
"proxy.istio.io/config: terminationDrainDuration: 5s"),
LifecycleIssue("App เริ่มก่อน Proxy พร้อม",
"Connection Refused ตอน App เริ่ม",
"App พยายามเชื่อมต่อก่อน Envoy Proxy พร้อม",
"holdApplicationUntilProxyStarts: true",
"Pod Annotation proxy.istio.io/config"),
LifecycleIssue("Certificate Expire ระหว่าง Long Job",
"TLS Handshake Error หลังรัน Job นาน",
"Certificate หมดอายุ (Default 24hr) ระหว่าง Job รัน",
"ตั้ง Certificate Lifetime ยาวขึ้น หรือ Rotation graceful",
"PILOT_CERT_DURATION env var ใน istiod"),
LifecycleIssue("External Service mTLS Fail",
"Connection Refused ไป External DB",
"External Service ไม่อยู่ใน Mesh ไม่มี mTLS",
"ใช้ ServiceEntry + DestinationRule tls.mode: DISABLE/SIMPLE",
"ServiceEntry + DestinationRule"),
LifecycleIssue("High Memory Usage",
"Sidecar ใช้ Memory มาก ใน Batch Pod",
"Default Envoy Resource ไม่เหมาะกับ Batch",
"ตั้ง Sidecar Resource Limit ให้เหมาะสม",
"sidecar.istio.io/proxyMemory: 128Mi"),
]
print("=== Lifecycle Issues ===")
for i in issues:
print(f"\n [{i.issue}]")
print(f" Symptom: {i.symptom}")
print(f" Cause: {i.cause}")
print(f" Solution: {i.solution}")
print(f" Config: {i.config}")
Certificate Management
# === Certificate Management ===
@dataclass
class CertConfig:
aspect: str
default_value: str
recommended: str
monitor: str
cert_configs = [
CertConfig("Certificate Lifetime",
"24 ชั่วโมง (Istio Default)",
"24hr สำหรับ Short Job, 72hr สำหรับ Long Job",
"istio_agent_cert_expiry_seconds Prometheus"),
CertConfig("Rotation Grace Period",
"ก่อนหมดอายุ 50%",
"ใช้ Default (ดีแล้ว)",
"istio_agent_cert_rotation_count"),
CertConfig("Root CA",
"Self-signed by Istio (istiod)",
"ใช้ Custom CA (cert-manager/Vault) สำหรับ Prod",
"Root CA Expiry Alert"),
CertConfig("SPIFFE Identity",
"spiffe://cluster.local/ns/{ns}/sa/{sa}",
"ใช้ Service Account แยกต่อ Job Type",
"AuthorizationPolicy source.principals"),
CertConfig("Multi-cluster",
"แต่ละ Cluster มี CA แยก",
"ใช้ Shared Root CA + Intermediate CA ต่อ Cluster",
"Cross-cluster mTLS Connectivity"),
]
print("=== Certificate Management ===")
for c in cert_configs:
print(f" [{c.aspect}]")
print(f" Default: {c.default_value}")
print(f" Recommended: {c.recommended}")
print(f" Monitor: {c.monitor}")
เคล็ดลับ
- STRICT: เปิด mTLS STRICT Mode ทุก Namespace ที่มี Batch Job
- Lifecycle: ตั้ง holdApplicationUntilProxyStarts สำหรับทุก Job
- AuthZ: ใช้ AuthorizationPolicy จำกัด Service-to-Service Access
- Resource: ตั้ง Sidecar Memory Limit ให้เหมาะกับ Batch Pod
- Monitor: Monitor Certificate Expiry ด้วย Prometheus Alert
การนำไปใช้งานจริงในองค์กร
สำหรับองค์กรขนาดกลางถึงใหญ่ แนะนำให้ใช้หลัก Three-Tier Architecture คือ Core Layer ที่เป็นแกนกลางของระบบ Distribution Layer ที่ทำหน้าที่กระจาย Traffic และ Access Layer ที่เชื่อมต่อกับผู้ใช้โดยตรง การแบ่ง Layer ชัดเจนช่วยให้การ Troubleshoot ง่ายขึ้นและสามารถ Scale ระบบได้ตามความต้องการ
เรื่อง Network Security ก็สำคัญไม่แพ้กัน ควรติดตั้ง Next-Generation Firewall ที่สามารถ Deep Packet Inspection ได้ ใช้ Network Segmentation แยก VLAN สำหรับแต่ละแผนก ติดตั้ง IDS/IPS เพื่อตรวจจับการโจมตี และทำ Regular Security Audit อย่างน้อยปีละ 2 ครั้ง
mTLS คืออะไร
mutual TLS ยืนยันทั้ง Client Server Certificate Envoy Sidecar อัตโนมัติ Zero Trust Man-in-the-Middle SPIFFE Identity
ใช้กับ Batch Processing อย่างไร
Istio Inject Sidecar Batch Pod mTLS Database API Sidecar Lifecycle holdApplicationUntilProxyStarts EXIT Certificate Rotation
ตั้งค่า Istio อย่างไร
PeerAuthentication STRICT AuthorizationPolicy principals DestinationRule ServiceEntry Sidecar Annotation Namespace Label
Certificate Management ทำอย่างไร
Citadel istiod CA อัตโนมัติ 24hr Lifetime Rotation SPIFFE Identity Custom CA cert-manager Vault Multi-cluster Prometheus Monitor
สรุป
mTLS Service Mesh Batch Processing Istio STRICT AuthorizationPolicy Sidecar Lifecycle Certificate SPIFFE Zero Trust Kubernetes Security
