Reputation:
I have a service which is accessible on 8081. If I do via docker-compose or swarm without any specific changing on port it's work.
http://$(minikube ip):8081
but when i run my app via Kubernetes(minikube) is assign a nodePort in range of 30000-32767. Then i have to call as follow:
http://$(minikube ip):30546
which is not acceptable from my service. Is there any way to map randomly given port to my own defined port? When call second url then i am getting connection refused I also used
kubectl port forward my-service 8081
but still no success.
Upvotes: 0
Views: 451
Reputation: 31
One way you can do this in a non-cloud environment is by using the type: NodePort
with the external IPs configuration. I have this API service defined here:
apiVersion: v1
kind: Service
metadata:
labels:
service: api
name: api
spec:
externalIPs:
- <ip of your node>
type: NodePort
ports:
- name: "http"
port: 80
protocol: TCP
targetPort: 80
Now when the service is applied it will have a defined external IP:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
api NodePort 10.43.138.4 <specified IP> 80:30685/TCP 41m
When you use this service's IP address and port combination, traffic is forwarded to the service's pods based on the service's selector. And now I can access the API outside of my cluster curl <specified IP>:80
This method works well when you want control over which specific IPs can be used to access your service.
Upvotes: 0
Reputation: 13688
This answer is not specific to Minikube but applicable to any Kubernetes cluster running inside a docker container.
In order to send a request from the host machine to the Kubernetes pod running in a container, you have to map ports from host machine to all the way to the pod.
Here is how you do it:
--publish
or -p
.# Map port 8080 on host machine to 31080 inside the container
docker run -p 8080:31080 ...
# You need to specify the exposed port as the nodePort value
# Otherwise Kubernetes will generate a random nodePort for you
kubectl create service nodeport myservice --node-port=31080 --tcp=3000:80
The application inside the pod listens to port 80
which is exposed as a service at port 3000
. The traffic received at port 31080 on Kubernetes node will be directed at this service.
The query you send to 8080 on your host machine will follow this path:
Request -> Host Machine -> Docker Container -> Kubernetes Node -> Service -> Pod
↑ ↑ ↑ ↑ ↑
localhost:8080 :31080 :31080 :3000 :80
References:
Upvotes: 0
Reputation: 17679
kubectl port-forward command is incorrect. try below one
kubectl port-forward svc/my-service 8081:8081
then you should be able to access the service at http//:127.0.0.1:8081
Upvotes: 1