To expose k8s application you can use kubectl expose
to create service of type NodePort:
kubectl expose pod <pod_name> --type NodePort --port 8080
or
kubectl expose deployment <deployment_name> --type NodePort --port 8080
then when you list your services you will see:
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
<service_name> NodePort 10.99.147.24 <none> 8080:31208/TCP 3s
Notice two ports under PORT column: 8080:31208/TCP
. First is in-cluster port and the second is a node port. So now you can access your service with nodePort using: <node-IP>:31208
from outside of a cluster.
There is another option which only applies of you are running in cloud environment and LoadBalancers are supported (so if you are either using k8s as a service solution or running self hosted k8s in cloud with cloud provider configured).
You can create a service of type LoadBalancer like following:
kubectl expose pod <pod_name> --type LoadBalacer --port 8080
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
<service_name> LoadBalancer 10.107.151.19 x.x.x.x 8080:31111/TCP 2s
and now use EXTERNAL-IP address to connect to your service: x.x.x.x:8080