[英]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.