引入springboot监控工具
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
打包镜像
在springboot项目下建立2个文件
Dockerfile:
FROM openjdk:8-jre
MAINTAINER xxx xxx@xxx.com
COPY target/*.jar /app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
build.sh
#!/usr/bin/env bash
mvn package
docker build -t registry.xxx.com/xxx/springbootapp:0.1 .
运行 build.sh后会在本地生成镜像,然后将镜像推送到镜像仓库:
docker push registry.xxx.com/xxx/app:0.1
在kubernetes中部署
新建部署清单,app.yml
apiVersion: v1
kind: ConfigMap
metadata:
name: springbootapp
namespace: default
data:
application.yml: |
# 这里放的是springboot配置文件的内容
server:
port: 8080
# mybatis-plus配置
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.xxx.entity
global-config:
db-config:
id-type: auto #主键策略,数据库自增
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
# health 健康检查
management:
server:
port: 8081
endpoints:
enabled-by-default: true
web:
base-path: /actuator
exposure:
include: "*"
endpoint:
health:
probes:
enabled: true
---
apiVersion: v1
kind: Service
metadata:
name: springbootapp
namespace: default
labels:
app: springbootapp
spec:
ports:
- port: 8080
type: NodePort
selector:
app: springbootapp
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: springbootapp
namespace: default
labels:
app: springbootapp
spec:
selector:
matchLabels:
app: springbootapp
replicas: 2
template:
metadata:
labels:
app: springbootapp
spec:
containers:
- name: springbootapp
image: registry.xxx.com/xxx/springbootapp:0.1
resources:
limits:
cpu: 1000m
requests:
cpu: 1000m
ports:
- containerPort: 8080
# 存活监测
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8081
initialDelaySeconds: 10
periodSeconds: 10
# 就绪监测
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8081
periodSeconds: 10
# 挂载配置文件
volumeMounts:
- name: config
mountPath: /application.yml
subPath: application.yml
volumes:
- name: config
configMap:
name: springbootapp
items:
- key: application.yml
path: application.yml
部署:
kubectl apply -f app.yml

本文介绍如何在Spring Boot项目中引入监控工具并构建Docker镜像,随后通过Kubernetes进行部署。内容覆盖依赖添加、Dockerfile编写、镜像推送及Kubernetes配置清单示例。
1247

被折叠的 条评论
为什么被折叠?



