Kubernetes 部署代码仓库 Gitlab
系统环境:
- Gitlab 版本:13.6.2
- 系统版本:CentOS 7.9
- Docker 版本:19.03.13
- Kubernetes 版本:1.20.1
参考地址:
如果本博文对你有帮助,别忘了 github 给颗星哦~
一、部署组件安排
这里一共需要部署三个组件,提前对所需部署的组件进行部署安排,如下:
| 组件名称 | 存储位置 | 存储分配 | 资源分配 |
|---|---|---|---|
| Redis | /nfs/git/redis | 5GB | 1C & 2G |
| Postgresql | /nfs/git/postgresql | 20GB | 2C & 2G |
| Gitlab | /nfs/git/gitlab | 100GB | 4C & 8G |
二、部署 Redis
1、创建存储资源
创建 PV、PVC 资源 yaml 文件 redis-storage.yaml
## PVapiVersion: v1kind: PersistentVolumemetadata: name: redis labels: app: redisspec: capacity: storage: 5Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain mountOptions: - hard - nfsvers=4.1 nfs: server: 192.168.2.11 path: /nfs/gitlab/redis---## PVCkind: PersistentVolumeClaimapiVersion: v1metadata: name: redisspec: resources: requests: storage: 5Gi accessModes: - ReadWriteOnce selector: matchLabels: app: redis执行创建 PV、PVC 命令
- -n:指定部署应用的 Namespace 命名空间。
$ kubectl create -f redis-storage.yaml -n devops2、部署 Redis
创建 Redis 部署文件 redis-deploy.yaml
## Servicekind: ServiceapiVersion: v1metadata: name: gitlab-redis labels: name: gitlab-redisspec: type: ClusterIP ports: - name: redis protocol: TCP port: 6379 targetPort: redis selector: name: gitlab-redis---## Deploymentkind: DeploymentapiVersion: apps/v1metadata: name: gitlab-redis labels: name: gitlab-redisspec: replicas: 1 selector: matchLabels: name: gitlab-redis template: metadata: name: gitlab-redis labels: name: gitlab-redis spec: containers: - name: gitlab-redis image: 'sameersbn/redis:4.0.9-3' ports: - name: redis containerPort: 6379 protocol: TCP resources: limits: cpu: 1000m memory: 2Gi requests: cpu: 1000m memory: 2Gi volumeMounts: - name: data mountPath: /var/lib/redis livenessProbe: exec: command: - redis-cli - ping initialDelaySeconds: 5 timeoutSeconds: 5 periodSeconds: 10 successThreshold: 1 failureThreshold: 3 readinessProbe: exec: command: - redis-cli - ping initialDelaySeconds: 5 timeoutSeconds: 5 periodSeconds: 10 successThreshold: 1 failureThreshold: 3 volumes: - name: data persistentVolumeClaim: claimName: redis执行命令部署 Redis 组件
$ kubectl create -f redis-deploy.yaml -n devops三、部署 PostgraSql
1、创建存储资源
创建 PV、PVC 资源 yaml 文件 postgresql-storage.yaml
## PVapiVersion: v1kind: PersistentVolumemetadata: name: postgresql labels: app: postgresqlspec: capacity: storage: 20Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain mountOptions: - hard - nfsvers=4.1 nfs: server: 192.168.2.11 path: /nfs/gitlab/postgresql---## PVCkind: PersistentVolumeClaimapiVersion: v1metadata: name: postgresqlspec: resources: requests: storage: 20Gi accessModes: - ReadWriteOnce selector: matchLabels: app: postgresql执行创建 PV、PVC 命令
$ kubectl create -f postgresql-storage.yaml -n devops2、创建 Postgresql
创建部署文件 postgresql-deploy.yaml
## Servicekind: ServiceapiVersion: v1metadata: name: gitlab-postgresql labels: name: gitlab-postgresqlspec: ports: - name: postgres protocol: TCP port: 5432 targetPort: postgres selector: name: postgresql type: ClusterIP---## Deploymentkind: DeploymentapiVersion: apps/v1metadata: name: postgresql labels: name: postgresqlspec: replicas: 1 selector: matchLabels: name: postgresql template: metadata: name: postgresql labels: name: postgresql spec: containers: - name: postgresql image: sameersbn/postgresql:12-20200524 ports: - name: postgres containerPort: 5432 env: - name: DB_USER value: gitlab - name: DB_PASS value: admin@mydlq - name: DB_NAME value: gitlabhq_production - name: DB_EXTENSION value: 'pg_trgm,btree_gist' resources: requests: cpu: 2 memory: 2Gi limits: cpu: 2 memory: 2Gi livenessProbe: exec: command: ["pg_isready","-h","localhost","-U","postgres"] initialDelaySeconds: 30 timeoutSeconds: 5 periodSeconds: 10 successThreshold: 1 failureThreshold: 3 readinessProbe: exec: command: ["pg_isready","-h","localhost","-U","postgres"] initialDelaySeconds: 5 timeoutSeconds: 1 periodSeconds: 10 successThreshold: 1 failureThreshold: 3 volumeMounts: - name: data mountPath: /var/lib/postgresql volumes: - name: data persistentVolumeClaim: claimName: postgresql变量说明:
| 参数名称 | 默认值 | 描述 |
|---|---|---|
| DB_USER | - | 创建一个数据库用户 |
| DB_PASS | - | 指定创建的用户的密码 |
| DB_NAME | - | 创建一个数据库并指定库名 |
| DB_EXTENSION | - | 指定安装的扩展包 |
详情可查看该镜像的 Github 文档:https://github.com/sameersbn/docker-postgresql
执行命令部署 Postgresql 数据库
$ kubectl create -f postgresql-deploy.yaml -n devops四、部署 Gitlab
1、创建存储资源
创建部署文件 gitlab-storage.yaml
## PVapiVersion: v1kind: PersistentVolumemetadata: name: gitlab labels: app: gitlabspec: capacity: storage: 100Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain mountOptions: - hard - nfsvers=4.1 nfs: server: 192.168.2.11 path: /nfs/gitlab/git---## PVCkind: PersistentVolumeClaimapiVersion: v1metadata: name: gitlabspec: resources: requests: storage: 100Gi accessModes: - ReadWriteOnce selector: matchLabels: app: gitlab执行创建 PV、PVC 命令
$ kubectl create -f redis-storage.yaml -n devops2、创建 Gitlab 部署文件
创建部署文件 gitlab-deploy.yaml
## Servicekind: ServiceapiVersion: v1metadata: name: gitlab labels: name: gitlabspec: ports: - name: http protocol: TCP port: 80 targetPort: http nodePort: 31001 - name: ssh protocol: TCP port: 22 targetPort: ssh nodePort: 31002 selector: name: gitlab type: NodePort---## Deploymentkind: DeploymentapiVersion: apps/v1metadata: name: gitlab labels: name: gitlabspec: replicas: 1 selector: matchLabels: name: gitlab template: metadata: name: gitlab labels: name: gitlab spec: containers: - name: gitlab image: 'sameersbn/gitlab:13.6.2' ports: - name: ssh containerPort: 22 - name: http containerPort: 80 - name: https containerPort: 443 env: - name: TZ value: Asia/Shanghai - name: GITLAB_TIMEZONE value: Beijing - name: GITLAB_SECRETS_DB_KEY_BASE value: long-and-random-alpha-numeric-string - name: GITLAB_SECRETS_SECRET_KEY_BASE value: long-and-random-alpha-numeric-string - name: GITLAB_SECRETS_OTP_KEY_BASE value: long-and-random-alpha-numeric-string - name: GITLAB_ROOT_PASSWORD value: admin@mydlq - name: GITLAB_ROOT_EMAIL value: mynamedlq@163.com - name: GITLAB_HOST value: '192.168.2.11' - name: GITLAB_PORT value: '31001' - name: GITLAB_PORT value: '80' - name: GITLAB_SSH_PORT value: '22' - name: GITLAB_NOTIFY_ON_BROKEN_BUILDS value: 'true' - name: GITLAB_NOTIFY_PUSHER value: 'false' - name: DB_TYPE value: postgres - name: DB_HOST value: gitlab-postgresql - name: DB_PORT value: '5432' - name: DB_USER value: gitlab - name: DB_PASS value: admin@mydlq - name: DB_NAME value: gitlabhq_production - name: REDIS_HOST value: gitlab-redis - name: REDIS_PORT value: '6379' resources: requests: cpu: 4 memory: 8Gi limits: cpu: 4 memory: 8Gi livenessProbe: httpGet: path: / port: 80 scheme: HTTP initialDelaySeconds: 300 timeoutSeconds: 5 periodSeconds: 10 successThreshold: 1 failureThreshold: 3 readinessProbe: httpGet: path: / port: 80 scheme: HTTP initialDelaySeconds: 5 timeoutSeconds: 30 periodSeconds: 10 successThreshold: 1 failureThreshold: 3 volumeMounts: - name: data mountPath: /home/git/data - name: localtime mountPath: /etc/localtime volumes: - name: data persistentVolumeClaim: claimName: gitlab - name: localtime hostPath: path: /etc/localtime参数说明:
| 参数名称 | 默认值 | 描述 |
|---|---|---|
| GITLAB_TIMEZONE | UTC | 指定时区 |
| GITLAB_SECRETS_DB_KEY_BASE | - | 用于加密数据库中的CI机密变量以及导入凭据。如果丢失或旋转了此机密,则将无法使用现有的CI机密。 |
| GITLAB_SECRETS_SECRET_KEY_BASE | - | 用于密码重置链接和其他“标准”身份验证功能。如果丢失或旋转了此机密,电子邮件中的密码重置令牌将重置。 |
| GITLAB_SECRETS_OTP_KEY_BASE | - | 用于加密数据库中的2FA机密。如果您丢失或旋转了此机密,则您的所有用户都将无法使用 2FA 登录。 |
| GITLAB_ROOT_PASSWORD | 5iveL!fe | 指定 root 用户在首次运行时的密码。(注意:GitLab 要求长度至少为8个字符)。 |
| GITLAB_ROOT_EMAIL | admin@example.com!fe | 指定 root 用户在首次运行时的电子邮件。 |
| GITLAB_HOST | localhost | 指定 GitLab 服务器的主机名,默认为 localhost,修改此参数可用配置 Gitlab 库中的克隆地址。 |
| GITLAB_PORT | 80 | 指定 GitLab 服务器的端口号,修改此参数可用配置 Gitlab 库中的克隆地址的端口号。 |
| GITLAB_SSH_PORT | $GITLAB_SSH_LISTEN_PORT | 指定 ssh 端口号。 |
| GITLAB_NOTIFY_ON_BROKEN_BUILDS | true | 启用或禁用通知的电子邮件。 |
| GITLAB_NOTIFY_PUSHER | true | 将推送程序添加到构建通知电子邮件的收件人列表中。 |
| GITLAB_NOTIFY_PUSHER | false | 将推送程序添加到构建通知电子邮件的收件人列表中。 |
| DB_TYPE | postgres | 指定数据库类型。 |
| DB_HOST | localhost | 指定数据库主机地址(k8s service地址)。 |
| DB_PORT | 5432 | 指定数据库服务器端口。 |
| DB_USER | root | 指定数据库用户名。 |
| DB_PASS | - | 指定数据库密码。 |
| DB_NAME | gitlabhq_production | 指定数据库名。 |
| REDIS_HOST | localhost | 指定 Redis 的主机地址。 |
| REDIS_PORT | 6379 | 指定 Redis 端口。 |
详情可查看该镜像的 Github 文档:https://github.com/sameersbn/docker-gitlab
执行命令部署 Gitlab 组件
$ kubectl create -f gitlab-deploy.yaml -n devops五、访问 Gitlab
上面已经成功配置了 Gitlab,其中 Servcie 配置的 NodePort 为 31001,所以,这里我们可以通过 Kubernetes 集群的 IP+NodePort 端口对服务进行访问。例如,本人这里 Kubernetes 集群中一个节点 IP 为 192.168.2.11,可以输入地址 192.168.2.11:31001 访问 Gitlab 主页,打开后可以看到如下:

Gitlla 提供了默认的管理员用户 root,密码在部署 Gitlab 的 yaml 文件的环境变量中进行了定义:
- 用户名: root
- 密码: admin@mydlq
输入用户名/密码后我们就可与成功进入 Gitlab 界面。

到此 Gitlab 已经部署完毕,那么该组件如何配置调整参数使其性能更优的任务就交给大家自信摸索了~
