I have created a cluster using the google cloud platform (container engine) and deployed a pod using the following YAML file:
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: deployment-name spec: replicas: 1 template: metadata: name: pod-name labels: app: app-label spec: containers: - name: container-name image: gcr.io/project-id/image-name resources: requests: cpu: 1 ports: - name: port80 containerPort: 80 - name: port443 containerPort: 443 - name: port6001 containerPort: 6001
Then I want to create a service that enables the pod to listen on all these ports. I know that the following YAML file works to create a service that listens on one port:
apiVersion: v1 kind: Service metadata: name: service-name spec: ports: - port: 80 targetPort: 80 selector: app: app-label type: LoadBalancer
However when I want the pod to listen on multiple ports like this, it doesn't work:
apiVersion: v1 kind: Service metadata: name: service-name spec: ports: - port: 80 targetPort: 80 - port: 443 targetPort: 443 - port: 6001 targetPort: 6001 selector: app: app-label type: LoadBalancer
How can I make my pod listen to multiple ports?
Exposing multiple ports in the same service Your service exposes only a single port, but services can also support multiple ports. For example, if your pods listened on two ports—let's say 8080 for HTTP and 8443 for HTTPS—you could use a single service to forward both port 80 and 443 to the pod's ports 8080 and 8443.
From the Service type drop-down list, select Node port. Click Expose. When your Service is ready, the Service details page opens, and you can see details about your Service. Under Ports, make a note of the Node Port that Kubernetes assigned to your Service.
The default protocol for Services is TCP; you can also use any other supported protocol. As many Services need to expose more than one port, Kubernetes supports multiple port definitions on a Service object. Each port definition can have the same protocol , or a different one.
This kind of algorithm works by monitoring changes in response latency as the load adjusts based on server capacity. The Kubernetes load balancer sends connections to the first server in the pool until it is at capacity, and then sends new connections to the next available server.
You have two options:
In your case, the service becomes:
apiVersion: v1 kind: Service metadata: name: service-name spec: ports: - name: http port: 80 targetPort: 80 - name: https port: 443 targetPort: 443 - name: something port: 6001 targetPort: 6001 selector: app: app-label type: LoadBalancer
This is necessary so that endpoints can be disambiguated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With