# Step 03. Rolling Update
Production 운영환경에 배포하는 방법으로 설명. Master branch사용
# Rolling update
# Deployment 수정
- spec.strategy.type: Rollingupdate # 배포방식 설정
- spec.strategy.rollingUpdate.maxSurge: 1 # up pod 최대 단위
- spec.strategy.rollingUpdate.maxUnavailable: 1 # down pod 최대 단위
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-boot-cicd-demo
labels:
app: spring-boot-cicd-demo
spec:
replicas: 5
selector:
matchLabels:
app: spring-boot-cicd-demo
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Pipeline 작성
sam-zcp-lab-rolling 이름으로 Pipeline작성
- Pipeline 설정에서 Parameter 설정 : VERSION 처리를 위해 Pipeline에 변수 추가
- General 영역에서 이 빌드는 매개변수가 있습니다. 체크
- 매개변수 추가 Click > String Parameter
- 매개변수명 : VERSION
- Default Value: develop
- Pipeline 영역
- Script Path : Production용 파일로 변경. jenkins-pipeline/rolling-pipeline
- Git project의 jenkins-pipeline/rolling-pipeline 파일 편집
- VERSION 변수선언 주석 처리
// def VERSION = 'develop'
1
- Job 설정의 Deploy 변경
@Library('retort-lib') _
def label = "jenkins-${UUID.randomUUID().toString()}"
def ZCP_USERID='edu01'
def DOCKER_IMAGE='edu01/sam-zcp-lab'
def K8S_NAMESPACE='edu01'
// def VERSION = 'develop'
podTemplate(label:label,
serviceAccount: "zcp-system-sa-${ZCP_USERID}",
containers: [
containerTemplate(name: 'maven', image: 'maven:3.5.2-jdk-8-alpine', ttyEnabled: true, command: 'cat'),
containerTemplate(name: 'docker', image: 'docker:17-dind', ttyEnabled: true, command: 'dockerd-entrypoint.sh', privileged: true),
containerTemplate(name: 'kubectl', image: 'lachlanevenson/k8s-kubectl:v1.13.6', ttyEnabled: true, command: 'cat')
],
volumes: [
persistentVolumeClaim(mountPath: '/root/.m2', claimName: 'zcp-jenkins-mvn-repo')
]) {
node(label) {
stage('SOURCE CHECKOUT') {
def repo = checkout scm
}
stage('BUILD') {
container('maven') {
mavenBuild goal: 'clean package', systemProperties:['maven.repo.local':"/root/.m2/${ZCP_USERID}"]
}
}
stage('BUILD DOCKER IMAGE') {
container('docker') {
dockerCmd.build tag: "${HARBOR_REGISTRY}/${DOCKER_IMAGE}:${VERSION}"
dockerCmd.push registry: HARBOR_REGISTRY, imageName: DOCKER_IMAGE, imageVersion: VERSION, credentialsId: 'HARBOR_CREDENTIALS'
}
}
stage('DEPLOY') {
container('kubectl') {
kubeCmd.apply file: 'k8s/service.yaml', namespace: K8S_NAMESPACE
yaml.update file: 'k8s/deployment.yaml', update: ['.spec.template.spec.containers[0].image': "${HARBOR_REGISTRY}/${DOCKER_IMAGE}:${VERSION}"]
kubeCmd.apply file: 'k8s/deployment.yaml', wait: 300, recoverOnFail: false, namespace: K8S_NAMESPACE
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 소스변경
- Open file(./src/main/resource/static/css/style.css)
- 47 line의 색상 값 변경 (background-color: #157ed2; --> background-color: red)
- Stage & Commit
- Push to origin/master(Git Server)
# Rolling update 실행
Click : Build with Parameters
VERSION 입력 : rolling-1
pod 상태 확인 : kubecrl get deploy -n edu01
# Rolling rollback
# Pipeline 설정
sam-zcp-lab-rollback 이름으로 Pipeline작성
Script Path 수정 : jenkins-pipeline/rollback-pipeline
환경구성 & Volume은 Deploy 용만 작성
Deploy rollback jenkins scrupt 작성
@Library('retort-lib') _
def label = "jenkins-${UUID.randomUUID().toString()}"
def ZCP_USERID = 'edu01'
def K8S_NAMESPACE = 'edu01'
def TYPE = 'deployment'
def DEPLOY_NAME = 'spring-boot-cicd-demo'
podTemplate(label:label,
serviceAccount: "zcp-system-sa-${ZCP_USERID}",
containers: [
containerTemplate(name: 'kubectl', image: 'lachlanevenson/k8s-kubectl:v1.13.6', ttyEnabled: true, command: 'cat')
]) {
node(label) {
stage('ROLLBACK') {
container('kubectl') {
kubeCmd.rolloutUndo type: TYPE, name: DEPLOY_NAME, namespace: K8S_NAMESPACE, wait: 300
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Rollback 실행
Application의 변경된 내용 확인.