What is the kubectl equivalent commands to "minikube service <service name>"? - command

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?

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

Port forward worked for me
kubectl port-forward deployment/es-manual 9200:9200

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/

equivalent to minikube service mynginx1
would be
kubectl get service mynginx1

Related

ClusterIP not reachable within the Cluster

I'm struggling with kubernates configurations. What I want to get it's just to reach a deployment within the cluster. The cluster is on my dedicated server and I'm deploying it by using Kubeadm.
My nodes:
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 9d v1.19.3
k8s-worker1 Ready <none> 9d v1.19.3
I've a deployment running (nginx basic example)
$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 29m
I've created a service
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9d
my-service ClusterIP 10.106.109.94 <none> 80/TCP 20m
The YAML file for my service is the following:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: nginx-deployment
ports:
- protocol: TCP
port: 80
Now I should expect, if I run curl 10.106.109.94:80 on my k8s-master to get the http answer.. but what I got is:
curl: (7) Failed to connect to 10.106.109.94 port 80: Connection refused
I've tried with NodePort as well and with targetPort and nodePort but the result is the same.
The cluster ip can not be reachable from outside of your cluster that means you will not get any response from the host machine that host your k8s cluster as this ip is not a part of your machine or any other machine rather than its a cluster ip which is used by your cluster CNI network like flunnel,weave.
So to get your services accessible from the outside or atleast from the host machine you have to change the type of your service like NodePort,LoadBalancer,K8s port-forward.
If you can change the service type NodePort then you will get response with any of your host machine ip and the allocated nodeport.
For example,if your k8s-master is 192.168.x.x and nodePort is 33303 then you can get response by
curl http://192.168.x.x:33303
or
curl http://worker_node_ip:33303
if your cluster is in locally installed, then you can install metalLB to get the privilege of load balancer.
You can also use port-forward to get your service accessible from the host that has kubectl client with k8s cluster access.
kubectl port-forward svc/my-service 80:80
kubectl -n namespace port-forward svc/service_name Port:Port

Cannot access Microk8s service from browser using NodePort service

I installed microk8s on my ubuntu machine based on steps here https://ubuntu.com/kubernetes/install#single-node
Then I followed kubernetes official tutorial and created and exposed a deployment like this
microk8s.kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
microk8s.kubectl expose deployment/kubernetes-bootcamp --type=NodePort --port 8083
This is my kubectl get services output
akila#ubuntu:~$ microk8s.kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 25h
kubernetes-bootcamp NodePort 10.152.183.11 <none> 8083:31695/TCP 17s
This is my kubectl get pods output
akila#ubuntu:~$ microk8s.kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-6f6656d949-rllgt 1/1 Running 0 51m
But I can't access the service from my browser using http://localhost:8083 OR using http://10.152.183.11:31695
When I tried http://localhost:31695 I'm getting ERR_CONNECTION_REFUSED.
How can I access this "kubernetes-bootcamp" service from my browser ?
Am I mising anything ?
The IP 10.152.183.11 is CLUSTER-IP and not accessible from outside the cluster i.e from a browser. You should be using http://localhost:31695 where 31695 is the NodePort opened on the host system.
The container of the gcr.io/google-samples/kubernetes-bootcamp:v1 image need to listen on port 8083 because you are exposing it on that port. Double check that because otherwise this will lead to ERR_CONNECTION_REFUSED error.
If the container is listening on port 8080 then use below command to expose that port
microk8s.kubectl expose deployment/kubernetes-bootcamp --type=NodePort --port 8080
Try this
kubectl port-forward <pod_name> <local_port>:<pod_port>
then access http://localhost:<local_port>

k8s: Get access to pods

I newbie question related with k8s. I've just installed a k3d cluster.
I've deployed an this helm chart:
$ helm install stable/docker-registry
It's been installed and pod is running correctly.
Nevertheless, I don't quite figure out how to get access to this just deployed service.
According to documentation, it's listening on 5000 port, and is using a ClusterIP. A service is also deployed.
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 42h
docker-registry-1580212517 ClusterIP 10.43.80.185 <none> 5000/TCP 19m
EDIT
I've been able to say to chard creates an ingress:
$ kubectl get ingresses.networking.k8s.io -n default
NAME HOSTS ADDRESS PORTS AGE
docker-registry-1580214408 chart-example.local 172.20.0.4 80 10m
Nevertheless, I'm still without being able tp push images to registry:
$ docker push 172.20.0.4/feedly:v1
The push refers to repository [172.20.0.4/feedly]
Get https://172.20.0.4/v2/: x509: certificate has expired or is not yet valid
Since the service type is ClusterIP, you can't access the service from host system. You can run below command to access the service from your host system.
kubectl port-forward --address 0.0.0.0 svc/docker-registry-1580212517 5000:5000 &
curl <host IP/name>:5000

Kubernetes service stays in pending state

Service showing pending status after exposing the deployment.
packet#ubuntu:/home/gss$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 22h
wms1 LoadBalancer 10.106.19.103 <pending> 8000:32461/TCP 17h
Installed kubeadm with one master and 4 worker nodes.
created deployment with the command:
sudo docker run -p 8000:8000 w1
here w1 is my image name.
created service with the command:
kubectl expose deployment wms1 --type=LoadBalancer --port=8000
To retrieve external ip for your application in Kubernetes Cluster you have to use cloud provider like Google Kubernetes Engine or Amazon Web Services.
Please check:
https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/#external-load-balancer-providers
Otherwise you can use Type NodePort and in that case Kubernetes master will allocate a port from a range specified by --service-node-port-range flag (default: 30000-32767), and each Node will proxy that port (the same port number on every Node) into your Service.
For detailed information: https://kubernetes.io/docs/concepts/services-networking/service/#nodeport
In a second terminal, run the command:
minikube tunnel
restart your service and you should see the EXTERNAL-IP populated

Kubernetes service external IP address remains pending with IBM Cloud (earlier called as Bluemix)

I'm following an example from Kubernetes in Action to run a simple docker image in kubernetes:
$ bx login --apikey #apiKey.json -a https://api.eu-de.bluemix.net
$ bx cs cluster-config my_kubernetes
$ export KUBECONFIG=..my_kubernetes.yml
Next, run the container:
$ kubectl run kubia --image=luksa/kubia --port=8080 --generator=run/v1
$ kubectl expose rc kubia --type=LoadBalancer --name kubia-http
$ kubectl get service
$ kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.10.10.1 <none> 443/TCP 20h
kubia-http 10.10.10.12 <pending> 8080:32373/TCP 0m
Fifteen minutes later ...
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.10.10.1 <none> 443/TCP 20h
kubia-http 10.10.10.12 <pending> 8080:32373/TCP 15m
I don't have anything else running on the Kubernetes cluster.
To close out the thread here, LoadBalancer cannot be used in a lite (aka free) cluster tier. The differences between lite and standard clusters can be found here - https://console.bluemix.net/docs/containers/cs_planning.html#cs_planning.
Run the following to determine if there are any failure events.
kubectl describe svc kubia-http
Thanks to Chris Rosen's answer, I was able to find a workaround:
$ bx cs workers my_kubernetes
OK
ID Public IP Private IP Machine Type State Status
kube-par01-xxxxx 1.2.3.4 6.7.8.9 free normal Ready
Note the Public IP address: 1.2.3.4
Expose the service with NodePort:
$ kubectl expose rc kubia --type=NodePort --name kubia-http2
Check the NodePort details:
$ kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.10.10.1 <none> 443/TCP 21h
kubia-http2 10.10.10.193 <nodes> 8080:31247/TCP 10s
Access the service using the exposed port on the worker Public IP address:
$ curl http://1.2.3.4:31247/
You've hit kubia-bjb59
Based on the posts above I was getting the following steps to work:
Prerequisites: Create a free Kubernetes cluster in the IBM Cloud and follow the steps (you need to have the ibmcloud and kubectl installed and connect to the remote cluster first)
kubectl get nodes
should return something like this
NAME STATUS ROLES AGE VERSION
10.76.197.55 Ready <none> 4h18m v1.18.10+IKS
Then,
kubectl apply -f https://k8s.io/examples/controllers/replication.yaml
replicationcontroller/nginx created
kubectl expose rc nginx --type=NodePort
service/nginx exposed
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 172.21.19.73 80:30634/TCP 70s
Note down the port, 30634 in my case
kubectl describe nodes |grep ExternalIP (to find out the external IP)
call IP:port
Have fun!
If your purpose is to test your application by having it the accessible to the external world , I would suggest using the NodePort service which can be used in the free tier service.
More Info can be found here : Expose service to world