使用 docker-compose 运行但不在 kubernetes 部署中的 Angular 应用程序镜像

标签 angular docker kubernetes

我有简单的Dockerfile对于 Angular 10 应用程序。

FROM node:12.2.0-alpine
WORKDIR /usr/src/app/e-wallet-web

COPY package*.json ./

RUN npm install -g @angular/cli@10.0.1 @angular-devkit/build-angular @angular/material@10.0.1 @angular/flex-layout@10.0.0-beta.32 && npm install

EXPOSE 4200

CMD ng serve --host 0.0.0.0
现在我创建了docker-compose与后端、数据库等其他应用程序一起部署。
enter image description here
当我运行 docker-compose up命令,它工作正常。现在我想在 kubernetes 上部署,为此我创建了一个 deployment.yml像这样的文件。
apiVersion: apps/v1
kind: Deployment
metadata:
  name: walletaweb-deployment
  namespace: ewallet
spec:
  selector:
    matchLabels:
      app: walletaweb-pod
  template:
    metadata:
      labels:
        app: walletaweb-pod
    spec:
      containers:
      - name: ewalletweb
        image: ewalletweb:latest
        imagePullPolicy: IfNotPresent
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 80
它不工作。当我看到该 pod 的日志时,它显示错误 The serve command requires to be run in an Angular project, but a project definition could not be found. enter image description here
pods 说明:
PS > kubectl describe -n ewallet pod walletaweb-deployment-6789b56ccf-prmbk
Name:         walletaweb-deployment-6789b56ccf-prmbk
Namespace:    ewallet
Priority:     0
Node:         docker-desktop/192.168.65.3
Start Time:   Tue, 21 Jul 2020 12:58:42 +0600
Labels:       app=walletaweb-pod
              pod-template-hash=6789b56ccf
Annotations:  <none>
Status:       Running
IP:           10.1.0.58
IPs:
  IP:           10.1.0.58
Controlled By:  ReplicaSet/walletaweb-deployment-6789b56ccf
Containers:
  ewalletweb:
    Container ID:   docker://fa1daff061031490043d83d322c11da7ba308a333deb61c6fdbee4fa21e96e26
    Image:          ewalletweb:latest
    Image ID:       docker://sha256:21bf5c443bd740aaab1fd66ce3342d8093648f884a91b1ccf6b4d76d20d11304
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Tue, 21 Jul 2020 13:20:10 +0600
      Finished:     Tue, 21 Jul 2020 13:20:11 +0600
    Ready:          False
    Restart Count:  9
    Limits:
      cpu:     500m
      memory:  128Mi
    Requests:
      cpu:        500m
      memory:     128Mi
    Environment:  <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-fz7rk (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  default-token-fz7rk:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-fz7rk
    Optional:    false
QoS Class:       Guaranteed
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason     Age                 From                     Message
  ----     ------     ----                ----                     -------
  Normal   Scheduled  <unknown>           default-scheduler        Successfully assigned ewallet/walletaweb-deployment-6789b56ccf-prmbk to docker-desktop
  Normal   Pulled     20m (x5 over 21m)   kubelet, docker-desktop  Container image "ewalletweb:latest" already present on machine
  Normal   Created    20m (x5 over 21m)   kubelet, docker-desktop  Created container ewalletweb
  Normal   Started    20m (x5 over 21m)   kubelet, docker-desktop  Started container ewalletweb
  Warning  BackOff    96s (x92 over 21m)  kubelet, docker-desktop  Back-off restarting failed container

最佳答案

您的 Dockerfile 没有 COPY Angular 应用程序到图像中,所以它在 ng serve 时不存在运行。您需要在 RUN npm install 之后添加到 Dockerfile线,类似的东西

COPY . .
确保您还有 .dockerignore导致主机的 node_modules 的文件要从构建中排除的目录。
看起来像您的 docker-compose.yml文件将主机代码注入(inject)到容器中,然后告诉 Docker 使用 node_modules 的某些(可能是旧的)版本。树来覆盖 Dockerfile 所做的事情。如果你删除 volumes:,你会得到同样的错误。阻止那里。 (这通常是一个很好的做法:如果您要替换容器中的所有代码和整个应用程序目录,那么您在 Compose 中运行的内容与您尝试在 Kubernetes 中运行的内容根本不同。)
您几乎无法在 Kubernetes 中复制这种面向开发人员的设置。 (原则上你可以,但只能在像 Minikube 这样的单节点集群上,并且只要你当前拥有的部署规范就可以做到这一点。)图像必须是自包含的,并包含它们的所有代码和依赖项。

关于使用 docker-compose 运行但不在 kubernetes 部署中的 Angular 应用程序镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63009258/

相关文章:

kubernetes - Kubernetes 中的 Prometheus HA (AKS)

css - Angular 将自定义 .css 文件添加到 Angular-CLI 元素

angular - 错误 : Error on worker #1: Error: Debug Failure. 未处理的 SyntaxKind:未知

javascript - 使用颜色选择器更改 div 的背景颜色

docker - 无法将Docker镜像推送到gcp-cluster

docker - 为 Rstudio Docker 安装包

javascript - 嵌套 *ngFor 改变不正确的索引

docker - 如何恢复空间不足的已停止的 docker 容器?

docker - 无法在glusterfs上运行具有容量的容器

kubernetes - 如何将 minikube 集群的 API 服务器暴露给公网(LAN)?