Kubernetes - How to expose 2 different containers in a pod?

后端 未结 2 1042
一向
一向 2021-02-04 07:31

I am trying to create a pod with 2 containers each having different images! Am not sure how to expose the two different containers to the client. Following is my yaml file for d

相关标签:
2条回答
  • 2021-02-04 07:47

    You have multiple Options:

    1. Create multiple services exposing one port each, on the same deployment.

    2. Create single service exposing multiple ports:

      ---
      kind: Service
      apiVersion: v1
      metadata:
        name: my-service
      spec:
        selector:
          app: MyApp
        ports:
        - name: http
          protocol: TCP
          port: 80
          targetPort: 9376
        - name: https
          protocol: TCP
          port: 443
          targetPort: 9377
      
    3. Using kubectl expose:

      kubectl expose service nginx --port=443 --target-port=8443 --name=nginx-https
      

    Note that if no port is specified via –port and the exposed resource has multiple ports, all will be re-used by the new service. Also if no labels are specified, the new service will re-use the labels from the resource it exposes.

    Source

    When to use multi container pods: A pod is a group of one or more containers, the shared storage for those containers, and options about how to run the containers. Pods are always co-located and co-scheduled, and run in a shared context. A pod models an application-specific “logical host” - it contains one or more application containers which are relatively tightly coupled — in a pre-container world, they would have executed on the same physical or virtual machine.

    0 讨论(0)
  • 2021-02-04 08:09

    You can actually do this in the command line:

    kubectl expose deployment xxx --port=8080,18000 --type=NodePort
    

    Set the ports just by a comma

    0 讨论(0)
提交回复
热议问题