gpt4 book ai didi

reactjs - Kubernetes 中的 Dockerfile 入口点未执行

转载 作者:行者123 更新时间:2023-12-05 00:44:23 25 4
gpt4 key购买 nike

我有一个基于 docker 容器的基于 ReactJS 的应用程序,一个 shell 脚本在 docker image 中定义为 ENTRYPOINT,我可以成功使用 docker run image-name。

现在的任务是使用标准的 deployment.yaml 文件模板使用这个 docker 镜像进行 Kubernetes 部署,如下所示

# Deployment file
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
terminationGracePeriodSeconds: 120
containers:
- name: my-app
imagePullPolicy: Always
image: my-docker-image
command: ["/bin/bash"]
args: ["-c","./entrypoint.sh;while true; do echo hello; sleep 10;done"]

kind: Service
apiVersion: v1
metadata:
name: my-service
spec:
type: NodePort
selector:
app: my-app
ports:
- port: 3000
targetPort: 3000
protocol: TCP
nodePort: 31110

spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-service
port:
number: 3000

当我执行 kubectl apply -f mydeployment.yaml 时,它会创建所需的 pod,但与直接运行 docker 镜像不同,在创建 pod 时不会执行 entrypoint.sh 脚本。有人可以帮忙分享一下上面的 yaml 文件有什么问题吗,我错过了还是做错了什么?

我还尝试在 yaml 中的命令 [] 中直接调用 npm run start,但没有运气。我可以使用 kubectl exec 进入 pod 容器,但我没有看到 react app 正在运行,我可以手动执行 entrypoint.sh 并在浏览器中查看所需的输出。

编辑:添加 kubectl 日志并描述输出

日志:当我从 yaml 中删除 command/args 并应用 deploy.yaml 时,我得到以下日志,直到启动 dev server 行,除此之外什么都没有。

> myapp start /app
> react-scripts start

ℹ 「wds」: Project is running at http://x.x.x.x/
ℹ 「wds」: webpack output is served from
ℹ 「wds」: Content not from webpack is served from /app/public
ℹ 「wds」: 404s will fallback to /
Starting the development server...

描述输出

Name:         my-view-85b597db55-72jr8
Namespace: default
Priority: 0
Node: my-node/x.x.x.x
Start Time: Fri, 16 Apr 2021 11:13:20 +0800
Labels: app=my-app
pod-template-hash=85b597db55
Annotations: cni.projectcalico.org/podIP: x.x.x.x/xx
cni.projectcalico.org/podIPs: x.x.x.x/xx
Status: Running
IP: x.x.x.x
IPs:
IP: x.x.x.x
Controlled By: ReplicaSet/my-view-container-85b597db55
Containers:
my-ui-container:
Container ID: containerd://671a1db809b7f583b2f3702e06cee3477ab1412d1e4aa8ac93106d8583f2c5b6
Image: my-docker-image
Image ID: my-docker-image@sha256:29f5fc74aa0302039c37d14201f5c85bc8278fbeb7d70daa2d867b7faa6d6770
Port: <none>
Host Port: <none>
State: Terminated
Reason: Completed
Exit Code: 0
Started: Fri, 16 Apr 2021 11:13:41 +0800
Finished: Fri, 16 Apr 2021 11:13:43 +0800
Last State: Terminated
Reason: Completed
Exit Code: 0
Started: Fri, 16 Apr 2021 11:13:24 +0800
Finished: Fri, 16 Apr 2021 11:13:26 +0800
Ready: False
Restart Count: 2
Environment:
MY_ENVIRONMENT_NAME: TEST_ENV
MY_SERVICE_NAME: my-view-service
MY_SERVICE_MAIN_PORT: 3000
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-9z8bw (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-9z8bw:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-9z8bw
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 32s default-scheduler Successfully assigned default/my-view-container-85b597db55-72jr8 to my-host
Normal Pulled 31s kubelet Successfully pulled image "my-docker-image" in 184.743641ms
Normal Pulled 28s kubelet Successfully pulled image "my-docker-image" in 252.382942ms
Normal Pulling 11s (x3 over 31s) kubelet Pulling image "my-docker-image"
Normal Pulled 11s kubelet Successfully pulled image "my-docker-image" in 211.2478ms
Normal Created 11s (x3 over 31s) kubelet Created container my-view-container
Normal Started 11s (x3 over 31s) kubelet Started container my-view-container
Warning BackOff 8s (x2 over 26s) kubelet Back-off restarting failed container

我的 entrypoint.sh 是

#!/bin/bash
( export REACT_APP_ENV_VAR=env_var_value;npm run start )
exec "$@"

最佳答案

当你在 pod 描述中写下这个时:

      containers:
- name: my-app
imagePullPolicy: Always
image: my-docker-image
command: ["/bin/bash"]
args: ["-c","./entrypoint.sh;while true; do echo hello; sleep 10;done"]

command 参数覆盖容器 ENTRYPOINT。以上大致相当于:

docker run --entrypoint /bin/bash my-docker-image ...args here...

如果您想使用图像中的 ENTRYPOINT,则只需设置 args

关于reactjs - Kubernetes 中的 Dockerfile 入口点未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67113409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com