基于kubernetes和jenkins的gitops实战-jenkins安装
作者:互联网
[root@master01 jenkins]# cat 02-rbac.yaml apiVersion: v1 kind: ServiceAccount metadata: name: jenkins-sa namespace: devops --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: jenkins-cr rules: - apiGroups: ["extensions", "apps"] resources: ["deployments"] verbs: ["create", "delete", "get", "list", "watch", "patch", "update"] - apiGroups: [""] resources: ["services"] verbs: ["create", "delete", "get", "list", "watch", "patch", "update"] - 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: ClusterRoleBinding metadata: name: jenkins-crd roleRef: kind: ClusterRole name: jenkins-cr apiGroup: rbac.authorization.k8s.io subjects: - kind: ServiceAccount name: jenkins-sa namespace: devops
RBAC
[root@master01 jenkins]# cat 03-jenkins-svc.yaml apiVersion: v1 kind: Service metadata: name: jenkins namespace: devops labels: app: jenkins spec: selector: app: jenkins type: NodePort ports: - name: web port: 8080 targetPort: web nodePort: 31080 - name: agent port: 50000 targetPort: 50000
jenkins的svc
[root@master01 jenkins]# cat 04-jenkins-deploy.yaml apiVersion: apps/v1 kind: Deployment metadata: name: jenkins namespace: devops spec: replicas: 1 selector: matchLabels: app: jenkins template: metadata: labels: app: jenkins spec: nodeName: node01 terminationGracePeriodSeconds: 10 serviceAccount: jenkins-sa containers: - name: jenkins image: jenkins/jenkins:2.287-jdk11 imagePullPolicy: IfNotPresent env: - name: JAVA_OPTS value: -XshowSettings:vm -Dhudson.slaves.NodeProvisioner.initialDelay=0 -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85 -Duser.timezone=Asia/Shanghai ports: - containerPort: 8080 name: web protocol: TCP - containerPort: 50000 name: agent protocol: TCP resources: limits: cpu: 1000m memory: 4Gi requests: cpu: 500m memory: 512Mi livenessProbe: httpGet: path: /login port: 8080 initialDelaySeconds: 60 timeoutSeconds: 5 failureThreshold: 12 readinessProbe: httpGet: path: /login port: 8080 initialDelaySeconds: 60 timeoutSeconds: 5 failureThreshold: 12 volumeMounts: - name: jenkinshome mountPath: /var/jenkins_home securityContext: fsGroup: 1000 volumes: - name: jenkinshome hostPath: type: DirectoryOrCreate path: /mnt/nfs/jenkins
jenkins的deoply
使用:
1. 安装kubernetes插件,
2. 如果需要修改为国内镜像,就在主界面右下角有一个“jenkins中文社区”
3. 配置kubernetes集群:
jenkins的配置如下:
[root@master01 jenkins]# kubectl get svc -n devops NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE gitlab-svc NodePort 10.110.90.27 <none> 80:30080/TCP,443:34638/TCP,22:30022/TCP 45m jenkins NodePort 10.103.165.206 <none> 8080:31080/TCP,50000:64091/TCP 7d1h #8080是http端口,50000是他的ftp端口 openldap NodePort 10.96.104.144 <none> 389:389/TCP,636:636/TCP,80:8080/TCP 7d2h
4. 配置pod模板
5. 可以使用了,agent选择使用kubernetes,于kubernetes集群上指定的pod运行stage或pipeline代码,该参数同样要求jenkinsfile必须从Multibranch Pipeline或者Pipeline from SCM中加载。
该操作需要在kubernetes参数中指定Pod模板
创建一个流水线任务:
pipeline { agent { kubernetes{ inheritFrom 'jenkins-slave' #选择使用的pod模板,jenkins为自己定义的pod模板 } } stages { stage ("hello") { steps { sh 'java -version' } } } }
jenkins会在kubernetes上构建pod:Created Pod: kubernetes devops/pipeline-demo-01-1-3c2dm-dgmqj-pcjr6
slave镜像使用是:image: "jenkins/inbound-agent:4.3-4"
以上,我们的分布式节点就会以Pod来运行了,后面每次构建都会新建一个pod来处理任务。
6. pod模板定义根据项目来的,如果是go环境需要找go镜像,可以做go构建的镜像;如果是maven工程,就找maven镜像等;
下面以maven环境测试,
首先需要配置pod模板:
测试maven环境是否可用
pipeline { agent { kubernetes{ inheritFrom 'maven-3.6' #刚刚创建的pod模板 } } stages { stage ("hello") { steps { container('maven'){ sh 'mvn -version' } } } } }
现在开始测试镜像打包功能,需要使用到docker镜像:docker-in-docker,需要安装docker-pipeline插件;
同时,编辑集群,新增一个maven-docker的Pod模板,他的父级模板为maven。
docker镜像需要在主机上运行并完成打包推送行为,就需要使用主机的docker.sock文件,所以需要将宿主机的docker.sock文件使用hostpath方式映射到pod模板里:
编写pipeline:
pipeline { environment { appName = "spring-boot-helloworld" appVersion = "v0.9.0" } agent { kubernetes { inheritFrom 'maven-docker' } } stages { stage('Source') { steps { git branch: 'master',url: 'http://192.168.3.100:30080/root/spring-boot-helloworld.git' } } stage('Build'){ steps { container('maven') { sh 'mvn clean test package' } } } stage('Building app image') { steps { container('docker') { script { dockerImage = docker.build appName + ":" + appVersion } } } } } }
备注:前面关于maven的数据卷配置地址写错了,应该是:/root/.m2
结果验证如下:
[root@node02 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE spring-boot-helloworld v0.9.0 1c14896dc20e 3 minutes ago 165MB [root@node02 ~]# docker run --name test --rm spring-boot-helloworld:v0.9.0 . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.3.RELEASE) [root@node02 ~]# docker ps -a |grep test 61e01064cf21 spring-boot-helloworld:v0.9.0 "/bin/sh -c '/opt/ja…" About a minute ago Up About a minute test [root@node02 ~]# docker inspect test …… "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", …… [root@node02 ~]# curl 172.17.0.2 Hello Spring Boot 2.0!
标签:gitops,name,kubernetes,maven,jenkins,docker,root 来源: https://www.cnblogs.com/wuhuanchn/p/15159596.html