具有两个域的K8s入口都在端口80上侦听

时间:2019-03-18 23:12:28

标签: kubernetes kubernetes-ingress

我正在尝试在一个部署中复制带有两个docker映像的Name based virtual hosting。不幸的是,由于端口冲突,我只能运行1个:

2019/03/19 20:37:52 [ERR] Error starting server: listen tcp :5678: bind: address already in use

作为同一部署的一部分,真的不可能让两个映像在同一端口上侦听吗?还是我在其他地方出错?

here改编而成的最小示例

# set up ingress
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/mandatory.yaml

# set up load balancer
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/provider/cloud-generic.yaml

# spin up two containers in one deployment, same container port
kubectl apply -f test.yaml

test.yaml

apiVersion: v1
kind: Service
metadata:
  name: echo1
spec:
  ports:
    - port: 80
      targetPort: 5678
  selector:
    app: echo1
---
apiVersion: v1
kind: Service
metadata:
  name: echo2
spec:
  ports:
    - port: 80
      targetPort: 5678
  selector:
    app: echo2
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo12
spec:
  selector:
    matchLabels:
      app: echo12
  replicas: 1
  template:
    metadata:
      labels:
        app: echo12
    spec:
      containers:
        - name: echo1
          image: hashicorp/http-echo
          args:
            - "-text=echo1"
          ports:
            - containerPort: 5678
        - name: echo2
          image: hashicorp/http-echo
          args:
            - "-text=echo2"
          ports:
            - containerPort: 5678
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: echo-ingress
spec:
  rules:
    - host: echo1.example.com
      http:
        paths:
          - backend:
              serviceName: echo1
              servicePort: 80
    - host: echo2.example.com
      http:
        paths:
          - backend:
              serviceName: echo2
              servicePort: 80

更新: 如果我添加一个单独的部署,它将起作用。是设计使然,还是有什么方法可以一次部署完成(原因:我希望能够一次重置所有已部署的域)?

1 个答案:

答案 0 :(得分:2)

问题1 :在一个部署的一个容器中创建两个不同的服务后端。这不是吊舱设计的目的。如果要公开多个服务,则应该(至少)有一个容器来支持每个服务。通过允许您定义复制和生动性选项,部署围绕着Pod。对于您的情况,您应该为其相应的服务进行一个部署(创建一个或多个Pod,这些Pod将响应一个回显请求)。

问题2 :您没有将服务正确链接到后端。该服务显然正在尝试选择标签app=echoapp=echo2。在您的部署中,您的app=echo12。因此,该服务将根本无法找到任何活动的端点。

要解决上述问题,请尝试以下操作:

apiVersion: v1
kind: Service
metadata:
  name: echo1
spec:
  ports:
    - port: 80
      targetPort: 5678
  selector:
    app: echo1
---
apiVersion: v1
kind: Service
metadata:
  name: echo2
spec:
  ports:
    - port: 80
      targetPort: 5678
  selector:
    app: echo2
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo1
spec:
  selector:
    matchLabels:
      app: echo1
  replicas: 1
  template:
    metadata:
      labels:
        app: echo1
    spec:
      containers:
        - name: echo1
          image: hashicorp/http-echo
          args:
            - "-text=echo1"
          ports:
            - containerPort: 5678
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo2
spec:
  selector:
    matchLabels:
      app: echo2
  replicas: 1
  template:
    metadata:
      labels:
        app: echo2
    spec:
      containers:
        - name: echo2
          image: hashicorp/http-echo
          args:
            - "-text=echo2"
          ports:
            - containerPort: 5678
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: echo-ingress
spec:
  rules:
    - host: echo1.example.com
      http:
        paths:
          - backend:
              serviceName: echo1
              servicePort: 80
    - host: echo2.example.com
      http:
        paths:
          - backend:
              serviceName: echo2
              servicePort: 80

我已经在自己的集群中测试了以上内容,并验证了它是否可以正常工作(当然使用了不同的入口网址)。希望这会有所帮助!