SpringBoot部署应用到本地k8s

本文介绍了如何在Windows+IDEA环境下,利用Gradle将SpringBoot应用打包成jar,创建Docker镜像,并通过Minikube部署到本地Kubernetes集群,实现Web应用的本地访问。涉及内容包括配置文件、Dockerfile编写、镜像制作与推送、yaml文件应用及服务暴露。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在windows+IDEA环境下,使用gradle将springboot打包,制作成镜像发布到docker上并部署到本地k8s访问web应用。

配置文件

build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("plugin.jpa") version "1.6.10"
    id("org.springframework.boot") version "2.6.4"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.6.10"
    kotlin("plugin.spring") version "1.6.10"
    kotlin("plugin.allopen") version "1.4.32"
    kotlin("kapt") version "1.4.32"
}

group = "com.xun"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}

dependencies {
    kapt("org.springframework.boot:spring-boot-configuration-processor")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-mustache")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    runtimeOnly("com.h2database:h2")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(module = "junit")
        exclude(module = "mockito-core")
    }
    testImplementation("org.junit.jupiter:junit-jupiter-api")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
    testImplementation("com.ninja-squad:springmockk:3.0.1")
}

allOpen{
    annotation("javax.persistence.Entity")
    annotation("javax.persistence.Embeddable")
    annotation("javax.persistence.MappedSuperclass")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

使用gradle打jar包

在这里插入图片描述
打包后的jar包会放在build->libs目录下
在这里插入图片描述

编写Dockerfile

FROM java
MAINTAINER 3340556441@qq.com
VOLUME /tmp
ADD /build/libs/springbootkotlin1-0.0.1-SNAPSHOT.jar myspringbootkotlinapp.jar
ENTRYPOINT ["java","-jar","myspringbootkotlinapp.jar"]

制作image

启动docker,启动minikube

minikube start

在Dockerfile所在目录下运行shell,制作镜像

docker build -t myspringbootkotlinapp .

在这里插入图片描述

查看镜像

docker images

在这里插入图片描述

上传镜像

打标签

docker tag myspringbootkotlinapp sunzhexun/myspringbootkotlinapp

登陆docker

docker login

在这里插入图片描述

push镜像

 docker push sunzhexun/myspringbootkotlinapp

在这里插入图片描述
在这里插入图片描述

minikube创建deployment

自动创建yaml文件

kubectl create deployment myspringbootkotlinapp --image=sunzhexun/myspringbootkotlinapp --dry-run=client -o=yaml > deployment.yaml
echo --- >> deployment.yaml

在这里插入图片描述

kubectl create service clusterip myspringbootkotlinapp --tcp=8080:8080 --dry-run=client -o=yaml >> deployment.yaml

在这里插入图片描述

apply yaml文件

kubectl apply -f deployment.yaml

在这里插入图片描述

查看deployment/svc

kubectl get all

在这里插入图片描述

创建容器可能需要几秒到几分钟,可以另起一个终端,查看dashboard

minikube dashboard

在这里插入图片描述
创建完成后,
在这里插入图片描述
暴露服务

kubectl port-forward svc/myspringbootkotlinapp 8080:8080

在这里插入图片描述

访问服务

localhost:8080
此时就可以本地访问我们的springboot应用啦
在这里插入图片描述

附录

此springboot项目代码(使用的是kotlin+jpa)

目录

在这里插入图片描述

在这里插入图片描述

build.gradle.kts配置

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("plugin.jpa") version "1.6.10"
    id("org.springframework.boot") version "2.6.4"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.6.10"
    kotlin("plugin.spring") version "1.6.10"
    kotlin("plugin.allopen") version "1.4.32"
    kotlin("kapt") version "1.4.32"
}


group = "com.xun"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}

dependencies {
    kapt("org.springframework.boot:spring-boot-configuration-processor")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-mustache")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    runtimeOnly("com.h2database:h2")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(module = "junit")
        exclude(module = "mockito-core")
    }
    testImplementation("org.junit.jupiter:junit-jupiter-api")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
    testImplementation("com.ninja-squad:springmockk:3.0.1")
}

allOpen{
    annotation("javax.persistence.Entity")
    annotation("javax.persistence.Embeddable")
    annotation("javax.persistence.MappedSuperclass")
}


tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

Entities.kt

实体类

import java.time.LocalDateTime
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.persistence.ManyToOne

@Entity
class Article(
    var title: String,
    var headline: String,
    var content: String,
    @ManyToOne var author: User,
    var slug: String = title.toSlug(),
    var addedAt: LocalDateTime = LocalDateTime.now(),
    @Id @GeneratedValue var id: Long? = null)

@Entity
class User(
    var login: String,
    var firstname: String,
    var lastname: String,
    var description: String? = null,
    @Id @GeneratedValue var id: Long? = null)

@Entity
class Product(
    var productName: String,
    var categoryId: Int,
    @Id @Generate
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值