Jenkins在Kubernetes中持续部署

网友投稿 1025 2022-11-30

Jenkins在Kubernetes中持续部署

Jenkins在Kubernetes中持续部署

Jenkins在Kubernetes中持续部署

pipeline { agent { kubernetes { label "jenkins-slave" yaml '''apiVersion: v1kind: Podmetadata: name: jenkins-slavespec: containers: - name: jnlp image: jenkins-slave-jdk:1.8 imagePullPolicy: Never''' } } stages { stage('TestAgent') { steps { sh 'hostname' } } stage('CheckOut') { steps { checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'bcfe14cb-2215-4862-a472-641f0241b573', url: ' } } stage('Build') { steps { script{ sh 'ls' sh 'pwd' } } } }}

Started by user adminRunning in Durability level: MAX_SURVIVABILITY[Pipeline] Start of Pipeline[Pipeline] echo[WARNING] label option is deprecated. To use a static pod template, use the 'inheritFrom' option.[Pipeline] podTemplate[Pipeline] {[Pipeline] nodeCreated Pod: kubernetes ops/jenkins-slave-4l66j-8c8gs[Normal][ops/jenkins-slave-4l66j-8c8gs][Scheduled] Successfully assigned ops/jenkins-slave-4l66j-8c8gs to node2[Normal][ops/jenkins-slave-4l66j-8c8gs][Pulled] Container image "jenkins-slave-jdk:1.8" already present on machine[Normal][ops/jenkins-slave-4l66j-8c8gs][Created] Created container jnlp[Normal][ops/jenkins-slave-4l66j-8c8gs][Started] Started container jnlpAgent jenkins-slave-4l66j-8c8gs is provisioned from template jenkins-slave-4l66j---apiVersion: "v1"kind: "Pod"metadata: annotations: buildUrl: " runUrl: "job/devops-eurka-service/1/" labels: jenkins: "slave" jenkins/label-digest: "03ddc3eddf95d5470d5c7fb6d2937abaeca3b79e" jenkins/label: "jenkins-slave" name: "jenkins-slave-4l66j-8c8gs"spec: containers: - env: - name: "JENKINS_SECRET" value: "********" - name: "JENKINS_AGENT_NAME" value: "jenkins-slave-4l66j-8c8gs" - name: "JENKINS_NAME" value: "jenkins-slave-4l66j-8c8gs" - name: "JENKINS_AGENT_WORKDIR" value: "/home/jenkins/agent" - name: "JENKINS_URL" value: " image: "jenkins-slave-jdk:1.8" imagePullPolicy: "Never" name: "jnlp" resources: limits: {} requests: memory: "256Mi" cpu: "100m" volumeMounts: - mountPath: "/home/jenkins/agent" name: "workspace-volume" readOnly: false nodeSelector: kubernetes.io/os: "linux" restartPolicy: "Never" volumes: - emptyDir: medium: "" name: "workspace-volume"Running on jenkins-slave-4l66j-8c8gs in /home/jenkins/agent/workspace/devops-eurka-service[Pipeline] {[Pipeline] stage[Pipeline] { (TestAgent)[Pipeline] sh+ hostnamejenkins-slave-4l66j-8c8gs[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (CheckOut)[Pipeline] checkoutThe recommended git tool is: NONEusing credential bcfe14cb-2215-4862-a472-641f0241b573Cloning the remote Git repositoryCloning repository > git init /home/jenkins/agent/workspace/devops-eurka-service # timeout=10Fetching upstream changes from > git --version # timeout=10 > git --version # 'git version 1.8.3.1'using GIT_ASKPASS to set credentials gitlab-devop > git fetch --tags --progress +refs/heads/*:refs/remotes/origin/* # timeout=10Avoid second fetchChecking out Revision 8a64bae344504217761314054e6dec360e22cc89 (refs/remotes/origin/master) > git config remote.origin.url # timeout=10 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git config core.sparsecheckout # timeout=10 > git checkout -f 8a64bae344504217761314054e6dec360e22cc89 # timeout=10Commit message: "all"First time build. Skipping changelog.[Pipeline] }[Pipeline] // stage[Pipeline] stage (hide)[Pipeline] { (Build)[Pipeline] script[Pipeline] {[Pipeline] sh+ lsLICENSEREADME.mdbasic-commondbeureka-servicegateway-servicek8slombok.configorder-servicepom.xmlportal-serviceproduct-servicestock-service[Pipeline] sh+ pwd/home/jenkins/agent/workspace/devops-eurka-service[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // node[Pipeline] }[Pipeline] // podTemplate[Pipeline] End of PipelineFinished: SUCCESS

[root@master ~]# kubectl get pod -n ops -wNAME READY STATUS RESTARTS AGEgrafana-757fcd5f7c-fx72b 1/1 Running 15 11djenkins-797ccbdbc5-88qrd 1/1 Running 6 45hjenkins-slave-mhzxx-g29pz 0/1 Pending 0 0sjenkins-slave-mhzxx-g29pz 0/1 Pending 0 0sjenkins-slave-mhzxx-g29pz 0/1 ContainerCreating 0 0sjenkins-slave-mhzxx-g29pz 0/1 ContainerCreating 0 1sjenkins-slave-mhzxx-g29pz 1/1 Running 0 2sjenkins-slave-mhzxx-g29pz 1/1 Terminating 0 4m30sjenkins-slave-mhzxx-g29pz 1/1 Terminating 0 4m31sjenkins-slave-mhzxx-g29pz 0/1 Terminating 0 4m31sjenkins-slave-mhzxx-g29pz 0/1 Terminating 0 4m39sjenkins-slave-mhzxx-g29pz 0/1 Terminating 0 4m39s

自动部署应用(yaml): 将kubectl工具封装到Slave镜像中,并通过Config File Provider插件存储连接K8s集群的kubeconfig认证文件,然后挂载到Slave容器中,这样就能用kubectl apply deploy.yaml --kubeconfig=config

注:为提高安全性,kubeconfig文件应分配权限 除了上述方式,还可以使用Kubernetes Continuous Deploy插件,将资源配置(YAML)部署到Kubernetes,这种 不是很灵活性。

def project = "dev"def app_name = "java-demo"def image_name = "${registry}/${project}/${app_name}:${BUILD_NUMBER}"pipeline { agent { kubernetes { label "jenkins-slave" yaml '''apiVersion: v1kind: Podmetadata: name: jenkins-slavespec: containers: - name: jnlp image: jenkins-slave-jdk:1.8 imagePullPolicy: Never volumeMounts: - name: docker-cmd mountPath: /usr/bin/docker - name: docker-sock mountPath: /var/run/docker.sock - name: maven-cache mountPath: /root/.m2 volumes: - name: docker-cmd hostPath: path: /usr/bin/docker - name: docker-sock hostPath: path: /var/run/docker.sock - name: maven-cache hostPath: path: /tmp/m2''' } } stages { stage('TestAgent') { steps { sh 'hostname' } } stage('CheckOut') { steps { checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${credentialsId}", url: "${srcUrl}"]]]) } } stage('Build') { steps { script{ sh """ mvn clean package -Dmaven.test.skip=true mvn -v """ } } } stage('Artifact') { steps { script{ withCredentials([usernamePassword(credentialsId: 'd8e0980c-1ffb-4096-a0b7-8f9379982c3f', passwordVariable: 'passwd', usernameVariable: 'user')]) { sh """ docker build -t ${image_name} . docker login -u ${user} -p ${passwd} ${registry} docker push ${image_name} """ } } } } stage('Deploy') { steps { script{ configFileProvider([configFile(fileId: '22bdbd6d-14f4-49c1-a924-5c844073ea69', targetLocation: "admin.kubeconfig")]){ sh """ sed -i 's#IMAGE_NAME#${image_name}#' deployment.yaml sed -i 's#REPLICAS#${ReplicaCount}#' deployment.yaml kubectl apply -f deployment.yaml -n ${Namespace} --kubeconfig=admin.kubeconfig """ } } } } }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Jenkins Config File Provider 插件 创建kubeconfig文件
下一篇:数据链路层 抓包观察 以太网 PPP HDLC数据链路层封装
相关文章

 发表评论

暂时没有评论,来抢沙发吧~