尝试将数据插入 Kubernetes 上的 CockroachDB 时出错

Getting error when trying to INSERT data into CockroachDB on Kubernetes

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

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

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

在我尝试调试这个问题后,我知道问题的原因是我用来将数据插入 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 (,)"
    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 服务。

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

# 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 上以使其无效。如果不知道如何使用内置的 kubernetes 自我发现,我邀请您遵循这个。 https://www.youtube.com/watch?v=fvpq4jqtuZ8&list=PLIivdWyY5sqL3xfXz5xJvwzFW_tlQB_GB&index=7&t=0s