首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular应用镜像使用docker-compose运行,但未在kubernetes部署中运行

Angular应用镜像使用docker-compose运行,但未在kubernetes部署中运行
EN

Stack Overflow用户
提问于 2020-07-21 15:13:36
回答 1查看 482关注 0票数 0

我有一个用于Angular 10应用程序的简单Dockerfile

代码语言:javascript
复制
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,以便与后端、数据库等其他应用程序一起部署。

当我运行docker-compose up命令时,它工作得很好。现在我想在kubernetes上部署,为此我创建了一个deployment.yml文件,如下所示。

代码语言:javascript
复制
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.

实例描述:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2020-07-21 18:05:30

您的doesn文件不会将Angular应用程序COPY到图像中,因此doesn文件在doesn运行时并不存在。您需要在Dockerfile文件中的RUN npm install行之后添加类似以下内容的内容

代码语言:javascript
复制
COPY . .

确保您还有一个导致主机的node_modules目录被排除在构建之外的.dockerignore文件。

看起来您的Docker文件将主机的代码注入到容器中,然后告诉docker-compose.yml使用某个(可能是旧的) node_modules树版本来覆盖Dockerfile所做的事情。如果您删除那里的volumes:块,您将得到相同的错误。(这通常是一个很好的实践:如果您要替换容器中的所有代码和整个应用程序目录,那么您在Compose中运行的内容与您试图在Kubernetes中运行的内容是完全不同的。)

您几乎不能在Kubernetes中复制这种面向开发人员的设置。(原则上可以,但仅在Minikube这样的单节点集群上,执行此操作的YAML与您当前拥有的部署规范一样长。)图像必须是自包含的,并包含它们的所有代码和依赖项。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63009258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档