【问题标题】:Exposing the hello-minikube service fails to find a port暴露 hello-minikube 服务找不到端口
【发布时间】:2018-02-24 00:00:11
【问题描述】:

我可以创建容器:

$ kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=
deployment "hello-minikube" created

我现在正试图公开一项服务:

$ kubectl expose deployment hello-minikube --type=NodePort
error: couldn't find port via --port flag or introspection

即使我删除它,它仍然会自行恢复:

$ kubectl delete pod hello-minikube-2138963058-2szl7
pod "hello-minikube-2138963058-2szl7" deleted
[stephane@stephane-ThinkPad-X201 ~]
$ kubectl get pods
NAME                              READY     STATUS    RESTARTS   AGE
hello-minikube-2138963058-nhh1q   1/1       Running   0          3m

这是豆荚:

$ kubectl get pods
NAME                              READY     STATUS    RESTARTS   AGE
hello-minikube-2138963058-2szl7   1/1       Running   0          16m

及其描述:

$ kubectl describe pod hello-minikube-2138963058-2szl7
Name:       hello-minikube-2138963058-2szl7
Namespace:  default
Node:       minikube/192.168.42.196
Start Time: Thu, 14 Sep 2017 23:20:03 +0200
Labels:     pod-template-hash=2138963058
        run=hello-minikube
Annotations:    kubernetes.io/created-by={"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"ReplicaSet","namespace":"default","name":"hello-minikube-2138963058","uid":"2b37ca13-9968-11e7-a720-525400...
Status:     Running
IP:     172.17.0.3
Created By: ReplicaSet/hello-minikube-2138963058
Controlled By:  ReplicaSet/hello-minikube-2138963058
Containers:
  hello-minikube:
    Container ID:   docker://5e4ba407d8869e6e843ec3d7876e953147cc01104e980c7febfea218808ab379
    Image:      gcr.io/google_containers/echoserver:1.4
    Image ID:       docker-pullable://gcr.io/google_containers/echoserver@sha256:5d99aa1120524c801bc8c1a7077e8f5ec122ba16b6dda1a5d3826057f67b9bcb
    Port:       <none>
    State:      Running
      Started:      Thu, 14 Sep 2017 23:20:05 +0200
    Ready:      True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-t8qx7 (ro)
Conditions:
  Type      Status
  Initialized   True 
  Ready     True 
  PodScheduled  True 
Volumes:
  default-token-t8qx7:
    Type:   Secret (a volume populated by a Secret)
    SecretName: default-token-t8qx7
    Optional:   false
QoS Class:  BestEffort
Node-Selectors: <none>
Tolerations:    <none>
Events:
  FirstSeen LastSeen    Count   From            SubObjectPath           Type        Reason          Message
  --------- --------    -----   ----            -------------           --------    ------          -------
  17m       17m     1   default-scheduler                   Normal      Scheduled       Successfully assigned hello-minikube-2138963058-2szl7 to minikube
  17m       17m     1   kubelet, minikube                   Normal      SuccessfulMountVolume   MountVolume.SetUp succeeded for volume "default-token-t8qx7" 
  17m       17m     1   kubelet, minikube   spec.containers{hello-minikube} Normal      Pulled          Container image "gcr.io/google_containers/echoserver:1.4" already present on machine
  17m       17m     1   kubelet, minikube   spec.containers{hello-minikube} Normal      Created         Created container
  17m       17m     1   kubelet, minikube   spec.containers{hello-minikube} Normal      Started         Started container

所有这些都在 Linux 上的主机 minikube 上。

【问题讨论】:

    标签: linux kubernetes ubuntu-16.04 minikube


    【解决方案1】:

    您收到此错误是因为您没有从命令kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port= 设置容器端口,因此暴露命令不知道将哪个容器端口映射到节点端口然后出错

    您必须按照kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=80 设置确切的容器端口,假设80 是端口号,然后再次运行暴露。

    请参阅下文,逐步了解我如何能够复制您的错误然后修复

    C:\Users\innocent.anigbo\.minikube>kubectl run hello-kube --image=gcr.io/google_
    containers/echoserver:1.4 --port=
    deployment "hello-kube" created
    
    C:\Users\innocent.anigbo\.minikube>kubectl get pods
    NAME                             READY     STATUS    RESTARTS   AGE
    hello-kube-1448409582-c9sm5      1/1       Running   0          1m
    hello-minikube-938614450-417hj   1/1       Running   1          8d
    hello-nginx-3322088713-c4rp4     1/1       Running   0          6m
    
    C:\Users\innocent.anigbo\.minikube>kubectl get deployment
    NAME             DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    hello-kube       1         1         1            1           2m
    hello-minikube   1         1         1            1           8d
    hello-nginx      1         1         1            1           7m
    
    C:\Users\innocent.anigbo\.minikube>kubectl get service
    NAME          CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
    hello-nginx   10.0.0.136   <nodes>       80:32155/TCP   4m
    kubernetes    10.0.0.1     <none>        443/TCP        20d
    
    C:\Users\innocent.anigbo\.minikube>kubectl expose deployment hello-kube --type=N
    odePort
    error: couldn't find port via --port flag or introspection
    See 'kubectl expose -h' for help and examples.
    
    C:\Users\innocent.anigbo\.minikube>kubectl delete deployment hello-kube
    deployment "hello-kube" deleted
    
    C:\Users\innocent.anigbo\.minikube>kubectl get pods
    NAME                             READY     STATUS    RESTARTS   AGE
    hello-minikube-938614450-417hj   1/1       Running   1          8d
    hello-nginx-3322088713-c4rp4     1/1       Running   0          11m
    
    C:\Users\innocent.anigbo\.minikube>kubectl run hello-kube --image=gcr.io/google_
    containers/echoserver:1.4 --port=80
    deployment "hello-kube" created
    
    C:\Users\innocent.anigbo\.minikube>kubectl get pods
    NAME                             READY     STATUS    RESTARTS   AGE
    hello-kube-2715294448-0rxf2      1/1       Running   0          3s
    hello-minikube-938614450-417hj   1/1       Running   1          8d
    hello-nginx-3322088713-c4rp4     1/1       Running   0          11m
    
    C:\Users\innocent.anigbo\.minikube>kubectl expose deployment hello-kube --type=N
    odePort
    service "hello-kube" exposed
    
    C:\Users\innocent.anigbo\.minikube>kubectl get service
    NAME          CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
    hello-kube    10.0.0.137   <nodes>       80:30004/TCP   3s
    hello-nginx   10.0.0.136   <nodes>       80:32155/TCP   9m
    kubernetes    10.0.0.1     <none>        443/TCP        20d
    

    【讨论】:

    • 我可以公开服务kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=82kubectl expose deployment hello-minikube --type=NodePort 显示服务$ kubectl get services NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello-minikube 10.0.0.81 &lt;nodes&gt; 82:32515/TCP 4h 但尝试连接失败$ curl $(minikube service hello-minikube --url) curl: (7) Failed to connect to 192.168.42.196 port 32515: Connection refused
    • 但是,如果我不使用端口 82,而是使用端口 8080,它就可以工作。我想我可以在这里选择我自己的港口。不是这样吗?
    • 不,您不能使用自己的端口或任何随机端口。这必须是容器侦听流量的端口,如图像echoserver:1.4 中所示。您将从记录图像的存储库中获取此端口号。 8080 之所以有效,是因为您很幸运猜到了正确的端口。下次最好从图像文档中检查这一点,以使其更容易。
    【解决方案2】:

    您的 pod 正在重新出现,因为部署会为容器创建一个副本集,如果当前 pod 终止,该容器会创建一个新副本。

    运行此检查是否部署了副本集。

    kubectl get rs
    

    最好删除整个部署

    kubectl delete deployment <name>
    

    【讨论】:

    • 作为一个 k8s 菜鸟,我对环境和 pod 了解不够,无法删除正确的。您的回答加上 Innocent Anigbo 以上的回答解决了我的问题。
    猜你喜欢
    • 2019-06-04
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2020-10-21
    • 2019-10-24
    • 2019-06-22
    相关资源
    最近更新 更多