Giter Club home page Giter Club logo

k8s-spring-boot-demo's Introduction

在 Kubernetes 中部署 SpringBoot 应用

在 Kubernetes 中通过yaml 配置文件预先声明部署 SpringBoot 应用 代码地址 https://github.com/helloworlde/k8s-service

创建 SpringBoot 应用

  • 创建名为 k8s-service 的 SpringBoot 应用
  • 添加 REST API
    • K8sController.java
package cn.com.hellowood.k8sservice.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class K8sController {

    @GetMapping("/")
    public String root() {
        return "Hello Kubernetes";
    }

    @GetMapping("/healthz")
    public String healthz() {
        return "ok";
    }
}
  • 添加 Dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-Duser.timezone=GMT+08", "-jar","/app.jar"]
  • 编译打包
./gradlew clean build -x test
  • 生成并推送镜像
./gradlew dockerPush -i

在 Kubernetes 中添加服务

  • 添加 k8s-demo.yaml
apiVersion: v1
kind: Service
metadata:
  name: k8s-service
  namespace: default
  labels:
    app: k8s-service
spec:
  type: NodePort
  ports:
  - port: 8080
    nodePort: 30002
  selector:
    app: k8s-service

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8s-service
  labels:
    app: k8s-service
spec:
  replicas: 1
  selector:
    matchLabels:
      app: k8s-service
  template:
    metadata:
      labels:
        app: k8s-service
    spec:
      containers:
      - name: k8s-service
        image: registry.cn-qingdao.aliyuncs.com/hellowoodes/k8s-service
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
        livenessProbe:
          httpGet:
            port: 8080
            path: /healthz
            scheme: HTTP
          periodSeconds: 15
          initialDelaySeconds: 30
  • 创建服务
kubectl apply -f k8s-service.yaml
  • 等待服务启动之后访问 ${NodeIP}:30002,会返回 Hello Kubernetes,部署完成

k8s-spring-boot-demo's People

Contributors

helloworlde avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.