Is it possible to expose 2 ports in Kubernetes pod?
I have the following scenario -
The position simulator will get its longitude and latitude data based on time from a gps tracker. So this position simulator has a connection to activemq, but to receive the raw data, is it possible to expose another port of this service to receive the live tracking data?
Solution 1:
If you want to expose for example you deployment
on two or more ports you can do it at least two ways.
- Proper configuration in
YAML
. This method is well described in Kubernetes documentation.For some Services, you need to expose more than one port. Kubernetes lets you configure multiple port definitions on a Service object. When using multiple ports for a Service, you must give all of your ports names so that these are unambiguous. For example:
Example:
apiVersion: v1
kind: Service
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
- name: <another-name>
protocol: TCP
port: XXX
targetPort: XXX
- Using
kubectl expose
.
$ kubectl exposed <resource(deployment/Replicaset/etc)> <deployment-name> --port=XXX,XYZ,ABC --target-port=XXX
$ kubectl expose deployment nginx --port=80,8080,6000 --target-port=8080
service/nginx exposed
$ kubectl describe svc nginx
Name: nginx
Namespace: default
Labels: run=nginx
Annotations: <none>
Selector: run=nginx
Type: ClusterIP
IP: 10.0.74.75
Port: port-1 80/TCP
TargetPort: 8080/TCP
Endpoints: 10.48.0.13:8080
Port: port-2 8080/TCP
TargetPort: 8080/TCP
Endpoints: 10.48.0.13:8080
Port: port-3 6000/TCP
TargetPort: 8080/TCP
Endpoints: 10.48.0.13:8080
Session Affinity: None
Events: <none>
As mentioned in Kubernetes docs
targetPort
: is the port the container accepts traffic on,
port
: is the abstracted Service
port, which can be any port other pods use to access the Service