Gitlab部署

预备知识

如果你不知道Gitlab是做什么的,那么请参考下面链接(包括但不限于)进行学习:

仓库设置

添加choerodon chart仓库并同步

  1. helm repo add c7n https://openchart.choerodon.com.cn/choerodon/c7n/
  2. helm repo update

部署Gitlab

  • 创建参数配置文件 gitlab.yaml

    1. core:
    2. env:
    3. OAUTH_ENABLED: false
    4. OAUTH_AUTO_SIGN_IN_WITH_PROVIDER: "oauth2_generic"
    5. OAUTH_ALLOW_SSO: "'oauth2_generic'"
    6. OAUTH_BLOCK_AUTO_CREATED_USERS: false
    7. OAUTH_GENERIC_API_KEY: "gitlabhq"
    8. OAUTH_GENERIC_APP_SECRET: "gitlabhq"
    9. # choerodon api 访问地址
    10. OAUTH_GENERIC_SITE: "http://api.example.choerodon.io"
    11. OAUTH_GENERIC_USER_INFO_URL: "/oauth/api/user"
    12. OAUTH_GENERIC_AUTHORIZE_URL: "/oauth/oauth/authorize"
    13. OAUTH_GENERIC_TOKEN_URL: "/oauth/oauth/token"
    14. OAUTH_GENERIC_ROOT_PATH: "'userAuthentication','principal'"
    15. OAUTH_GENERIC_ID_PATH: "'userAuthentication','principal','userId'"
    16. OAUTH_GENERIC_USER_NICKNAME: "username"
    17. OAUTH_GENERIC_USER_NAME: "username"
    18. RACK_ATTACK_WHITELIST: 0.0.0.0/0
    19. redis:
    20. internal:
    21. password: password
    22. persistence:
    23. enabled: true
    24. persistentVolumeClaim:
    25. core:
    26. storageClass: nfs-provisioner
    27. redis:
    28. storageClass: nfs-provisioner
    29. database:
    30. storageClass: nfs-provisioner
    31. expose:
    32. ingress:
    33. host: "gitlab.example.choerodon.io"
    34. database:
    35. internal:
    36. password: "password"
  • 执行安装

    1. helm upgrade --install gitlab c7n/gitlab-ha \
    2. -f gitlab.yaml \
    3. --create-namespace \
    4. --version 0.2.2 \
    5. --namespace c7n-system
  • 参数

    参数含义默认值
    Expose
    expose.type暴露服务的方式:ingressClusterIPLoadBalancernodePortingress
    expose.tls.enabled是否开启 TLSfalse
    expose.ingress.hostGitlab 在 ingress 中的域名gitlab.cluster.local
    expose.ingress.annotationsingress 中使用的注解
    expose.clusterIP.nameClusterIP 服务名Release.Name-gitlab-core
    expose.clusterIP.ports.sshGitlab 监听的 service SSH 端口22
    expose.clusterIP.ports.httpGitlab 监听的 service HTTP 端口80
    expose.nodePort.nameNodePort 服务名Release.Name-gitlab-core
    expose.nodePort.ports.ssh.portGitlab 监听的 service SSH 端口22
    expose.nodePort.ports.ssh.nodePortGitlab 监听的 service SSH nodePort30022
    expose.nodePort.ports.http.portGitlab 监听的 service HTTP 端口80
    expose.nodePort.ports.http.nodePortGitlab 监听的 service HTTP nodePort30007
    expose.loadBalancer.nameLoadBalancer 服务名Release.Name-gitlab-core
    expose.loadBalancer.ports.sshGitlab 监听的 SSH 端口22
    expose.loadBalancer.ports.httpGitlab 监听的 HTTP 端口80
    Persistence
    persistence.enabled是否开启持久化存储persistence.enabled
    persistence.resourcePolicy将它设置成 keep 避免 helm delete 时删掉 PVC。keep
    persistence.persistentVolumeClaim.core.existingClaim使用已经绑定 PV 的PVC,你使用共享的 PVC 必须指定 subPath
    persistence.persistentVolumeClaim.core.storageClass指定 storageClass 提供数据卷。如果有默认的 storageClass 会直接使用它,设置 - 禁用动态配置
    persistence.persistentVolumeClaim.database.existingClaim使用已经绑定 PV 的PVC,你使用共享的 PVC 必须指定 subPath
    persistence.persistentVolumeClaim.database.storageClass指定 storageClass 提供数据卷。如果有默认的 storageClass 会直接使用它,设置 - 禁用动态配置
    persistence.persistentVolumeClaim.redis.existingClaim使用已经绑定 PV 的PVC,你使用共享的 PVC 必须指定 subPath
    persistence.persistentVolumeClaim.redis.storageClass指定 storageClass 提供数据卷。如果有默认的 storageClass 会直接使用它,设置 - 禁用动态配置
    General
    imagePullPolicy镜像拉取策略IfNotPresent
    Core
    core.image.repositorygitlab 镜像地址registry.cn-shanghai.aliyuncs.com/c7n/docker-gitlab
    core.image.tag镜像 tagv11.11.7
    core.env.GITLAB_HOSTGitlab 主机名localhost
    core.env.OAUTH_ENABLED是否启用 oauth 认证false
    core.env.OAUTH_GENERIC_API_KEYoauth 客户端ID
    core.env.OAUTH_GENERIC_APP_SECREToauth 客户端密钥
    core.env.OAUTH_GENERIC_SITEGitlab 域名
    core.env.RACK_ATTACK_WHITELISTGitlab 并发限制白名单0.0.0.0/0
    Database
    database.type如果使用外部数据库,设置为 externalinternal
    database.internal.password数据库密码changeit
    database.external.adapter外部数据库适配器postgresql
    database.external.host外部数据主机名192.168.0.1
    database.external.port外部数据库端口5432
    database.external.username外部数据库用户
    database.external.password外部数据库密码
    database.external.databaseNameGitlab 数据的数据库gitlabhq_production
    Redis
    redis.type如果使用外部数据库,设置为 externalinternal
    redis.internal.passwordRedis密码
    redis.external.host外部redis主机名192.168.0.2
    redis.external.port外部redis端口6379

验证部署

  • 访问设置的Gitlab域名出现以下界面即部署成功

    Gitlab部署 - 图1

配置Choerodon Oauth认证

  • 以下操作须将Choerodon搭建完成后再继续进行,若未搭建,请跳过。
  • 配置Choerodon的Oauth认证后Gitlab的root用户是无法再通过Gitlab自有的界面进行登录的了。

如标题所描述,进行此步配置就是使用Choerodon的Oauth认证进行登陆Gitlab。这是Gitlab与Choerodon进行整合的必要操作。

更新Gitlab配置

  • 修改参数配置文件 gitlab.yaml

    1. core:
    2. env:
    3. OAUTH_ENABLED: true
    4. ...
  • 执行更新

    1. helm upgrade --install gitlab c7n/gitlab-ha \
    2. -f gitlab.yaml \
    3. --create-namespace \
    4. --version 0.2.2 \
    5. --namespace c7n-system

添加Gitlab Client

  • 在执行里面前请根据实际情况修改参数 web_server_redirect_uri
  • 编写参数配置文件 gitlab-client.yaml

    1. env:
    2. MYSQL_HOST: c7n-mysql.c7n-system.svc
    3. MYSQL_PASS: password
    4. MYSQL_PORT: 3306
    5. MYSQL_USER: root
    6. SQL_SCRIPT: |
    7. INSERT INTO hzero_platform.oauth_client (name,organization_id,resource_ids,secret,scope,authorized_grant_types,web_server_redirect_uri,access_token_validity,refresh_token_validity,additional_information,auto_approve,object_version_number,created_by,creation_date,last_updated_by,last_update_date,enabled_flag,time_zone)VALUES('gitlabhq',1,'default','gitlabhq','default','password,implicit,client_credentials,authorization_code,refresh_token','http://gitlab.example.choerodon.io',3600,3600,'{}','default',1,0,NOW(),0,NOW(),1,'GMT+8');
  • 部署服务

    1. helm upgrade --install gitlab-client c7n/mysql-client \
    2. -f gitlab-client.yaml \
    3. --version 0.1.0 \
    4. --namespace c7n-system

添加管理员用户关联

执行完添加管理员用户关联步骤前请不要去Gitlab界面进行登录操作

  • 执行下面语句进行关联:

    1. helm upgrade --install gitlab-user-identities c7n/postgresql-client \
    2. --set env.PG_HOST=gitlabhq-gitlab-database.c7n-system.svc \
    3. --set env.PG_PORT=5432 \
    4. --set env.PG_USER=gitlab \
    5. --set env.PG_PASS=changeit \
    6. --set env.PG_DBNAME=gitlabhq_production \
    7. --set env.SQL_SCRIPT="\
    8. INSERT INTO identities(extern_uid\, provider\, user_id\, created_at\, updated_at) \
    9. VALUES ('2'\, 'oauth2_generic'\, 1\, NOW()\, NOW());" \
    10. --version 0.1.0 \
    11. --namespace c7n-system

验证更新

  • 访问设置的Gitlab域名出现以下界面即更新成功

    Gitlab部署 - 图2