简体   繁体   English

如何在Kubernetes中公开外部IP?

[英]How to expose external ip in Kubernetes?

I want to expose my kubernetes cluster with minikube. 我想用minikube公开我的kubernetes集群。

consider my tree 考虑我的树

.                                                                                                                                                                            
├── deployment.yaml                                                                                                                                                          
├── Dockerfile                                                                                                                                                               
├── server.js                                                                                                                                                                
└── service.yaml    

I build my docker image locally and am able to run all pods via 我在本地构建docker映像,并能够通过运行所有Pod

kubectl create -f deployment.yaml 
kubectl create -f service.yaml 

. However when I run 但是当我跑步时

 $ kubectl get services                                                                                                                                        
NAME                      TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE                                                                                    
kubernetes                ClusterIP      10.96.0.1       <none>        443/TCP        2h
nodeapp                   LoadBalancer   10.110.106.83   <pending>     80:32711/TCP   9m

There is no external ip to be able to connect to the cluster. 没有外部IP可以连接到群集。 Tried to expose one pod but the the external Ip stays none. 尝试公开一个Pod,但外部IP却没有。 Why Is there no external ip? 为什么没有外部IP?

 $ cat deployment.yaml                                                                                                                                         
apiVersion: apps/v1                                                                                                                                                          
kind: Deployment
metadata:
  name: nodeapp
  labels:
    app: nodeapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nodeapp
  template:
    metadata:
      labels:
        app: nodeapp
    spec:
      containers:
      - name: hello-node
        image: hello-node:v2
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3000

and

 cat service.yaml                                                                                                                                            
kind: Service                                                                                                                                                                
apiVersion: v1
metadata:
  name: nodeapp
spec:
  selector:
    app: nodeapp
  ports:
  - name: http 
    port: 80
    targetPort: 3000
    protocol: TCP
  type: LoadBalancer


$ cat server.js                                                                                                                                               
var http = require('http');                                                                                                                                                  

var handleRequest = function(request, response) {
  console.log('Received request for URL: ' + request.url);
  response.writeHead(200);
  response.end('Hello User');
};
var www = http.createServer(handleRequest);

According to the K8S documentation here . 根据此处的K8S文档。 So, type=LoadBalancer can be used on AWS, GCP and other supported Clouds, not on Minikube. 因此, type=LoadBalancer可以在AWS,GCP和其他受支持的云上使用,而不能在Minikube上使用。

On cloud providers which support external load balancers, setting the type field to LoadBalancer will provision a load balancer for your Service. 在支持外部负载平衡器的云提供程序上,将类型字段设置为LoadBalancer将为您的服务配置负载平衡器。

Specify type as NodePort as mentioned here and the service will be exposed on a port on the Minikube. 指定类型NodePort提到这里 ,该服务将在Minikube端口上被曝光。 Then the service can be accessed by using url from the host OS. 然后可以使用主机操作系统中的url访问该服务。

minikube service nodeapp --url minikube服务nodeapp --url

A load balancer type service can be achieved in minikube using the metallb project https://github.com/google/metallb 可以使用metallb项目https://github.com/google/metallb在minikube中实现负载均衡器类型的服务

This allows you to use external ip offline and in minikube and not only with a cloud provider. 这使您不仅可以通过云提供商,还可以在minikube中离线使用外部ip。

Good luck! 祝好运!

If you run the following command: 如果运行以下命令:

kubectl expose deployment nodeapp --type=NodePort

Then run: 然后运行:

kubectl get services

It should show you the service and what port it's exposed on. 它应该向您显示服务及其公开的端口。

You can get your Minikube IP using: 您可以使用以下方法获取Minikube IP:

curl $(minikube service nodeapp --url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Googlecloud中的弹性kubernetes集群暴露给外部ip? - How to expose elastic kubernetes cluster in Googlecloud to external ip? 如何将一个节点 Kubernetes 入口服务的外部 IP 暴露到互联网中 - How to expose external IP of one Node Kubernetes Ingress Service into the internet Kubernetes公开服务未分配外部IP - Kubernetes expose a service does not assign the external ip 如何将 kubernetes pod 暴露给外部 IP? - How to expose kubernetes pod to outside IP? 使用带有 Kubernetes 服务的谷歌云中的外部 IP 将其公开到互联网 - Use External IP in Google cloud with Kubernetes service to expose it to the internet Kubernetes 负载均衡器外部 IP 仅对特定集群公开 - Kubernetes Loadbalancer External IP expose to only specific cluster 如何在 Kubernetes 中公开 Ingress 以供外部访问? - How to expose a Ingress for external access in Kubernetes? 如何将docker容器ip暴露给外网? - How to expose the docker container ip to the external network? 如何将 Minkube IP 暴露给主机 IP (Kube.netes/k8s) - How to expose a Minkube IP to the Host IP (Kubernetes/k8s) 如何在没有硬编码到minion IP的情况下向公众公开kubernetes服务? - How to expose kubernetes service to public without hardcoding to minion IP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM