无法从 minikube 获取 ClusterIP 服务 url

Unable to get ClusterIP service url from minikube

我已经根据下面的配置文件创建了一个 ClusterIP 服务,但是我似乎无法从 minikube 获得该服务的 URL

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

并且如果我尝试将 NodePort 添加到 service-cluster-definition.yaml 的 ports 部分,它会报错,这样的密钥已被弃用。

我错过了什么或做错了什么?

服务集群-definition.yaml

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

部署-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)

要访问集群内部,执行kubectl get svc获取集群ip或直接使用服务名称。 要访问外部集群,您可以使用 NodePort 作为服务类型。

首先让我们澄清一些来自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.


问题一:

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.

  • 由于 Minikube 是单个主机上的虚拟化环境,我们往往会忘记集群与主机计算机是隔离的。如果您将服务设置为 ClusterIP,Minikube 将不会提供外部访问。

问题二:

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.

  • 可能你贴错了位置。您应该将字段 type: ClusterIP 替换为 type: NodePort。这是您的 yaml 的正确形式:
apiVersion: v1
kind: Service
metadata:
  name: myapp-frontend
spec:
  type: NodePort
  ports:
    - targetPort: 80
      port: 80
  selector:
    app: myapp
    type: etl

复制:

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}}...
  • 如您所见,将服务设置为 NodePort,minikube 开始使用 MinikubeIP:NodePort 将连接路由到匹配的 pods 来为应用程序提供服务。
    • 请注意,nodeport 将默认选择 30000:32767

如果您有任何问题,请在评论中告诉我。