Gitlab + Jenkins + k8s 实现企业 CI/CD 落地

程序猿辉辉

共 4390字,需浏览 9分钟

 ·

2023-05-04 18:44

gitlab-pipeline

Gitlab + Jenkins + k8s 实现企业 CI/CD 落地

1、启动 docker、kubernetes(docker-desktop Mac本地环境)

f799c5ad29e7e60a48268eed2b69ad44.webp

2、使用K8s集群启动 jenkins

      apiVersion: v1
kind: ServiceAccount
metadata:
name: jenkins
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: jenkins
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["create","delete","get","list","patch","update","watch"]
- apiGroups: [""]
resources: ["pods/exec"]
verbs: ["create","delete","get","list","patch","update","watch"]
- apiGroups: [""]
resources: ["pods/log"]
verbs: ["get","list","watch"]
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: jenkins
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: jenkins
subjects:
- kind: ServiceAccount
name: jenkins
---
apiVersion: v1
kind: Service
metadata:
name: jenkins
spec:
selector:
app: jenkins
type: NodePort
ports:
- name: http
port: 8080
targetPort: 8080
protocol: TCP
- name: agent
port: 50000
protocol: TCP
targetPort: 50000
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: jenkins
spec:
replicas: 1
selector:
matchLabels:
app: jenkins
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 2
maxUnavailable: 0
template:
metadata:
labels:
app: jenkins
spec:
securityContext:
fsGroup: 1000
serviceAccountName: jenkins
containers:
- name: jenkins
image: jenkinsci/blueocean:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
name: web
protocol: TCP
- containerPort: 50000
name: agent
protocol: TCP
volumeMounts:
- name: jenkins-home
mountPath: /var/jenkins_home
env:
- name: LIMITS_MEMORY
valueFrom:
resourceFieldRef:
resource: limits.memory
divisor: 1Mi
- name: JAVA_OPTS
# 解决jenkins 2.2以上版本无法关闭跨站请求伪造保护 -Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true
value: -Xmx$(LIMITS_MEMORY)m -XshowSettings:vm -Dhudson.slaves.NodeProvisioner.initialDelay=0 -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85 -Duser.timezone=Asia/Shanghai -Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true
volumes:
- name: jenkins-home
hostPath:
path: "/home/jenkins"

配置触发远程构建,也可以不配置手动构建,配置的作用就是,git提交代码后,会向jenkins发送webhook,通知jenkins开始构建项目(jenkins 安装 gitlab 的插件,可以使用secret token的方式配置令牌)

5d39acc10c6b0d2a86d20013d4ca26e3.webp

3、启动 gitlab(本地docker跑的)

      docker pull registry.cn-hangzhou.aliyuncs.com/imooc/gitlab-ce:latest

# 编写启动脚本,并配置 hosts

cat <<EOF > start.sh
#!/bin/bash
HOST_NAME=gitlab.localhost.com
GITLAB_DIR=`pwd`
docker stop gitlab
docker rm gitlab
docker run -d \\
--hostname \${HOST_NAME} \\
-p 8443:443 -p 8080:80 -p 2222:22 \\
--name gitlab \\
-v \${GITLAB_DIR}/config:/etc/gitlab \\
-v \${GITLAB_DIR}/logs:/var/log/gitlab \\
-v \${GITLAB_DIR}/data:/var/opt/gitlab \\
registry.cn-hangzhou.aliyuncs.com/imooc/gitlab-ce:latest
EOF

# 给 start.sh 执行权限
chmod + x start.sh

把 gitlab-pipeline 的代码丢到你本地的 gitlab 仓库,然后配置 webhook,由于 gitlab 是从 docker 启动的,需要访问宿主机的IP http://host.docker.internal,所以应该是http://host.docker.internal:30802/job/gitlab-pipeline/build?token=123456

b4ad41111ef9ce97e2560e0b73bce39b.webp

4、jenkins 收到 gitlab 的 webhook 请求,开始构建(gitlab 提交代码 jenkins 就会收到 webhook 请求)

a163c5d5fdcd38fbe0c49323d65d15f6.webp

5、为 jenkins 配置 gitlab 凭据

b5febca0633aa33127e96fe3dee47500.webp

6、为 jenkins 配置阿里云镜像仓库(registry.cn-beijing.aliyuncs.com)的凭据

7、给 jenkins 配置 k8s 凭据

先安装 Kubernetes Continuous Deploy 插件,然后创建对应的凭据(就是把 .kube/config 的内容粘贴过来)

3b973aa2db62f9228a508c9dd85dc98b.webp

8、生成三个凭据如下

71a94344621fb14c7f37a04492745067.webp

9、记录凭据的ID后面会在写 Jenkinsfile 用到

      gitlab -> bda1c18e-3c03-48db-85d2-0910405ab8c7
阿里云镜像 -> e79820d3-2996-4f19-b69c-3171836c0eaf
k8s -> 987545c2-1be9-4d64-a8a5-ecfb163d5fbb

10、k8s 添加 aliyun 仓库 secret

      kubectl create secret docker-registry aliyun-pull-secret --docker-username=用户名 \
--docker-password=密码 \
--docker-email=邮箱 \
--docker-server=registry.cn-beijing.aliyuncs.com

11、创建一个 kubernetes 云,安装 kubernetes plugin

kubernetes 云配置,如果 jenkins 是安装到k8s内部 https://kubernetes.default.svc.cluster.local ,如果是安装到 k8s 外部,使用kubectl cluster-info查看 k8s 地址

1ce27b65daf0096759fc225ab0c26b10.webp

jenkins地址: http://jenkins.default:8080

a9b0d23def7c5a65b7c4b7a1934d6845.webp

12、为 job 添加 pipeline 脚本,这里使用 git 的方式

git 地址:http://host.docker.internal:8080/root/gitlab-pipeline.git

ae0a34a1204d9eb94d4fc43a3eca235b.webp

13、点击立即构建

a2cf689976c8b0df33691cce6ffdc747.webpd9d89bd844d2e84a9b3df99240fd2018.webp

14、pod启动成功

6d7278c3005f93e9b09eed50cabe9b0c.webp

15、访问测试 http://127.0.0.1:30666/

d7654ed1115d0d8c8c4f6d45e9700136.webp

60e7d69b1a8cc334590751ae5b0aac94.webp

项目源码地址

https://github.com/it-wwh/gitlab-pipeline


浏览 31
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报