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

时间:2020-07-21 07:13:36

标签: angular docker kubernetes

我对Angular 10应用有简单的Dockerfile

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

广告连播说明:

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

1 个答案:

答案 0 :(得分:3)

您的Dockerfile不会将Angular应用程序COPY插入映像,因此当ng serve运行时它不会存在。您需要在RUN npm install行之后向Dockerfile添加类似

的内容
COPY . .

请确保您还有一个.dockerignore文件,该文件会将主机的node_modules目录从构建中排除。

看起来您的docker-compose.yml文件将主机的代码注入到容器中,然后告诉Docker使用node_modules树的某些(可能是较旧的)版本来覆盖Dockerfile的功能。如果在此处删除volumes:块,将会得到相同的错误。 (通常,这是一个好习惯:如果要替换容器中的所有代码和整个应用程序目录,则在Compose中运行的内容与在Kubernetes中运行的内容本质上是不同的。)

您大约无法在Kubernetes中复制此面向开发人员的设置。 (原则上可以,但是只能在Minikube这样的单节点群集上,并且YAML可以做到这一点,只要您当前具有部署规范即可。)映像必须是自包含的,并包括其所有代码和依赖项。