Kubernetes实战:使用Service访问集群中的应用

Kubernetes实战:使用Service访问集群中的应用

website Kubernetes website and documentation repo: website 项目地址: https://gitcode.com/gh_mirrors/webs/website

概述

在Kubernetes集群中部署应用后,如何让外部客户端访问这些应用是一个常见需求。本文将详细介绍如何通过Kubernetes Service对象暴露集群中的应用,实现外部访问能力。我们将以一个Hello World应用为例,演示从部署应用到创建Service的完整流程。

前置准备

  • 已安装Kubernetes集群环境
  • 配置好kubectl命令行工具
  • 具备基本的Kubernetes概念知识(如Pod、Deployment等)

核心目标

  • 部署一个Hello World应用的多个实例
  • 创建NodePort类型的Service
  • 通过Service访问应用实例

详细步骤

1. 部署应用

首先,我们需要创建一个包含两个副本的Deployment。以下是应用部署的YAML配置:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
  labels:
    run: load-balancer-example
spec:
  replicas: 2
  selector:
    matchLabels:
      run: load-balancer-example
  template:
    metadata:
      labels:
        run: load-balancer-example
    spec:
      containers:
      - name: hello-world
        image: gcr.io/google-samples/node-hello:1.0
        ports:
        - containerPort: 8080

执行部署命令:

kubectl apply -f hello-application.yaml

2. 验证部署

查看Deployment状态:

kubectl get deployments hello-world
kubectl describe deployments hello-world

查看ReplicaSet:

kubectl get replicasets
kubectl describe replicasets

3. 创建Service

通过以下命令创建NodePort类型的Service:

kubectl expose deployment hello-world --type=NodePort --name=example-service

查看Service详情:

kubectl describe services example-service

输出示例:

Name:                   example-service
Namespace:              default
Labels:                 run=load-balancer-example
Selector:               run=load-balancer-example
Type:                   NodePort
IP:                     10.32.0.16
Port:                   8080/TCP
TargetPort:             8080/TCP
NodePort:               31496/TCP
Endpoints:              10.200.1.4:8080,10.200.2.5:8080

注意输出的NodePort值(如31496),这是外部访问的端口。

4. 获取Pod信息

查看运行中的Pod:

kubectl get pods --selector="run=load-balancer-example" --output=wide

输出示例:

NAME                           READY   STATUS    IP           NODE
hello-world-2895499144-bsbk5   1/1     Running   10.200.1.4   worker1
hello-world-2895499144-m1pwt   1/1     Running   10.200.2.5   worker2

5. 配置网络访问

根据集群环境不同,获取节点公网IP的方式也不同:

  • 本地集群(如Minikube):kubectl cluster-info
  • 云服务商集群:使用对应平台的CLI工具查询

然后配置防火墙规则,允许TCP流量通过NodePort端口(如31496)。

6. 访问应用

使用curl测试访问:

curl http://<节点公网IP>:<NodePort端口>

成功响应示例:

Hello, world!
Version: 2.0.0
Hostname: hello-world-cdd4458f4-m47c8

替代方案:使用Service配置文件

除了kubectl expose命令,也可以通过YAML文件定义Service:

apiVersion: v1
kind: Service
metadata:
  name: example-service
spec:
  type: NodePort
  selector:
    run: load-balancer-example
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
      nodePort: 31496

清理资源

删除Service:

kubectl delete services example-service

删除Deployment及相关资源:

kubectl delete deployment hello-world

深入理解

NodePort类型的Service工作原理:

  1. 在每个节点上开放指定端口(NodePort)
  2. 将到达该端口的流量转发到Service
  3. Service再将流量负载均衡到后端Pod

这种方式的优点是简单直接,适合开发和测试环境。在生产环境中,通常会结合LoadBalancer或Ingress使用,提供更专业的流量管理能力。

总结

通过本文,我们学习了如何在Kubernetes中使用Service暴露应用的基本方法。Service不仅提供了稳定的访问端点,还实现了负载均衡能力,是Kubernetes网络模型的核心组件之一。

website Kubernetes website and documentation repo: website 项目地址: https://gitcode.com/gh_mirrors/webs/website

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郝言元

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值