如何创建“DOckerfile”来容器化“Flutter”应用程序并将其部署在 Kubernetes 集群上?

如何创建“DOckerfile”来容器化“Flutter”应用程序并将其部署在 Kubernetes 集群上?

我只是想知道如何docker为 Flutter 应用创建文件然后将其部署到Kubernetes集群上?

我找到了以下内容Dockerfileserver.sh脚本网站,但我不确定这是否是正确的做法?

# Install Operating system and dependencies
FROM ubuntu:22.04

RUN apt-get update 
RUN apt-get install -y curl git wget unzip libgconf-2-4 gdb libstdc++6 libglu1-mesa fonts-droid-fallback lib32stdc++6 python3
RUN apt-get clean

# download Flutter SDK from Flutter Github repo
RUN git clone https://github.com/flutter/flutter.git /usr/local/flutter

# Set flutter environment path
ENV PATH="/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin:${PATH}"

# Run flutter doctor
RUN flutter doctor

# Enable flutter web
RUN flutter channel master
RUN flutter upgrade
RUN flutter config --enable-web

# Copy files to container and build
RUN mkdir /app/
COPY . /app/
WORKDIR /app/
RUN flutter build web

# Record the exposed port
EXPOSE 5000

# make server startup script executable and start the web server
RUN ["chmod", "+x", "/app/server/server.sh"]

ENTRYPOINT [ "/app/server/server.sh"]

和:

#!/bin/bash

# Set the port
PORT=5000

# Stop any program currently running on the set port
echo 'preparing port' $PORT '...'
fuser -k 5000/tcp

# switch directories
cd build/web/

# Start the server
echo 'Server starting on port' $PORT '...'
python3 -m http.server $PORT

我完成了所有步骤,看起来运行良好,但只要我使用,skaffold我就不知道如何/在哪里放置以下命令来自动执行此步骤(我已经手动运行了此命令):

docker run -i -p 8080:5000 -td flutter_docker

我仍然想知道上述文件是否是正确/官方的方法,或者是否有更好的方法?

EDIT:我创建了以下deployment & service文件,将创建的映像部署到Kubernetes本地Kind集群上,但运行时kubectl get pods找不到该映像,但我通过执行找到了它docker images。为什么会发生这种情况,我该如何将其放在Kubernetespod 上而不是docker图像上?

apiVersion: apps/v1
kind: Deployment
metadata:
  name: client-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: client
  template:
    metadata:
      labels:
        app: client
    spec:
      containers:
        - name: client
          image: front
---
apiVersion: v1
kind: Service
metadata:
  name: client-srv
spec:
  selector:
    app: client
  ports:
    - name: client
      protocol: TCP
      port: 3000
      targetPort: 3000

答案1

  • 您链接的网站仅谈论构建 flutter app docker 镜像并使用运行它docker run

  • 要将镜像部署到 Kubernetes 集群,您首先应该将镜像推送到公开可用的镜像注册表(例如 dockerhub)。

  • 接下来,要将镜像作为 Kubernetes 集群上的 pod 运行,有很多选项。最简单的一个,相当于docker runkubectl run --image [path_to_your_pushed_image]我在面向 Docker 用户的 Kubernetes Pods 介绍文章。

  • kubectl get pods之后,如果 pod 没有崩溃或者正常完成运行,您就应该能够看到它的使用情况。

相关内容