0Pricing
Docker & Kubernetes for Developers · บทเรียน

การเปิดให้แอปพลิเคชันเข้าถึงด้วยบริการ

ค้นพบบริการ Kubernetes ประเภทต่าง ๆ (ClusterIP, NodePort, LoadBalancer) ที่ช่วยให้เข้าถึงแอปพลิเคชันได้

การเปิดให้แอปพลิเคชันเข้าถึงด้วยบริการ เป็นบทเรียน Docker & Kubernetes for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Docker & Kubernetes for Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What are K8s Services?

When you deploy applications in Kubernetes, your Pods can come and go. They get new IPs, they scale up or down, and they might even crash and restart.

This creates a challenge: how do other parts of your application, or external users, consistently find and communicate with these ever-changing Pods?

Services: Stable Access Point

Kubernetes Services solve this problem by providing a stable network abstraction over a set of Pods. Think of a Service as a single, unchanging IP address and DNS name for your application.

  • Services act as load balancers, distributing traffic to healthy Pods.
  • They ensure your application remains reachable even if individual Pods change or fail.

How Services Find Pods

Services don't directly manage Pods. Instead, they use labels and selectors to find the Pods they should route traffic to. When a Pod matches a Service's selector, it becomes an endpoint for that Service.

  • Labels: Key-value pairs attached to objects (like Pods) for identification.
  • Selectors: Rules defined in a Service to match Pod labels.

ClusterIP: Internal Only

The ClusterIP Service type is the default. It exposes the Service on an internal IP address within the cluster. This means the Service is only reachable from inside the cluster.

It's perfect for internal communication between different services in your application, like a frontend talking to a backend database.

ClusterIP Service Example

Here's a YAML definition for a ClusterIP Service. It targets Pods with the label app: my-app and exposes port 80, routing traffic to container port 8080.

Apply it with kubectl apply -f service.yaml.

apiVersion: v1
kind: Service
metadata:
  name: my-app-clusterip
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: ClusterIP

NodePort: External via Node IP

A NodePort Service opens a specific port on all worker nodes in your cluster. External traffic can reach your Service by contacting any node's IP address on that specific port (e.g., NodeIP:NodePort).

This is useful for exposing services to the outside world, especially in small setups or for development, without needing a cloud load balancer.

NodePort Service Example

This NodePort Service will expose port 80 of your application on a high port (typically 30000-32767) on every node. You can specify a nodePort or let Kubernetes assign one.

After applying, find the assigned port with kubectl get svc.

apiVersion: v1
kind: Service
metadata:
  name: my-app-nodeport
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
      nodePort: 30007 # Optional: K8s assigns if omitted
  type: NodePort

LoadBalancer: Cloud Integration

A LoadBalancer Service is primarily used when running Kubernetes on a cloud provider (like AWS, GCP, Azure). It automatically provisions an external cloud load balancer.

This provides a dedicated, stable external IP address and often integrates with cloud-specific features like SSL termination and advanced routing.

LoadBalancer Service Example

When deployed on a cloud provider, this Service will automatically create and configure an external load balancer, assigning it an external IP address.

The external IP will be visible via kubectl get svc once provisioned.

apiVersion: v1
kind: Service
metadata:
  name: my-app-loadbalancer
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer

Service Type Overview

Let's quickly compare the primary Service types:

  • ClusterIP: Internal access only, within the cluster.
  • NodePort: External access via a specific port on each node.
  • LoadBalancer: External access via a cloud provider's load balancer, stable external IP.

Choosing the right type depends on whether your application needs to be accessible internally or externally, and your deployment environment.

Service Type Quiz

Which Kubernetes Service types provide a direct mechanism for external traffic to reach applications running inside the cluster?

Recap: Exposing Apps

Great job! You've learned how Kubernetes Services abstract network access to your Pods and make your applications reachable. We explored three main types:

  • ClusterIP: For internal-only communication within the cluster.
  • NodePort: Exposes services on a static port across all nodes for external access.
  • LoadBalancer: Integrates with cloud providers to provision an external load balancer, providing a stable external IP.

Understanding these Service types is fundamental for designing accessible and robust applications in Kubernetes.

คำถามที่พบบ่อย

บทเรียน “การเปิดให้แอปพลิเคชันเข้าถึงด้วยบริการ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเปิดให้แอปพลิเคชันเข้าถึงด้วยบริการ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Docker & Kubernetes for Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเปิดให้แอปพลิเคชันเข้าถึงด้วยบริการ”

ค้นพบบริการ Kubernetes ประเภทต่าง ๆ (ClusterIP, NodePort, LoadBalancer) ที่ช่วยให้เข้าถึงแอปพลิเคชันได้ คุณปฏิบัติ Docker & Kubernetes for Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Docker & Kubernetes for Developers หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Docker & Kubernetes for Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การเปิดให้แอปพลิเคชันเข้าถึงด้วยบริการ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Docker & Kubernetes for Developers นี้ได้ไหม

ได้ บทเรียน Docker & Kubernetes for Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ทำความเข้าใจการนำไปใช้งานของ Kubernetes
  2. การเปิดให้แอปพลิเคชันเข้าถึงด้วยบริการ
  3. การปรับขนาดและการกู้คืนแอปพลิเคชันด้วยตนเอง
  4. การอัปเดตแบบทยอยและการย้อนกลับ
← กลับไปที่ Docker & Kubernetes for Developers