这篇文章主要介绍“Drone怎么在kubernetes环境下打包并部署”,在日常操作中,相信很多人在Drone怎么在kubernetes环境下打包并部署问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Drone怎么在kubernetes环境下打包并部署”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
1. drone是一款使用 Go 开发的开源的 CI 自动构建平台。原生 Docker 支持,kubernetes也是支持的。drone比argo, tekton更快,更简单,比jenkins更轻量化。drone云原生概念+1,做了很多事不用考虑+1,gitlab/github能看到构建结果+1
环境:kubernetes 1.18+, helm3 参考官方
创建namespace, 添加仓库
kubectl create ns drone
helm repo add drone https://charts.drone.io
helm repo update
在gitlab中创建一个OAuth应用。Redirect URI是drone的地址并加一个/login,授权两个api, read_user
增加一个文件drone-server-overrides.yaml
。这里使用的traefik。需要一个数据库链接,默认使用的sqllite,本例使用postgres
image:
tag: 1.10.1
ingress:
enabled: true
annotations:
traefik.ingress.kubernetes.io/router.tls: "true"
traefik.ingress.kubernetes.io/router.entrypoints: websecure
traefik.ingress.kubernetes.io/router.tls.certresolver: aliyun
traefik.ingress.kubernetes.io/router.tls.domains.0.main: drone.your_domain.com
hosts:
- host: drone.your_domain.com
paths:
- "/"
env:
DRONE_SERVER_HOST: drone.your_domain.com:31000
DRONE_SERVER_PROTO: https
# 更新已存在并关连的gitlab帐号为admin权限,需要重启pod,参考https://docs.drone.io/server/user/admin/
DRONE_USER_CREATE: username:your_gitlab_username,admin:true
# 通过openssl rand -hex 16生成一个
DRONE_RPC_SECRET: c7a536a3af5e2809f3d0d34a71a13302
DRONE_GITLAB_CLIENT_ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
DRONE_GITLAB_CLIENT_SECRET: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
DRONE_GITLAB_SERVER: https://your_gitlab_url
# oauth会验证gitlab证书,如果验证不过,需要打开
#DRONE_GITLAB_SKIP_VERIFY: true
DRONE_DATABASE_DRIVER: postgres
DRONE_DATABASE_DATASOURCE: postgres://postgres:password@192.168.1.10:5433/drone?sslmode=disable
persistentVolume:
enabled: false
创建drone server
helm install --namespace drone \
server \
drone/drone \
-f drone-server-overrides.yaml
创建runner文件drone-runner-kube-values.yaml
image:
tag: latest
rbac:
buildNamespaces:
- drone
env:
# 同server的kubernetes service名称
DRONE_RPC_HOST: server-drone
DRONE_RPC_SECRET: c7a536a3af5e2809f3d0d34a71a13302
DRONE_NAMESPACE_DEFAULT: drone
运行runner
helm install --namespace drone \
drone-runner-kube \
drone/drone-runner-kube \
-f drone-runner-kube-values.yaml
查看pod是否跑起来了
$ kubectl get pods -n drone
NAME READY STATUS RESTARTS AGE
drone-runner-kube-6554c9df64-2znff 1/1 Running 0 3h26m
server-drone-647875c9f-t5bfx 1/1 Running 0 3h31m
解析域名并访问导出的Ingress。第一次访问会跳转到gitlab,要求授权
在项目下创建.drone.yml
。drone docker插件参考
kind: pipeline
type: kubernetes
name: default
steps:
- name: 编译&构建镜像
image: plugins/docker
# 挂载主机的docker
volumes:
- name: docker
path: /var/run/docker.sock
settings:
repo: registry.your_registry.com:31000/your_project/test-ci
registry: registry.your_registry.com:31000
mirror: https://hub-mirror.c.163.com
username: username
password: password
# 更多变量参考https://docs.drone.io/pipeline/environment/reference/
tags:
- ${DRONE_TAG=latest}
- build-${DRONE_BUILD_NUMBER}
- name: 部署到k8s
image: jeessy/dron8s:0.4.0
settings:
yaml: ./deployment-test.yaml
image_addr: registry.your_registry.com:31000/your_project/${DRONE_REPO_NAME}:build-${DRONE_BUILD_NUMBER}
11.1 deployment-test.yaml
apiVersion: v1
kind: Namespace
metadata:
name: {{.repo_namespace}}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{.repo_name}}
namespace: {{.repo_namespace}}
spec:
selector:
matchLabels:
app: {{.repo_name}}
replicas: 1
template:
metadata:
labels:
app: {{.repo_name}}
spec:
imagePullSecrets:
- name: harbor-registry-secret
containers:
- name: {{.repo_name}}
image: {{.image_addr}}
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: {{.repo_name}}-svc
namespace: {{.repo_namespace}}
spec:
selector:
app: {{.repo_name}}
ports:
- protocol: TCP
port: 80
targetPort: 80
---
# 私有仓库
apiVersion: v1
kind: Secret
metadata:
name: harbor-registry-secret
namespace: {{.repo_namespace}}
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: xxxxx
说明:部署到k8s可以使用pelotech/drone-helm3
或jeessy/dron8s
。两个插件本人都提交过代码。根据情况使用,helm配置更复杂,个人推介使用dron8s,可使用集群中使用,无需在drone中添加kube_token https://github.com/bh90210/dron8s#in-cluster-use
pelotech/drone-helm3
是连接helm和kubernetes的一个插件。通过它就可以把项目下的helm自动部到kubernetes中。参考官方
使用helm create test-ci
创建helm项目,并放到项目下。根据实际情况修改templates
文件夹下的模板。参考官方教程
图为运行后效果
到此,关于“Drone怎么在kubernetes环境下打包并部署”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注天达云网站,小编会继续努力为大家带来更多实用的文章!