K8s 集群中运行 GitLab-Runner 来执行 GitLab-CI

阿里云实例教程

https://help.aliyun.com/document_detail/106968.html?spm=a2c4g.11186623.6.926.71cd629fRdBjk2
(本地使用 pv pvc 须要修改。 )

官方用 helm 安装有问题。

https://docs.gitlab.com/runner/install/kubernetes.html

Error: parse error in "gitlab-runner/templates/_helpers.tpl": template: gitlab-runner/templates/_helpers.tpl:2: function "ternary" not defined

无奈研究了下面的方法。
参考: https://blog.csdn.net/aixiaoyang168/article/details/81149264
直接修改相应参数,会直接加入

用root登录gitlab 在管理区域-- Runners 里面可以看到 注册信息。

处理

ERROR: Job failed (system failure): pods is forbidden: User “system:serviceaccount:dev:default” cannot create resource “pods” in API group “” in the namespace “gitlab-runner”

kubectl create clusterrolebinding gitlab-cluster-admin --clusterrole=cluster-admin --group=system:serviceaccounts --namespace=gitlab-runner
# 可进入系统运行 gitlab-runner register -h 查看更多参数
#--non-interactive 非交互式
#--url gitlab注册URL
#--registration-token gitlab注册令牌
#--name pod-runner 描述
#--tag-list k8s,test 标签(这行删掉就没有标签,任何任务不写标签都能调用)
#--executor kubernetes (这是意思是在k8s集群里面运行,会自动创建新的pod的来运行任务)

---

apiVersion: v1
kind: Namespace
metadata:
  name: gitlab-runner
  namespace: gitlab-runner
  
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: gitlab-runner
  namespace: gitlab-runner
spec:
  replicas: 1
  revisionHistoryLimit: 3
  selector:
    matchLabels:
      name: gitlab-runner
  template:
    metadata:
      labels:
        name: gitlab-runner
    spec:
      containers:
      - args:
        - run
        image: gitlab/gitlab-runner:latest
        imagePullPolicy: IfNotPresent
        command: ["bash","-c","gitlab-runner register \
     --non-interactive \
     --url http://192.168.6.189/ \
     --registration-token hWj9_sVbcx9jSF7MJCvx \
     --name runner-pod \
     --tag-list k8s,test \
     --executor kubernetes \
     --kubernetes-namespace gitlab-runner && \
      gitlab-runner restart && \
      gitlab-runner run --user=gitlab-runner --working-directory=/home/gitlab-runner"]
        name: gitlab-runner
        volumeMounts:
        - mountPath: /etc/ssl/certs
          name: cacerts
          readOnly: true
      restartPolicy: Always
      volumes:
      - hostPath:
          path: /usr/share/ca-certificates/mozilla
        name: cacerts

下面是手动创建

gitlab-runner-deployment.yaml

运行完yaml文件后。 exec 到创建的pod里 执行 : gitlab-runner register
手动注册到服务。

# kubectl exec -it pod sh
# gitlab-runner register
# 填写 IP Token Tag kubernetes
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: gitlab-runner
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      name: gitlab-runner
  template:
    metadata:
      labels:
        name: gitlab-runner
    spec:
      containers:
      - args:
        - run
        image: gitlab/gitlab-runner:latest
        imagePullPolicy: IfNotPresent
        name: gitlab-runner
        volumeMounts:
       # - mountPath: /etc/gitlab-runner/
       #  name: config
        - mountPath: /etc/ssl/certs
          name: cacerts
          readOnly: true
      restartPolicy: Always
      volumes:
      #- configMap:
      #    name: gitlab-runner
      #  name: config
      - hostPath:
          path: /usr/share/ca-certificates/mozilla
        name: cacerts
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐