【问题标题】:Getting error when trying to INSERT data into CockroachDB on Kubernetes尝试在 Kubernetes 上将数据插入 CockroachDB 时出错
【发布时间】:2020-11-05 07:05:22
【问题描述】:

我正在为我的网站创建上传文件功能。当用户点击上传时,我的网页将调用 API,该 API 具有将文件上传到我的 Kubernetes 上的 minIO 节点并将该文件的元数据存储到我的 Kubernetes 上的 cockroachDB 节点的功能

问题是当我在本地环境中测试它时它工作正常:

  • (网址:http://localhost:5000,API 网址:http://localhost:8080/upload

但是当我创建 pod 并在 Kubernetes 上运行它时会导致错误 [503 service unavailable]

  • (网址:https://[myWebName].com,API 网址:https://[myWebName].com/upload

在我尝试调试此问题后,我知道问题的原因是我用于将数据插入 cockroachDB 的代码,但我不知道如何解决此问题,也不知道为什么它在我的本地环境,但是当它上传到 Kubernetes 时,这个函数会导致错误。

导致问题的函数:

func cockroachUpload(data Workspace ,w http.ResponseWriter){
    //Work Fine
    db, err := sql.Open("postgres",
        "postgresql://root@128.199.248.147:31037/goliath?ssl=true&sslmode=require&sslrootcert=certs/ca.crt&sslkey=certs/client.root.key&sslcert=certs/client.root.crt")
    if err != nil {
        w.Write([]byte(err.Error()))
        log.Fatal("error connecting to the database: ", err)
    }
    defer db.Close()

    //cause error
    query:="INSERT INTO workspace(name,permission) VALUES ($1,$2)"
    rows, err := db.Query(query,"test",true)

    if err != nil {
        log.Fatal(err)
    }
    defer rows.Close()
    fmt.Println("done workspace") 
}

PS:我使用 nodeport 连接到我的 Kubernetes 上的 minIO 和 CockroachDB 服务。

【问题讨论】:

  • 您能在某处分享您的 Kubernetes 清单吗?你能分享运行你的工作负载的 Pod 的日志吗? kubectl logs <pod-name>。谢谢
  • easy cake pal...您的 docker 容器不包含证书,并且永远不会上传文件。在本地你可以这样做..但是在集群容器上必须生成证书;)当使用缩小的 docker 镜像 sich 作为 alpine 等时会发生这种情况。这可以在你的 dockerfile 上解决...

标签: kubernetes https cockroachdb


【解决方案1】:

这在很大程度上是一个 Dockerfile 问题,我可以评估。当您使用来自供应商(例如 alpine)的收缩 docker 图像时,通常会发生这种情况。等等。这是因为在本地主机上您不需要评估证书,但在生产中,服务器将根据您的服务器评估证书。它不可用。对于这种情况,我们需要按照以下示例代码重新创建它。

# FROM golang:1.14.1 AS builder
FROM golang:alpine as builder

RUN apk update && apk add --no-cache git

# Download and install the latest release of dep
ADD https://github.com/golang/dep/releases/download/v0.4.1/dep-linux-amd64 /usr/bin/dep
RUN chmod +x /usr/bin/dep

# Copy the code from the host and compile it
WORKDIR $GOPATH/src/mycontainer
COPY Gopkg.toml Gopkg.lock ./
RUN dep ensure --vendor-only

COPY . ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /app .

# FROM scratch
FROM alpine:latest

RUN apk --no-cache add ca-certificates

COPY --from=builder /app ./
# COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
ENTRYPOINT ["./app"]
# Expose the application on port 9000
EXPOSE 9000

deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: production-deployment
  namespace: staging
  labels:
    app: staging-customer
spec:
  replicas: 2
  selector:
    matchLabels:
      app: staging-customer
  template:
    metadata:
      labels:
        app: staging-customer
    spec:
      containers:
        - image: registry.gitlab.com/customer:000001
          name: staging-customer
          imagePullPolicy: Always
          ports:
            - containerPort: 9000
              protocol: TCP

service.yml

apiVersion: v1
kind: Service
metadata:
  name: production-service
  namespace: staging
spec:
  type: NodePort
  selector:
    app: staging-customer
  ports:
  - name: http
    port: 80
    targetPort: 9000

还要检查您是否有与此部署入口类似的内容。

ingress.yml

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: production-ingress
  namespace: staging
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: upload.mydomain.com
      http:
        paths:
          - path: /
            backend:
              serviceName: production
              servicePort: 9000

顺便说一句。您可以使用 IP 地址将您的数据库映射到 kubernetes 以 void .. 如果不知道如何使用内置的 kubernetes 自我发现,我邀请您关注这个。 https://www.youtube.com/watch?v=fvpq4jqtuZ8&list=PLIivdWyY5sqL3xfXz5xJvwzFW_tlQB_GB&index=7&t=0s

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多