kubernetes - 如何在 EKS CLI 中检查微服务的网络连接

标签 kubernetes microservices devops

我在 EKS 1.22 中运行微服务。 有没有一种方法可以通过 CLI 检查集群内的微服务通信以及 pod 到 pod 通信?

最佳答案

开箱即用的一切都应该正常工作(假设您有 AWS EKS )。

我认为这篇文章- Debug Services有非常有用的提示。

让我们首先使用简单的方法 - ping 命令检查 pod 与 pod 之间的通信。我创建了两个 NGINX 部署(一个在默认命名空间中,第二个在命名空间 test 中):

kubectl create deployment nginx --image=nginx
kubectl create deployment nginx --image=nginx -n test

现在我将检查他们两个的 IP 地址:

user@shell:~$ kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP
nginx-6799fc88d8-jxpj4   1/1     Running   0          3m13s   172.17.0.2

还有在 test 命名空间中:

user@shell:~$ kubectl get pods -o wide -n test
NAME                     READY   STATUS    RESTARTS   AGE    IP
nginx-6799fc88d8-z2glk   1/1     Running   0          103s   172.17.0.4

现在我会execute into one pod并检查与第二个的连接:

user@shell:~$ kubectl exec -it nginx-6799fc88d8-jxpj4 -- sh
# apt update
Hit:1 http://security.debian.org/debian-security bullseye-security InRelease
...
All packages are up to date.
# apt-get install inetutils-ping
Reading package lists... Done
...
Setting up inetutils-ping (2:2.0-1) ...
# ping 172.17.0.4
PING 172.17.0.4 (172.17.0.4): 56 data bytes
64 bytes from 172.17.0.4: icmp_seq=0 ttl=64 time=0.058 ms
64 bytes from 172.17.0.4: icmp_seq=1 ttl=64 time=0.123 ms

好的,所以 pod 到 pod 连接正常。请记住,容器镜像是最小的,因此您可以像我在示例中所做的那样安装 ping。此外,根据您的应用程序,您可以使用不同的方法来检查连接性 - 我也可以使用 curl 命令,我将获得标准的 NGINX 主页。

现在是创建 services 的时候了(我假设这就是您所说的微服务的意思)并测试连接性。服务是一种在网络上公开 pod 的抽象机制。因此,我们可以通过获取端点列表 - 与此服务关联的 pod 的 IP 地址 - kubectl get endpoints my-service 来测试连接性,然后像前面的示例一样检查 pod 到 pod 的连接,或者我们可以只是 curl 服务 IP 地址/主机名。对于命名空间之间的主机名,它有点不同!检查以下内容:

让我们创建具有 3 个副本的部署:

kubectl create deployment nginx --image=nginx --replicas=3
kubectl create deployment nginx --image=nginx --replicas=3 -n test

对于每个部署,我们将 create service using kubectl expose :

kubectl expose deployment nginx --name=my-service --port=80
kubectl expose deployment nginx --name=my-service-test --port=80 -n test

获取服务IP地址的时间:

user@shell:~$ kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   64d
my-service   ClusterIP   10.107.224.54   <none>        80/TCP    12m

test 命名空间中:

user@shell:~$ kubectl get svc -n test
NAME              TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
my-service-test   ClusterIP   10.110.51.62   <none>        80/TCP    8s

我将执行默认命名空间中的 pod 和第二个命名空间中 my-service-testcurl IP 地址:

user@shell:~$ kubectl exec -it nginx-6799fc88d8-w5q8s -- sh
# curl 10.110.51.62
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>

好的,它正在工作......让我们尝试使用主机名:

# curl my-service-test
curl: (6) Could not resolve host: my-service-test

不工作...为什么?让我们检查 /etc/resolv.conf 文件:

# cat resolv.conf
nameserver 10.96.0.10
search test.svc.cluster.local svc.cluster.local cluster.local
options ndots:5

正在寻找hostnames only in namespace where pod is located .

因此 test 命名空间中的 pod 将具有如下内容:

# cat resolv.conf
nameserver 10.96.0.10
search test.svc.cluster.local svc.cluster.local cluster.local
options ndots:5

让我们尝试从默认命名空间中的 pod curl my-service-test.test.svc.cluster.local:

# curl my-service-test.test.svc.cluster.local
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

它正在工作。

总结:

  • 集群中 pod 之间的通信应该在所有命名空间之间正常工作(假设您在 AWS EKS 上安装了正确的 CNI plugin)
  • pods 容器在大多数情况下是 Linux 容器,所以只需使用 Linux tools检查连接(如 pingcurl)
  • 服务是一种在网络上公开 pod 的抽象机制,您可以连接到它们,例如使用 curl 命令
  • IP 地址在集群范围内,主机名在命名空间范围内——如果您想从其他命名空间连接到资源,您需要使用完全限定名称(它适用于服务、pod...)

同时查看这些文章:

关于kubernetes - 如何在 EKS CLI 中检查微服务的网络连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70195191/

相关文章:

kubernetes - 入口 Controller nginx 从 basic_auth 中排除 IP

error-handling - 无法将已处理的GraphQL错误从一个Apollo服务器API传递到另一个API

php - 使用 Docker 隔离 PHP 应用程序

azure - 使用 terraform 从 azure signalr 检索primary_connection_string

docker - skaffold dev返回错误错误400:请求中指定的用户项目无效。

c# - 确定 Kubernetes 中应用程序关闭的原因

Kubernetes:PersistentVolume 和 PersistentVolumeClaim - 共享声明

docker - 微服务架构下docker容器之间的认证,内部调用绕过JWT auth

node.js - 带有 node-express 的微服务架构

用于环境版本控制的 Docker 镜像存储库名称或标签