前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Jenkins流水线发布实现CICD到Kubernetes

Jenkins流水线发布实现CICD到Kubernetes

作者头像
烂猪皮
发布2020-12-29 14:43:44
6900
发布2020-12-29 14:43:44
举报
文章被收录于专栏:JAVA烂猪皮JAVA烂猪皮

第一步 本地安装好Kubernetes

第二步— Install Jenkins

a) Install Java

代码语言:javascript
复制
sudo apt update
sudo apt install openjdk-8-jdk

b) Add Jenkins Repository & append package repository address to the server’s source.list

代码语言:javascript
复制
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'

c) Install Jenkins

代码语言:javascript
复制
sudo apt update
sudo apt install jenkins

d) check Jenkins status

e) Configure Jenkins with necessary credentials and login to Jenkins

f) Add Jenkins to the docker group “sudo usermod -aG docker jenkins”

g) Install the following plugins for Jenkins

Docker Pipeline Kubernetes Kubernetes Continuous Deploy

h) The final step is to use ngrok to expose localhost Jenkins URL as public URL.

Download ngrok and execute it as shown in the image, http://a0ecbd0426f6.ngrok.io/ is the new URL to login to Jenkins and accessible over the internet.

使用ngrok 内网穿透实现本地jenkins互联网能够访问到

第三步: 配置Jenkins Configure Jenkins

a) 选择 流水线项目Select Demo1 as New Pipeline Project

b) 填入 the GitHub link的仓库地址

c) Mention the build trigger配置构建触发器

d) 在流水线中配置GIT地址 In the Pipeline mention the git links and the branch it has to operate upon

e) 配置dockerhub密钥 Configure docker hub credentials

f) Update Jenkinsfile -Its a declarative pipeline which is used as a code. It helps the pipeline code easier to read and write. This code is written in a Jenkinsfile.

当代码提交到github时,Jenkins会收到一个webhook已配置于Github, 整个阶段流程如下:

In this implementation, the moment code is checked-in, Jenkins will have a notification through a webhook configured in Github. it follows the following stages below

i) Check out the source

ii)Build the new image

iii) Push it to the new repository

iv) Deploy the app on Kube

代码语言:javascript
复制
pipeline {

  agent any

  stages {

    stage('Checkout Source') {
       steps {
         git url:'https://github.com/vamsijakkula/hellowhale.git', branch:'master'
       }
     }

       stage("Build image") {
             steps {
                 script {
                     myapp = docker.build("vamsijakkula/hellowhale:${env.BUILD_ID}")
                 }
             }
         }

       stage("Push image") {
             steps {
                 script {
                     docker.withRegistry('https://registry.hub.docker.com', 'dockerhub') {
                             myapp.push("latest")
                             myapp.push("${env.BUILD_ID}")
                     }
                 }
             }
         }


     stage('Deploy App') {
       steps {
         script {
           kubernetesDeploy(configs: "hellowhale.yml", kubeconfigId: "mykubeconfig")
         }
       }
     }

  }

}

g) Have the configuration of mykubeconfig in Jenkins defined

h) Also have the connection between Jenkins and Kubernetes in place

i) Mention the Jenkins URL

j)Update the pod template.

k) Configure the webhook in Github

第4步 — Develop application deployment manifest. In this tutorial, we are deploying a simple hellowhale app exposed as NodePort.

代码语言:javascript
复制
---
apiVersion: apps/v1
kind: Deployment
metadata:
   name: hello-blue-whale
spec:
   replicas: 3
   selector:
     matchLabels:
       app: hello-whale-app
       version: blue
   template:
     metadata:
       name: hello-blue-whale-pod
       labels:
         app: hello-whale-app
         version: blue
     spec:
       containers:
       - name: hello-whale-container
         image: vamsijakkula/hellowhale:latest
         imagePullPolicy: Always
         ports:
         - containerPort: 80
---
apiVersion: v1
kind: Service 
metadata:
   name: hello-whale-svc
   labels:
     app: hello-whale-app
spec:
   selector:
     app: hello-whale-app
     version: blue
   type: NodePort
   ports:
   - nodePort: 31113
     port: 80
     targetPort: 80

第5步: Check-in the application manifest in GitHub and we can see the deployment starting in Jenkins

第6步 Once the deployment is completed, we can see the following pods(3) are in place and exposed as NodePort(31113)

Access the deployment on NodePort.

第7步: 通过deployment调整实例从3个到5个,如下在kube可以看到。Update the deployment by changing the replica’s from 3 to 5 and we can see 5 pods are deployed on Kube.

GIT示例仓库 https://github.com/vamsijakkula/hellowhale

总结

此示例以本地环境部署Kubernetes的2个简单结点,内网穿透暴露于互联网, 基于nginx的前端程序构建docker,通过jenkins的pipeline流水线发布到Kubernetes. 实现CI CD过程,示例代码于公网。对云原生技术CI CD是关键的DevOps基础过程,需要今天广大开发工程师了解。

看完本文记得给作者点赞+在看哦~~~大家的支持,是作者源源不断出文的动力

作者:PetterLiu

出处:https://www.cnblogs.com/wintersun/p/13905167.html

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-12-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 JAVA烂猪皮 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档