当前位置:   article > 正文

docker+jenkins+gitlab+k8s实现自动化部署_gitlab+jenkins+docker+kubernetes 实现容器自动化部署

gitlab+jenkins+docker+kubernetes 实现容器自动化部署

1 环境准备

192.168.58.128k8s(master节点)
192.168.58.129k8s(node1节点)
192.168.58.130k8s(node2节点)
192.168.58.131jenkins-agent节点
192.168.58.132gitlab
192.168.58.133jenkins 节点

jenkins-agent 节点需要安装 jdk  maven git ,这台机器用于拉取代码  构建代码 

部署采用流水线的方式部署

jenkins 配置

配置从节点 

jenkins-agent节点  和master 节点进行免密登录

jenkins-agent节点

保存

新建流水线测试:

测试打印输出192.168.58.131 的hostname

保存  立即构建

控制台输出

拉取代码

片段生成器生成拉取git 代码的命令

  1. pipeline {
  2. agent {
  3. label 'jenkins-pipeline-k8s-131'
  4. }
  5. stages {
  6. stage('环境测试') {
  7. steps {
  8. sh '''java -version
  9. git version
  10. docker -v
  11. mvn -v'''
  12. }
  13. }
  14. stage('拉取代码') {
  15. steps {
  16. checkout scmGit(branches: [[name: '*/master']], extensions: [], gitTool: '', userRemoteConfigs: [[credentialsId: '698f26a1-7b10-4cef-8f06-ccf0bf7fd96c', url: 'ssh://git@192.168.58.132:222/root/jenkinsdemo.git']])
  17. }
  18. }
  19. }
  20. }

保存立即构建

构建时有可能会报如下错误:

  1. Cloning repository ssh://git@192.168.58.132:222/root/jenkinsdemo.git
  2. ERROR: Error cloning remote repo 'origin'
  3. hudson.plugins.git.GitException: Could not init /data/k8s/workspace/jenkins-pipeline-k8s
  4. at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$5.execute(CliGitAPIImpl.java:1073)
  5. at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$2.execute(CliGitAPIImpl.java:819)
  6. at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler$GitCommandMasterToSlaveCallable.call(RemoteGitImpl.java:170)
  7. at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler$GitCommandMasterToSlaveCallable.call(RemoteGitImpl.java:161)
  8. at hudson.remoting.UserRequest.perform(UserRequest.java:211)
  9. at hudson.remoting.UserRequest.perform(UserRequest.java:54)
  10. at hudson.remoting.Request$2.run(Request.java:377)
  11. at hudson.remoting.InterceptingExecutorService.lambda$wrap$0(InterceptingExecutorService.java:78)
  12. at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
  13. at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
  14. at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
  15. at java.base/java.lang.Thread.run(Thread.java:829)
  16. Suppressed: hudson.remoting.Channel$CallSiteStackTrace: Remote call to jenkins-pipeline-k8s-131
  17. at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1787)
  18. at hudson.remoting.UserRequest$ExceptionResponse.retrieve(UserRequest.java:356)
  19. at hudson.remoting.Channel.call(Channel.java:1003)
  20. at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler.execute(RemoteGitImpl.java:153)
  21. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  22. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
  23. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  24. at java.base/java.lang.reflect.Method.invoke(Method.java:568)
  25. at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler.invoke(RemoteGitImpl.java:138)
  26. at jdk.proxy98/jdk.proxy98.$Proxy190.execute(Unknown Source)
  27. at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1222)
  28. at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1305)
  29. at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:129)
  30. at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:97)
  31. at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:84)
  32. at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
  33. at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
  34. at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
  35. at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
  36. at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
  37. at java.base/java.lang.Thread.run(Thread.java:840)
  38. Caused by: hudson.plugins.git.GitException: Error performing git command: init /data/k8s/workspace/jenkins-pipeline-k8s
  39. at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2858)
  40. at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2762)
  41. at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2757)
  42. at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommand(CliGitAPIImpl.java:2051)
  43. at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$5.execute(CliGitAPIImpl.java:1071)
  44. ... 11 more
  45. Caused by: java.io.IOException: Cannot run program "" (in directory "/data/k8s/workspace/jenkins-pipeline-k8s"): error=2, 没有那个文件或目录
  46. at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
  47. at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
  48. at hudson.Proc$LocalProc.<init>(Proc.java:252)
  49. at hudson.Proc$LocalProc.<init>(Proc.java:221)
  50. at hudson.Launcher$LocalLauncher.launch(Launcher.java:994)
  51. at hudson.Launcher$ProcStarter.start(Launcher.java:506)
  52. at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2835)
  53. ... 15 more
  54. Caused by: java.io.IOException: error=2, 没有那个文件或目录
  55. at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
  56. at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:340)
  57. at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:271)
  58. at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
  59. ... 21 more

此错误是因为在192.168.58.131 上找不到git 命令,解决方法为在节点信息中添加git 的目录

重新构建

构建成功

代码存在工作目录下

构建代码

会在相应的目录生成target 文件

构建镜像

此处在工程里边引用了dockerfile插件直接推送的harbor仓库

pom.xml文件详情

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.3.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.zrz</groupId>
  12. <artifactId>jenkinsdemo</artifactId>
  13. <version>1.0</version>
  14. <name>jenkinsdemo</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. <exclusions>
  29. <exclusion>
  30. <groupId>org.junit.vintage</groupId>
  31. <artifactId>junit-vintage-engine</artifactId>
  32. </exclusion>
  33. </exclusions>
  34. </dependency>
  35. </dependencies>
  36. <build>
  37. <finalName>jenkinsdemo</finalName>
  38. <plugins>
  39. <plugin>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-maven-plugin</artifactId>
  42. </plugin>
  43. <plugin>
  44. <groupId>com.spotify</groupId>
  45. <artifactId>dockerfile-maven-plugin</artifactId>
  46. <version>1.4.13</version>
  47. <configuration>
  48. <repository>192.168.58.131:8888/jenkins-demo/${project.build.finalName}</repository>
  49. <username>admin</username>
  50. <password>123456</password>
  51. <!-- <repository>${project.build.finalName}</repository>-->
  52. <tag>${project.version}</tag>
  53. <buildArgs>
  54. <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
  55. </buildArgs>
  56. </configuration>
  57. </plugin>
  58. </plugins>
  59. </build>
  60. </project>

将deployment.yaml service.yaml 文件复制到k8s 集群中

k8s 上部署服务:

完整的pipline 脚本

  1. pipeline {
  2. agent {
  3. label 'jenkins-pipeline-k8s-131'
  4. }
  5. stages {
  6. stage('环境测试') {
  7. steps {
  8. sh '''java -version
  9. git version
  10. docker -v
  11. mvn -v'''
  12. }
  13. }
  14. stage('拉取代码') {
  15. steps {
  16. checkout scmGit(branches: [[name: '*/master']], extensions: [], gitTool: '', userRemoteConfigs: [[credentialsId: '698f26a1-7b10-4cef-8f06-ccf0bf7fd96c', url: 'ssh://git@192.168.58.132:222/root/jenkinsdemo.git']])
  17. }
  18. }
  19. stage('构建代码') {
  20. steps {
  21. sh 'mvn clean package'
  22. }
  23. }
  24. stage('删除容器'){
  25. steps{
  26. sh ''' echo \'查询镜像是否存在\'
  27. a=`docker ps -a |grep jenkinsdemo |awk \'{print $1}\'`
  28. if [ "$a" != "" ];then
  29. echo \'删除容器\'
  30. docker rm -f $a
  31. echo \'删除成功\'
  32. fi
  33. '''
  34. }
  35. }
  36. stage('删除镜像'){
  37. steps{
  38. sh ''' echo \'查询镜像是否存在\'
  39. a=`docker images |grep jenkinsdemo |awk \'{print $3}\'`
  40. if [ "$a" != "" ];then
  41. echo \'删除镜像\'
  42. docker rmi -f $a
  43. echo \'删除成功\'
  44. fi
  45. '''
  46. }
  47. }
  48. stage('构建镜像') {
  49. steps {
  50. echo '构建镜像'
  51. sh 'mvn dockerfile:build dockerfile:push'
  52. }
  53. }
  54. stage('复制yaml文件') {
  55. steps {
  56. sh '''pwd
  57. scp deployment.yaml root@192.168.58.128:/root/deployment/jenkins-demo
  58. scp service.yaml root@192.168.58.128:/root/deployment/jenkins-demo'''
  59. }
  60. }
  61. stage('部署到k8s') {
  62. agent {
  63. label 'master-128'
  64. }
  65. steps {
  66. sh '''cd /root/deployment/jenkins-demo/
  67. kubectl apply -f deployment.yaml
  68. kubectl apply -f service.yaml
  69. '''
  70. }
  71. }
  72. }
  73. }

测试

访问192.168.58.128:30003

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/911801
推荐阅读
相关标签
  

闽ICP备14008679号