Services
Service ใน Kubernetes ใช้สำหรับการเชื่อมต่อ Pods กับผู้ใช้งานหรือ Pods อื่น ๆ โดยทำหน้าที่เป็น Abstraction Layer ระหว่างกลุ่มของ Pods และคลัสเตอร์หรือเครือข่ายภายนอก
ประเภทของ Services
-
ClusterIP (ค่าเริ่มต้น)
- สร้าง IP ภายในคลัสเตอร์ที่สามารถเข้าถึงได้เฉพาะในคลัสเตอร์
- ใช้สำหรับการสื่อสารระหว่าง Pods ภายในคลัสเตอร์
-
NodePort
- เปิดพอร์ตเฉพาะบน Node เพื่อให้สามารถเข้าถึงได้จากภายนอกคลัสเตอร์
- ใช้ร่วมกับ ClusterIP
-
LoadBalancer
- สร้าง Load Balancer ภายนอกเพื่อแจกจ่ายทราฟฟิกไปยัง Pods
- เหมาะสำหรับการเปิดให้บริการแก่ผู้ใช้งานภายนอกคลัสเตอร์
-
ExternalName
- ใช้สำหรับการแมปชื่อ DNS ภายนอกโดยตรง
- ไม่ได้สร้าง IP ภายในคลัสเตอร์
การสร้าง Service
ตัวอย่างไฟล์ YAML สำหรับ ClusterIP
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP
การสร้าง Service ด้วย NodePort
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
nodePort: 30007
type: NodePort
การสร้าง Service ด้วย LoadBalancer
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
การสร้าง Service ด้วย ExternalName
apiVersion: v1
kind: Service
metadata:
name: external-service
spec:
type: ExternalName
externalName: example.com
การจัดการ Service
การสร้าง Service
ใช้คำสั่ง:
kubectl apply -f my-service.yaml
การดู Service ทั้งหมด
kubectl get services
การดูรายละเอียดของ Service
kubectl describe service <service-name>
การลบ Service
kubectl delete service <service-name>
การทำงานของ Services
-
Selector และ Label Matching
- Service ใช้
selectorเพื่อตรวจสอบ Pods ที่ต้องการเชื่อมต่อ โดยอ้างอิงจาก Labels
- Service ใช้
-
Endpoints
- Service จะสร้าง Endpoints เพื่อบอกว่า Pods ใดบ้างที่ตรงกับ Selector
-
การแจกจ่ายทราฟฟิก
- ทราฟฟิกจะถูกกระจายไปยัง Pods ที่ตรงกับ Selector แบบ Round Robin (ค่าเริ่มต้น)
ตัวอย่างการใช้งานจริง
-
สร้าง Deployment และ Service
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP -
ตรวจสอบ Service
kubectl get services -
เข้าถึง Service ภายในคลัสเตอร์ ใช้
curlหรือโปรแกรมอื่น ๆ:curl http://<service-name>:80
Service เป็นส่วนสำคัญที่ช่วยจัดการการเชื่อมต่อระหว่าง Pods และระบบเครือข่ายใน Kubernetes การเลือกประเภทของ Service ให้เหมาะสมกับการใช้งานช่วยเพิ่มประสิทธิภาพและความสะดวกในการบริหารจัดการแอปพลิเคชัน