Kubernetes之部署SpringBoot项目
- 创建一个SpringBoot项目
- 将SpringBoot项目打成Jar包
- 使用Dockerfile制作镜像
- 部署SpringBoot项目
创建一个SpringBoot项目
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>k8s-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.encoding>UTF-8</project.encoding>
<maven-plugin.version>3.8.1</maven-plugin.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<dependencies>
<!-- spring-boot-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<!-- 修改maven打包的项目名 -->
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.encoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
AppDemo.java
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author honey
* @date 2023-02-25 20:14:47
*/
@SpringBootApplication
public class AppDemo {
public static void main(String[] args) {
SpringApplication.run(AppDemo.class);
}
}
DemoController.java
package com.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.net.InetAddress;
/**
* @author honey
* @date 2023-02-25 20:13:27
*/
@RestController
public class DemoController {
@RequestMapping("/")
public String index() {
return "ip:" + getIp();
}
public static String getIp() {
try {
InetAddress ia = InetAddress.getLocalHost();
return ia.getHostAddress();
} catch (Exception e) {
return "500";
}
}
}
将SpringBoot项目打成Jar包
mvn clean package
使用Dockerfile制作镜像
Dockerfile文件
FROM java:8
# 复制文件到容器
ADD k8s-demo.jar /k8s-demo.jar
# 配置容器启动后执行的命令
ENTRYPOINT ["java","-jar","-Duser.timezone=GMT+08 -XX:+HeapDumpOnOutOfMemoryError -Xms512m -Xmx512m","/k8s-demo.jar"]
操作两台工作节点,将Dockerfile文件和Jar包上传至服务器
mkdir -p /home/demo
cd /home/demo
docker build -f Dockerfile -t k8s-demo:v1 .
docker images
部署SpringBoot项目
apiVersion: apps/v1
kind: Deployment
metadata:
name: pc-demo
namespace: bubble-dev
spec:
replicas: 3
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: k8s-demo
image: k8s-demo:v1
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: svc-demo
namespace: bubble-dev
spec:
selector:
app: demo
type: NodePort
ports:
- port: 8080
nodePort: 30008
targetPort: 8080
kubectl create ns bubble-dev
vi k8s-demo.yaml
cat k8s-demo.yaml
kubectl create -f k8s-demo.yaml
操作Master节点
kubectl get pods -n bubble-dev
kubectl describe pods -n bubble-dev
使用NodeIP:NodePort从外部访问 http://192.168.102.160:30008/ 或者 http://192.168.102.161:30008/