首页 文章

在minikube中暴露端口

提问于
浏览
15

在minikube中,如何使用nodeport公开服务?

例如,我使用以下命令启动kubernetes集群,并创建并公开如下所示的端口:

$ minikube start
$ kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=8080
$ kubectl expose deployment hello-minikube --type=NodePort
$ curl $(minikube service hello-minikube --url)
CLIENT VALUES:
client_address=192.168.99.1
command=GET
real path=/ ....

现在如何从主机访问公开的服务?我想minikube节点也需要配置为暴露这个端口 .

3 回答

  • 10

    我不确定你在问什么,因为你似乎已经知道了 minikube service <SERVICE_NAME> --url 命令,它会给你一个你可以访问该服务的网址 . 要打开公开的服务,可以使用 minikube service <SERVICE_NAME> 命令:

    $ kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=8080
    deployment "hello-minikube" created
    $ kubectl expose deployment hello-minikube --type=NodePort
    service "hello-minikube" exposed
    $ kubectl get svc
    NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
    hello-minikube   10.0.0.102   <nodes>       8080/TCP   7s
    kubernetes       10.0.0.1     <none>        443/TCP    13m
    
    $ minikube service hello-minikube
    Opening kubernetes service default/hello-minikube in default browser...
    

    此命令将在默认浏览器中打开指定的服务 . 这是minikube服务文档:https://github.com/kubernetes/minikube/blob/master/docs/minikube_service.md

    还有一个 --url 选项用于打印服务的URL,这是在浏览器中打开的:

    $ minikube service hello-minikube --url
    http://192.168.99.100:31167
    
  • 41

    minikube运行像 192.168.99.100 . 所以你应该可以在你公开服务的 NodePort 上访问它 . 例如,假设你的 NodePort30080 ,那么你的服务将被 192.168.99.100:30080 访问 .

    要获取minikube ip,请运行命令 minikube ip .

    Update Sep 14 2017:

    这是一个与minikube v0.16.0 一起使用的小例子 .

    1)运行以下命令创建一个在8080上运行的nginx和一个 NodePort svc 转发到它:

    $ kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=8080
    deployment "hello-minikube" created
    $ kubectl expose deployment hello-minikube --type=NodePort
    service "hello-minikube" exposed
    

    2)找到svc使用的nodeport:

    $ kubectl get svc hello-minikube
    NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
    hello-minikube   10.0.0.76    <nodes>       8080:30341/TCP   4m
    

    3)找到minikube ip:

    $ minikube ip
    192.168.99.100
    

    4)用卷曲说话:

    $ curl 192.168.99.100:30341
    CLIENT VALUES:
    client_address=172.17.0.1
    command=GET
    real path=/
    ...
    
  • 0

    由于minikube通过 nodeIP:nodePort 而不是 localhost:nodePort 公开访问,你可以通过使用 kubectl 的端口转发功能来实现这一点 . 例如,如果您正在运行mongodb服务:

    kubectl port-forward svc/mongo 27017:27017
    

    这将在 localhost:27017 ,FWIW上公开服务 . 此外,您可能想知道如何在后台运行它 .

相关问题