天行健,君子以自强不息;地势坤,君子以厚德载物。
每个人都有惰性,但不断学习是好好生活的根本,共勉!
文章均为学习整理笔记,分享记录为主,如有错误请指正,共同学习进步。
在k8s中部署gitlab,然后使用gitlab来实现代码打包到打镜像再到使用镜像自动生成容器服务的过程
gitlab部署主要有三个部分,用到了reids、postgresql、gitlab,将三个应用配置好之后启动即可安装gitlab
安装持久化存储工具,可以使用nfs或ebs
安装后在配置持久化时可根据安装的持久化工具将storageClassName参数的值填充:
如果安装的是nfs则可以是
storageClassName: “managed-nfs-storage”
如果安装的是ebs则可以是
storageClassName: openebs-hostpath
以ebs为例
kubectl apply -f https://openebs.github.io/charts/openebs-operator.yaml
查看集群的StorageClass
kubectl get sc
设置openobs-hostpath为default
kubectl patch storageclass openebs-hostpath -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
在部署之前先创建一个namespace用于分类管理服务
创建一个名为gitlab-dev的命名空间
kubectl create namespace gitlab-dev
分别创建存储账号和密码的文件
账号文件username,用于存储账号信息
echo -n "gitlab-admin" > ./username
密码文件password,用于存储密码信息
echo -n "gitlab.123" > ./password
查看文件
ls
查看文件内容
cat ./username
cat ./password
secret对象生成
kubectl create secret generic git-user-pass --from-file=./username --from-file=./password -n gitlab-dev
查看secret
kubectl -n gitlab-dev get secret git-user-pass -o yaml
拓展:
如果创建错误或者想重新创建secret,则需先删除
kubectl delete secret git-user-pass -n gitlab-dev
创建gitlab-yaml文件夹用于存储gitlab相关的yaml部署文件
mkdir -p gitlab-yaml
为了方便管理以及后续修改更新文件,本篇对每个结构部分都创建一个yaml文件,且创建的文件都放在当前目录下的gitlab-yaml文件夹下,后续redis和gitlab的部分与此相同
参数:
pgs:Postgresql
dplm:Deployment
pvc:PersistentVolumeClaim
svc:Service
pgs-pvc.yaml
创建文件pgs-pvc.yaml
vim ./gitlab-yaml/pgs-pvc.yaml
将以下内容复制粘贴到文件中,其中name可自定义,namespace前面创建了gitlab-dev
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pgs-pvc
namespace: gitlab-dev
spec:
accessModes:
- ReadWriteOnce
storageClassName: openebs-hostpath
resources:
requests:
storage: 1Gi
查看文件内容
cat ./gitlab-yaml/pgs-pvc.yaml
根据文件来部署服务
kubectl apply -f ./gitlab-yaml/pgs-pvc.yaml
查看创建的服务
kubectl get pvc -n gitlab-dev pgs-pvc
注:如需删除创建的服务请参考:kubectl操作命令集合
pgs-dplm.yaml
创建文件pgs-dplm.yaml
vim ./gitlab-yaml/pgs-dplm.yaml
注:公有镜像可省略版本号,默认拉取最新版本。私有镜像必须加版本号,不然部署不成功,Pod状态显示Pending
将以下内容复制粘贴到文件中
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgresql
namespace: gitlab-dev
labels:
name: postgresql
spec:
replicas: 1
selector:
matchLabels:
name: postgresql
template:
metadata:
name: postgresql
labels:
name: postgresql
spec:
#nodeSelector:
#key: gitlab-dev
containers:
- name: postgresql
image: sameersbn/postgresql
imagePullPolicy: IfNotPresent
env:
- name: DB_USER
value: gitlab
- name: DB_PASS
value: passw0rd
- name: DB_NAME
value: gitlab_production
- name: DB_EXTENSION
value: pg_trgm
ports:
- name: postgres
containerPort: 5432
volumeMounts:
- mountPath: /var/lib/postgresql
name: data
livenessProbe:
exec:
command:
- pg_isready
- -h
- localhost
- -U
- postgres
initialDelaySeconds: 30
timeoutSeconds: 5
readinessProbe:
exec:
command:
- pg_isready
- -h
- localhost
- -U
- postgres
initialDelaySeconds: 5
timeoutSeconds: 1
volumes:
- name: data
persistentVolumeClaim:
claimName: pgs-pvc
注:nodeSelector的key的值就是namespace,最后的claimName的值是持久化配置文件的名称pgs-pvc
查看文件内容
cat ./gitlab-yaml/pgs-dplm.yaml
部署服务
kubectl apply -f ./gitlab-yaml/pgs-dplm.yaml
查看已创建的服务
kubectl get pod -n gitlab-dev
注:pod启动需要一点时间,开始状态为ContainerCreating,过一会才会显示Running
pgs-svc.yaml
创建文件pgs-svc.yaml
vim ./gitlab-yaml/pgs-svc.yaml
将以下内容复制粘贴到文件中
apiVersion: v1
kind: Service
metadata:
name: postgresql
namespace: gitlab-dev
labels:
name: postgresql
spec:
ports:
- name: postgres
port: 5432
targetPort: postgres
selector:
name: postgresql
查看文件内容
cat ./gitlab-yaml/pgs-svc.yaml
部署服务
kubectl apply -f ./gitlab-yaml/pgs-svc.yaml
查看已部署的服务
kubectl get svc -n gitlab-dev
数据缓存redis
redis-pvc.yaml
创建文件redis-pvc.yaml
vim ./gitlab-yaml/redis-pvc.yaml
将以下内容复制粘贴到文件中
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-pvc
namespace: gitlab-dev
spec:
accessModes:
- ReadWriteOnce
storageClassName: openebs-hostpath
resources:
requests:
storage: 1Gi
查看文件内容
cat ./gitlab-yaml/redis-pvc.yaml
部署服务
kubectl apply -f ./gitlab-yaml/redis-pvc.yaml
查看已部署的服务
kubectl get pvc -n gitlab-dev redis-pvc
redis-dplm.yaml
创建redis-dplm.yaml文件
vim ./gitlab-yaml/redis-dplm.yaml
注:公有镜像可省略版本号,默认拉取最新版本。私有镜像必须加版本号,不然部署不成功,Pod状态显示Pending
将以下内容复制粘贴到文件中
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: gitlab-dev
labels:
name: redis
spec:
replicas: 2
selector:
matchLabels:
name: redis
template:
metadata:
name: redis
labels:
name: redis
spec:
#nodeSelector:
#key: gitlab-dev
containers:
- name: redis
image: sameersbn/redis
imagePullPolicy: IfNotPresent
ports:
- name: redis
containerPort: 6379
volumeMounts:
- mountPath: /var/lib/redis
name: data
livenessProbe:
exec:
command:
- redis-cli
- ping
initialDelaySeconds: 30
timeoutSeconds: 5
readinessProbe:
exec:
command:
- redis-cli
- ping
initialDelaySeconds: 5
timeoutSeconds: 1
volumes:
- name: data
persistentVolumeClaim:
claimName: redis-pvc
查看文件内容
cat ./gitlab-yaml/redis-dplm.yaml
部署服务
kubectl apply -f ./gitlab-yaml/redis-dplm.yaml
查看已部署的服务
kubectl get pod -n gitlab-dev
因为文件中配置的副本数为2,故生成了两个redis的pod
redis-svc.yaml
创建文件redis-svc.yaml
vim ./gitlab-yaml/redis-svc.yaml
讲一下内容复制粘贴进文件中
apiVersion: v1
kind: Service
metadata:
name: redis-svc
namespace: gitlab-dev
labels:
name: redis-svc
spec:
ports:
- name: redis
port: 6379
targetPort: redis
selector:
name: redis
查看文件内容
cat ./gitlab-yaml/redis-svc.yaml
部署服务
kubectl apply -f ./gitlab-yaml/redis-svc.yaml
查看已部署的服务
kubectl get svc -n gitlab-dev redis-svc
gitlab
gitlab-pvc.yaml
创建文件gitlab-pvc.yaml
vim ./gitlab-yaml/gitlab-pvc.yaml
将以下内容复制粘贴到文件中
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: gitlab-pvc
namespace: gitlab-dev
spec:
accessModes:
- ReadWriteOnce
storageClassName: openebs-hostpath
resources:
requests:
storage: 5Gi
查看文件内容
cat ./gitlab-yaml/gitlab-pvc.yaml
部署服务
kubectl apply -f ./gitlab-yaml/gitlab-pvc.yaml
查看已部署的服务
kubectl get pvc -n gitlab-dev gitlab-pvc
gitlab-dplm.yaml
创建文件gitlab-dplm.yaml
vim ./gitlab-yaml/gitlab-dplm.yaml
注:公有镜像可省略版本号,默认拉取最新版本。私有镜像必须加版本号,不然部署不成功,Pod状态显示Pending
讲一下内容复制粘贴到文件中
apiVersion: apps/v1
kind: Deployment
metadata:
name: gitlab
namespace: gitlab-dev
labels:
name: gitlab
spec:
replicas: 1
selector:
matchLabels:
name: gitlab
template:
metadata:
name: gitlab
labels:
name: gitlab
spec:
#nodeSelector:
#key: gitlab-dev
containers:
- name: gitlab
image: sameersbn/gitlab
imagePullPolicy: IfNotPresent
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: admin321
valueFrom:
secretKeyRef:
name: git-user-pass
key: password
- name: GITLAB_ROOT_EMAIL
value: hslb@163.com
- name: GITLAB_HOST
value: gitlab.hslb.com
- name: GITLAB_PORT
value: "30021"
- name: GITLAB_SSH_PORT
value: "30022"
- name: GITLAB_NOTIFY_ON_BROKEN_BUILDS
value: "true"
- name: GITLAB_NOTIFY_PUSHER
value: "false"
- name: GITLAB_BACKUP_SCHEDULE
value: daily
- name: GITLAB_BACKUP_TIME
value: 01:00
- name: DB_TYPE
value: postgres
- name: DB_HOST
value: postgresql
- name: DB_PORT
value: "5432"
- name: DB_USER
value: gitlab
- name: DB_PASS
value: passw0rd
- name: DB_NAME
value: gitlab_production
- name: REDIS_HOST
value: redis
- name: REDIS_PORT
value: "6379"
ports:
- name: http
containerPort: 80
- name: ssh
containerPort: 22
volumeMounts:
- mountPath: /home/git/data
name: data
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 180
timeoutSeconds: 5
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
timeoutSeconds: 1
volumes:
- name: data
persistentVolumeClaim:
claimName: gitlab-pvc
GITLAB_ROOT_PASSWORD
密码部分,可以直接将值设为密码,这里从第二章中设置的密码文件中读取
GITLAB_ROOT_EMAIL
邮箱部分,自定义即可
GITLAB_HOST
主机地址,可自定义
查看文件内容
cat ./gitlab-yaml/gitlab-dplm.yaml
部署服务
kubectl apply -f ./gitlab-yaml/gitlab-dplm.yaml
查看已部署的服务
kubectl get pod -n gitlab-dev
gitlab-svc.yaml
创建文件gitlab-svc.yaml
vim ./gitlab-yaml/gitlab-svc.yaml
将以下内容复制粘贴到文件中
apiVersion: v1
kind: Service
metadata:
name: gitlab
namespace: gitlab-dev
labels:
name: gitlab
spec:
ports:
- name: http
port: 80
targetPort: http
nodePort: 30021
- name: ssh
port: 22
targetPort: ssh
nodePort: 30022
selector:
name: gitlab
type: NodePort
查看文件内容
cat ./gitlab-yaml/gitlab-svc.yaml
部署服务
kubectl apply -f ./gitlab-yaml/gitlab-svc.yaml
查看已部署的服务
kubectl get svc -n gitlab-dev
以上就是redis、postgresql、gitlab三个部分的部署,如果出现问题需要删除创建的服务,可参考以下命令
部署pvc
kubectl apply -f ./gitlab-yaml/pgs-pvc.yaml
kubectl apply -f ./gitlab-yaml/redis-pvc.yaml
kubectl apply -f ./gitlab-yaml/gitlab-pvc.yaml
查看pvc
kubectl get pvc -n gitlab-dev
删除指定pvc
kubectl delete pvc pgs-pvc -n gitlab-dev
删除全部pvc (bukeyong)
kubectl delete pvc -n gitlab-dev
部署pod
kubectl apply -f ./gitlab-yaml/pgs-dplm.yaml
kubectl apply -f ./gitlab-yaml/redis-dplm.yaml
kubectl apply -f ./gitlab-yaml/gitlab-dplm.yaml
查看pod
kubectl get pod -n gitlab-dev
删除无副本设置的pod
kubectl delete pod postgresql -n gitlab-dev
删除设置副本的pod
kubectl delete deployment postgresql -n gitlab-dev
部署svc
kubectl apply -f ./gitlab-yaml/pgs-svc.yaml
kubectl apply -f ./gitlab-yaml/redis-svc.yaml
kubectl apply -f ./gitlab-yaml/gitlab-svc.yaml
查看svc
kubectl get svc -n gitlab-dev
删除指定svc
kubectl delete svc postgresql -n gitlab-dev
删除全部svc
kubectl delete svc -n gitlab-dev
注:此注释可无视,由于操作是在阿里云服务器的运维管理浏览器窗口操作,复制粘贴功能有瑕疵导致内容格式会错乱,故安装了ossutil工具,在本地创建了文件后上传到oss然后再拉取到服务器指定位置(gitlab-yaml文件夹),如有需要可参考linux服务器中oss工具安装和上传下载文件
关于yaml文件中image参数的写法格式
https://blog.csdn.net/m0_46629123/article/details/124144654
官网的image可以省略tag
但是私有的image必须带上tag
感谢阅读,祝君暴富!