单机k8s集群部署——gitlab篇

今天废话不多说,单刀直入

准备工作创建目录:

sudo mkdir -p /mnt/data/{postgresql,redis,gitlab/{data,config,logs}}
sudo chmod -R 777 /mnt/data  

1.先创建一个命名空间
kubectl create namespace gitlab
2.部署PostgreSQL数据库
vim postgresql.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: gitlab-postgresql
  namespace: gitlab
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gitlab-postgresql
  template:
    metadata:
      labels:
        app: gitlab-postgresql
    spec:
      containers:
      - name: postgresql
        image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/postgres:latest
        env:
        - name: POSTGRES_PASSWORD
          value: "gitlab-postgres"
        - name: POSTGRES_USER
          value: "gitlab"
        - name: POSTGRES_DB
          value: "gitlabhq_production"
        ports:
        - containerPort: 5432
        volumeMounts:
        - name: postgresql-data
          mountPath: /var/lib/postgresql/data
      volumes:
      - name: postgresql-data
        persistentVolumeClaim:
          claimName: postgresql-pvc

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgresql-pvc
  namespace: gitlab
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 8Gi

---
apiVersion: v1
kind: Service
metadata:
  name: gitlab-postgresql
  namespace: gitlab
spec:
  ports:
  - port: 5432
  selector:
    app: gitlab-postgresql

3.部署Redis
vim redis.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: gitlab-redis
  namespace: gitlab
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gitlab-redis
  template:
    metadata:
      labels:
        app: gitlab-redis
    spec:
      containers:
      - name: redis
        image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/redis:6.2.19
        ports:
        - containerPort: 6379
        volumeMounts:
        - name: redis-data
          mountPath: /data
      volumes:
      - name: redis-data
        persistentVolumeClaim:
          claimName: redis-pvc

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-pvc
  namespace: gitlab
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

---
apiVersion: v1
kind: Service
metadata:
  name: gitlab-redis
  namespace: gitlab
spec:
  ports:
  - port: 6379
  selector:
    app: gitlab-redis

4.创建pv文件
vim postgresql-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgresql-pv
  namespace: gitlab
spec:
  capacity:
    storage: 8Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage 
  hostPath:
    path: "/mnt/data/postgresql"

vim redis-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-pv
  namespace: gitlab
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  hostPath:
    path: "/mnt/data/redis"

vim gitlab-data-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: gitlab-data-pv
  namespace: gitlab
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  hostPath:
    path: "/mnt/data/gitlab/data"

vim gitlab-config-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: gitlab-config-pv
  namespace: gitlab
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  hostPath:
    path: "/mnt/data/gitlab/config"

vim gitlab-logs-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: gitlab-logs-pv
  namespace: gitlab
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  hostPath:
    path: "/mnt/data/gitlab/logs"

5.vim gitlab.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: gitlab
  namespace: gitlab
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gitlab
  template:
    metadata:
      labels:
        app: gitlab
    spec:
      containers:
      - name: gitlab
        image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/gitlab/gitlab-ce:latest
        env:
        - name: GITLAB_OMNIBUS_CONFIG
          value: |
            external_url 'http://gitlab.local'
            gitlab_rails['db_adapter'] = 'postgresql'
            gitlab_rails['db_encoding'] = 'unicode'
            gitlab_rails['db_host'] = 'gitlab-postgresql'
            gitlab_rails['db_port'] = '5432'
            gitlab_rails['db_username'] = 'gitlab'
            gitlab_rails['db_password'] = 'gitlab-postgres'
            gitlab_rails['redis_host'] = 'gitlab-redis'
            gitlab_rails['redis_port'] = '6379'
            gitlab_rails['gitlab_shell_ssh_port'] = 2222
        ports:
        - containerPort: 80
        - containerPort: 443
        - containerPort: 2222
        resources:
          requests:
            cpu: "1000m"       # 1 CPU core
            memory: "2Gi"       # 2GB RAM
          limits:
            cpu: "2000m"        # 2 CPU cores
            memory: "4Gi"       # 4GB RAM
        volumeMounts:
        - name: gitlab-data
          mountPath: /var/opt/gitlab
        - name: gitlab-config
          mountPath: /etc/gitlab
        - name: gitlab-logs
          mountPath: /var/log/gitlab
      volumes:
      - name: gitlab-data
        persistentVolumeClaim:
          claimName: gitlab-data-pvc
      - name: gitlab-config
        persistentVolumeClaim:
          claimName: gitlab-config-pvc
      - name: gitlab-logs
        persistentVolumeClaim:
          claimName: gitlab-logs-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: gitlab-data-pvc
  namespace: gitlab
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: gitlab-config-pvc
  namespace: gitlab
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: gitlab-logs-pvc
  namespace: gitlab
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
  name: gitlab
  namespace: gitlab
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    targetPort: 80
    nodePort: 30060
  - name: https
    port: 443
    targetPort: 443
    nodePort: 30443
  - name: ssh
    port: 2222
    targetPort: 2222
    nodePort: 30222
  selector:
    app: gitlab

5.部署文件

kubectl apply -f postgresql-pv.yaml
kubectl apply -f redis-pv.yaml
kubectl apply -f gitlab-data-pv.yaml
kubectl apply -f gitlab-config-pv.yaml
kubectl apply -f gitlab-logs-pv.yaml
kubectl apply -f postgresql.yaml
kubectl apply -f redis.yaml
kubectl apply -f gitlab.yaml

6.三个应用都处于running状态之后,通过ip访问gitlab
IP:30060
用户名:root
密码:通过以下命令查询
kubectl exec -it -n gitlab gitlab-6f4966b889-xxxxx -- cat /etc/gitlab/initial_root_password

posted @ 2025-07-09 17:51  努力成为OM大师  阅读(71)  评论(0)    收藏  举报