快速了解Kubernetes微服务中的通信

by Adam Henson

亚当·汉森(Adam Henson)

快速了解Kubernetes微服务中的通信 (A quick look at communication in Kubernetes microservices)

“服务”概念和一个Node.js示例 (The “service” concept and a Node.js example)

Based on complexity, a layer of microservices can require a variety of communication. Kubernetes provides a rich set of features in managing services, load balancing, and networking.

基于复杂性,微服务层可能需要各种通信。 Kubernetes在管理服务,负载平衡和网络方面提供了丰富的功能。

This post is intended to provide a simple example. For an in-depth overview — see the official documentation about Services.

这篇文章旨在提供一个简单的示例。 有关深入的概述,请参阅有关服务的官方文档

服务 (Services)

A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them - sometimes called a micro-service. ~ Kubernetes Docs

甲Kubernetes Service是定义的一组逻辑的抽象Pods和通过该访问它们的策略-有时被称为微服务。 〜Kubernetes文件

As documented, we have a number of options in exposing and communicating with services. Let’s take a look at Kubernetes DNS. Details about DNS naming can be found here.

如记录所示,在服务公开和通信方面,我们有许多选择。 让我们看一下Kubernetes DNS。 有关DNS命名的详细信息,请参见此处

一个简单的例子 (A Simple Example)

Consider a set of microservices that need to communicate with each other through the HTTP protocol. As an example, let’s say we need to create a cron job to ping a health check endpoint from another pod and log the response status code.

考虑一组需要通过HTTP协议相互通信的微服务。 举例来说,假设我们需要创建一个cron作业,以从另一个Pod ping健康检查端点并记录响应状态代码。

We have a Node.js Express app.

我们有一个Node.js Express应用程序。

Fair enough, this app can receive HTTP GET requests to “/healthcheck” and respond with JSON.

公平地说,此应用程序可以接收HTTP GET请求以“ / healthcheck”并以JSON进行响应。

Okay, now let’s create a little cron job app to execute requests to this endpoint at 8:00am every day.

好的,现在让我们创建一个小型cron作业应用程序,以每天8:00 AM执行对此端点的请求。

Very good, very good… nothing out of the ordinary here. Well, wait a minute — let’s take a closer look at the line below which defines the endpoint to fetch.

很好,很好...这里没有什么与众不同的。 好吧,请稍等一下-让我们仔细看看下面的行,该行定义了要提取的端点。

const apiUrl = 'http://api:3000/healthcheck';

In utilizing Kubernetes DNS, communicating with other pods is that simple! Our service configuration for our first app above (the Express app) could be as simple as the below.

在利用Kubernetes DNS时,与其他Pod进行通信就这么简单! 我们上面第一个应用程序(Express应用程序)的服务配置可以像下面这样简单。

Our cron app could look very similar to the above. Configuring pods is out of scope of this post, but you can read about how you could do so utilizing Deployments.

我们的cron应用看起来可能与上面的非常相似。 配置pod超出了本文的范围,但是您可以阅读有关如何使用Deployments进行配置的信息

结论 (Conclusion)

Kubernetes offers a plethora of features and documentation supporting various means of communication internally and exposure to the outside world. We can get significant mileage simply from DNS.

Kubernetes提供了许多功能和文档,支持内部的各种通信方式以及与外界的接触。 我们可以简单地从DNS中获得可观的里程。

翻译自: https://www.freecodecamp.org/news/communication-in-kubernetes-microservices-bf0a2af06551/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值