Loading
Loading...

Kubernetes 部署代码仓库 Gitlab

系统环境:

  • Gitlab 版本:13.6.2
  • 系统版本:CentOS 7.9
  • Docker 版本:19.03.13
  • Kubernetes 版本:1.20.1

参考地址:

如果本博文对你有帮助,别忘了 github 给颗星哦~

一、部署组件安排

这里一共需要部署三个组件,提前对所需部署的组件进行部署安排,如下:

组件名称存储位置存储分配资源分配
Redis/nfs/git/redis5GB1C & 2G
Postgresql/nfs/git/postgresql20GB2C & 2G
Gitlab/nfs/git/gitlab100GB4C & 8G

二、部署 Redis

1、创建存储资源

创建 PV、PVC 资源 yaml 文件 redis-storage.yaml

## PV
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis
labels:
app: redis
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
mountOptions:
- hard
- nfsvers=4.1
nfs:
server: 192.168.2.11
path: /nfs/gitlab/redis
---
## PVC
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: redis
spec:
resources:
requests:
storage: 5Gi
accessModes:
- ReadWriteOnce
selector:
matchLabels:
app: redis

执行创建 PV、PVC 命令

  • -n:指定部署应用的 Namespace 命名空间。
Terminal window
$ kubectl create -f redis-storage.yaml -n devops

2、部署 Redis

创建 Redis 部署文件 redis-deploy.yaml

## Service
kind: Service
apiVersion: v1
metadata:
name: gitlab-redis
labels:
name: gitlab-redis
spec:
type: ClusterIP
ports:
- name: redis
protocol: TCP
port: 6379
targetPort: redis
selector:
name: gitlab-redis
---
## Deployment
kind: Deployment
apiVersion: apps/v1
metadata:
name: gitlab-redis
labels:
name: gitlab-redis
spec:
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 组件

Terminal window
$ kubectl create -f redis-deploy.yaml -n devops

三、部署 PostgraSql

1、创建存储资源

创建 PV、PVC 资源 yaml 文件 postgresql-storage.yaml

## PV
apiVersion: v1
kind: PersistentVolume
metadata:
name: postgresql
labels:
app: postgresql
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
mountOptions:
- hard
- nfsvers=4.1
nfs:
server: 192.168.2.11
path: /nfs/gitlab/postgresql
---
## PVC
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: postgresql
spec:
resources:
requests:
storage: 20Gi
accessModes:
- ReadWriteOnce
selector:
matchLabels:
app: postgresql

执行创建 PV、PVC 命令

Terminal window
$ kubectl create -f postgresql-storage.yaml -n devops

2、创建 Postgresql

创建部署文件 postgresql-deploy.yaml

## Service
kind: Service
apiVersion: v1
metadata:
name: gitlab-postgresql
labels:
name: gitlab-postgresql
spec:
ports:
- name: postgres
protocol: TCP
port: 5432
targetPort: postgres
selector:
name: postgresql
type: ClusterIP
---
## Deployment
kind: Deployment
apiVersion: apps/v1
metadata:
name: postgresql
labels:
name: postgresql
spec:
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 数据库

Terminal window
$ kubectl create -f postgresql-deploy.yaml -n devops

四、部署 Gitlab

1、创建存储资源

创建部署文件 gitlab-storage.yaml

## PV
apiVersion: v1
kind: PersistentVolume
metadata:
name: gitlab
labels:
app: gitlab
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
mountOptions:
- hard
- nfsvers=4.1
nfs:
server: 192.168.2.11
path: /nfs/gitlab/git
---
## PVC
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: gitlab
spec:
resources:
requests:
storage: 100Gi
accessModes:
- ReadWriteOnce
selector:
matchLabels:
app: gitlab

执行创建 PV、PVC 命令

Terminal window
$ kubectl create -f redis-storage.yaml -n devops

2、创建 Gitlab 部署文件

创建部署文件 gitlab-deploy.yaml

## Service
kind: Service
apiVersion: v1
metadata:
name: gitlab
labels:
name: gitlab
spec:
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
---
## Deployment
kind: Deployment
apiVersion: apps/v1
metadata:
name: gitlab
labels:
name: gitlab
spec:
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_TIMEZONEUTC指定时区
GITLAB_SECRETS_DB_KEY_BASE-用于加密数据库中的CI机密变量以及导入凭据。如果丢失或旋转了此机密,则将无法使用现有的CI机密。
GITLAB_SECRETS_SECRET_KEY_BASE-用于密码重置链接和其他“标准”身份验证功能。如果丢失或旋转了此机密,电子邮件中的密码重置令牌将重置。
GITLAB_SECRETS_OTP_KEY_BASE-用于加密数据库中的2FA机密。如果您丢失或旋转了此机密,则您的所有用户都将无法使用 2FA 登录。
GITLAB_ROOT_PASSWORD5iveL!fe指定 root 用户在首次运行时的密码。(注意:GitLab 要求长度至少为8个字符)。
GITLAB_ROOT_EMAILadmin@example.com!fe指定 root 用户在首次运行时的电子邮件。
GITLAB_HOSTlocalhost指定 GitLab 服务器的主机名,默认为 localhost,修改此参数可用配置 Gitlab 库中的克隆地址。
GITLAB_PORT80指定 GitLab 服务器的端口号,修改此参数可用配置 Gitlab 库中的克隆地址的端口号。
GITLAB_SSH_PORT$GITLAB_SSH_LISTEN_PORT指定 ssh 端口号。
GITLAB_NOTIFY_ON_BROKEN_BUILDStrue启用或禁用通知的电子邮件。
GITLAB_NOTIFY_PUSHERtrue将推送程序添加到构建通知电子邮件的收件人列表中。
GITLAB_NOTIFY_PUSHERfalse将推送程序添加到构建通知电子邮件的收件人列表中。
DB_TYPEpostgres指定数据库类型。
DB_HOSTlocalhost指定数据库主机地址(k8s service地址)。
DB_PORT5432指定数据库服务器端口。
DB_USERroot指定数据库用户名。
DB_PASS-指定数据库密码。
DB_NAMEgitlabhq_production指定数据库名。
REDIS_HOSTlocalhost指定 Redis 的主机地址。
REDIS_PORT6379指定 Redis 端口。

详情可查看该镜像的 Github 文档:https://github.com/sameersbn/docker-gitlab

执行命令部署 Gitlab 组件

$ kubectl create -f gitlab-deploy.yaml -n devops

五、访问 Gitlab

上面已经成功配置了 Gitlab,其中 Servcie 配置的 NodePort31001,所以,这里我们可以通过 Kubernetes 集群的 IP+NodePort 端口对服务进行访问。例如,本人这里 Kubernetes 集群中一个节点 IP192.168.2.11,可以输入地址 192.168.2.11:31001 访问 Gitlab 主页,打开后可以看到如下:

Gitlla 提供了默认的管理员用户 root,密码在部署 Gitlab 的 yaml 文件的环境变量中进行了定义:

  • 用户名: root
  • 密码: admin@mydlq

输入用户名/密码后我们就可与成功进入 Gitlab 界面。

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

---END---
如果本文对你有帮助,可以关注我的公众号 "小豆丁技术栈" 了解最新动态,顺便也请帮忙 Github 点颗星哦,感谢~

本文作者:超级小豆丁 @ 小豆丁技术栈

本文链接:http://www.mydlq.club/article/101/

本文标题:Kubernetes 部署代码仓库 Gitlab

本文版权:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!