Docker & Kubernetes for Developers · 课时

使用服务暴露应用

了解不同类型的 Kubernetes 服务(ClusterIP、NodePort、LoadBalancer),以便让应用能够被访问。

第 2 / 4 课12 个步骤

使用服务暴露应用 是 CoddyKit 上的免费 Docker & Kubernetes for Developers 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 导师学习 Docker & Kubernetes for Developers — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「使用服务暴露应用」课时是免费的吗?

是的 — 「使用服务暴露应用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Docker & Kubernetes for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Docker & Kubernetes for Developers 课程共包含 4 节课。

「使用服务暴露应用」这节课中我会学到什么?

了解不同类型的 Kubernetes 服务(ClusterIP、NodePort、LoadBalancer),以便让应用能够被访问。 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Docker & Kubernetes for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Docker & Kubernetes for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「使用服务暴露应用」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?

能。每节 Docker & Kubernetes for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 了解 Kubernetes 部署
  2. 使用服务暴露应用
  3. 扩展与自愈应用
  4. 滚动更新与回滚
← 返回 Docker & Kubernetes for Developers