【问题标题】:Containe runs locally on port 80, but crashes on KubernetesContaine 在 80 端口本地运行,但在 Kubernetes 上崩溃
【发布时间】:2021-07-30 11:11:56
【问题描述】:

我有一个图像,它是一个在端口 80 上运行的简单 Web 服务器。当我在本地运行它时,我得到:

The app is listening at http://localhost:80

一切都很好。

但是,当我将以下内容部署到 K8s 时,它会不断崩溃。

部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  namespace: apps
  labels:
    app: my-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: myimage:dev
        imagePullPolicy: Always
        ports:
        - containerPort: 80

其中一个 pod 的日志:

node:events:371
throw er; // Unhandled 'error' event
^

Error: listen EACCES: permission denied 0.0.0.0:80
at Server.setupListenHandle [as _listen2] (node:net:1298:21)
at listenInCluster (node:net:1363:12)
at Server.listen (node:net:1450:7)
at Function.listen (/app/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/app/index.js:17:5)
at Module._compile (node:internal/modules/cjs/loader:1095:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:816:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1342:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EACCES',
errno: -13,
syscall: 'listen',
address: '0.0.0.0',
port: 80
}

为什么我的镜像能在本地机器上成功运行,而在 Kubernetes 上却失败了?

【问题讨论】:

  • 尝试以管理员权限运行该进程。端口
  • 确实如此。但是为什么图像在本地运行良好?使用sudo docker run myimage 是否会授予容器本身的管理员权限?

标签: node.js kubernetes networking


【解决方案1】:

非 root 用户(非特权)无法在低于 1024 的端口上打开侦听套接字。

您可以找到解决方案here

请记住,我们不想以 root 用户身份运行您的应用程序,但有一个问题:您的安全用户无权使用默认 HTTP 端口 (80)。您的目标是能够通过导航到易于使用的 URL(例如 http://example.com)来发布访问者可以使用的网站。

很遗憾,除非您以 root 身份登录,否则您通常必须使用像 http://example.com:3000 这样的 URL - 注意端口号。

很多人都被困在这里,但解决方案很简单。有几个选择,但这是我喜欢的一个。键入以下命令:

sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /usr/local/bin/node

你也可以看到this similar question

【讨论】:

  • 好的,我明白了。我不清楚的一件事是为什么图像在本地运行时能够成功运行(sudo docker run ...),但在 Kubernetes 运行时却无法运行。 sudo docker run 中的 SUDO 是否在容器本身中应用 root 权限?那会很奇怪。
  • 如果我理解了这个问题,仅仅打开端口是没有用的。容器有一个开放的 80 端口,这是由于 yaml 文件的原因,但是里面有一个问题,node.js 中的应用程序无法访问它。 Here 是另一个例子。
猜你喜欢
  • 2023-04-06
  • 2021-08-25
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-27
  • 2020-07-17
相关资源
最近更新 更多