【问题标题】:Kubernetes: expose multiple containers using service and ingress with static ipKubernetes:使用静态 IP 的服务和入口公开多个容器
【发布时间】:2019-11-26 08:25:46
【问题描述】:

我正在尝试使用 Kubernetes、一个已定义的服务和一个带有静态 ip 和 ssl 证书的入口将两个 nodejs 应用程序部署为两个独立的容器

我想使用 GCP 的 Kubernetes Engine 部署这些微服务。我添加的第二个微服务比另一个晚。 pod 中只有一个容器,一切正常。 我定义了三个 yaml 文件:deployment.yaml、service.yaml、ingress.yaml。

deployment.yaml

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: qa-chatbot-backend-deployment
spec:
  selector:
    matchLabels:
      app: service-backend1
  replicas: 1
  template:
    metadata:
      labels:
        app: service-backend1
    spec:
      containers:
        - name: serice-backend1
          image: gcr.io/project-id/serice-backend1:v1.0.1
          imagePullPolicy: Always
          command: ["npm", "start"]
          livenessProbe:
            httpGet:
              path: /
              port: 8081
              scheme: HTTP
            initialDelaySeconds: 30
            timeoutSeconds: 25
            periodSeconds: 30
            successThreshold: 1
            failureThreshold: 2
          readinessProbe:
            httpGet:
              path: /
              port: 8081
              scheme: HTTP
            initialDelaySeconds: 30
            timeoutSeconds: 25
            periodSeconds: 30
            successThreshold: 1
            failureThreshold: 2
          ports:
            - name: service1-port
              containerPort: 8081
        - name: service-backend2
          image: gcr.io/project-id/serice-backend2:v1.0.1
          imagePullPolicy: Always
          command: ["npm", "start"]
          livenessProbe:
            httpGet:
              path: /api/test
              port: 8082
              scheme: HTTP
            initialDelaySeconds: 30
            timeoutSeconds: 25
            periodSeconds: 30
            successThreshold: 1
            failureThreshold: 2
          readinessProbe:
            httpGet:
              path: /api/test
              port: 8082
              scheme: HTTP
            initialDelaySeconds: 30
            timeoutSeconds: 25
            periodSeconds: 30
            successThreshold: 1
            failureThreshold: 2
          ports:
            - name: service2-port
              containerPort: 8082

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: service-kube
spec:
  type: LoadBalancer
  ports:
    - targetPort: service1-port
      port: 80
      protocol: TCP
  selector:
    app: service-backend1

ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  labels:
    app: service-backend1
  name: ingress-kube
  annotations:
    kubernetes.io/ingress.global-static-ip-name: app-static-ip
spec:
  tls:
  - hosts:
    - custom-host.com
    secretName: custom-host-secret-name
  rules:
  - host: custom-host.com
    http:
      paths:
      - backend:
          serviceName: service-kube
          servicePort: 80

使用此配置,只有一个服务可以访问,第一个

我尝试在 service.yaml 中添加多个端口

apiVersion: v1
kind: Service
metadata:
  name: service-kube
spec:
  type: LoadBalancer
  ports:
    - targetPort: service1-port
      port: 80
      protocol: TCP
    - targetPort: service2-port
      port: 80
      protocol: TCP
  selector:
    app: service-backend1

但我收到一个错误。

The Service "service-kube" is invalid: spec.ports[1]: Duplicate value: core.ServicePort{Name:"", Protocol:"TCP", Port:80, TargetPort:intstr.IntOrString{Type:0, IntVal:0, StrVal:""}, NodePort:0}

我的目标是在域 custom-host.com 上公开两个后端;一个可在特定路径 (api/*) 上到达,另一个可到达所有可能的端点。

感谢您的帮助

【问题讨论】:

    标签: kubernetes microservices kubernetes-ingress static-ip-address kubernetes-service


    【解决方案1】:

    您不能让单个服务端口将流量发送到两个不同的目标端口。 您的服务上必须有两个不同的端口(或使用两个单独的服务)。 然后你应该在你的入口中有两个paths 路由到相应的服务端口。

    你需要做这样的事情......

    apiVersion: v1
    kind: Service
    metadata:
      name: service-kube
    spec:
      type: LoadBalancer
      ports:
        - targetPort: service1-port
          port: 81
          protocol: TCP
        - targetPort: service2-port
          port: 82
          protocol: TCP
      selector:
        app: service-backend1
    
    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      labels:
        app: service-backend1
      name: ingress-kube
      annotations:
        kubernetes.io/ingress.global-static-ip-name: app-static-ip
    spec:
      tls:
      - hosts:
        - custom-host.com
        secretName: custom-host-secret-name
      rules:
      - host: custom-host.com
        http:
          paths:
          - backend:
              serviceName: service-kube
              servicePort: 81
            path: /api
          - backend:
              serviceName: service-kube
              servicePort: 82
            path: /
    

    【讨论】:

    • 您好,感谢您的回答。那么每个文件中的 selectorlabels 呢?另一种选择可能是使用例如 nginx?
    • 它们看起来不错,就像你拥有它们一样。您只是向服务添加另一个端口。将以相同的方式选择端点。
    猜你喜欢
    • 2022-08-04
    • 1970-01-01
    • 2017-11-25
    • 2017-12-22
    • 2019-01-26
    • 2020-03-07
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    相关资源
    最近更新 更多