繁体   English   中英

无法访问 Kubernetes 上公开的 Dockerized React 应用程序

[英]Cannot access exposed Dockerized React app on Kubernetes

我正在尝试将我的 Dockerized React 应用程序部署到 Kubernetes。 我相信我已经正确地对其进行了 dockerized,但是我在访问暴露的 pod 时遇到了问题。

我没有 Docker 或 Kubernetes 的经验,所以任何帮助将不胜感激。

我的 React 应用程序只是由 Tomcat 提供的静态文件(来自 npm run build)。

我的 Dockerfile 在下面。 总之,我将我的应用程序放在 Tomcat 文件夹中并公开端口 8080。

FROM private-docker-registry.com/repo/tomcat:latest

EXPOSE 8080:8080

# Copy build directory to Tomcat webapps directory
RUN mkdir -p /tomcat/webapps/app
COPY /build/sample-app /tomcat/webapps/app

# Create a symbolic link to ROOT -- this way app starts at root path 
(localhost:8080)

RUN ln -s /tomcat/webapps/app /tomcat/webapps/ROOT

# Start Tomcat
ENTRYPOINT ["catalina.sh", "run"]

我构建并将 Docker 映像推送到私有 Docker 注册表。 我通过像这样运行它来验证容器是否正确运行:

docker run -p 8080:8080 private-docker-registry.com/repo/sample-app:latest

然后,如果我转到 localhost:8080,我会看到我的 React 应用程序的主页。

现在,我遇到的问题是部署到 Kubernetes 并从外部访问应用程序。

这是我的 deployment.yaml 文件:

kind: Deployment
apiVersion: apps/v1beta2
metadata:
  name: sample-app
  namespace: dev
  labels:
    app: sample-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sample-app
  template:
    metadata:
      labels:
        app: sample-app
    spec:
      containers:
      - name: sample-app
        image: private-docker-registry.com/repo/sample-app:latest
        ports:
        - containerPort: 8080
          protocol: TCP
      nodeSelector:
        TNTRole: luxkube
---
kind: Service
apiVersion: v1
metadata:
  name: sample-app
  labels:
    app: sample-app
spec:
  selector:
    app: sample-app
  type: NodePort
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP

我通过运行 kubectl --namespace=dev create -f deployment.yaml 创建了部署和服务

Output of 'describe deployment'

    Name:                   sample-app
    Namespace:              dev
    CreationTimestamp:      Sat, 21 Jul 2018 12:27:30 -0400
    Labels:                 app=sample-app
    Annotations:            deployment.kubernetes.io/revision=1
    Selector:               app=sample-app
    Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
    StrategyType:           RollingUpdate
    MinReadySeconds:        0
    RollingUpdateStrategy:  25% max unavailable, 25% max surge
    Pod Template:
      Labels:  app=sample-app
      Containers:
       sample-app:
        Image:        private-docker-registry.com/repo/sample-app:latest
        Port:         8080/TCP
        Host Port:    0/TCP
        Environment:  <none>
        Mounts:       <none>
      Volumes:        <none>
    Conditions:
      Type           Status  Reason
      ----           ------  ------
      Available      True    MinimumReplicasAvailable
      Progressing    True    NewReplicaSetAvailable
    OldReplicaSets:  <none>
    NewReplicaSet:   sample-app-bb6f59b9 (1/1 replicas created)
    Events:          <none>

Output of 'describe service'

Name:                     sample-app
Namespace:                fab-dev
Labels:                   app=sample-app
Annotations:              <none>
Selector:                 app=sample-app
Type:                     NodePort
IP:                       10.96.29.199
Port:                     <unset>  80/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  34604/TCP
Endpoints:                192.168.138.145:8080
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

现在我不知道应该使用哪个 IP 和端口来访问应用程序。 我尝试了所有组合,但没有一个已加载我的应用程序。 我相信端口应该是 80,所以如果我只有 IP,我应该能够进入浏览器并通过访问 http:// 来访问 React 应用程序。

有没有人有建议?

简短版本是服务正在侦听集群中每个节点 ( 34604 ) 上的相同 TCP/IP 端口, describe service的输出所示:

NodePort:                 <unset>  34604

如果您希望通过“不错”的 URL 访问应用程序,您需要一个负载均衡器,它可以将主机名转换为集群内 IP 和端口组合。 这就是 Ingress 控制器的设计目的,但这不是唯一的方法——将 Service 更改为type: LoadBalancer将为您完成此操作,如果您在 Kubernetes 知道如何以编程方式创建的云环境中运行负载均衡器。

我相信你现在已经找到了答案:),当我面临这个问题时,我来到了这里。 自行解决,希望对大家有帮助。

以下是可以提供帮助的内容:

  1. 部署您的应用程序(例如:react-app)。
  2. 运行以下命令: kubectl expose deployment <workload> --namespace=app-dev --name=react-app --type=NodePort --port=3000 output: service/notesui-app exposed

发布服务端口为 3000,目标端口 3000,节点端口(自动选择 32250)

kubectl get svc react-app --namespace=notesui-dev
NAME          TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
react-app   NodePort   10.23.22.55   <none>        3000:32250/TCP   48m

Yaml:(示例)

apiVersion: v1
kind: Service
  name: react-app
  namespace: app-dev
spec:
  selector: <workload>
  ports:
  - nodePort: 32250
    port: 3000
    protocol: TCP
    targetPort: 3000

  type: NodePort
status: {}

在浏览器上访问应用程序:

http://<Host>:32250/index

是运行 pod 的节点 IP。 如果您的应用程序在多个节点中运行(已缩放)。 它是每个节点上的 NodePort 设置。 可以访问应用程序:

http://<Host1>:32250/index
http://<Host2>:32250/index

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM