kubernetes - 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 ?
解决方案
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-svc
on 端口5020
。
例如,对于其他驱动程序,
Virtualbox
您需要访问它的文档才能minikube
在 LAN 上公开您的实例。
看到这部分问题:
(对我来说至关重要)我必须能够从同一本地网络内的其他机器上 curl $MY_LOCAL_MACHINE_IP:$NODE_PORT 。
补充一点可能是受益者,除此之外还有其他解决方案minikube
可以配置您的 Kubernetes 集群。它们之间存在一些差异,您需要选择最适合您要求的一种。其中一些是:
旁注!
要为您分配
IP addresses
一种LoadBalancer
类型,Service
您可以查看metallb(使用上述选项时)。
其他资源:
推荐阅读
- java - 无法从具有 setView 的 AlertDialog 中的 EditText 获取文本
- api - 如何在 Flutter 中使用 CoinMarketCap API
- react-native - 不变违规:本机模块不能在开玩笑反应本机中为空
- intellij-idea - 如何在intellij中显示原始文件结构?
- java - 是否有可能在 Selenium 中刷新某个帧
- pandas - FinViz - 股票抓取给出错误 - AMZN 未找到 'NoneType' 对象没有属性 'find_next'
- r - r - 根据另一个 DF 的列中的值选择 DF
- python - 在 Django 中设置 PasswordResetForm 的语言?
- c# - 在 DDD 整体中跨服务/模块时,在我的处理程序中调用调解器是否正确
- python - 无法创建多个 sqlite 表