5

I have executed minikube service mynginx1 and the result is:

 |-----------|----------|-------------|-----------------------------|
 | NAMESPACE |   NAME   | TARGET PORT |             URL             |
 |-----------|----------|-------------|-----------------------------| 
 | default   | mynginx1 | 8080-80     | http://192.168.85.153:31706 |
 |-----------|----------|-------------|-----------------------------|

What are the kubectl equivalent commands so that I can retrieve the URL if I am not using minikube?

somebadhat
  • 744
  • 1
  • 5
  • 17
Godfrey Tan
  • 79
  • 1
  • 6
  • 1
    I also created a service using kubectl on a master and two slave nodes and created the same mynginx1 service. However, I can't connect to the "Welcome to Nginx" using the mater node's IP address and NodePort 30980. On Minikube I can. I don't know what other steps to take. – Godfrey Tan May 06 '20 at 06:10

4 Answers4

3

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

Matt
  • 7,419
  • 1
  • 11
  • 22
  • Hi, I used the "kubectl expose deployment..." and got an 404 error. At least its an improvement but I still don't get the "Welcome to Nginx" page. I used the master node's IP address and the port is "31216". That is http://:31216. – Godfrey Tan May 08 '20 at 05:45
  • 1
    If you are receiving 404 from nginx then it's no longer kubernetes problem but nginx problem. Please check your nginx config. If you are having any nginx issues you can post a new question on stackoverflow akings for this very specific problem. – Matt May 08 '20 at 10:30
  • 1
    What is the value of `` in your example where `:31208` is being used for a NodePort connection? The Cluster-IP doesn't work, and I think the IP would be something like 198.168.64.2 but I'm not sure where to get that value from – Shiraz Oct 05 '22 at 14:21
3

Port forward worked for me

kubectl port-forward deployment/es-manual 9200:9200
frognonus
  • 113
  • 1
  • 7
0

I had the same issue as the OP when trying to use Docker Desktop instead of Minikube - Docker desktop now comes with a Kubernetes single-node cluster so I didn't see the need to install Minikube in order to play with Kubernetes on my dev machine.

Assuming you use kubectl get services to list your services, you can then use kubectl cluster-info to get your master node ip. OOTB, Docker Desktop sets 127.0.0.1 as the master node ip (or you could use kubernetes.docker.internal)

>> kubectl get service
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
helloworld   NodePort    10.107.197.207   <none>        80:32714/TCP   71m
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        3h4m
>> kubectl cluster-info
Kubernetes control plane is running at https://kubernetes.docker.internal:6443
CoreDNS is running at https://kubernetes.docker.internal:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

With the above information I can launch helloworld at http://kubernetes.docker.internal:32714/ or http://127.0.0.1:32714/

Shiraz
  • 2,310
  • 24
  • 26
0

equivalent to minikube service mynginx1 would be kubectl get service mynginx1