Kubernetes部署gitea

Kubernetes部署代码仓库Gitea

      Gitea 是一个开源社区驱动的轻量级代码托管解决方案,周末抽空在Kubernetes部署代码仓库Gitea,过程虽然有些坑,但是最后还是成功在Kubernetes上部署运行,今天将这个过程分享给大家,有需要可以参考。

      1、设置持久卷

      我们先创建一个持久卷配置,空间大小给5个G,持久卷名字为gitea-pvc

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: gitea-pvc
spec:
  storageClassName: openebs-hostpath
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

      2、部署容器

      我们设置容器名为gitea-deployment,期望副本为1,同时gitea默认监听端口3000以及需要ssh端口22,我们这里容器监听3000、22端口,设置持久卷目录为/home/storageVolume/gitea。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: gitea-deployment
  labels:
    app: gitea
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gitea
  template:
    metadata:
      labels:
        app: gitea
    spec:
      containers:
      - name: gitea
        image: gitea/gitea
        ports:
        - containerPort: 3000
          name: gitea-http
        - containerPort: 22
          name: gitea-ssh
        volumeMounts:
        - mountPath: /home/storageVolume/gitea
          name: gitea-data
      volumes:
      - name: gitea-data
        persistentVolumeClaim:
          claimName: gitea-pvc

      3、部署服务

      我们部署gitea服务,设置服务名为gitea-service,端口监听方式为LoadBalancer,方便IP流量转发到服务上。

      LoadBalancer 服务是暴露服务到 internet 的标准方式。在 GKE 上,这种方式会启动一个 Network Load Balancer,它将给你一个单独的 IP 地址,转发所有流量到你的服务。

kind: Service
apiVersion: v1
metadata:
  name: gitea-service
  annotations:
    metallb.universe.tf/address-pool: default
spec:
  selector:
    app: gitea
  type: LoadBalancer
  ports:
  - name: gitea-http
    port: 3000
    targetPort: gitea-http
  - name: gitea-ssh
    port: 22
    targetPort: gitea-ssh

      4、Ingress设置访问域名

      为了避免使用ip+port的形式访问gitea,我们利用Ingress设置域名来访问我们的服务,为了防止在初始化安装的时候超时,我们需要设置超时限制,避免安装过程中因为超市失败。

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: gitea-ingress
  annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: "100m"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "600"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "600"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/load-balance: "ip_hash"
    nginx.ingress.kubernetes.io/upstream-hash-by: "$request_uri"
spec:
  rules:
  - host: gitea.test.cn
    http:
      paths:
      - path: /
        backend:
          serviceName: gitea-service
          servicePort: 3000

      5、整合配置

      为了方便配置的管理跟部署,我们将上面全部配置整合在一起,命名一个文件为gitea-deployment.yaml,配置如下

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: gitea-pvc
spec:
  storageClassName: openebs-hostpath
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gitea-deployment
  labels:
    app: gitea
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gitea
  template:
    metadata:
      labels:
        app: gitea
    spec:
      containers:
      - name: gitea
        image: gitea/gitea
        ports:
        - containerPort: 3000
          name: gitea-http
        - containerPort: 22
          name: gitea-ssh
        volumeMounts:
        - mountPath: /home/storageVolume/gitea
          name: gitea-data
      volumes:
      - name: gitea-data
        persistentVolumeClaim:
          claimName: gitea-pvc
---
kind: Service
apiVersion: v1
metadata:
  name: gitea-service
  annotations:
    metallb.universe.tf/address-pool: default
spec:
  selector:
    app: gitea
  type: LoadBalancer
  ports:
  - name: gitea-http
    port: 3000
    targetPort: gitea-http
  - name: gitea-ssh
    port: 22
    targetPort: gitea-ssh
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: gitea-ingress
  annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: "100m"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "600"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "600"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/load-balance: "ip_hash"
    nginx.ingress.kubernetes.io/upstream-hash-by: "$request_uri"
spec:
  rules:
  - host: gitea.test.cn
    http:
      paths:
      - path: /
        backend:
          serviceName: gitea-service
          servicePort: 3000

我们输入以下命令行来部署服务

kubectl apply -f gitea-deployment.yaml

等待部署完成后我们直接访问设置的域名,配置参数按照自己的实际情况填就可以。

      这样我们就成功完成gitea部署运行。

0条评论

发表评论