Unable to get ClusterIP service url from minikube

孤街浪徒 提交于 2021-01-28 01:33:10

问题


I have created a ClusterIP service according to configuration files below, however I can't seem to get the URL from minikube for that service

k create -f service-cluster-definition.yaml
➜ minikube service myapp-frontend --url                                           
😿  service default/myapp-frontend has no node port

And if I try to add NodePort into the ports section of service-cluster-definition.yaml it complains with error, that such key is deprecated.

What am I missing or doing wrong?

service-cluster-definition.yaml

apiVersion: v1
kind: Service
metadata:
  name: myapp-frontend
spec:
  type: ClusterIP
  ports:
    - targetPort: 80
      port: 80
  selector:
    app: myapp
    type: etl

deployment-definition.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
  labels:
    app: myapp
    env: experiment
    type: etl
spec:
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        env: experiment
        type: etl
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.7.1
  replicas: 3
  selector:
    matchLabels:
      type: etl
➜ k get pods --selector="app=myapp,type=etl" -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
myapp-deployment-59856c4487-2g9c7   1/1     Running   0          45m   172.17.0.9   minikube   <none>           <none>
myapp-deployment-59856c4487-mb28z   1/1     Running   0          45m   172.17.0.4   minikube   <none>           <none>
myapp-deployment-59856c4487-sqxqg   1/1     Running   0          45m   172.17.0.8   minikube   <none>           <none>


(⎈ |minikube:default)
Projects/experiments/kubernetes 
➜ k version     
Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.3", GitCommit:"06ad960bfd03b39c8310aaf92d1e7c12ce618213", GitTreeState:"clean", BuildDate:"2020-02-11T18:14:22Z", GoVersion:"go1.13.6", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.3", GitCommit:"06ad960bfd03b39c8310aaf92d1e7c12ce618213", GitTreeState:"clean", BuildDate:"2020-02-11T18:07:13Z", GoVersion:"go1.13.6", Compiler:"gc", Platform:"linux/amd64"}
(⎈ |minikube:default)

回答1:


First let's clear some concepts from Documentation:

  • ClusterIP: Exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster.

  • NodePort: Exposes the Service on each Node’s IP at a static port (the NodePort). You’ll be able to contact the NodePort Service, from outside the cluster, by requesting NodeIP:NodePort.


Question 1:

I have created a ClusterIP service according to configuration files below, however I can't seem to get the URL from minikube for that service.

  • Since Minikube is a virtualized environment on a single host we tend to forget that the cluster is isolated from the host computer. If you set a service as ClusterIP, Minikube will not give external access.

Question 2:

And if I try to add NodePort into the ports section of service-cluster-definition.yaml it complains with error, that such key is deprecated.

  • Maybe you were pasting on the wrong position. You should just substitute the field type: ClusterIP for type: NodePort. Here is the correct form of your yaml:
apiVersion: v1
kind: Service
metadata:
  name: myapp-frontend
spec:
  type: NodePort
  ports:
    - targetPort: 80
      port: 80
  selector:
    app: myapp
    type: etl

Reproduction:

user@minikube:~$ kubectl apply -f deployment-definition.yaml 
deployment.apps/myapp-deployment created

user@minikube:~$ kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
myapp-deployment-59856c4487-7dw6x   1/1     Running   0          5m11s
myapp-deployment-59856c4487-th7ff   1/1     Running   0          5m11s
myapp-deployment-59856c4487-zvm5f   1/1     Running   0          5m11s

user@minikube:~$ kubectl apply -f service-cluster-definition.yaml 
service/myapp-frontend created

user@minikube:~$ kubectl get service myapp-frontend
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
myapp-frontend   NodePort    10.101.156.113   <none>        80:32420/TCP   3m43s

user@minikube:~$ minikube service list
|-------------|----------------|-----------------------------|-----|
|  NAMESPACE  |      NAME      |         TARGET PORT         | URL |
|-------------|----------------|-----------------------------|-----|
| default     | kubernetes     | No node port                |     |
| default     | myapp-frontend | http://192.168.39.219:32420 |     |
| kube-system | kube-dns       | No node port                |     |
|-------------|----------------|-----------------------------|-----|

user@minikube:~$ minikube service myapp-frontend --url
http://192.168.39.219:32420

user@minikube:~$ curl http://192.168.39.219:32420
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...{{output suppressed}}...
  • As you can see, with the service set as NodePort the minikube start serving the app using MinikubeIP:NodePort routing the connection to the matching pods.
    • Note that nodeport will be chosen by default between 30000:32767

If you have any question let me know in the comments.




回答2:


To access inside cluster, do kubectl get svc to get the cluster ip or use the service name directly. To access outside cluster, you can use NodePort as service type.



来源:https://stackoverflow.com/questions/60556096/unable-to-get-clusterip-service-url-from-minikube

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!