1. 首页
  2. 技术知识

Kubernetes实现CI与CD配置教程

目录

    一、基本介绍二、基于 Kubernetes 实现 CI/CD 配置

      1.配置 GitLab2.配置 Jenkins3.实现 CI/CD 配置4.验证

一、基本介绍

基于 Kubernetes 实现 CI/CD 配置,其实和往常那些 CI/CD 配置并没有太大区别。都是通过 提交代码,拉取代码,构建代码,发布代码来实现的。 只不过要是通过 K8s 来实现的话,则是需要将构建好的代码打包成镜像,通过镜像的方式来运行。

CI/CD 流程图:

开发将代码提交代码仓库后,我们便可以通过在 Jenkins 上配置脚本或是 Pipline 的方式来实现代码发布,其中发布有两种方式,一种是通过手动发布,另外一种可以通过 WebHook 插件来实现提交代码便自动发布(生产环境不建议自动发布)

脚本内容一般分为:克隆代码、编译代码、将编译好的代码打包成镜像、运行镜像几个步骤。

二、基于 Kubernetes 实现 CI/CD 配置

下面我们是通过容器的方式安装配置,物理安装可以看我前面的文章:传送门

1.配置 GitLab

1)安装 Docker-Compose

  1. [root@k8s-master01 ~]# wget “https://github.com/docker/compose/releases/download/v2.3.2/docker-compose-$(uname -s)-$(uname -m)” -O /usr/local/bin/docker-compose       
  2. [root@k8s-master01 ~]# chmod +x /usr/local/bin/docker-compose
  3. [root@k8s-master01 ~]# docker-compose –version

复制代码 2)安装 GitLab

  1. [root@k8s-master01 ~]# vim docker-compose.yml
  2. version: ‘3’
  3. services:
  4.   web:
  5.     image: ‘gitlab/gitlab-ce:14.8.5-ce.0’
  6.     restart: always
  7.     hostname: 192.168.1.1
  8.     environment:
  9.       GITLAB_OMNIBUS_CONFIG: |
  10.         external_url ‘http://192.168.1.1’
  11.     ports:
  12.       – ‘1080:80’
  13.       – ‘1443:443’
  14.       – ‘1022:22’
  15.     volumes:
  16.       – ‘/APP/gitlab/config:/etc/gitlab’
  17.       – ‘/app/gitlab/logs:/var/log/gitlab’
  18.       – ‘/app/gitlab/data:/var/opt/gitlab’
  19. [root@k8s-master01 ~]# docker-compose up -d

复制代码 因为博主的电脑配置不是很高,所以就不使用上面的方式安装 GitLab,而是直接使用 GitHub 上面的仓库。

2.配置 Jenkins

1)安装 NFS 存储,并配置共享目录

  1. [root@k8s-master01 ~]# yum -y install nfs-utils rpcbind
  2. [root@k8s-master01 ~]# echo “/app/jenkins *(rw,sync,no_root_squash)” > /etc/exports
  3. [root@k8s-master01 ~]# mkdir /app/jenkins
  4. [root@k8s-master01 ~]# systemctl start rpcbind nfs

复制代码 2)创建 PV 和 PVC

  1. [root@k8s-master01 ~]# vim jenkins-pv.yaml
  2. apiVersion: v1
  3. kind: PersistentVolume
  4. metadata:
  5.   name: jenkins-pv
  6. spec:
  7.   capacity:
  8.     storage: 10Gi
  9.   accessModes:
  10.     – ReadWriteMany
  11.   nfs:
  12.     server: 192.168.1.1
  13.     path: /app/jenkins
  14. apiVersion: v1
  15. kind: PersistentVolumeClaim
  16. metadata:
  17.   name: jenkins-pvc
  18. spec:
  19.   resources:
  20.     requests:
  21.       storage: 10Gi
  22.   accessModes:
  23.     – ReadWriteMany   
  24. [root@k8s-master01 ~]# kubectl create -f jenkins-pv.yaml

复制代码 3)创建 RBAC 授权

  1. [root@k8s-master01 ~]# vim jenkins-sa.yaml
  2. apiVersion: v1
  3. kind: ServiceAccount
  4. metadata:
  5.   name: jenkins-sa
  6. apiVersion: rbac.authorization.k8s.io/v1beta1
  7. kind: ClusterRole
  8. metadata:
  9.   name: jenkins-cr
  10. rules:
  11.   – apiGroups: [“extensions”,”apps”]
  12.     resources: [“deployments”]
  13.     verbs: [“create”,”delete”,”get”,”list”,”watch”,”patch”,”update”]
  14.   – apiGroups: [“”]
  15.     resources: [“services”]
  16.     verbs: [“create”,”delete”,”get”,”list”,”watch”,”patch”,”update”]
  17.   – apiGroups: [“”]
  18.     resources: [“pods”]
  19.     verbs: [“create”,”delete”,”get”,”list”,”patch”,”update”]
  20.   – apiGroups: [“”]
  21.     resources: [“pods/exec”]
  22.     verbs: [“create”,”delete”,”get”,”list”,”patch”,”update”]
  23.   – apiGroups: [“”]
  24.     resources: [“pods/log”]
  25.     verbs: [“get”,”list”,”update”]
  26.   – apiGroups: [“”]
  27.     resources: [“secrets”]
  28.     verbs: [“get”]
  29. apiVersion: rbac.authorization.k8s.io/v1beta1
  30. kind: ClusterRoleBinding
  31. metadata:
  32.   name: jenkins-crb
  33. roleRef:
  34.   kind: ClusterRole
  35.   name: jenkins-cr
  36.   apiGroup: rbac.authorization.k8s.io
  37. subjects:
  38. – kind: ServiceAccount
  39.   name: jenkins-sa
  40.   namespace: default
  41. [root@k8s-master01 ~]# kubectl create -f jenkins-sa.yaml

复制代码 4)创建 StatefulSet

  1. [root@k8s-master01 ~]# vim jenkins-statefulset.yaml
  2. apiVersion: apps/v1
  3. kind: StatefulSet
  4. metadata:
  5.   name: jenkins
  6. spec:
  7.   serviceName: jenkins
  8.   replicas: 1
  9.   selector:
  10.     matchLabels:
  11.       app: jenkins
  12.   template:
  13.     metadata:
  14.       name: “jenkins”
  15.       labels:
  16.         app: jenkins
  17.     spec:
  18.       serviceAccountName: jenkins-sa
  19.       containers:
  20.       – name: jenkins
  21.         image: jenkins/jenkins:lts
  22.         imagePullPolicy: IfNotPresent
  23.         ports:
  24.         – containerPort: 8080
  25.         – containerPort: 50000
  26.         volumeMounts:
  27.         – name: jenkins
  28.           mountPath: /var/jenkins_home
  29.       volumes:
  30.       – name: jenkins
  31.         persistentVolumeClaim:
  32.           claimName: jenkins-pvc         
  33. [root@k8s-master01 ~]# chown -R 1000 /app/jenkins
  34. [root@k8s-master01 ~]# kubectl create -f jenkins-statefulset.yaml

复制代码 5)创建 Service

  1. [root@k8s-master01 ~]# vim jenkins-svc.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5.   name: jenkins
  6. spec:
  7.   type: NodePort
  8.   ports:
  9.   – name: http
  10.     port: 8080
  11.     targetPort: 8080
  12.     nodePort: 30080
  13.   – name: agent
  14.     port: 50000
  15.     targetPort: 50000
  16.     nodePort: 30090
  17.   selector:
  18.     app: jenkins
  19. [root@k8s-master01 ~]# kubectl create -f jenkins-svc.yaml

复制代码 6)配置 Jenkins

  1. [root@k8s-master01 ~]# cat /app/jenkins/secrets/initialAdminPassword
  2. a303d66e915e4ee5b26648a64fdff4be

复制代码 http://192.168.1.1:30080/

我们这里安装推荐的插件即可,后面有需求可以再进行安装

3.实现 CI/CD 配置

1)在 Jenkins 宿主机上创建 SSH 密钥

  1. [root@k8s-master01 ~]# ssh-keygen -t rsa                                                                # 三连回车
  2. [root@k8s-master01 ~]# cat ~/.ssh/id_rsa.pub                                                        # 查看公钥

复制代码 2)将公钥上传到 GitLab 上

3)将仓库克隆到本地

  1. [root@k8s-master01 ~]# git clone git@github.com:ChenZhuang1217/test.git

复制代码 4)编写 Go 代码

  1. [root@k8s-master01 ~]# cd test
  2. [root@k8s-master01 test]# vim main.go
  3. package main
  4. import (
  5.         “fmt”
  6.         “net/http”
  7. )
  8. func HelloHandler(w http.ResponseWriter, r *http.Request) {
  9.         fmt.Fprintf(w, “Hello World”)
  10. }
  11. func main() {
  12.         http.HandleFunc(“/”, HelloHandler)
  13.         http.ListenAndServe(“:8080”, nil)
  14. }

复制代码 5)编写 Dockerfile

  1. [root@k8s-master01 test]# vim Dockerfile
  2. FROM golang:1.16 as builder
  3. ENV GO111MODULE=on \
  4.     GOPROXY=https://goproxy.cn,direct
  5. WORKDIR /app
  6. COPY . .
  7. RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags=”-w -s” -o main main.go
  8. FROM busybox:1.28.4
  9. WORKDIR /app
  10. COPY –from=builder /app/ .
  11. EXPOSE 8080
  12. CMD [“./main”]
  13. [root@k8s-master01 test]# docker build -t test-web-server:devops-$(date +%Y-%m-%d-%H-%M-%S) .

复制代码 6)提交代码

Git 教程可以看博主前面写的文章:Git

  1. [root@k8s-master01 test]# git add .                                                                                                                # 提交到暂存区
  2. [root@k8s-master01 test]# git config –global user.email “Zhuang_zz1217@163.com”                # 配置用户邮箱
  3. [root@k8s-master01 test]# git commit -m “This is test CI/CD”                                                        # 提交到本地仓库
  4. [root@k8s-master01 test]# git push                                                                                                                # X到远程仓库

复制代码 7)创建 Deployment 和 Service

  1. [root@k8s-master01 ~]# vim test-web-server.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5.   name: test-web-server
  6. spec:
  7.   replicas: 1
  8.   selector:
  9.     matchLabels:
  10.       app: test-web-server
  11.   template:
  12.     metadata:
  13.       labels:
  14.         app: test-web-server
  15.     spec:
  16.       containers:
  17.       – name: test-web-server
  18.         image: test-web-server:devops-2022-04-25-17-16-54
  19.         imagePullPolicy: IfNotPresent
  20.         ports:
  21.         – containerPort: 8080
  22. apiVersion: v1
  23. kind: Service
  24. metadata:
  25.   name: test-web-server
  26. spec:
  27.   type: NodePort
  28.   ports:
  29.   – name: test-web-server
  30.     port: 8080
  31.     targetPort: 8080
  32.     nodePort: 30188
  33.   selector:
  34.     app: test-web-server
  35. [root@k8s-master01 ~]# kubectl create -f test-web-server.yaml

复制代码

8)编写 Jenkins 发版脚本

  1. [root@k8s-master01 ~]# vim test.sh
  2. #!/bin/bash
  3. # 固定时间格式
  4. Second=$(date +%Y-%m-%d-%H-%M-%S)
  5. # 备份旧的镜像
  6. Image=$(kubectl -s https://192.168.1.1:6443 describe pod | grep Image: | awk ‘{print $2}’ | grep test)
  7. echo $Image > /opt/test-image-$Second
  8. # 克隆代码
  9. cd /root
  10. if [ -d test ];
  11. then
  12.   mv test /opt/test-devops-$Second
  13.   git clone git@github.com:ChenZhuang1217/test.git
  14. else
  15.   git clone git@github.com:ChenZhuang1217/test.git
  16. fi
  17. # 发布新的镜像
  18. cd /root/test && docker build -t test-web-server:devops-$Second .
  19. # 上传到镜像仓库
  20. if [ $? -eq 0 ];
  21. then
  22.   docker tag test-web-server:devops-$dateImage harbor.tianya.com:5000/test-web-server:devops-$Second
  23.   docker push harbor.tianya.com:5000/test-web-server:devops-$Second
  24. else
  25.   exit 1                        # 退出 (防止运行下面命令)
  26. fi
  27. # 替换镜像
  28. sed -i ‘s/image:.*/image: harbor.tianya.com:5000\/test-web-server:devops-‘$Second’/g’ /root/test-web-server.yaml
  29. # 重启应用
  30. kubectl delete -f /root/test-web-server.yaml
  31. kubectl create -f /root/test-web-server.yaml
  32. [root@k8s-master01 ~]# chmod +x test.sh

复制代码 上面这个脚本有两步需要注意:

上传到镜像仓库: 如果你们没有自己的镜像仓库,可以选择调整脚本或看博主前面写的文章来安装 Harbor 仓库。

替换镜像: 我们上面配置的脚本是针对单个模块的,多个模块可以根据 for 循环来实现。

4.验证

1)在 Jenkins 上安装 SSH 插件

安装 SSH 插件的原因是因为,我们这个 Jenkins 是容器安装的,而脚本是在宿主机写的,所以通过远程到宿主机来运行脚本。

2)配置远程主机的用户名和密码

3)创建 Jenkins 私钥凭证(类型选择:SSH Username with private key)

4)配置 Jenkins 流水线

5)修改代码

6)在 Jenkins 上发布

以上就是Kubernetes实现CI与CD配置教程的详细内容,更多关于Kubernetes CI与CD配置的资料请关注软件技术网其它相关文章!

原创文章,作者:starterknow,如若转载,请注明出处:https://www.starterknow.com/109022.html

联系我们