【问题标题】:Go Microservices with Ambassador API Gateway带大使 API 网关的 Go 微服务
【发布时间】:2019-08-07 22:41:00
【问题描述】:

我在让 Ambassador 正常工作时遇到了一些问题。我是 Kubernetes 新手,只是自学。

我已经成功地完成了大使提供的演示材料 - 例如 /httpbin/ 端点工作正常,但是当我尝试部署 Go 服务时它崩溃了。

当点击“qotm”端点时,页面是响应:

upstream request timeout

Pod 状态:

CrashLoopBackOff

根据我的研究,这似乎与未正确配置 yaml 文件有关,但我正在努力寻找与此用例相关的任何文档。

我的集群在 AWS EKS 上运行,并且正在将图像推送到 AWS ECR。

main.go:

package main

import (
    "fmt"
    "net/http"
    "os"
)

func main() {
    var PORT string
    if PORT = os.Getenv("PORT"); PORT == "" {
        PORT = "3001"
    }
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello World from path: %s\n", r.URL.Path)
    })
    http.ListenAndServe(":" + PORT, nil)
}

Dockerfile:

FROM golang:alpine
ADD ./src /go/src/app
WORKDIR /go/src/app
EXPOSE 3001
ENV PORT=3001
CMD ["go", "run", "main.go"]

test.yaml:

apiVersion: v1
kind: Service
metadata:
  name: qotm
  annotations:
    getambassador.io/config: |
      ---
      apiVersion: ambassador/v1
      kind:  Mapping
      name:  qotm_mapping
      prefix: /qotm/
      service: qotm
spec:
  selector:
    app: qotm
  ports:
    - port: 80
      name: http-qotm
      targetPort: http-api
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: qotm
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: qotm
    spec:
      containers:
        - name: qotm
          image: ||REMOVED||
          ports:
            - name: http-api
              containerPort: 3001
          readinessProbe:
            httpGet:
              path: /health
              port: 5000
            initialDelaySeconds: 30
            periodSeconds: 3
          resources:
            limits:
              cpu: "0.1"
              memory: 100Mi

吊舱描述:

Name:               qotm-7b9bf4d499-v9nxq
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               ip-192-168-89-69.eu-west-1.compute.internal/192.168.89.69
Start Time:         Sun, 17 Mar 2019 17:19:50 +0000
Labels:             app=qotm
                    pod-template-hash=3656908055
Annotations:        <none>
Status:             Running
IP:                 192.168.113.23
Controlled By:      ReplicaSet/qotm-7b9bf4d499
Containers:
  qotm:
    Container ID:   docker://5839996e48b252ac61f604d348a98c47c53225712efd503b7c3d7e4c736920c4
    Image:          IMGURL
    Image ID:       docker-pullable://IMGURL
    Port:           3001/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Sun, 17 Mar 2019 17:30:49 +0000
      Finished:     Sun, 17 Mar 2019 17:30:49 +0000
    Ready:          False
    Restart Count:  7
    Limits:
      cpu:     100m
      memory:  200Mi
    Requests:
      cpu:        100m
      memory:     200Mi
    Readiness:    http-get http://:3001/health delay=30s timeout=1s period=3s #success=1 #failure=3
    Environment:  <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-5bbxw (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  default-token-5bbxw:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-5bbxw
    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  12m                  default-scheduler                                     Successfully assigned default/qotm-7b9bf4d499-v9nxq to ip-192-168-89-69.eu-west-1.compute.internal
  Normal   Pulled     10m (x5 over 12m)    kubelet, ip-192-168-89-69.eu-west-1.compute.internal  Container image "IMGURL" already present on machine
  Normal   Created    10m (x5 over 12m)    kubelet, ip-192-168-89-69.eu-west-1.compute.internal  Created container
  Normal   Started    10m (x5 over 11m)    kubelet, ip-192-168-89-69.eu-west-1.compute.internal  Started container
  Warning  BackOff    115s (x47 over 11m)  kubelet, ip-192-168-89-69.eu-west-1.compute.internal  Back-off restarting failed container

【问题讨论】:

  • 可以添加失败容器的日志吗?
  • 已修改原帖。谢谢
  • 这看起来像 kubectl describe,你能试试 - kubectl logs
  • 我在绑定时遇到此错误,任何想法:2019/03/17 12:14:02 listen tcp :80: bind: permission denied
  • 您是否尝试将 EXPOSE 3001 添加到您的 Dockerfile 中?

标签: docker go kubernetes yaml amazon-eks


【解决方案1】:

在您的 kubernetes 部署文件中,您在端口 5000 上公开了一个就绪探测,而您的应用程序在端口 3001 上公开,而且在运行容器几次时,我得到了 OOMKilled,因此增加了内存限制。无论如何,下面的部署文件应该可以正常工作。

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: qotm
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: qotm
    spec:
      containers:
        - name: qotm
          image: <YOUR_IMAGE>
          imagePullPolicy: Always
          ports:
            - name: http-api
              containerPort: 3001
          readinessProbe:
            httpGet:
              path: /health
              port: 3001
            initialDelaySeconds: 30
            periodSeconds: 3
          resources:
            limits:
              cpu: "0.1"
              memory: 200Mi

【讨论】:

  • 我在 minikube 上试过这个并没有遇到任何问题,你能使用kubectl describe pod 命令检查事件的详细信息吗,它可能会提供一些关于它为什么会进入 CrashLoopBackOff 的见解
  • 我刚刚意识到我在部署您的更改时犯了一个错误!现在一切正常。非常感谢。
猜你喜欢
  • 2016-01-14
  • 2018-04-16
  • 2018-10-23
  • 2019-04-06
  • 1970-01-01
  • 2020-07-24
  • 1970-01-01
  • 2020-05-05
  • 2018-01-25
相关资源
最近更新 更多