2

我有一个多容器 pod 部署,它公开端口 8080,当我在 pod 本地主机上远程登录时,容器内的端口可以通过 localhost 访问,但不能通过 pod IP 访问,但是当我在 / 中的 pod IP 上远程登录时etc/hosts 我连接被拒绝。

部署.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: test
 namespace: yara
 labels:
   component: test-multi-container-pod
  spec:
 replicas: 1
 template:
   metadata:
     labels:
       app: test
          spec:
     serviceAccountName: test
     containers:
       - name: container-1
         image: "gcr.io/projectID/my-image1:v1.9.3"
         pullPolicy: "IfNotPresent"
         resources:
           limits:
             cpu: 1000m
             memory: 2Gi
            requests:
             cpu: 500m
             memory: 2Gi             
       - name: container2
         image: "gcr.io/projectID/my-image2:0.0.107"
          pullPolicy: "IfNotPresent"
          securityContext:
           runAsUser: 0
         resources:
           limits:
             cpu: 1000m
             memory: 2Gi
           requests:
             cpu: 500m
             memory: 2Gi   
       - name:  "app-container"
       ## nodejs image that exposes ports 3000 & 8080
         image: "gcr.io/projectID/node:8.9.4_1804082101"
         workingDir: "/usr/src/app"
         pullPolicy: "Always"
         command: ["tail", "-f", "/dev/null"]
         ports:
         - name: http
           containerPort: 3000
         - name: graphql
           containerPort: 8080
         resources:
           limits:
             cpu: 1500m
             memory: 2Gi
           requests:
             cpu: 1500m
             memory: 2Gi

服务.yaml

apiVersion: v1
kind: Service
metadata:
  name: test-app
  namespace: "yara"
  labels:
    component: test-multi-container-pod
spec:
  type: NodePort
  ports:
  - protocol: TCP
    name: http
    port: 3000
    targetPort: http
  - protocol: TCP
    name: graphql
    port: 8080
    targetPort: graphql
  selector:
    component: test-multi-container-pod
4

1 回答 1

1

Pod Spec 中的command选项会覆盖EntrypointDocker 容器中的选项,这就是您实际运行 tail 而不是应用程序的原因

  - name: "app-container" 
    ...
    command: ["tail", "-f", "/dev/null"]

根据文档,在 kubernetes 中使用以下规则command覆盖 docker 容器:entrypoint

  • 如果您不为容器提供命令或参数,则使用 Docker 映像中定义的默认值。
  • 如果您为容器提供命令但没有参数,则仅使用提供的命令。Docker 镜像中定义的默认 EntryPoint 和默认 Cmd 将被忽略。
  • 如果您只为容器提供 args,则 Docker 映像中定义的默认入口点将使用您提供的 args 运行。
  • 如果您提供命令和参数,则会忽略 Docker 映像中定义的默认入口点和默认 Cmd。您的命令使用您的参数运行。

Pod 中的所有容器共享同一个网络命名空间。它看起来很相似,好像 Pod 中容器的进程将在同一主机上运行,​​并且只能绑定到同一 Pod 中其他进程未占用的端口。实际上,如果您配置两个使用相同端口绑定的容器,其中一个无法启动并出现错误:“[emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)” .

如果您需要其他 Pod 和服务找到和访问该特定 pod 容器进程,您可以使用Pod Specport:中的指令来描述它。它为系统提供有关容器使用的网络连接的附加信息,但主要是信息性的。在 Pod Spec 中未指定端口不会阻止该端口被暴露。任何侦听容器内默认“0.0.0.0”地址的端口都可以通过 Pod 地址从网络访问,也可以通过 Pod 中的其他容器访问。localhost

因此,您从 localhost:8080 收到的响应可以从绑定到该端口的 pod 中的另一个容器传递。

您可以在本文中找到对 Pod 网络的一个很好的解释。

于 2018-04-20T13:25:14.303 回答