我有一个简单的golang应用程序,它与postgres数据库对话。 posrgres容器按预期工作,但是我的golang应用程序未启动。
在我的config.go中,环境变量指定为

type Config struct {
    Port        uint   `env:"PORT" envDefault:"8000"`
    PostgresURL string `env:"POSTGRES_URL" envDefault:"postgres://user:[email protected]/simple-service"`
}
我的golang应用程序服务和部署yaml如下所示:
apiVersion: v1
kind: Service
metadata:
  name: simple-service-webapp-service
  labels:
    app: simple-service-webapp
spec:
  ports:
  - port: 8080
    name: http
  selector:
    app: simple-service-webapp
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: simple-service-webapp-v1
  labels:
    version: v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: simple-service-webapp
      version: v1
  template:
    metadata:
      labels:
        app: simple-service-webapp
        version: v1
    spec:
      containers:
      - name: simple-service-webapp
        image: docker.io/225517/simple-service-webapp:v1
        resources:
          requests:
            cpu: 100m
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
        env:
          - name: POSTGRES_HOST
            value: postgresdb
          - name: POSTGRES_PORT
            value: "5432"
          - name: POSTGRES_DB
            value: simple-service
          - name: POSTGRES_USER
            value: user
          - name: POSTGRES_PASSWORD
            value: pass
        readinessProbe:
            httpGet:
                path: /live
                port: 8080
---
以下是Postgres服务的服务和部署yaml文件
apiVersion: v1
kind: Service
metadata:
  name: postgresdb
  labels:
    app: postgresdb
spec:
  ports:
  - port: 5432
    name: tcp
  selector:
    app: postgresdb
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgresdb-v1
  labels:
    app: postgresdb
    version: v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgresdb
      version: v1
  template:
    metadata:
      labels:
        app: postgresdb
        version: v1
    spec:
      containers:
      - name: postgresdb
        image: postgres
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 5432
        env:
          - name: POSTGRES_DB
            value: simple-service
          - name: POSTGRES_USER
            value: user
          - name: POSTGRES_PASSWORD
            value: pass
        readinessProbe:
          exec:
            command: ["psql", "-W", "pass", "-U", "user", "-d", "simple-service", "-c", "SELECT 1"]
          initialDelaySeconds: 15
          timeoutSeconds: 2
        livenessProbe:
          exec:
            command: ["psql", "-W", "pass", "-U", "user", "-d", "simple-service", "-c", "SELECT 1"]
          initialDelaySeconds: 45
          timeoutSeconds: 2
---
我需要在config.go中进行任何更改吗?您能帮忙吗,谢谢!

最佳答案

这是一个基于评论信息的社区Wiki答案,请随时对其进行扩展。我将其放置在社区中以提高知名度。
通过设置适当的postgres DB端口(5432)并重写config来解决问题,以便在部署上寻找正确的变量集。

关于docker - Kubernetes:Golang应用程序容器无法启动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63114925/

10-10 18:03