本文介绍了如何通过kubernetes集群(minikube)路由测试流量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个Pod的minikube集群(带有ubuntu容器).我需要做的是通过此minikube集群将测试流量从一个端口路由到另一个端口.该流量应通过图片中的这两个Pod发送.我是Kubernetes知识的初学者,所以我真的不知道该怎么做以及该怎么走...请帮助我或给我一些提示.

I have a minikube cluster with two pods (with ubuntu containers). What I need to do is route test traffic from one port to another through this minikube cluster. This traffic should be sent through these two pods like in the picture. I am a beginner in this Kubernetes stuff so I really don't know how to do this and which way to go... Please, help me or give me some hints.

我正在使用ubuntu服务器版本. 18.04.

I am working on ubuntu server ver. 18.04.

在此处输入图片描述

推荐答案

我同意@Harsh Manvar提供的答案,并且我也想对此主题进行一些扩展.

I agree with an answer provided by @Harsh Manvar and I would also like to expand a little bit on this topic.

已经有一个类似设置的答案.我鼓励您检查一下:

There already is an answer with a similar setup. I encourage you to check it out:

有不同的驱动程序可用于运行minikube . 在处理入站流量方面,它们会有所不同..我错过了讲述设置(注释)中使用的驱动程序的部分.如果是标记中显示的Docker,则可以按照以下示例进行操作.

There are different drivers that could be used to run your minikube. They will have differences when it comes to dealing with inbound traffic. I missed the part that was telling about the driver used in the setup (comment). If it's the Docker shown in the tags, you could follow below example.

步骤:

  • 生成nginx-onenginx-two Deployments以从图像中模仿Pods
  • 创建一个服务,该服务将用于将流量从nginx-one发送到nginx-two
  • 创建一项服务,使您可以从LAN连接到nginx-one
  • 测试设置
  • Spawn nginx-one and nginx-two Deployments to imitate Pods from the image
  • Create a service that will be used to send traffic from nginx-one to nginx-two
  • Create a service that will allow you to connect to nginx-one from LAN
  • Test the setup

您可以使用以下定义来生成两个Deployments,其中每个将具有一个Pod:

You can use following definitions to spawn two Deployments where each one will have a single Pod:

  • nginx-one.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-one
spec:
  selector:
    matchLabels:
      app: nginx-one
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-one
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

  • nginx-two.yaml
    • nginx-two.yaml
    • apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nginx-two
      spec:
        selector:
          matchLabels:
            app: nginx-two
        replicas: 1
        template:
          metadata:
            labels:
              app: nginx-two
          spec:
            containers:
            - name: nginx
              image: nginx
              ports:
              - containerPort: 80
      

      创建一个服务,该服务将用于将流量从nginx-one发送到nginx-two

      您将需要使用服务来发送流量nginx-onenginx-two.这样的Service的示例如下:

      Create a service that will be used to send traffic from nginx-one to nginx-two

      You will need to use a Service to send the traffic from nginx-one to nginx-two. Example of such Service could be following:

      apiVersion: v1
      kind: Service
      metadata:
        name: nginx-two-service
      spec:
        type: ClusterIP # could be changed to NodePort
        selector:
          app: nginx-two # IMPORTANT
        ports:
        - name: http
          protocol: TCP
          port: 80
          targetPort: 80
      

      应用此定义后,您将能够使用服务名称(nginx-two-service)将流量发送到nginx-two

      After applying this definition you will be able to send the traffic to nginx-two by using the service name (nginx-two-service)

      您可以在不使用Service的情况下使用Pod的IP,但这不是推荐的方法.

      You can use the IP of the Pod without the Service but this is not a recommended way.

      创建一个服务,使您可以从LAN连接到nginx-one

      假设您要使用Docker驱动程序将minikube实例公开到LAN,则需要创建一个服务并将其公开.此类设置的示例如下:

      Create a service that will allow you to connect to nginx-one from LAN

      Assuming that you want to expose your minikube instance to LAN with Docker driver you will need to create a service and expose it. Example of such setup could be the following:

      apiVersion: v1
      kind: Service
      metadata:
        name: nginx-one-service
      spec:
        type: ClusterIP # could be changed to NodePort
        selector:
          app: nginx-one # IMPORTANT
        ports:
        - name: http
          protocol: TCP
          port: 80
          targetPort: 80
      

      您还需要运行:

      • $ kubectl port-forward --address 0.0.0.0 service/nginx-one-service 8000:80

      上面的命令(在您的minikube主机上运行!)将使您的nginx-one-service可以在LAN上使用.它将运行该命令的计算机上的端口8000映射到该服务的端口80.您可以通过在LAN上的另一台计算机上执行来检查它:

      Above command (ran on your minikube host!) will expose your nginx-one-service to be available on LAN. It will map port 8000 on the machine that ran this command to the port 80 of this service. You can check it by executing from another machine at LAN:

      • curl IP_ADDRESS_OF_MINIKUBE_HOST:8000

      您将需要root访问权限,以使入站流量在小于1024的端口上输入.

      You will need root access to have your inbound traffic enter on ports lesser than 1024.

      测试设置

      您将需要检查对象之间是否存在通信,如下面的连接图"所示.

      Test the setup

      You will need to check if there is a communication between the objects as shown in below "connection diagram".

      PC-> nginx-one-> nginx-two-> example.com

      PC -> nginx-one -> nginx-two -> example.com

      测试方法如下:

      PC-> nginx-one:

      • 在局域网中的计算机上运行:
        • curl MINIKUBE_IP_ADDRESS:8000
        • Run on a machine in your LAN:
          • curl MINIKUBE_IP_ADDRESS:8000

          nginx-one-> nginx-two:

          • 执行进入nginx-one Pod并运行命令:
            • $ kubectl exec -it NGINX_POD_ONE_NAME -- /bin/bash
            • $ curl nginx-two-service
            • Exec into your nginx-one Pod and run command:
              • $ kubectl exec -it NGINX_POD_ONE_NAME -- /bin/bash
              • $ curl nginx-two-service

              nginx-two-> example.com:

              • 执行进入nginx-two Pod并运行命令:
                • $ kubectl exec -it NGINX_POD_TWO_NAME -- /bin/bash
                • $ curl example.com
06-19 10:54