Golang REST API 在 AWS EKS 上出现 CrashLoopBackOff 导致部署失败
时间:2024-02-09
浏览:165
作者:佚名
编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《Golang REST API 在 AWS EKS 上出现 CrashLoopBackOff 导致部署失败》,文章讲解的知识点主要包括,如果你对Golang方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。
我正在尝试将一个用 golang 编写的简单 rest api 部署到 aws eks。
我使用 terraform 在 aws 上创建了一个 eks 集群,并向其应用了 aws 负载均衡器控制器 helm 图表。
集群中的所有资源如下所示:
namespace name ready status restarts age kube-system pod/aws-load-balancer-controller-5947f7c854-fgwk2 1/1 running 0 75m kube-system pod/aws-load-balancer-controller-5947f7c854-gkttb 1/1 running 0 75m kube-system pod/aws-node-dfc7r 1/1 running 0 120m kube-system pod/aws-node-hpn4z 1/1 running 0 120m kube-system pod/aws-node-s6mng 1/1 running 0 120m kube-system pod/coredns-66cb55d4f4-5l7vm 1/1 running 0 127m kube-system pod/coredns-66cb55d4f4-frk6p 1/1 running 0 127m kube-system pod/kube-proxy-6ndf5 1/1 running 0 120m kube-system pod/kube-proxy-s95qk 1/1 running 0 120m kube-system pod/kube-proxy-vdrdd 1/1 running 0 120m namespace name type cluster-ip external-ip port(s) age default service/kubernetes clusterip 10.100.0.1 <none> 443/tcp 127m kube-system service/aws-load-balancer-webhook-service clusterip 10.100.202.90 <none> 443/tcp 75m kube-system service/kube-dns clusterip 10.100.0.10 <none> 53/udp,53/tcp 127m namespace name desired current ready up-to-date available node selector age kube-system daemonset.apps/aws-node 3 3 3 3 3 <none> 127m kube-system daemonset.apps/kube-proxy 3 3 3 3 3 <none> 127m namespace name ready up-to-date available age kube-system deployment.apps/aws-load-balancer-controller 2/2 2 2 75m kube-system deployment.apps/coredns 2/2 2 2 127m namespace name desired current ready age kube-system replicaset.apps/aws-load-balancer-controller-5947f7c854 2 2 2 75m kube-system replicaset.apps/coredns-66cb55d4f4 2 2 2 127m
我可以使用 go 和 docker 在本地运行应用程序。但在 aws eks 上发布它总是会抛出 crashloopbackoff。
运行 kubectl 描述 pod podname 显示:
name: go-api-55d74b9546-dkk9g
namespace: default
priority: 0
node: ip-172-16-1-191.ec2.internal/172.16.1.191
start time: tue, 15 mar 2022 07:04:08 -0700
labels: app=go-api
pod-template-hash=55d74b9546
annotations: kubernetes.io/psp: eks.privileged
status: running
ip: 172.16.1.195
ips:
ip: 172.16.1.195
controlled by: replicaset/go-api-55d74b9546
containers:
go-api:
container id: docker://a4bc07b60c85fd308157d967d2d0d688d8eeccfe4c829102eb929ca82fb25595
image: saurabhmish/golang-hello:latest
image id: docker-pullable://saurabhmish/golang-hello@sha256:f79a495ad17710b569136f611ae3c8191173400e2cbb9cfe416e75e2af6f7874
port: 3000/tcp
host port: 0/tcp
state: waiting
reason: crashloopbackoff
last state: terminated
reason: error
exit code: 1
started: tue, 15 mar 2022 07:09:50 -0700
finished: tue, 15 mar 2022 07:09:50 -0700
ready: false
restart count: 6
environment: <none>
mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-jt4gp (ro)
conditions:
type status
initialized true
ready false
containersready false
podscheduled true
volumes:
kube-api-access-jt4gp:
type: projected (a volume that contains injected data from multiple sources)
tokenexpirationseconds: 3607
configmapname: kube-root-ca.crt
configmapoptional: <nil>
downwardapi: true
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 7m31s default-scheduler successfully assigned default/go-api-55d74b9546-dkk9g to ip-172-16-1-191.ec2.internal
normal pulled 7m17s kubelet successfully pulled image "saurabhmish/golang-hello:latest" in 12.77458991s
normal pulled 7m16s kubelet successfully pulled image "saurabhmish/golang-hello:latest" in 110.127771ms
normal pulled 7m3s kubelet successfully pulled image "saurabhmish/golang-hello:latest" in 109.617419ms
normal created 6m37s (x4 over 7m17s) kubelet created container go-api
normal started 6m37s (x4 over 7m17s) kubelet started container go-api
normal pulled 6m37s kubelet successfully pulled image "saurabhmish/golang-hello:latest" in 218.952336ms
normal pulling 5m56s (x5 over 7m30s) kubelet pulling image "saurabhmish/golang-hello:latest"
normal pulled 5m56s kubelet successfully pulled image "saurabhmish/golang-hello:latest" in 108.105083ms
warning backoff 2m28s (x24 over 7m15s) kubelet back-off restarting failed container
运行 kubectl 日志 podname 和 kubectl 日志 podname -c go-api 显示 standard_init_linux.go:228: exec 用户进程导致:exec 格式错误
清单:
go-deploy.yaml(这是带有文档的 docker hub 映像)
---
apiversion: apps/v1
kind: deployment
metadata:
name: go-api
labels:
app: go-api
spec:
replicas: 2
selector:
matchlabels:
app: go-api
strategy: {}
template:
metadata:
labels:
app: go-api
spec:
containers:
- name: go-api
image: saurabhmish/golang-hello:latest
ports:
- containerport: 3000
resources: {}
go-service.yaml
---
kind: Service
apiVersion: v1
metadata:
name: go-api
spec:
selector:
app: go-api
type: NodePort
ports:
- protocol: TCP
port: 80
targetPort: 3000
如何修复此错误?
正确答案
将其发布为社区 wiki 以获得更好的可见性。 请随意扩展它。
感谢@david maze,他指出了解决方案。有一篇文章“从 mac m1 (arm) 构建兼容 intel64 的 docker 映像”(作者:beppe catanese)here。
本文很好地描述了根本问题。
您正在 arm 架构 (mac m1) 上进行开发/构建,但您将 docker 映像部署到基于 x86-64 架构的 kubernetes 集群。
解决方案:
选项 a:使用 buildx
Buildx 是一个 docker 插件,除其他功能外,它还允许为各种目标平台构建映像。
$ docker buildx build --platform linux/amd64 -t myapp .
选项 b:设置 docker_default_platform
docker_default_platform 环境变量允许为采用 --platform 标志的命令设置默认平台。
export DOCKER_DEFAULT_PLATFORM=linux/amd64
crashloopbackoff 意味着您有一个 pod 启动、崩溃、再次启动,然后再次崩溃。
也许错误来自应用程序本身,它无法连接到数据库、redis...
您可能会在这里找到有用的东西:
My kubernetes pods keep crashing with "CrashLoopBackOff" but I can't find any log
好了,本文到此结束,带大家了解了《Golang REST API 在 AWS EKS 上出现 CrashLoopBackOff 导致部署失败》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
站内热门
最新文章
热门专题
