Background:
Follow closely Use of spinnaker in Kubernetes . Various simple Triggers are completed, and deploy Mainfest deploys a simple pipeline of kubernetes. According to the actual environment, I want to go deeper into the pipeline steps: parametric construction, webhook triggering, e-mail sending, jenkins pipeline integration and so on
First of all, the pipeline is composed of multiple stage s:
For the default stage, please refer to the official website: https://spinnaker.io/docs/reference/pipeline/stages/ . The environment is mainly kubernetes environment, which focuses on:
Start with a pipeline
Create application
Create application pipeline test permissions and give the operation and maintenance group read, write and execute permissions (other group permissions depend on individual needs and are only used for demonstration here)
Create pipeline
Create pipeline -- Parameters-test1
Parameters - about parametric construction
Preparation premise:
The parameterized build is in the Configuration step
According to common practice Use of spinnaker in Kubernetes The assembly line in is used for experiment!
apiVersion: apps/v1 kind: Deployment metadata: labels: k8s-app: nginxdemo name: nginxdemo namespace: dev spec: replicas: 1 selector: matchLabels: k8s-app: nginxdemo template: metadata: labels: k8s-app: nginxdemo name: nginxdemo namespace: dev spec: containers: - image: 'harbor.xxxx.com/spinnaker/spinnaker-nginx-demo:1.2.4' imagePullPolicy: Always name: nginxdemo ports: - containerPort: 80 name: web protocol: TCP imagePullSecrets: - name: harbor-layame
Define Parameters:
Define the image parameter and set the default image tag to nginx:1.18.0
deploy Mainfest
The stage name here can be customized. Directly set the stage name as publishing application:
Manifest Configuration
apiVersion: apps/v1 kind: Deployment metadata: labels: k8s-app: nginxdemo name: nginxdemo namespace: dev spec: replicas: 1 selector: matchLabels: k8s-app: nginxdemo template: metadata: labels: k8s-app: nginxdemo name: nginxdemo namespace: dev spec: containers: - image: '${ parameters.image }' imagePullPolicy: Always name: nginxdemo ports: - containerPort: 80 name: web protocol: TCP
Add webhook stage save pipeline
Payload is as follows: of course, you can also improve it in more details!
{ "msgtype": "text", "text": { "content": "Assembly line ${execution['name']}In operation, Run user ${execution['trigger']['user']}" }
Operation in cluster
Ensure that the dev namespace exists (delete the existing deployments here for comparison!)
run the pipeline and verify the deployment results
In the pipelines interface, Start Manual Execution, select Parameters-test1 pipeline image, and use the default nginx:1.18.0, Run
The OK results are as follows: the pipeline status is SUCCEEDED, wechat webhook receives the notification, and the deployments under dev namespace are successfully deployed.
Add parameters replicas parameter
Why choose the number of relicas copies as an example? To emphasize parameterization, the value tolnt is used when deploying non string values
replicas: '${#toInt(parameters.replicas)}'
The Manifest Configuration is as follows:
apiVersion: apps/v1 kind: Deployment metadata: labels: k8s-app: nginxdemo name: nginxdemo namespace: dev spec: replicas: '${#toInt(parameters.replicas)}' selector: matchLabels: k8s-app: nginxdemo template: metadata: labels: k8s-app: nginxdemo name: nginxdemo namespace: dev spec: containers: - image: '${ parameters.image }' imagePullPolicy: Always name: nginxdemo ports: - containerPort: 80 name: web protocol: TCP
running pipeline. Here, in order to distinguish from the last replica, I manually entered 2!
The verification results are as follows:
Pipeline rollback
Temporary rollback (ignore not found)
There is a temporary rollback method in the course of Zeyang boss:
However, the layout of version 1.26.6 seems to have changed. I looked around and didn't find a temporary rollback or a full automatic rollback!
Configure rollback
Let's talk about the stage in the assembly line first
Configuration is not included by default. The publishing application is the first one to add. Its id is 0. The deployment notification is the second stage. Its id is 1. The Manual Judgment is the third one to create. Its id is 2. Undo Rollout is the last one to add. Its id is 3.
This place seems a little winding. Stages start from 0 and are created in the order in which they are created.
Create Manual Judgment stage
Manually judge the stage. Add the rollback done option. By convention, done is completion and rollback is rollback. As the trigger condition of Undo Rollout in the next step!
Manual Judgment rollback stage selection
Emphasize that the id of Manual Judgment is 2. So the stages here is 2. The meaning of the following expression is that the judgmentInput in pipeline stages 2 is rollback.
${ execution['stages'][2].context.judgmentInput == "rollback" }
Run pipeline verification:
It has been verified..... The image of the pod in the specific cluster will not be screenshot. The most important thing to pay attention to here is the use of expressions!
Improve the assembly line on the first line:
Take the spinner nginx demo pipeline in jenkins. Let's talk about what we want to achieve first:
Well, gitlab triggers jenkins to package the image (here redefine the image tag time). Then propagate the parameters to the spinnaker trigger!
git warehouse and jenkins configuration:
reference jenkins Trigger Of course, a little parameterized construction has been modified here, and the dynimic parameter (how to find a plug-in without Baidu) parameter has been added. Define the name data (you can define the name yourself. You can use data for personal habits)!
The Default Value Script is as follows:
return new Date().format('yyyyMMddHHmm')
jenkins pipeline
Change the image tag to data
//Docker image warehouse information registryServer = "harbor.xxxxx.com" projectName = "${JOB_NAME}".split('-')[0] repoName = "${JOB_NAME}" imageName = "${registryServer}/${projectName}/${repoName}" //pipeline pipeline{ agent { node { label "build01"}} //Set build trigger triggers { GenericTrigger( causeString: 'Generic Cause', genericVariables: [[defaultValue: '', key: 'branchName', regexpFilter: '', value: '$.ref']], printContributedVariables: true, printPostContent: true, regexpFilterExpression: '', regexpFilterText: '', silentResponse: true, token: 'spinnaker-nginx-demo') } stages{ stage("CheckOut"){ steps{ script{ srcUrl = "https://gitlab.xxxx.com/zhangpeng/spinnaker-nginx-demo.git" branchName = branchName - "refs/heads/" currentBuild.description = "Trigger by ${branchName}" println("${branchName}") checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlab-admin-user', url: "${srcUrl}"]]]) } } } stage("Push Image "){ steps{ script{ withCredentials([usernamePassword(credentialsId: 'harbor-admin-user', passwordVariable: 'password', usernameVariable: 'username')]) { sh """ sed -i -- "s/VER/${branchName}/g" app/index.html docker login -u ${username} -p ${password} ${registryServer} docker build -t ${imageName}:${data} . docker push ${imageName}:${data} docker rmi ${imageName}:${data} """ } } } } stage("Trigger File"){ steps { script{ sh """ echo IMAGE=${imageName}:${data} >trigger.properties echo ACTION=DEPLOY >> trigger.properties cat trigger.properties """ archiveArtifacts allowEmptyArchive: true, artifacts: 'trigger.properties', followSymlinks: false } } } } }
Take a look at trigger.properties
pipeline settings in spinnaker
Create pipeline
Create spinnaker nginx demo pipeline in pipeline test applications. Here, I directly copy the pipeline of Parameters-test1
Automated Triggers Configuration in Configuration
jenkins trigger and add Property File (artifact in jenkins) trigger.properties (note that no more spaces are copied)
Note: the image parameter is deleted under the Parameters configuration, but the replicas parameter is retained. The default parameter for auto trigger is still 2, so the number of subsequent copies is 2.
Deploy (Manifest) Configuration
image: "${trigger['properties']['IMAGE']}"
Undo Rollout (Manifest) rolls back the configuration of the application
git file modification file trigger and jenkins linkage
jenkins build 79
Log in to spinnaker web for verification
Choose done first
This is the following. The rollback stage is skipped
Verify mirroring
Rollback rollback verification
Select rollback again
Here, spinnaker shows that jenkins builds 80 slightly slower
Pipeline webhook information
Of course, you can personalize the actions and build parameters here? Here is just a simple run through. You can input and share more fun in the follow-up
Postscript
- The id of the pipeline stage in the pipeline tool. You must pay attention to this place
- toInt for non string parameters
- The space in the parameter when lazy copying nouns
- It's the same... I refitted spinnaker to practice. When setting up external redis, the configuration file Ctrip redis.yaml has not taken effect... I found the problem for a long time and finally found it. Remember yaml vs yml.
- Only learn simple and practical functions, and don't study complex ones. For further research, please refer to the spinnaker course of Mr. Zeyang. Special thanks to big brother Zeyang. jenkins spinnaker courses follow the steps of big guys! devops cloud School of Zeyang boss.