DEV Community

drake
drake

Posted on

在K8s内自建Git远程仓库

  • 1、先解决持久化的问题

在Kind容器内创建持久化目录,宿主机(服务器),执行下面命令:

docker exec -it dbe0bb145add mkdir -p /data/gitea
Enter fullscreen mode Exit fullscreen mode

校验是否成功

(base) [root@ip-10-242-18-237 ec2-user]# docker exec -it dbe0bb145add ls  /data/
docker  gitea  jenkins

Enter fullscreen mode Exit fullscreen mode

创建持久化卷

apiVersion: v1
kind: PersistentVolume
metadata:
  name: gitea-pv-volume
  labels:
    type: local
spec:
  storageClassName: standard
  claimRef:
    name: gitea-pv-claim
    namespace: devops-tools
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  local:
    path: /data/gitea
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - spiders-control-plane
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: gitea-pv-claim
  namespace: devops-tools
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
Enter fullscreen mode Exit fullscreen mode
  • 2、部署gitea仓库

对数据库有依赖,先部署数据库

apiVersion: apps/v1
kind: Deployment
metadata:
  name: gitea-postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gitea-postgres
  template:
    metadata:
      labels:
        app: gitea-postgres
    spec:
      containers:
        - name: postgres
          image: postgres:13
          env:
            - name: POSTGRES_DB
              value: "gitea"
            - name: POSTGRES_USER
              value: "gitea"
            - name: POSTGRES_PASSWORD
              value: "666"
          ports:
            - containerPort: 5432
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgres-storage
          securityContext:
            runAsUser: 1000
            runAsGroup: 1000
            fsGroup: 1000
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: gitea-pv-claim
---
apiVersion: v1
kind: Service
metadata:
  name: gitea-postgres
  labels:
    app: gitea-postgres
spec:
  ports:
    - name: postgres
      port: 5432
      targetPort: 5432
  selector:
    app: gitea-postgres

Enter fullscreen mode Exit fullscreen mode

部署Gitea服务

apiVersion: apps/v1
kind: Deployment
metadata:
  name: gitea
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gitea
  template:
    metadata:
      labels:
        app: gitea
    spec:
      initContainers:
        - name: init-permissions
          image: busybox
          command: ["sh", "-c", "chown -R 1000:1000 /data/gitea"]
          volumeMounts:
            - name: gitea-storage
              mountPath: /data/gitea
      containers:
        - name: gitea
          image: gitea/gitea:1.16.0
          env:
            - name: USER_UID
              value: "1000"
            - name: USER_GID
              value: "1000"
            - name: GITEA__database__DB_TYPE
              value: "postgres"
            - name: GITEA__database__HOST
              value: "gitea-postgres:5432"
            - name: GITEA__database__NAME
              value: "gitea"
            - name: GITEA__database__USER
              value: "gitea"
            - name: GITEA__database__PASSWD
              value: "666"
          ports:
            - containerPort: 3000
            - containerPort: 22
          volumeMounts:
            - mountPath: /data
              name: gitea-storage
              readOnly: false
      volumes:
        - name: gitea-storage
          persistentVolumeClaim:
            claimName: gitea-pv-claim
---
apiVersion: v1
kind: Service
metadata:
  name: gitea
  labels:
    app: gitea
spec:
  ports:
    - name: http
      port: 3000
      targetPort: 3000
    - name: ssh
      port: 96
      targetPort: 22
  selector:
    app: gitea
Enter fullscreen mode Exit fullscreen mode
  • 3、测试Git远程仓库服务是否部署成功

Image description

根据Service提供的集群内的IP进行访问:http://10.96.126.83:3000
结果:集群内测试通过!

Image description

  • 4、配置Nginx路由,使Git远程仓库能够在集群外访问

Image description

Image description

Image description

Top comments (0)