springboot 2.4 部署到kubernetes中

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

引入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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值