2016-11-23 179 views
3

在minikube中,如何使用nodeport公開服務?在minikube中公開端口

例如,我開始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節點需要配置爲公開這個端口。

+0

你能澄清你的「訪問是什麼意思來自主機的暴露服務「?看起來你已經可以通過以下方式訪問主機上的hello-minikube服務了: 'minikube service hello-minikube --url' –

+0

是的你是對的。實際上,這是配置錯誤,因爲主機無法訪問端口,我很困惑我需要更改一些防火牆設置以使其可訪問。 – KarateKid

回答

14

我不確定你在問什麼,因爲看起來你已經知道minikube service <SERVICE_NAME> --url命令,它會給你一個你可以訪問服務的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選項用於打印服務的網址是什麼得到在瀏覽器中打開:

$ minikube service hello-minikube --url 
http://192.168.99.100:31167 
+0

在我的Linux主機上找不到端口'$ kubectl expose deployment hello-minikube --type = NodePort 錯誤:無法通過--port flag或introspection找到端口' – Stephane

2

minikube運行在類似192.168.99.100的東西上。因此,您應該能夠在您公開的服務所在的nodeport上訪問它。例如,假設你的nodeport是30080,那麼你的服務將被作爲192.168.99.100:30080訪問。

要獲得minikube ip,請運行命令minikube ip

更新2017年9月14日:

這裏是與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:

$ k 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=/ 
... 
+1

這看起來並不正確。 – macrael

+0

不能在MacOS minikube中工作 – icordoba

+0

1)kubernetes爲IP地址和端口分配公開的服務 2)IP和端口在具有虛擬IP的虛擬IP上運行,您的筆記本電腦不具有本地訪問權限。 – ives