Fork me on GitHub

容器化案例四:Java服务容器化

包括Springboot,Dubbo,Tomcat三个类型

编译

1
mvn clean package -Dmaven.test.skip=true -T 1C -Dmaven.compile.fork=true -P production --settings /root/.m2/settings.xml

Dockerfile文件编写

1
2
3
4
5
6
7
8
9
10
11
12
13
FROM harbor.k8s.xxx.com/iflow_public/java-app:oraclejdk-8u321-jre-alpine-1.0.61

COPY ./target/*.gz /data/source/

ENV JAVA_OPTS="-XX:ActiveProcessorCount=2 -XX:MaxRAMPercentage=75.0 -XX:InitialRAMPercentage=75.0 -XX:MinRAMPercentage=75.0 -XX:+DisableExplicitGC -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+ExplicitGCInvokesConcurrentAndUnloadsClasses -XX:+CMSClassUnloadingEnabled -XX:+ParallelRefProcEnabled -XX:+CMSScavengeBeforeRemark -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps -XX:ErrorFile=/data/var/log/hs_err_pid%p.log -XX:HeapDumpPath=/data/var/log/ -Xloggc:/data/var/log/gc%t.log -XX:+PrintGC -XX:+CMSParallelRemarkEnabled -XX:LargePageSizeInBytes=128m -XX:MaxTenuringThreshold=15 -XX:SurvivorRatio=6 -Dfile.encoding=UTF-8"

ENV START_CLASS="com.xxx.aigc.plateform.PlateformApplication"

ENV ENABLE_PP_AGENT="1"
ENV PP_AGENT_PATH="/usr/local/pinpoint-agent2"


CMD [ "sh", "-c", "start_app.sh" ]

Deployment及Service文件编写

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
{{$MODULE := "serve" }}
{{$TRUE_APP_NAME := (printf "%v-%v" .Env.FLOW_STANDARD_NAME $MODULE)}}


---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{$TRUE_APP_NAME}}-deploy
labels:
rd: {{.Env.CMP_RD}}
app: {{.Env.FLOW_STANDARD_NAME}}
project-name: {{.Env.FLOW_PROJECT_NAME}}
module: {{$MODULE}}
spec:
replicas: {{if eq .Env.FLOW_DIST_ENV "pre"}}1{{else}}1{{end}}
selector:
matchLabels:
app: {{.Env.FLOW_STANDARD_NAME}}
project-name: {{.Env.FLOW_PROJECT_NAME}}
module: {{$MODULE}}
template:
metadata:
annotations:
alibabacloud.com/burst-resource: eci
labels:
rd: {{.Env.CMP_RD}}
app: {{.Env.FLOW_STANDARD_NAME}}
project-name: {{.Env.FLOW_PROJECT_NAME}}
module: {{$MODULE}}
spec:
dnsConfig:
options:
- name: ndots
value: "3"
imagePullSecrets:
- name: harbor-registry
containers:
- name: serve
image: {{.Env.IMAGE_NAME}}
imagePullPolicy: Always
startupProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 15
periodSeconds: 10
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 30
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 10
periodSeconds: 10
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 15
env:
- name: JAVA_APP_OPTS
value: {{if eq .Env.FLOW_DIST_ENV "pre"}}"-Denv=PRO -Didc=pre"{{else}}"-Denv=PRO"{{end}}
- name: ENABLE_PP_AGENT
value: {{if eq .Env.FLOW_DIST_ENV "pre"}}"0"{{else}}"1"{{end}}
resources:
limits:
cpu: 200m
memory: 4094Mi
requests:
cpu: 100m
memory: 2048Mi
ports:
- containerPort: {{.Env.CMP_PORT}}
name: web
protocol: TCP
volumeMounts:
- name: logs
mountPath: /tmp/log
- name: filebeat-data
mountPath: /data/var/log
- name: filebeat-sidecar
image: harbor.k8s.xxx.com/iflow/custom-filebeat:7.10.1-5
resources:
limits:
cpu: 50m
memory: "100Mi"
requests:
cpu: 50m
memory: "100Mi"
volumeMounts:
- name: logs
mountPath: /tmp/log
- name: filebeat-data
mountPath: /data/var/log
volumes:
- name: logs
emptyDir: {}
- name: filebeat-data
emptyDir: {}
...
---
apiVersion: v1
kind: Service
metadata:
name: {{$TRUE_APP_NAME}}-svc
labels:
rd: {{.Env.CMP_RD}}
app: {{.Env.FLOW_STANDARD_NAME}}
module: {{$MODULE}}
spec:
type: ClusterIP
selector:
app: {{.Env.FLOW_STANDARD_NAME}}
module: {{$MODULE}}
ports:
- name: web
protocol: TCP
port: {{.Env.CMP_PORT}}
targetPort: {{.Env.CMP_PORT}}
...

======================================================
希望各位朋友支持一下

本文作者:dongsheng
本文地址https://mds1455975151.github.io/archives/bc1fe45c.html
版权声明:转载请注明出处!

坚持技术分享,您的支持将鼓励我继续创作!