GitLab无法连接数据库

时间:2017-09-06 15:06:43

标签: postgresql docker kubernetes docker-compose gitlab

我正在尝试使用Gluster在Kubernetes中为GitLab,Redis和PostgreSQL使用单独的容器。我按照PostgreSQL,Redis和GitLab的顺序启动了三个容器。前两个工作正常,但是当我尝试启动GitLab时,它不知何故找不到数据库配置。

[root@cent-gluster-workstation ~]# kubectl logs gitlab-3743104516-plk3x -p
Initializing logdir...
Initializing datadir...
Installing configuration templates...
Configuring gitlab...
Configuring gitlab::database
ERROR:
  Please configure the database connection.
  Refer http://git.io/wkYhyA for more information.
  Cannot continue without a database. Aborting...

我确认我能够从GitLab pod中远程登录到5432端口的PostgreSQL pod。

root@gitlab-1182173148-t47fj:/home/git/gitlab# telnet postgresql 5432
Trying 10.42.154.36...
Connected to postgresql.default.svc.cluster.local.
Escape character is '^]'. 

GitLab和PostgreSQL的部署和服务配置:

[root@cent-gluster-workstation ~]# cat postgresql-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: postgresql
  name: postgresql
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: postgresql
    spec:
      containers:
      - env:
        - name: POSTGRES_DB
          value: gitlabhq_production
        - name: POSTGRES_PASSWORD
          value: password
        - name: POSTGRES_USER
          value: gitlab
        image: postgres:9.6.3
        name: postgresql
        volumeMounts:
        - name: gluster-vol1
          mountPath: /var/lib/postgresql
        resources: {}
      volumes:
      - name: gluster-vol1
        persistentVolumeClaim:
          claimName: gluster-dyn-pvc
status: {}


[root@cent-gluster-workstation ~]# cat postgresql-service.yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: postgresql
  name: postgresql
spec:
  clusterIP: None
  ports:
  - name: postgres
    port: 5432
    targetPort: 5432
  selector:
    io.kompose.service: postgresql
status:
  loadBalancer: {}


[root@cent-gluster-workstation ~]# cat gitlab-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: gitlab
  name: gitlab
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: gitlab
    spec:
      containers:
      - env:
        - name: GITLAB_PORT
          value: "80"
        - name: GITLAB_SECRETS_DB_KEY_BASE
          value: 110Ect4ClClGBH06RSEQm4uKCxbpCyPzxfdLORkkssrStDnrQgpSSV
        - name: GITLAB_SECRETS_OTP_KEY_BASE
          value: MWxqmqqQkG1EXz9iz7Up4tEGBIEtFjxr1rL1Ry1rWCTWBjw3qxtvOM
        - name: GITLAB_SECRETS_SECRET_KEY_BASE
          value: 2KOeHYHNXB4r2XDZzy3XVrH2r0t6j71MBDUmfx4FxRPqjI5KZ9j4Md
        - name: GITLAB_SSH_PORT
          value: "10022"
        image: sameersbn/gitlab:9.3.9
        name: gitlab
        volumeMounts:
        - name: gluster-vol1
          mountPath: /home/git/data
        ports:
        - containerPort: 443
        - containerPort: 80
        - containerPort: 22
        resources: {}
      volumes:
      - name: gluster-vol1
        persistentVolumeClaim:
          claimName: gluster-dyn-pvc
status: {}

当我使用这三个没有Kubernetes和Gluster的容器时,我可以按照this链接的快速入门部分中的步骤启动并运行GitLab。在这种情况下,我使用卷装载路径作为/srv/docker/gitlab/用于所有三个容器(GitLab,Redis和PostgreSQL)。

在当前的情况下,正如您在上面的部署和服务YAML中所看到的,我正在使用Gluster卷来映射容器路径。这与这个问题有什么关系,或者其他地方出了什么问题?

更新1:我已将服务端口名称从“无头”更改为上面postgresql-service.yaml中的“postgres”。当我尝试使用psql从应用程序窗格访问数据库时,它无法连接:

[rancher@node-c ~]$ docker exec -it 07a05cef7378 /bin/bash
groups: cannot find name for group ID 2000
root@gitlab-1182173148-b33z2:/home/git/gitlab# psql -U postgres -h 10.42.20.99
Password for user postgres:
root@gitlab-1182173148-b33z2:/home/git/gitlab# psql -U gitlab -h 10.42.20.99
Password for user gitlab:
psql: FATAL:  database "gitlab" does not exist

但是,如果我尝试从数据库窗格中连接到psql,我就能够这样做。

[root@node-c ~]# docker exec -it 4c968c09bccf /bin/bash
root@postgresql-1124339636-4377k:/# psql -U gitlab
psql: FATAL:  database "gitlab" does not exist
root@postgresql-1124339636-4377k:/# psql -U postgres
psql (9.6.3)
Type "help" for help.

postgres=# \l
                                      List of databases
        Name         |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
---------------------+----------+----------+------------+------------+-----------------------
 gitlabhq_production | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 postgres            | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 template0           | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
                     |          |          |            |            | postgres=CTc/postgres
 template1           | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
                     |          |          |            |            | postgres=CTc/postgres
(4 rows)

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 gitlab    | Superuser                                                  | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

我很困惑的一件事是在app和db pod上,当我尝试访问数据库时,不知道为什么Postgres将gitlab视为数据库,尽管我明确提供了-U在它之前。

1 个答案:

答案 0 :(得分:1)

  

当我尝试使用psql从应用程序窗格访问数据库时,它无法连接

这实际上是假的; 连接就好了,它未能做的是认证。我期望重新运行该命令,因为psql --host postgresql.default.svc.cluster.local -d gitlabhq_production -U gitlab会按你的意思行事。

我认为10.42.20.99 Pod IP,因为第二个代码段显示10.42.154.36作为服务IP。我之所以提到这一点是因为当然欢迎您使用PG的Pod IP进行测试,但我强烈建议不要使用除服务[主机名或IP]以外的任何内容,我的首选项主机名,因为它使连接字符串的推理更加容易。

话虽如此,仅根据您发布的代码段,DB_部署描述符as shown here

中缺少4 gitlab个环境变量

虽然您没有提出要求,但environment for postgresgitlab部署的环境保持同步是Kubernetes Secrets的完美案例,并与{{3}相结合机制。您可能怀疑,也可以方便地存储GITLAB_SECRETS_DB_KEY_BASE和朋友的价值

相关问题