Feb 2026 • CNCF Sandbox Project

OpenCost

Kubernetes Cost Monitoring แบบ Real-time

ติดตามค่าใช้จ่าย Kubernetes ด้วย OpenCost พร้อม MCP Server สำหรับ AI agents สอบถามข้อมูลด้วยภาษาธรรมดา ประหยัดค่า Cloud ได้จริง

Kubernetes
AWS
GCP
AI MCP

1. บทนำ OpenCost

OpenCost เป็นเครื่องมือ open-source สำหรับติดตามและวิเคราะห์ค่าใช้จ่ายของ Kubernetes clusters แบบ real-time พัฒนาโดย Kubecost และเป็น CNCF Sandbox Project ตั้งแต่ปี 2022

ปัญหาใหญ่ขององค์กรที่ใช้ Kubernetes คือ การไม่รู้ว่าค่าใช้จ่ายไปไหน เพราะ Kubernetes ซ่อนการจัดสรรทรัพยากรไว้หลายชั้น ทำให้ยากต่อการติดตาม cost จริงของแต่ละ workload

OpenCost แก้ปัญหานี้โดยให้ visibility เต็มรูปแบบเกี่ยวกับการใช้ทรัพยากรและค่าใช้จ่าย ตั้งแต่ระดับ namespace, pod, container ไปจนถึง node

Architecture ของ OpenCost

☸️ Kubernetes Cluster Namespace A $150/day Namespace B $89/day Namespace C $42/day Idle $28/day 📊 OpenCost Cost Allocation Engine Prometheus Metrics Grafana Dashboards AI Agent MCP Server REST API Cost Queries Cloud Provider AWS/GCP/Azure

2. ทำไมต้อง OpenCost

Open Source

ฟรีทั้งหมด ไม่มีค่าใช้จ่าย และเป็น CNCF Sandbox Project ที่มี community แข็งแกร่ง

Real-time Monitoring

ติดตามค่าใช้จ่ายแบบ real-time ทุกวินาที ไม่ต้องรอ billing report จาก cloud provider

AI-Ready (MCP Server)

มี built-in MCP server สำหรับ AI agents สอบถามข้อมูลด้วย natural language (ใหม่ปี 2025)

Multi-Cloud Support

รองรับ AWS, GCP, Azure และ on-premise clusters ในเครื่องมือเดียว

📊 เปรียบเทียบ OpenCost vs ทางเลือกอื่น

Feature OpenCost Kubecost Cloud Native
Open Source
Real-time Cost
AI/MCP Support
Multi-Cloud
ราคา ฟรี $450+/เดือน ฟรี (จำกัด)

3. MCP Server คืออะไร

Model Context Protocol (MCP) เป็น open standard ที่พัฒนาโดย Anthropic ในปลายปี 2024 สำหรับเชื่อมต่อ AI agents กับ external tools และ data sources แบบ standard

เปรียบเสมือน "Universal Translator" ที่ให้ AI agents สามารถสื่อสารกับ infrastructure, databases, และ applications ต่างๆ ได้อย่างราบรื่น

🛠️ OpenCost MCP Tools (3 ตัว)

1. get_allocation_costs

ดึงข้อมูล cost allocation แบบละเอียดพร้อม filtering และ aggregation

ตัวอย่าง: "แสดงค่าใช้จ่ายแยกตาม namespace ใน 7 วันที่ผ่านมา"

2. get_asset_costs

เข้าถึงข้อมูล assets รวมถึง nodes, disks, load balancers

ตัวอย่าง: "Load balancer ตัวไหนแพงที่สุด?"

3. get_cloud_costs

Query ข้อมูล cloud costs พร้อม filter ตาม provider, service, region

ตัวอย่าง: "เทียบค่าใช้จ่าย AWS ระหว่าง ap-southeast-1 กับ us-east-1"

🔄 MCP Architecture

🤖 AI Agent (Claude/GPT) MCP Server Model Context Protocol OpenCost Cost Allocation Engine ☸️ K8s Cluster "ค่าใช้จ่ายเดือนนี้?" "$1,234.56/month"

4. คุณสมบัติหลัก

Cost Allocation

  • แบ่ง cost ตาม namespace, pod, container
  • ติดตาม CPU, Memory, GPU usage
  • รองรับ allocation ตาม labels
  • Share idle costs allocation

Asset Monitoring

  • Node costs พร้อม instance types
  • Disk/Storage costs
  • Load balancer costs
  • Network egress costs

Multi-Cloud Pricing

  • AWS pricing API integration
  • GCP pricing integration
  • Azure pricing integration
  • Custom pricing for on-premise

Optimization Insights

  • Idle resource recommendations
  • Right-sizing suggestions
  • Reserved instance recommendations
  • Savings opportunity alerts

5. การติดตั้ง

ทางเลือกในการติดตั้ง: เลือกวิธีใดวิธีหนึ่ง ไม่ใช่ทั้งสองวิธี

ทางเลือกที่ 1 ติดตั้งด้วย Helm (แนะนำ)

Step 1: Add Helm Repository
# เพิ่ม OpenCost Helm repository
helm repo add opencost https://opencost.github.io/opencost-helm-chart
helm repo update
Step 2: สร้าง namespace และติดตั้ง
# สร้าง namespace
kubectl create namespace opencost

# ติดตั้ง OpenCost
helm install opencost opencost/opencost \
  --namespace opencost \
  --set opencost.exporter.defaultClusterId=my-cluster \
  --set opencost.prometheus.enabled=true \
  --set opencost.prometheus.internal.enabled=true
Step 3: ตรวจสอบการติดตั้ง
# ตรวจสอบ pods
kubectl get pods -n opencost

# Output ที่ควรได้:
# NAME                        READY   STATUS    RESTARTS   AGE
# opencost-xxx-xxx           2/2     Running   0          2m
# prometheus-server-xxx      1/1     Running   0          2m

ทางเลือกที่ 2 ติดตั้งด้วย kubectl (Manual)

ติดตั้งแบบ Single YAML
# สร้าง namespace
kubectl create namespace opencost

# ติดตั้ง OpenCost (รวม Prometheus)
kubectl apply -n opencost \
  -f https://raw.githubusercontent.com/opencost/opencost/develop/kubernetes/opencost.yaml

เปิดใช้งาน MCP Server (สำหรับ AI Integration)

MCP Server เปิดใช้งานโดย default ตั้งแต่ OpenCost v1.118 แต่หากต้องการปรับแต่ง:

values.yaml (Helm)
# values.yaml
opencost:
  mcp:
    enabled: true  # เปิด MCP server (default: true)
    port: 9000     # MCP server port (default: 9000)

# ติดตั้งด้วย values file
helm install opencost opencost/opencost \
  --namespace opencost \
  -f values.yaml

6. การใช้งาน

🔌 เข้าถึง OpenCost UI

Port Forward เข้า UI
# Port forward OpenCost UI
kubectl port-forward --namespace opencost \
  service/opencost 9090:9090

# เปิด browser: http://localhost:9090

📡 REST API Examples

ดึง Allocation Costs (7 วัน)
# ดึง cost แยกตาม namespace ในช่วง 7 วัน
curl -s "http://localhost:9090/api/allocation/compute?window=7d&aggregate=namespace" | jq

# Output ตัวอย่าง:
{
  "code": 200,
  "data": [
    {
      "name": "production",
      "start": "2026-02-09T00:00:00Z",
      "end": "2026-02-16T00:00:00Z",
      "cpuCores": 2.5,
      "cpuCost": 45.50,
      "memoryBytes": 8589934592,
      "memoryCost": 23.30,
      "totalCost": 68.80
    }
  ]
}
ดึง Asset Costs
# ดึง node costs
curl -s "http://localhost:9090/api/assets/compute?window=1d" | jq

# Output แสดง node, disk, load balancer costs
ดึง Cloud Costs
# ดึง AWS costs แยกตาม service
curl -s "http://localhost:9090/api/cloudCost/aggregate?window=30d&aggregate=service&provider=AWS" | jq

# แยกตาม region
curl -s "http://localhost:9090/api/cloudCost/aggregate?window=30d&aggregate=region" | jq

📊 Grafana Dashboard Integration

OpenCost มี built-in Prometheus metrics ที่สามารถใช้กับ Grafana ได้ทันที

Prometheus Query Examples
# Total monthly cost
opencost_node_total_hourly_cost * 730

# Cost by namespace
sum by (namespace) (opencost_container_memory_working_set_bytes * opencost_memory_hourly_cost)

# Idle resource cost
opencost_node_idle_hourly_cost

Import Dashboard สำเร็จรูป: Grafana Dashboards

7. AI Integration กับ MCP Server

OpenCost MCP Server ช่วยให้ AI agents (เช่น Claude, GPT) สามารถสอบถามข้อมูล cost ด้วย natural language ได้โดยตรง โดยไม่ต้องเขียน API calls

💬 ตัวอย่างคำถามที่ AI เข้าใจ

"แสดงค่าใช้จ่ายแยกตาม namespace ใน 7 วันที่ผ่านมา"

→ MCP Tool: get_allocation_costs

"Pod ไหนใช้ memory เยอะที่สุดใน production?"

→ MCP Tool: get_allocation_costs (aggregate=pod)

"เทียบค่าใช้จ่าย AWS ระหว่าง Singapore กับ Virginia"

→ MCP Tool: get_cloud_costs (filter by region)

"มี idle resources ที่สามารถปิดเพื่อประหยัดค่าใช้จ่ายได้ไหม?"

→ AI วิเคราะห์และแนะนำการประหยัด

⚙️ ตั้งค่า Claude Desktop กับ OpenCost MCP

claude_desktop_config.json
{
  "mcpServers": {
    "opencost": {
      "command": "docker",
      "args": [
        "run", "--rm", "-i",
        "-e OPEN COST_URL=http://your-opencost:9090",
        "opencost/mcp-server:latest"
      ]
    }
  }
}

8. การประยุกต์ใช้ในประเทศไทย

Startups และ SMEs

เริ่มต้นฟรี ไม่มีค่าใช้จ่าย ช่วยลด cloud cost ได้ 20-40% ด้วยการตรวจจับ idle resources

Fintech และ Banking

Compliance requirements ต้องการ cost visibility และ audit trail ครบถ้วน

E-commerce

จัดการ workload spikes ในช่วง promotion และเข้าใจ cost ที่แท้จริง

Multi-Region Deployments

เปรียบเทียบ cost ระหว่าง AWS Singapore, GCP Taiwan, Azure Southeast Asia

💰 ตัวอย่างการประหยัดค่าใช้จ่าย

20-30%
ลด cost จาก idle resources
15-25%
ประหยัดจาก right-sizing
10-20%
Reserved instance planning

9. แก้ไขปัญหาที่พบบ่อย

ปัญหา: Prometheus metrics ไม่แสดง

สาเหตุ: Prometheus ไม่ได้ scrape metrics จาก OpenCost

วิธีแก้: ตรวจสอบ ServiceMonitor
# ตรวจสอบ Prometheus targets
kubectl get servicemonitor -n opencost
kubectl logs -n opencost -l app=prometheus -c prometheus

ปัญหา: Cost แสดงเป็น $0

สาเหตุ: Cloud pricing API ไม่สามารถเข้าถึงได้ หรือ cluster ID ไม่ถูกต้อง

วิธีแก้: ตั้งค่า cluster ID และ provider
# values.yaml
opencost:
  exporter:
    defaultClusterId: "my-production-cluster"
  prometheus:
    internal:
      enabled: true
  pricing:
    provider: "aws"  # หรือ gcp, azure

ปัญหา: MCP Server ไม่ทำงาน

สาเหตุ: MCP ต้องการ OpenCost v1.118+ และต้องเปิดใช้งาน

วิธีแก้: ตรวจสอบ version และ config
# ตรวจสอบ version
kubectl logs -n opencost -l app=opencost -c opencost | grep "OpenCost"

# ตรวจสอบ MCP port
kubectl get svc -n opencost opencost -o jsonpath='{.spec.ports[?(@.name=="mcp")].port}'

📋 สรุป

สิ่งที่เรียนรู้:

  • OpenCost คืออะไรและทำไมต้องใช้
  • MCP Server สำหรับ AI integration
  • การติดตั้งด้วย Helm และ kubectl
  • การใช้ REST API และ Grafana
  • การประยุกต์ใช้ในประเทศไทย

ขั้นตอนถัดไป:

  • Deploy OpenCost ใน cluster ของคุณ
  • ตั้งค่า alerts สำหรับ budget thresholds
  • ทดลอง MCP Server กับ AI agent
  • สร้าง Grafana dashboard ที่ปรับแต่งเอง