-
序言
-
云原生(Cloud Native)的定义
-
云原生的设计哲学
-
Play with Kubernetes
-
快速部署一个云原生本地实验环境
-
使用Rancher在阿里云上部署Kubenretes集群
-
Kubernetes与云原生应用概览
-
云原生应用之路——从Kubernetes到Cloud Native
- 云原生编程语言
-
云原生的未来
- Kubernetes架构
- Kubernetes中的网络
-
资源对象与基本概念解析
- Pod状态与生命周期管理
- 集群资源管理
- 控制器
- 服务发现
- 身份与权限控制
- 存储
- 集群扩展
- 资源调度
-
用户指南
- 资源对象配置
- 命令使用
- 集群安全性管理
- 访问Kubernetes集群
- 在Kubernetes中开发部署应用
-
最佳实践概览
- 在CentOS上部署Kubernetes集群
- 生产级的Kubernetes简化管理工具kubeadm
- 服务发现与负载均衡
- 运维管理
- 存储管理
- 集群与应用监控
- 分布式跟踪
- 服务编排管理
- 持续集成与发布
- 更新与升级
-
领域应用概览
- 微服务架构
- Service Mesh 服务网格
- 大数据
- Serverless架构
-
边缘计算
-
人工智能
-
开发指南概览
-
SIG和工作组
- 开发环境搭建
-
单元测试和集成测试
-
client-go示例
- Operator
-
kubebuilder
-
高级开发指南
-
社区贡献
-
Minikube
-
CNCF - 云原生计算基金会简介
-
CNCF章程
-
CNCF特别兴趣小组(SIG)说明
-
开源项目加入CNCF Sandbox的要求
-
CNCF中的项目治理
-
CNCF Ambassador
-
附录说明
-
Kubernetes中的应用故障排查
-
Kubernetes相关资讯和情报链接
-
Docker最佳实践
-
使用技巧
-
问题记录
- Kubernetes版本更新日志
- Kubernetes及云原生年度总结及展望
- CNCF年度报告解读
-
Kubernetes认证服务提供商(KCSP)说明
-
认证Kubernetes管理员(CKA)说明
使用 service 访问群集中的应用程序
本文向您展示如何创建 Kubernetes Service 对象,外部客户端可以使用它来访问集群中运行的应用程序。该 Service 可以为具有两个运行实例的应用程序提供负载均衡。
目的
- 运行 Hello World 应用程序的两个实例。
- 创建一个暴露 node 节点端口的 Service 对象。
- 使用 Service 对象访问正在运行的应用程序。
为在两个 pod 中运行的应用程序创建 service
在集群中运行 Hello World 应用程序:
kubectl run hello-world --replicas=2 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0 --port=8080
copy
上述命令创建一个 Deployment 对象和一个相关联的 ReplicaSet 对象。该 ReplicaSet 有两个 Pod,每个 Pod 中都运行一个 Hello World 应用程序。
显示关于该 Deployment 的信息:
kubectl get deployments hello-world kubectl describe deployments hello-world
copy
显示 ReplicaSet 的信息:
kubectl get replicasets kubectl describe replicasets
copy
创建一个暴露该 Deployment 的 Service 对象:
kubectl expose deployment hello-world --type=NodePort --name=example-service
copy
显示该 Service 的信息:
kubectl describe services example-service
copy
输出类似于:
Name: example-service Namespace: default Labels: run=load-balancer-example Selector: run=load-balancer-example Type: NodePort IP: 10.32.0.16 Port: <unset> 8080/TCP NodePort: <unset> 31496/TCP Endpoints: 10.200.1.4:8080,10.200.2.5:8080 Session Affinity: None No events.
copy
记下服务的 NodePort 值。例如,在前面的输出中,NodePort 值为 31496。
列出运行 Hello World 应用程序的 Pod:
kubectl get pods --selector="run=load-balancer-example" --output=wide
copy
输出类似于:
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
copy
获取正在运行 Hello World 应用程序的 Pod 的其中一个节点的 public IP 地址。如何得到这个地址取决于您的集群设置。例如,如果您使用 Minikube,可以通过运行
kubectl cluster-info
查看节点地址。如果您是使用 Google Compute Engine 实例,可以使用gcloud compute instances list
命令查看您的公共地址节点。在您选择的节点上,在您的节点端口上例如创建允许 TCP 流量的防火墙规则,如果您的服务 NodePort 值为 31568,创建防火墙规则,允许端口 31568 上的TCP流量。
使用节点地址和节点端口访问 Hello World 应用程序:
curl http://<public-node-ip>:<node-port>
copy
其中
<public-node-ip>
是您节点的 public IP地址,而<node-port>
是您服务的 NodePort 值。对成功请求的响应是一个 hello 消息:
Hello Kubernetes!
copy
使用 Service 配置文件
作为使用 kubectl expose
的替代方法,您可以使用 service 配置文件 来创建 Service。
要删除 Service,输入以下命令:
kubectl delete services example-service
copy
删除 Deployment、ReplicaSet 和正运行在 Pod 中的 Hello World 应用程序,输入以下命令:
kubectl delete deployment hello-world
copy
了解更多关于 使用 service 连接应用程序。