In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Deploy applications with k8s+jenkins+github+dockerhub:
There are two ways for jenkins to implement CI/CD
1. Free style, realized by shell code
2. Pipeline pipeline, which is realized by jenkins code or jenkinsfile file
There are two ways to create a Pipeline:
1. You can enter the jenkins code directly into the Web UI interface of Jenkins.
2. You can also create a Jenkinsfile script file and put it into the project source code library.
Steps to build a new project:
1. Create a new project on github
2. Clone the project locally
3. Write the code, dockerfile,jenkinsfile, and deploy the yaml file of the application
4. Push the project code to github
5. Use jenkins to implement CI/CD
Example:
1. Create a new project nginx-demo on github (abbreviated)
2. Clone the code to the local 192.168.1.244
$git clone https://github.com/dongyali521521/nginx-demo
Except for the first clone, each clone will delete the original project before clone.
$rm-rf nginx-demo
3. Modify the code
$cd nginx-demo/
$vim index.html
4. Push the modified code to github
$git add.
$git commit-m "2222"
$git push # need to enter the user name and password of github
5. Create a new task in jenkins to implement CI/CD
In general, in addition to the application code, dockerfile,jenkinsfile and the yaml file where the application is deployed are managed together in the source code.
A project includes at least the following files: all need to be ready before implementing CI/CD
$cd nginx-demo/
$ls
Dockerfile Jenkinsfile xxx.yaml index.html README.md
Using jenkins to implement CI/CD (pipeline mode) in k8s also includes the following steps:
1. Clone the project from git hub to jenkins slave pod's working directory / home/jenkins/agent/workspace/pipeline-name
2. Testing
3. Use Dockerfile to build an image
4. Push the constructed image to docker hub.
5. Modify the name of the image in the yaml file to the image you just built
6. Deploy applications in k8s cluster
The code of the above steps can be entered in the task configuration interface of jenkins master, or written into a Jenkinsfile file and managed in the project source code base.
The first four steps belong to the CI stage, and the last two steps belong to the CD phase.
Jenkins Pipeline has several core concepts:
Node: node. A Node is a Jenkins node, Master or slave.
Stage: phase, a Pipeline can be divided into several Stage, and each Stage represents a set of operations
Step: step. Step is the most basic unit of operation, either printing a sentence or building a Docker image.
Simulate CI/CD steps with jenkins code
New task-name-pipeline
Enter the following in the pipeline script-save build now
Node ('dongyali-jnlp') {stage (' Clone') {echo "1.Clone Stage"} stage ('Test') {echo "2.Test Stage"} stage (' Build') {echo "3.Build Docker Image Stage"} stage ('Push') {echo "4.Push Docker Image Stage"} stage (' YAML') {echo "5. Change YAML File Stage"} stage ('Deploy') {echo "6. Deploy Stage"}}
Suppose the project nginx-demo has been developed and uploaded to git hub
The documents include:
Dockerkfile k8s.yaml index.html README.md
K8s.yaml is the file where the application is deployed
$cd nginx-demo/
$cat Dockerfile
FROM docker.io/nginxMAINTAINER dongyaliADD index.html / usr/share/nginx/html/EXPOSE 80CMD ["nginx", "- g", "daemon off;"]
$cat k8s.yaml
ApiVersion: extensions/v1beta1kind: Deploymentmetadata: name: nginx-demo namespace: defaultspec: replicas: 3 template: metadata: labels: app: nginx-demo spec: containers:-image: dongyali521521/nginx-demo: imagePullPolicy: IfNotPresent name: nginx-demo ports:-containerPort: 80---apiVersion: v1kind: Servicemetadata: name: nginx-demo namespace: defaultspec: selector: app: nginx-demo type : NodePort ports:-protocol: TCP port: 80 targetPort: 80 name: myapp-http
If namespace is not specified, the application will be deployed to the kube-ops space of the K8s cluster by default, because jenkins slave pod runs in the kube-ops space.
Let's start deploying the above project in jenkins:
Since you have to build an image and redeploy the application after each git commit, the record of the git commit is used as the mirror tag
For security, when you push the built image to docker hub, you need to enter an encrypted user name and password
Credentials-Jenkins---- global credentials under Stores scoped to Jenkin-add credentials:
User name: user name of docker hub
Password: enter the user password for docker hub
ID:dockerHub, you can write anything you like, and you need to use it later.
Manual intervention is required before the CD phase begins to facilitate deployment to the environment of the user's choice
Http://192.168.1.243:30003
New task-name-pipeline
Enter the following in the pipeline script-save build now
Node ('dongyali-jnlp') {stage (' Clone') {echo "1.Clone Stage" git url: "https://github.com/dongyali521521/nginx-demo.git" script {build_tag = sh (returnStdout: true) Script: 'git rev-parse-- short HEAD'). Trim ()} stage (' Test') {echo "2.Test Stage"} stage ('Build') {echo "3.Build Docker Image Stage" sh "docker build-t dongyali521521/nginx-demo:$ {build_tag}."} stage (' Push') {echo "4.Push Docker Image Stage "withCredentials ([usernamePassword (credentialsId: 'dockerHub') PasswordVariable: 'dockerHubPassword', usernameVariable:' dockerHubUser']) {sh "docker login-u ${dockerHubUser}-p ${dockerHubPassword}" sh "docker push dongyali521521/nginx-demo:$ {build_tag}"} stage ('Deploy') {echo "5. Deploy Stage" def userInput = input (id:' userInput', message: 'Choose a deploy environment' Parameters: [$class: 'ChoiceParameterDefinition', choices: "Dev\ nQA\ nProd" Name: 'Env']]) echo "This is a deploy step to ${userInput}" sh "sed-I'Unix ${build_tag} /' k8s.yaml" if (userInput = = "Dev") {/ / deploy dev stuff} else if (userInput = = "QA") { / / deploy qa stuff} else {/ / deploy prod stuff} sh "kubectl apply-f k8s.yaml-- record"}}
Each time you build, a new jenkins slave pod is launched
View the deployment:
$kubectl get pod
Nginx-demo-65f6f679c6-5ljvd 1bat 1 Running 0 34m
Nginx-demo-65f6f679c6-p4g6m 1/1 Running 0 34m
Nginx-demo-65f6f679c6-xr7mx 1/1 Running 0 34m
$kubectl get svc
Nginx-demo NodePort 10.103.13.141 80:30185/TCP 18h
Http://http://192.168.1.243:30185/
Let's use the jenkinsfile file to deploy the application
Add a Jenkinsfile file to the project directory nginx-demo and upload it to git hub
$cd nginx-demo/
$cat Jenkinsfile
Node ('dongyali-jnlp') {stage (' Prepare') {echo "1.Prepare Stage" checkout scm script {build_tag = sh (returnStdout: true) Script: 'git rev-parse-- short HEAD'). Trim ()} stage (' Test') {echo "2.Test Stage"} stage ('Build') {echo "3.Build Docker Image Stage" sh "docker build-t dongyali521521/nginx-demo:$ {build_tag}."} stage (' Push') {echo "4.Push Docker Image Stage "withCredentials ([usernamePassword (credentialsId: 'dockerHub') PasswordVariable: 'dockerHubPassword', usernameVariable:' dockerHubUser']) {sh "docker login-u ${dockerHubUser}-p ${dockerHubPassword}" sh "docker push dongyali521521/nginx-demo:$ {build_tag}"} stage ('Deploy') {echo "5. Deploy Stage" def userInput = input (id:' userInput', message: 'Choose a deploy environment' Parameters: [$class: 'ChoiceParameterDefinition', choices: "Dev\ nQA\ nProd" Name: 'Env']]) echo "This is a deploy step to ${userInput}" sh "sed-I'Unix ${build_tag} /' k8s.yaml" if (userInput = = "Dev") {/ / deploy dev stuff} else if (userInput = = "QA") { / / deploy qa stuff} else {/ / deploy prod stuff} sh "kubectl apply-f k8s.yaml-- record"}}
Since Jenkinsfile and other code are in the git hub source library, the first step does not require clone, but checkout scm instead
Then select: in the jenkins configuration task interface:
Pipeline-pipeline script from SCM
SCM:git
Repository URL: https://github.com/dongyali521521/nginx-demo
Credentials: none
Just save the build.
Test one more project.
$git clone https://github.com/dongyali521521/jenkins-demo.git
$cd jenkins-demo
$ls
Dockerfile k8s.yaml main.go README.md
$cat Dockerfile
FROM golang:1.8.0-alpineADD. / go/src/appWORKDIR / go/src/appRUN GOOS=linux GOARCH=386 go build-v-o / go/src/app/jenkins-appCMD [". / jenkins-app"]
$cat main.go
Package main// Import the fmt for formatting strings// Import os so we can read environment variables from the systemimport ("fmt"os") func main () {fmt.Println ("Hello, Kubernetes! I'm from Jenkins CI! 4444") fmt.Println ("BRANCH_NAME:", os.Getenv ("branch"))}
$cat k8s.yaml
ApiVersion: extensions/v1beta1kind: Deploymentmetadata: name: jenkins-demo namespace: defaultspec: template: metadata: labels: app: jenkins-demo spec: containers:-image: dongyali521521/jenkins-demo: imagePullPolicy: IfNotPresent name: jenkins-demo env:-name: branch value:
$git add.
$git commit-m "4444"
$git push
New task-name-pipeline
Enter the following in the pipeline script-save build now
Node ('dongyali-jnlp') {stage (' Clone') {echo "1.Clone Stage" git url: "https://github.com/dongyali521521/jenkins-demo.git" script {build_tag = sh (returnStdout: true) Script: 'git rev-parse-- short HEAD'). Trim ()} stage (' Test') {echo "2.Test Stage"} stage ('Build') {echo "3.Build Docker Image Stage" sh "docker build-t dongyali521521/jenkins-demo:$ {build_tag}."} stage (' Push') {echo "4.Push Docker Image Stage "withCredentials ([usernamePassword (credentialsId: 'dockerHub') PasswordVariable: 'dockerHubPassword', usernameVariable:' dockerHubUser']) {sh "docker login-u ${dockerHubUser}-p ${dockerHubPassword}" sh "docker push dongyali521521/jenkins-demo:$ {build_tag}"} stage ('Deploy') {echo "5. Deploy Stage" def userInput = input (id:' userInput', message: 'Choose a deploy environment' Parameters: [$class: 'ChoiceParameterDefinition', choices: "Dev\ nQA\ nProd" Name: 'Env']]) echo "This is a deploy step to ${userInput}" sh "sed-I'Unix ${build_tag} /' k8s.yaml" if (userInput = = "Dev") {/ / deploy dev stuff} else if (userInput = = "QA") { / / deploy qa stuff} else {/ / deploy prod stuff} sh "kubectl apply-f k8s.yaml-- record"}}
$kubectl get pod
Jenkins-demo-67db45cf6c-mjdl4 0/1 CrashLoopBackOff 3 65s
$kubectl logs jenkins-demo-67db45cf6c-mjdl4
Hello, Kubernetes! I'm from Jenkins CI! 4444
BRANCH_NAME:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.