首页 > 解决方案 > How to run echo server inside Kubernetes (with docker container) to communicate with it form other machines in local environment?

问题描述

What I want to achieve is to run the simplest echo server using python and tornado, code here:

#!/usr/bin/env python3
import tornado.ioloop
import tornado.web
from tornado.log import enable_pretty_logging

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    port = 8080
    print ("Starting up echo server at port %d" % port)
    enable_pretty_logging()
    app = make_app()
    app.listen(int(port))
    
    tornado.ioloop.IOLoop.current().start()

I want to run it inside docker that is running inside kubernetes's Pod. I already achieved it but only partly. I'm running kubernetes cluster on physical machine, which is in my local network. To run cluster I used minikube. Here are my configuration files, which I use to run kubernetes Deployment and Service:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: testApp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: testApp
  template:
    metadata:
      labels:
        app: testApp
    spec:
      containers:
      - name: testApp-container
        image: testApp-docker:latest
        imagePullPolicy: Never
        command: ["python3"]
        args: ["-u", "echo_tornado.py"]

        ports:
        - containerPort: 5020
      restartPolicy: Always
apiVersion: v1
kind: Service
metadata:
  name: testApp-svc
  labels:
    app: testApp
spec:
  type: NodePort
  ports:
  - port: 5020
    targetPort: 5020
    nodePort: 5020
  selector:
    app: testApp

What is problem exactly ? I can sent request to my echo server by curl $NODE_IP:$NODE_PORT and I'm getting a response, but I also want to be able do curl localhost:$NODE_PORT and also (what is crucial for me) I must be able to do curl $MY_LOCAL_MACHINE_IP:$NODE_PORT from other machine inside same local network.

Is it possible to achieve it ? Should I use some kind of forwarding my local IP and port to node's ip ? Maybe I shouldn't use ServiceType : NodePort and I should use LoadBalancer ? Maybe minikube is a problem and I should use different tool ?

标签: kubernetesminikube

解决方案


Minikube是一种工具,可在您的机器(PC、笔记本电脑、服务器等)上生成单节点 Kubernetes 集群以用于开发目的。

它使用不同--drivers的方式来运行 Kubernetes(它可以部署为bare-metal、 in docker、 in virtualbox、 inkvm等)。这允许与主机和其他设备隔离。这也意味着此设置的网络部分存在差异。

关于 Kubernetes 的网络部分,有很多内容需要介绍。我鼓励您查看官方文档:


由于driver使用过且OS未知,因此可能很难确定确切的解决方案。一些可以帮助你的指针:

  • 当您使用 Linux 发行版时,您可以选择使用 driver --none。它将使用Docker,但所有内容(容器,如kubeapi-server,etcd等)都将直接在您的主机上配置。这样你就可以运行了:)$ curl $MY_LOCAL_MACHINE_IP:$NODE_PORT。我提到的所有东西都将--driver=docker放在一个Docker容器中。

旁注!

--driver=none有一些限制/问题(例如降低安全性)。您可以通过以下文档了解更多信息:

  • 作为一种解决方法/临时解决方案,您可以使用前面提到的(在您的主机上运行):

    • $ kubectl port-forward svc/testApp-svc 5020:5020 --address 0.0.0.0- 此命令会将来自端口上的机器的请求5020直接转发到Service: testApp-svcon 端口5020
  • 例如,对于其他驱动程序,Virtualbox您需要访问它的文档才能minikube在 LAN 上公开您的实例。


看到这部分问题:

(对我来说至关重要)我必须能够从同一本地网络内的其他机器上 curl $MY_LOCAL_MACHINE_IP:$NODE_PORT 。

补充一点可能是受益者,除此之外还有其他解决方案minikube可以配置您的 Kubernetes 集群。它们之间存在一些差异,您需要选择最适合您要求的一种。其中一些是:

旁注!

要为您分配IP addresses一种LoadBalancer类型,Service您可以查看metallb(使用上述选项时)。


其他资源:


推荐阅读