บทนำ
Kubernetes Networking เป็นหัวใจของการสื่อสารระหว่าง Pod ต่างๆ ในคลัสเตอร์ การเข้าใจโมเดลเครือข่ายของ Kubernetes เป็นสิ่งที่จำเป็นสำหรับผู้ดูแลระบบและ DevOps Engineers ที่ต้องการจัดการคลัสเตอร์อย่างมีประสิทธิภาพ บทความนี้จะเจาะลึกเกี่ยวกับ CoreDNS, Service Discovery, Network Policy และการสื่อสารระหว่าง Pod ต่างๆ โดยเฉพาะสำหรับผู้ใช้บริการ Cloud VPS ของ ผู้ให้บริการโฮสติ้ง ที่ต้องการเพิ่มประสิทธิภาพการจัดการคลัสเตอร์
1. Kubernetes Networking Model
Kubernetes ใช้โมเดลเครือข่ายแบบ Flat ที่อนุญาตให้ Pod สื่อสารกับ Pod อื่นได้โดยไม่ต้องการ NAT (Network Address Translation) ตรงตามข้อกำหนดของ CNI (Container Network Interface)
หลักการสำคัญของ K8s Networking:
- Pod-to-Pod Communication: ทุก Pod สามารถสื่อสารกับ Pod อื่นโดยตรงโดยไม่ต้องผ่าน NAT
- Node-to-Pod Communication: โหนดสามารถสื่อสารกับทุก Pod ได้
- Pod-to-External: Pod สามารถสื่อสารกับบริการภายนอกผ่าน Service
- Unique IP Addresses: ทุก Pod จะได้รับที่อยู่ IP เฉพาะที่ไม่ซ้ำกันภายในคลัสเตอร์
2. CNI (Container Network Interface) Plugins
CNI Plugins เป็นโปรแกรมติดตั้งเพิ่มเติม (Add-ons) ที่รับผิดชอบการจัดการเครือข่ายของ Pod ตัวอย่างเช่น
kind: Pod
apiVersion: v1
metadata:
name: pod-demo
namespace: default
spec:
containers:
- name: app
image: nginx:latest
ports:
- containerPort: 80
name: web
CNI Plugins ยอดนิยม:
- Flannel: ง่ายใช้ เหมาะสำหรับเริ่มต้น
- Calico: มีความสามารถสูง รองรับ Network Policy ดี
- Weave: การปรับใช้อย่างง่ายและปลอดภัย
- Cilium: ใช้ eBPF เพื่อประสิทธิภาพสูง
3. CoreDNS คืออะไร
CoreDNS เป็นเซิร์ฟเวอร์ DNS ที่ใช้ในการแก้ไขชื่อเซิร์ฟเวอร์ (DNS Resolution) ภายในคลัสเตอร์ Kubernetes เป็นตัวแปลงที่สำคัญสำหรับ Service Discovery
CoreDNS Configuration:
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
forward . /etc/resolv.conf {
max_concurrent 1000
}
cache 30
loop
reload
loadbalance
}
4. Service Discovery ผ่าน DNS
Service Discovery ช่วยให้ Pod สามารถค้นหาและสื่อสารกับเซอร์วิสอื่นได้โดยใช้ชื่อเซอร์วิสแทนที่จะใช้ที่อยู่ IP โดยตรง
DNS Naming Convention ใน Kubernetes:
service-name.namespace.svc.cluster.local
ตัวอย่าง: หากมี Service ชื่อ “my-app” ในเนมสเปซ “production” Pod สามารถเชื่อมต่อได้ที่ my-app.production.svc.cluster.local:8080
5. Cluster IP และ DNS Naming Convention
ClusterIP เป็นชนิดของ Service ที่สร้างที่อยู่ IP เสมือนจริงซึ่งใช้สำหรับการสื่อสารภายในคลัสเตอร์เท่านั้น
apiVersion: v1
kind: Service
metadata:
name: my-app
namespace: production
spec:
type: ClusterIP
selector:
app: my-app
ports:
- protocol: TCP
port: 8080
targetPort: 8080
DNS Record ที่สร้างขึ้น:
A Record: my-app.production.svc.cluster.local => 10.0.1.50
SRV Record: _8080._tcp.my-app.production.svc.cluster.local
6. Network Policy สำหรับความปลอดภัย
Network Policy ช่วยให้คุณสามารถควบคุมการไหลของข้อมูลระหว่าง Pod และ Namespace ได้อย่างละเอียด
ตัวอย่าง Network Policy:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-ingress
namespace: production
spec:
podSelector: {}
policyTypes:
- Ingress
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-from-frontend
namespace: production
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 8080
7. Pod to Pod Communication
การสื่อสารระหว่าง Pod มีการแลกเปลี่ยน IP Packet ผ่านเครือข่ายที่กำหนดโดย CNI Plugin
ขั้นตอนการสื่อสาร:
- Pod A ส่ง IP Packet ไปยัง Pod B ที่ที่อยู่ 10.0.2.50
- CNI Plugin ของโหนด A ส่งแพคเก็ตไปยังโหนด B
- CNI Plugin ของโหนด B ส่งแพคเก็ตไปยัง Pod B
- Pod B ได้รับและประมวลผลแพคเก็ต
8. External Traffic Management
Kubernetes มีวิธีหลายวิธีในการเปิดเผย Pod ให้กับการไหลข้อมูลภายนอก
NodePort Service:
apiVersion: v1
kind: Service
metadata:
name: my-app-nodeport
spec:
type: NodePort
selector:
app: my-app
ports:
- port: 8080
targetPort: 8080
nodePort: 30000
LoadBalancer Service:
apiVersion: v1
kind: Service
metadata:
name: my-app-lb
spec:
type: LoadBalancer
selector:
app: my-app
ports:
- port: 80
targetPort: 8080
Ingress Resource:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-app-ingress
spec:
ingressClassName: nginx
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app
port:
number: 8080
9. Debugging Network Issues
เมื่อเกิดปัญหาเครือข่าย คุณสามารถใช้เครื่องมือต่างๆ เพื่อวินิจฉัยและแก้ไข
คำสั่งที่มีประโยชน์:
# ตรวจสอบ DNS Resolution
kubectl run -it --rm debug --image=busybox -- sh
# ภายใน container
nslookup my-app.production.svc.cluster.local
dig my-app.production.svc.cluster.local
# ตรวจสอบ Network Policy
kubectl get networkpolicies -n production
kubectl describe networkpolicy allow-from-frontend -n production
# ดูข้อมูล CoreDNS
kubectl get pods -n kube-system -l k8s-app=kube-dns
kubectl logs -n kube-system -l k8s-app=kube-dns
# ทดสอบการเชื่อมต่อ
kubectl exec -it pod-name -- curl http://my-app.production.svc.cluster.local:8080
# ดูข้อมูลเครือข่าย
kubectl describe pod pod-name
kubectl get endpoints my-app -n production
10. การรวมกับ Cloud VPS ของ ผู้ให้บริการโฮสติ้ง
หากคุณใช้ Kubernetes บน Cloud VPS ของ ผู้ให้บริการโฮสติ้ง (https://de.co.th/cloud-vps) สามารถมี benefit ดังต่อไปนี้:
- Scalability: ขยายคลัสเตอร์ได้อย่างง่ายดายตามความต้องการ
- Reliability: ระบบการจัดการคลัสเตอร์ที่เสถียรและเชื่อถือได้
- Security: สามารถใช้ Network Policy และเครื่องมือความปลอดภัยอื่นๆ
- Cost-Effective: ใช้ทรัพยากรได้อย่างมีประสิทธิภาพ
สรุป
Kubernetes Networking เป็นหัวข้อที่ซับซ้อนแต่สำคัญอย่างยิ่งสำหรับการจัดการคลัสเตอร์อย่างมีประสิทธิภาพ การเข้าใจ CoreDNS, Service Discovery, Network Policy และวิธีการจัดการการไหลข้อมูลจากภายนอกจะช่วยให้คุณสามารถสร้างระบบที่มีความเสถียรและปลอดภัยได้ สำหรับผู้ใช้บริการ Cloud VPS ของ ผู้ให้บริการโฮสติ้ง คุณมีความยืดหยุ่นในการปรับแต่งการตั้งค่าเครือข่ายตามความต้องการของคุณ ลองฝึกฝนกับตัวอย่างที่ให้ไว้ในบทความนี้เพื่อให้คุ่นเคยกับระบบ Kubernetes Networking
แหล่งข้อมูลอ้างอิง
- Kubernetes Official Documentation: https://kubernetes.io/docs/concepts/services-networking/
- CoreDNS Official: https://coredns.io/
- Kubernetes Network Policies: https://kubernetes.io/docs/concepts/services-networking/network-policies/
- ผู้ให้บริการโฮสติ้ง Cloud VPS: https://de.co.th/cloud-vps
