Kubernetes:使用服务和静态ip进入来暴露多个容器

时间:2019-07-17 13:22:57

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

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

我想使用GCP的Kubernetes引擎部署这些微服务。我添加的第二项微服务晚于另一项。只需将一个容器放入豆荚,一切就可以正常进行。 我定义了三个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 / *)上可访问,而另一个在所有可能的端点上均可访问。

谢谢您的帮助

1 个答案:

答案 0 :(得分: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: /