Kubernetes部署Drone
Kubernetes部署Drone
上一篇完成了Gitea的部署,为了实现CD/CD功能,我们还需部署Drone,其实最开始是想部署jenkins,后面在查询资料的时候看到了Drone,并且相对来说比较轻量,所以果断选择了Drone。
什么是Drone
Drone 是一个基于Docker容器技术的可扩展的持续集成引擎,用于自动化测试、构建、发布。每个构建都在一个临时的Docker容器中执行,使开发人员能够完全控制其构建环境并保证隔离。开发者只需在项目中包含 .drone.yml文件,将代码推送到 git 仓库,Drone就能够自动化的进行编译、测试、发布。
一、申请证书、创建密钥
我部署Drone之后使用ingress去转发服务,这样可以通过域名来访问,并且加了https,如果你只是想单纯通过ip+port的形式访问,这一个可以略过,申请完证书之后将证书上传到服务器上,我们使用以下命令创建一个证书密钥,后面会用到
kubectl -n 项目空间 create secret tls 证书密钥名称 --key key证书所在地址 --cert crt证书所在地址
我们创建一个证书密钥
kubectl -n drone-system create secret tls drone.test.cn-secret --key ./tls.key --cert ./tls.crt
大家根据自己的实际情况修改项目空间以及证书所在地址。
二、Gitea创建应用
为了跟Gitea做关联,我们先到设置创建一个应用
重定向URL填:Drone服务域名(IP)/login,类似这样填写
drone.test.cn/login 192.12.12.1/login
然后记得保存我们的客户端ID、客户端密钥,下一步操作会用上
三、部署Drone服务
我们先通过以下命令来创建RPC通信的SECRET
openssl rand -hex 16
生成的字符串记得保存,创建一个名字为drone-deployment-status.yaml的文件,由于避免我的主节点资源超了,我将Drone应用部署在Node1节点,节点名k8s-node2,可通过以下配置来实现
nodeName: k8s-node2
下面贴出我的配置yaml
#部署有状态容器 apiVersion: apps/v1 kind: StatefulSet metadata: name: drone-deployment namespace: drone-system labels: app: drone spec: serviceName: drone replicas: 1 selector: matchLabels: app: drone template: metadata: labels: app: drone spec: terminationGracePeriodSeconds: 180 containers: - name: drone image: drone/drone imagePullPolicy: Always env: - name: DRONE_GITEA_SERVER #Gitea服务器地址 value: https://gitea.test.cn - name: DRONE_GITEA_CLIENT_ID #Gitea应用Id value: 56150d48-f479-4662-bfd3-41a6a784f014 - name: DRONE_GITEA_CLIENT_SECRET #Gitea应用Secret value: tIoYfwDbbrMQ9UJMre8RF5SkbIlUqLI1WMpylqDt8cVO - name: DRONE_RPC_SECRET #RPC通信的SECRET value: 7e2422631be1fa7aada777119a5d18bb - name: DRONE_SERVER_HOST #drone服务器的域名 value: drone.test.cn - name: DRONE_SERVER_PROTO value: https - name: DRONE_LOGS_TRACE # 打开日志 value: 'true' - name: DRONE_USER_CREATE # 用户 value: username:hongzx,admin:true ports: - containerPort: 80 name: port - containerPort: 443 name: ssl-port nodeName: k8s-node2 --- #设置服务 apiVersion: v1 kind: Service metadata: name: drone namespace: drone-system labels: app: drone spec: type: NodePort ports: ports: - name: http protocol: TCP port: 80 targetPort: 80 - name: grpc protocol: TCP port: 9000 targetPort: 9000 selector: app: drone --- #设置域名访问 apiVersion: extensions/v1beta1 kind: Ingress metadata: name: drone-ingress namespace: drone-system spec: tls: - hosts: - drone.test.cn secretName: drone.test.cn-secret rules: - host: drone.test.cn http: paths: - backend: serviceName: drone servicePort: 80 --- #设置权限 kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata: namespace: default name: drone rules: - apiGroups: - "" resources: - secrets verbs: - create - delete - apiGroups: - "" resources: - pods - pods/log verbs: - get - create - delete - list - watch - update --- #设置账户 kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: drone namespace: default subjects: - kind: ServiceAccount name: default namespace: default roleRef: kind: Role name: drone apiGroup: rbac.authorization.k8s.io --- #部署通信密钥 apiVersion: v1 kind: Secret metadata: name: drone-secrets namespace: drone-system data: server.secret: azhzZHJvbmU= --- #部署agent # drone-agent-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: drone-agent labels: app.kubernetes.io/name: drone-agent spec: replicas: 1 selector: matchLabels: app.kubernetes.io/name: drone-agent template: metadata: labels: app.kubernetes.io/name: drone-agent spec: containers: - name: runner image: drone/drone-runner-kube:latest ports: - containerPort: 3000 env: #- name: DRONE_SERVICE_ACCOUNT_DEFAULT # value: drone - name: DRONE_RPC_HOST value: drone.test.cn - name: DRONE_RPC_PROTO value: https - name: DRONE_RPC_SECRET value: 7e2422631be1fa7aada777119a5d18bb volumeMounts: - name: dockersocket mountPath: /var/run/docker.sock - name: dockersocket-2 mountPath: /run/docker.sock - name: docker-client mountPath: /usr/bin/docker restartPolicy: Always nodeName: k8s-node2 volumes: - name: dockersocket hostPath: path: /var/run/docker.sock - name: dockersocket-2 hostPath: path: /run/docker.sock - name: docker-client hostPath: path: /usr/bin/docker
四、部署配置
直接输入以下命令进行配置
kubectl apply -f drone-deployment-status.yaml
五、访问Drone
第一次访问可能需要授权,截图如下
授权之后进入Drone
这样我们就完成了CD/CD重要一环的部署。
0条评论