What is the kubectl equivalent commands to "minikube service <service name>"?
Asked Answered
W

4

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?

Whitten answered 6/5, 2020 at 5:32 Comment(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.Whitten
D
4

Port forward worked for me

kubectl port-forward deployment/es-manual 9200:9200
Dressler answered 24/3, 2021 at 20:58 Comment(0)
D
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

Dark answered 6/5, 2020 at 9:59 Comment(3)
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://<master node IP>:31216.Whitten
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.Dark
What is the value of <node-IP> in your example where <node-IP>: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 fromSchlessinger
S
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/

Schlessinger answered 5/10, 2022 at 15:5 Comment(0)
G
0

equivalent to minikube service mynginx1 would be kubectl get service mynginx1

Gowan answered 28/12, 2022 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.