[英]Unable to access node app from K8s cluster
I have deployed a very simple node application to a minikube cluster.我已经将一个非常简单的节点应用程序部署到 minikube 集群。 I created the docker image and everything is working fine while in the container.
我创建了 docker 映像,并且在容器中一切正常。 When I build it in the k8s cluster, I can see the deployment is working.
当我在 k8s 集群中构建它时,我可以看到部署正在运行。
I am trying to access the node application through a load balancer using the minikube IP and port provided when running "kubectl get svc".我正在尝试使用运行“kubectl get svc”时提供的 minikube IP 和端口通过负载均衡器访问节点应用程序。 I know I'm doing this part correct at least...
我知道我至少在做这部分是正确的......
When I try to access the URL and check the network tab, the request never finishes and just stays pending.当我尝试访问 URL 并检查网络选项卡时,请求永远不会完成,只是保持挂起状态。
The node app is a very simple API I made just for testing so I use the route /posts when trying to access the application.节点应用程序是一个非常简单的 API,我只是为了测试而制作的,因此我在尝试访问应用程序时使用路由 /posts。 It should return some JSON and this all works from the container.
它应该返回一些 JSON,这一切都在容器中起作用。 It all just stops working while in the cluster.
它只是在集群中停止工作。
apiVersion: apps/v1
kind: Deployment
metadata:
name: server-deployment
spec:
replicas: 3
selector:
matchLabels:
targets: nodes
template:
metadata:
labels:
targets: nodes
spec:
containers:
- name: server
image: brandonjones085/mean-backend
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: backend-load
spec:
selector:
targets: nodes
ports:
- protocol: "TCP"
port: 3000
targetPort: 3000
type: LoadBalancer
Just like @Hackerman described, if you using the LoadBalancer type of service you need to use minikube tunnel
.就像@Hackerman 描述的那样,如果您使用LoadBalancer类型的服务,则需要使用
minikube tunnel
。 You can follow the example .您可以按照 示例进行操作。 This type of service is usually meant to provision a provider Load Balancer, for example, an AWS ELB .
此类服务通常用于预置提供者负载均衡器,例如AWS ELB 。 But in the case of minikube there is no external provider and you need to trick it into thinking there is one.
但是在 minikube 的情况下没有外部提供者,您需要欺骗它认为有一个。
Basically, you need to run it before you create your Deployment.基本上,您需要在创建部署之前运行它。
On a separate terminal:在单独的终端上:
minikube tunnel
Then apply your manifest YAML file that includes your definition in your question:然后应用您的清单 YAML 文件,其中包含您在问题中的定义:
kubectl apply -f <your-deployment>.yaml
✌️ ✌️
In addition to @Rico answer: there are two ways to access applications deployed on minikube.除了@Rico 的回答:还有两种方法可以访问部署在 minikube 上的应用程序。
First - using NodePort .首先- 使用 NodePort 。 When exposing application using
NodePort
service type you are able to access it by executing minikube service list
and accessing URL shown by the output- it is result of minikube ip
and nodeport.当使用
NodePort
服务类型公开应用程序时,您可以通过执行minikube service list
和访问输出显示的 URL 来访问它 - 这是minikube ip
和 nodeport 的结果。 Output will be similar to this:输出将类似于:
|-------------|------------|----------------------------|-----|
| NAMESPACE | NAME | TARGET PORT | URL |
|-------------|------------|----------------------------|-----|
| |
| default | nginx | http://192.168.39.22:30345 |
| |
|-------------|------------|----------------------------|-----|
Second option is by using minikube tunnel
command.第二种选择是使用
minikube tunnel
命令。 It assigns externalIP to application exposed by LoadBalancer
service type.它将 externalIP 分配给
LoadBalancer
服务类型公开的应用程序。 When trying to access it minikube tunnel
has to be running and in separate terminal you can access it using externalIP:port
.尝试访问它时,
minikube tunnel
必须运行,并且在单独的终端中,您可以使用externalIP:port
访问它。 Example output of minikube tunnel
: minikube tunnel
输出示例:
$minikube tunnel
Status:
machine: minikube
pid: 9284
route: 10.96.0.0/12 -> 192.168.39.22
minikube: Running
services: [backend-load]
Then you can check external IP assigned to this service by running kubectl get service
:然后您可以通过运行
kubectl get service
来检查分配给此服务的外部 IP:
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
backend-load LoadBalancer 10.100.120.115 10.100.120.115 3000:31676/TCP 110s
so in this example application will be accessible under 10.100.120.115:3000
.所以在这个例子中,应用程序可以在
10.100.120.115:3000
下访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.