【问题标题】:How to access a service deployed as NodePort in Minikube in EC2 from outside如何从外部访问 EC2 中 Minikube 中部署为 NodePort 的服务
【发布时间】:2021-11-20 18:20:10
【问题描述】:

我已经在 EC2 Linux Instance 中安装了 minikube。

Minikube IP : 192.168.49.2, 
EC2 Instance IP: 10.X.X.X

我已将应用程序部署为 NodePort 服务类型,其中

port: 80
targetPort: 80
nodePort: 32768
and able to access it with curl http://192.168.49.2:32768 within the cluster.

我应该如何向外界公开这个应用程序?
如果端口转发是选项,那么我尝试了以下方法:

kubectl port-forward --address <InstanceIP> svc/my-service 8888:80

这总是报错:

Unable to listen on port 5000: 
Listeners failed to create with the following errors: 
[unable to create listener] error: 
unable to listen on any of the requested ports: [{8888 80}]

我确定端口 8888 上没有运行任何服务。

kubectl port-forward --address <InstanceIP> svc/my-service 8888:32768

这表示,错误:

Service my-service does not have a service port 32768.

感谢任何帮助

【问题讨论】:

    标签: kubernetes kubernetes-helm kubernetes-ingress minikube


    【解决方案1】:

    我应该如何将这个应用程序暴露给外界?

    您应该使用Servicetype: LoadBalancer

    LoadBalancer 将为您提供一个外部 IP,允许您从集群外部连接到它。

    如果您只需要它进行测试,这就是您使用minikube 的原因,那么端口转发应该满足要求。

    使用LoadBalancerminikube

    要将LoadBalanacer 与minikube 一起使用,您应该打开一个新终端并执行minikube tunnel

    # Port forward to the desired service, 
    # dont forget to add the namespace if any
    
    #
    # You should forward to port 80 which is the port defined in your service
    #     port : 80
    #     targetPort : 80
    kubectl port-forward svc/my-service 8888:80 -n <namespace>
    
    # As you mentioned you tried it already and it's not working
    # so follow below and try to expose your service with
    
    kubectl expose
    

    现在您应该可以连接到您的服务了。


    这是一个生成Service的完整示例

    https://github.com/nirgeier/KubernetesLabs/tree/master/Labs/02-Deployments-Imperative

    该示例会即时生成Service

    kubectl expose deployment -n codewizard multitool --port 80 --type NodePort
    

    【讨论】:

      猜你喜欢
      • 2020-09-15
      • 2020-05-13
      • 2020-04-26
      • 2020-05-20
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 2020-04-04
      相关资源
      最近更新 更多