Mr. Cappuccino的第45杯咖啡——Kubernetes之部署SpringBoot项目

Kubernetes之部署SpringBoot项目

      • 创建一个SpringBoot项目
      • 将SpringBoot项目打成Jar包
      • 使用Dockerfile制作镜像
      • 部署SpringBoot项目

创建一个SpringBoot项目

Mr. Cappuccino的第45杯咖啡——Kubernetes之部署SpringBoot项目_第1张图片
pom.xml


<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.0modelVersion>

    <groupId>com.demogroupId>
    <artifactId>k8s-demoartifactId>
    <version>1.0-SNAPSHOTversion>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.encoding>UTF-8project.encoding>
        <maven-plugin.version>3.8.1maven-plugin.version>
    properties>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.0.RELEASEversion>
    parent>
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>

    <build>
        
        <finalName>${project.artifactId}finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <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.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>${spring-boot.version}version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        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

Mr. Cappuccino的第45杯咖啡——Kubernetes之部署SpringBoot项目_第2张图片
Mr. Cappuccino的第45杯咖啡——Kubernetes之部署SpringBoot项目_第3张图片

使用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

Mr. Cappuccino的第45杯咖啡——Kubernetes之部署SpringBoot项目_第4张图片

部署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节点

Mr. Cappuccino的第45杯咖啡——Kubernetes之部署SpringBoot项目_第5张图片

kubectl get pods -n bubble-dev
kubectl describe pods -n bubble-dev

Mr. Cappuccino的第45杯咖啡——Kubernetes之部署SpringBoot项目_第6张图片

Mr. Cappuccino的第45杯咖啡——Kubernetes之部署SpringBoot项目_第7张图片

使用NodeIP:NodePort从外部访问 http://192.168.102.160:30008/ 或者 http://192.168.102.161:30008/

Mr. Cappuccino的第45杯咖啡——Kubernetes之部署SpringBoot项目_第8张图片
Mr. Cappuccino的第45杯咖啡——Kubernetes之部署SpringBoot项目_第9张图片

你可能感兴趣的:(Kubernetes,spring,boot,mr,kubernetes)