Drone 是用 Go 语言编写的基于 Docker 构建的开源轻量级 CI/CD 工具,可以和 Gitlab 集成使用。本文主要记录安装 Drone 的过程,并集成 Gitlab。

创建证书

参考 使用Cert Manager配置Let’s Encrypt证书 这篇完整,创建一个godaddy证书的签发机构:

  1. cat << EOF | kubectl create -f -
  2. apiVersion: cert-manager.io/v1alpha2
  3. kind: ClusterIssuer
  4. metadata:
  5. name: javachen-space-letsencrypt-prod
  6. spec:
  7. acme:
  8. server: https://acme-v02.api.letsencrypt.org/directory
  9. email: junecloud@163.com
  10. privateKeySecretRef:
  11. name: javachen-space-letsencrypt-prod
  12. solvers:
  13. - selector:
  14. dnsNames:
  15. - '*.javachen.space'
  16. dns01:
  17. webhook:
  18. groupName: acme.javachen.space
  19. solverName: godaddy
  20. config:
  21. authApiKey: e4hN4QrFgzdo_RHXe1ef2qpBPmiJPD2ZUcW
  22. authApiSecret: QsHuDdnnCbzp5DmEQzq4ts
  23. production: true
  24. ttl: 600
  25. EOF

创建一个命名空间:

  1. kubectl create namespace drone

创建一个证书:

  1. cat << EOF | kubectl create -f -
  2. apiVersion: cert-manager.io/v1alpha2
  3. kind: Certificate
  4. metadata:
  5. name: drone-javachen-space-cert
  6. namespace: drone
  7. spec:
  8. secretName: drone-javachen-space-cert
  9. renewBefore: 240h
  10. dnsNames:
  11. - "*.javachen.space"
  12. issuerRef:
  13. name: javachen-space-letsencrypt-prod
  14. kind: ClusterIssuer
  15. EOF

查看证书状态:

  1. kubectl get secret,Certificate -n drone
  2. kubectl describe secret drone-javachen-space-cert -n drone
  3. kubectl describe certificate drone-javachen-space-cert -n drone
  4. kubectl describe order drone-javachen-space-cert-2742582754 -n drone
  5. kubectl describe Challenge drone-javachen-space-cert-695846883-0 -n drone

安装Drone

查找chart
这里使用我修改过的chart:

  1. git clone https://github.com/javachen/charts
  2. cd charts

根据chart中 values.yaml文件创建drone-gitlab-values.yaml:

  1. cat <<EOF > drone-gitlab-values.yaml
  2. ingress:
  3. enabled: true
  4. annotations:
  5. kubernetes.io/ingress.class: nginx
  6. nginx.ingress.kubernetes.io/ssl-redirect: "true"
  7. nginx.ingress.kubernetes.io/proxy-body-size: 10m
  8. hosts:
  9. - drone.javachen.space
  10. tls:
  11. - secretName: drone-javachen-space-cert
  12. hosts:
  13. - drone.javachen.space
  14. sourceControl:
  15. provider: gitlab
  16. gitlab:
  17. clientID: 3a3c6b5d37b6557168759389080d331fed992218b9b8cac8b2bc6516b292429b
  18. clientSecretKey: clientSecret
  19. clientSecretValue: eb60347cffd7614eef2dba4abbd5783779647be2aed4a3fd78eae31ee1480138
  20. server: http://gitlab.javachen.space
  21. server:
  22. host: drone.javachen.space
  23. protocol: https
  24. adminUser: admin
  25. alwaysAuth: true
  26. envSecrets:
  27. drone-gitlab-login-secrets:
  28. - DRONE_GIT_USERNAME
  29. - DRONE_GIT_PASSWORD
  30. kubernetes:
  31. enabled: true
  32. #env:
  33. # DRONE_LOGS_DEBUG: "false"
  34. # DRONE_DATABASE_DRIVER: "mysql"
  35. # DRONE_DATABASE_DATASOURCE: "root:123456@tcp(192.168.1.100:3306)/drone?parseTime=true"
  36. persistence:
  37. enabled: true
  38. storageClass: ceph-rbd
  39. size: 5Gi
  40. EOF

注意:
1、nginx.ingress.kubernetes.io/proxy-body-size: 10m 设置上传文件大小
2、drone-gitlab-login-secrets 是设置获取gitlab仓库代码的用户名和密码的secret。创建过程如下:

  1. #假设登陆用户名和密码都为admin
  2. echo -n "admin" | base64
  3. cat << EOF | kubectl create -f -
  4. apiVersion: v1
  5. kind: Secret
  6. metadata:
  7. name: drone-gitlab-login-secrets
  8. namespace: drone
  9. type: Opaque
  10. data:
  11. DRONE_GIT_USERNAME: YWRtaW4=
  12. DRONE_GIT_PASSWORD: YWRtaW4=
  13. EOF

3、http://gitlab.javachen.space 是gitlab服务的地址,clientID 和 clientSecretValue 是在gitlab中创建一个应用,设置重定向地址:https://drone.javachen.space/hook ,得到的clientID 和 clientSecretValue
4、这里设置了persistence为启用,并且存储类为 ceph-rbd ,这个需要提前创建,可以参考我安装harbor的文章。
5、你也可以去掉上面的注释,设置drone使用mysql数据库。
使用helm3安装drone:

  1. helm install drone \
  2. --namespace drone \
  3. -f drone-gitlab-values.yaml \
  4. ./drone

查看状态:

  1. kubectl get all -n drone

浏览器输入 https://drone.javachen.space/ ,会跳转到 Gitlab 进行授权,接下来就可以同步仓库。

安装Drone CLI

安装drone cli:

  1. curl -L https://github.com/drone/drone-cli/releases/latest/download/drone_linux_amd64.tar.gz | tar zxsudo install -t /usr/local/bin drone

卸载Drone

  1. helm del drone -n dronekubectl delete pod,service,deploy,ingress,secret,pvc --all -n drone kubectl delete secret,certificate drone-javachen-space-cert -n drone

drone安装成功后,在 https://drone.javachen.space/account 上获取 Drone 的TOKEN,查看drone信息:

  1. export DRONE_SERVER= https://drone.javachen.space/account export DRONE_TOKEN=0FJZSq9dYtAnOvyXlL3Os6aIoBPtxRaadrone info

上面会输出登陆gitlab的用户名和密码。
drone 还提供了一些方法,例如可以创建 secret,参考 https://github.com/hectorqin/drone-kubectl 这个插件,假设 drone 从gitlab同步了一个仓库 叫做 chenzj/test,则可以通过下面命令创建几个Secret:

  1. DEFAULT_SECRET=`kubectl get secret -n drone|grep drone-deploy-token|awk '{print $1}'`
  2. KUBERNETES_SERVER=`kubectl config view|grep server|awk '{print $2}'`
  3. KUBERNETES_CERT=`kubectl get secret -n drone ${DEFAULT_SECRET} -o jsonpath="{.data.ca\.crt}"`
  4. KUBERNETES_TOKEN=`kubectl get secret -n drone ${DEFAULT_SECRET} -o jsonpath="{.data.token}" | base64 --decode`
  5. export DRONE_SERVER= https://drone.javachen.space/account
  6. export DRONE_TOKEN=0FJZSq9dYtAnOvyXlL3Os6aIoBPtxRaa
  7. drone info
  8. drone secret add chenzj/test --name KUBERNETES_SERVER --data ${KUBERNETES_SERVER} --allow-pull-request
  9. drone secret add chenzj/test --name KUBERNETES_CERT --data ${KUBERNETES_CERT} --allow-pull-request
  10. drone secret add chenzj/test --name KUBERNETES_TOKEN --data ${KUBERNETES_TOKEN} --allow-pull-request

执行完成之后,在drone页面的 chenzj/test 仓库 SETTINGS 里面可以看到上面创建的三个 secret。
然后,就可以在.drone.yaml中使用插件:

  1. - name: deploy
  2. image: quay.io/hectorqin/drone-kubectl
  3. settings:
  4. kubernetes_template: k8s/test/deployment.yaml
  5. kubernetes_namespace: test
  6. environment:
  7. KUBERNETES_SERVER:
  8. from_secret: KUBERNETES_SERVER
  9. KUBERNETES_CERT:
  10. from_secret: KUBERNETES_CERT
  11. KUBERNETES_TOKEN:
  12. from_secret: KUBERNETES_TOKEN
  13. depends_on:
  14. - build
  15. when:
  16. event:
  17. - push
  18. - tag

当然,还需要创建 RBAC :

  1. cat << EOF | kubectl create -f -
  2. apiVersion: v1
  3. kind: ServiceAccount
  4. metadata:
  5. name: drone-deploy
  6. namespace: drone
  7. ---
  8. apiVersion: rbac.authorization.k8s.io/v1beta1
  9. kind: ClusterRole
  10. metadata:
  11. name: drone-deploy
  12. rules:
  13. - apiGroups: ["","*"]
  14. resources: ["*"]
  15. verbs: ["*"]
  16. ---
  17. apiVersion: rbac.authorization.k8s.io/v1beta1
  18. kind: ClusterRoleBinding
  19. metadata:
  20. name: drone-deploy
  21. subjects:
  22. - kind: ServiceAccount
  23. name: drone-deploy
  24. namespace: drone
  25. roleRef:
  26. kind: ClusterRole
  27. name: drone-deploy
  28. apiGroup: rbac.authorization.k8s.io
  29. EOF