DevOps
Damoncai 10/25/2022 wogua,devops
# 第一章 介绍
本套系统搭建在k8s之上,采用kubesphere进行管理
之前为了方便,有介绍过kubesphere基础组件的搭建,比如redis、nacos等,这里将会直接从服务部署开始
# 第二章 网关流水线创建
流水线流程分为一下及步
- 拉取代码
- 编译打包
- Build Docker镜像,镜像打标签
- 上传镜像到阿里云
- 部署
# 2.1 镜像拉取凭证
部署时需要拉取镜像,这里配置相关秘钥
信息输入完成可以点击验证测试
# 2.2 流水线凭证
流水线在编写时需要登录阿里云镜像,所以需要相关凭证
# 2.3 工程中添加Dockerfile
FROM openjdk:8-jdk
LABEL maintainer=zhishun.cai
#docker run -e PARAMS="--server.port 9090"
# ENV PARAMS="--server.port=8080 --spring.profiles.active=prod --spring.cloud.nacos.discovery.server-addr=nacos.wogua-cloud:8848 --spring.cloud.nacos.config.server-addr=nacos.wogua-cloud:8848 --spring.cloud.nacos.config.namespace=prod --spring.cloud.nacos.discovery.namespace=prod --spring.cloud.nacos.config.file-extension=yml"
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
COPY target/*.jar /app.jar
EXPOSE 8080
#
ENTRYPOINT ["/bin/sh","-c","java -Dfile.encoding=utf8 -Djava.security.egd=file:/dev/./urandom -jar app.jar"]
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 2.4 Gateway配置多环境
Gateway Pom文件中添加
<profiles> <profile> <id>dev</id> <properties> <env>dev</env> </properties> <!-- 是否默认 true表示默认--> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <!-- 生产环境 --> <id>prod</id> <properties> <env>prod</env> </properties> </profile> </profiles> <build> <finalName>wogua-gateway</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <directory>src/main/resources/profile/${env}</directory> <includes> <include>bootstrap.yml</include> <include>bootstrap-*.yml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.txt</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
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父工程中添加
<profiles> <profile> <id>dev</id> <properties> <env>dev</env> </properties> <!-- 是否默认 true表示默认--> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <!-- 生产环境 --> <id>prod</id> <properties> <env>prod</env> </properties> </profile> </profiles>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
在流水线打包时指定相应环境即可
# 2.5 部署yml配置
在工程先创建文件夹config
在config中创建deploy.yml文件
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: wogua-cloud-gateway
name: wogua-cloud-gateway
namespace: wogua-cloud #一定要写名称空间
spec:
progressDeadlineSeconds: 600
replicas: 1
selector:
matchLabels:
app: wogua-cloud-gateway
strategy:
rollingUpdate:
maxSurge: 50%
maxUnavailable: 50%
type: RollingUpdate
template:
metadata:
labels:
app: wogua-cloud-gateway
spec:
imagePullSecrets:
- name: aliyun-docker-hub #提前在项目下配置访问阿里云的账号密码
containers:
- image: $REGISTRY/$ALIYUNHUB_NAMESPACE/wogua-gateway:SNAPSHOT-$BUILD_NUMBER
# readinessProbe:
# httpGet:
# path: /actuator/health
# port: 8080
# timeoutSeconds: 10
# failureThreshold: 30
# periodSeconds: 5
imagePullPolicy: Always
name: app
ports:
- containerPort: 8080
protocol: TCP
resources:
limits:
cpu: 300m
memory: 600Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
labels:
app: wogua-cloud-gateway
name: wogua-cloud-gateway
namespace: wogua-cloud
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: wogua-cloud-gateway
sessionAffinity: None
type: ClusterIP
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 2.6 创建流水线导入相关配置
pipeline {
agent {
node {
label 'maven'
}
}
stages {
stage('拉取代码') {
agent none
steps {
container('base') {
git(url: 'https://gitee.com/damoncai/wogua-cloud.git', credentialsId: 'gitee', changelog: true, poll: false)
}
sh 'ls'
}
}
stage('编译') {
agent none
steps {
container('maven') {
sh 'mvn clean package -P prod'
}
}
}
stage('构建镜像') {
agent none
steps {
container('maven') {
sh 'docker build -t wogua-gateway:v1.0 -f wogua-gateway/Dockerfile wogua-gateway/'
}
}
}
stage('推送镜像') {
agent none
steps {
container('maven') {
withCredentials([usernamePassword(credentialsId : 'aliyun-docker-register' ,passwordVariable : 'L_PWD' ,usernameVariable : 'L_USERNAME' ,)]) {
sh 'echo "$L_PWD" | docker login $REGISTRY -u "$L_USERNAME" --password-stdin'
sh 'docker tag wogua-gateway:v1.0 $REGISTRY/$DOCKERHUB_NAMESPACE/wogua-gateway:SNAPSHOT-$BUILD_NUMBER'
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/wogua-gateway:SNAPSHOT-$BUILD_NUMBER'
}
}
}
}
stage('deploy to dev') {
agent none
steps {
kubernetesDeploy(configs: 'wogua-gateway/deploy/**', enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID")
}
}
}
environment {
DOCKER_CREDENTIAL_ID = 'dockerhub-id'
GITHUB_CREDENTIAL_ID = 'github-id'
KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
REGISTRY = 'registry.cn-hangzhou.aliyuncs.com'
DOCKERHUB_NAMESPACE = 'wogua-cloud'
GITHUB_ACCOUNT = 'kubesphere'
APP_NAME = 'devops-java-sample'
ALIYUNHUB_NAMESPACE = 'wogua-cloud'
}
parameters {
string(name: 'TAG_NAME', defaultValue: '', description: '')
}
}
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
认证服务,系统服务等只需要按上面步骤修改相应配置信息
具体参考:http://www.helloworld.center/docs/cloudnative/05.kubesphere/03.kubesphere-devops.html
# 第三章 前端部署
参考:http://www.helloworld.center/docs/cloudnative/05.kubesphere/03.kubesphere-devops.html#_4-8-%E5%89%8D%E7%AB%AF%E9%83%A8%E7%BD%B2
# 3.1 Dockerfile创建
FROM nginx
#将dist目录内容复制到nginx容器html内部
COPY dist /usr/share/nginx/html/
EXPOSE 80
1
2
3
4
5
6
2
3
4
5
6
# 3.2 部署配置文件创建
在工程先创建文件夹config
在config中创建deploy.yml文件
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: wogua-cloud-web
name: wogua-cloud-web
namespace: wogua-cloud #一定要写名称空间
spec:
progressDeadlineSeconds: 600
replicas: 1
selector:
matchLabels:
app: wogua-cloud-web
strategy:
rollingUpdate:
maxSurge: 50%
maxUnavailable: 50%
type: RollingUpdate
template:
metadata:
labels:
app: wogua-cloud-web
spec:
imagePullSecrets:
- name: aliyun-docker-hub #提前在项目下配置访问阿里云的账号密码
containers:
- image: $REGISTRY/$ALIYUNHUB_NAMESPACE/wogua-cloud-web:SNAPSHOT-$BUILD_NUMBER
# readinessProbe:
# httpGet:
# path: /actuator/health
# port: 8080
# timeoutSeconds: 10
# failureThreshold: 30
# periodSeconds: 5
imagePullPolicy: Always
name: app
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: 300m
memory: 600Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
labels:
app: wogua-cloud-web
name: wogua-cloud-web
namespace: wogua-cloud
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
nodePort: 32248
selector:
app: wogua-cloud-web
sessionAffinity: None
type: NodePort
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67