部署springboot项目到GKE(Google Kubernetes Engine)

news2025/2/25 7:38:51

GKE是 Google Cloud Platform 提供的托管 Kubernetes 服务,允许用户在 Google 的基础设施上部署、管理和扩展容器。本文介绍如何部署一个简单的springboot项目到GKE.

本文使用podman.

如果你用的是docker, 只需要把本文中所有命令中的podman替换成docker即可

  1. 非Helm部署方式
    准备工作: 在springboot项目路径下,新增3个文件:deployment.yaml,service.yaml,Dockerfile:
    结构如下:
    在这里插入图片描述
    deployment.yaml, 注意文件里的image地址,由后续podman push image gcr.io后得到
apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: springboot-app
  template:
    metadata:
      labels:
        app: springboot-app
    spec:
      containers:
        - name: springboot-app
          image: gcr.io/xxxx/yyyy/springboot-app@sha256:3725a57f9cd0b6fb63eb91e49c2305a6b684abd129f3f075838a80b54472455c
          ports:
            - containerPort: 8080

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: springboot-app-service
spec:
  type: LoadBalancer
  selector:
    app: springboot-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

Dockerfile

# 使用Amazon Corretto 17作为构建环境
FROM amazoncorretto:17 as build
WORKDIR /workspace/app

# 复制Maven Wrapper和其他构建文件
COPY mvnw .
COPY .mvn .mvn
COPY pom.xml .
COPY src src

# 使用Maven Wrapper进行构建,跳过测试
RUN ./mvnw install -DskipTests

# 使用Amazon Corretto 17作为生产环境
FROM amazoncorretto:17
VOLUME /tmp
ARG JAR_FILE=/workspace/app/target/*.jar
COPY --from=build ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

podman build -t gcr.io/xxxx/yyyy/springboot-app:v1 .
podman push gcr.io/xxxx/yyyy/springboot-app:v1
会生成如下路径
在这里插入图片描述

kubectl apply -f deployment.yaml -n infra --使用这个命令之前,先修改一下image的地址。
kubectl apply -f service.yaml -n infra
ok啦,此时你应该可以访问你的应用了。

  1. Helm部署方式

a. helm create spacex-chart -n infra
生成的chart目录结构如下:
在这里插入图片描述

关于values的优先级:
命令行参数或自定义 values.yaml > 集群中存储的值 > Chart 的默认 values.yaml
可以用这个命令查看生成的k8s组件是否符合你的预期。

helm template spacex-chart ./spacex-chart
--values=./spacex-chart/values.yaml > generated_boot.yaml

b. 修改一些文件,修改过的文件如下: 另外还移除了service-account等一些不必要的文件
values.yaml

# Default values for spacex-chart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
  repository: gcr.io/ebay-mag/kubein/springboot-app
  pullPolicy: IfNotPresent
  # Overrides the image tag whose default is the chart appVersion.
  tag: "v1.1"

imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

service:
  type: LoadBalancer
  port: 80
  targetPort: 8080

ingress:
  enabled: false
  className: ""
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  hosts:
    - host: chart-example.local
      paths:
        - path: /
          pathType: ImplementationSpecific
  tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local

resources: {}
  # We usually recommend not to specify default resources and to leave this as a conscious
  # choice for the user. This also increases chances charts run on environments with little
  # resources, such as Minikube. If you do want to specify resources, uncomment the following
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  # limits:
  #   cpu: 100m
  #   memory: 128Mi
  # requests:
  #   cpu: 100m
  #   memory: 128Mi

autoscaling:
  enabled: false
  minReplicas: 1
  maxReplicas: 2
  targetCPUUtilizationPercentage: 80
  # targetMemoryUtilizationPercentage: 80

nodeSelector: {}

tolerations: []

affinity: {}

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "spacex-chart.fullname" . }}
  labels:
    {{- include "spacex-chart.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "spacex-chart.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "spacex-chart.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: {{ include "spacex-chart.fullname" . }}
  labels:
    {{- include "spacex-chart.labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: {{ .Values.service.targetPort }}
      protocol: TCP
      name: http
  selector:
    {{- include "spacex-chart.selectorLabels" . | nindent 4 }}

c. 安装 helm install boot-chart-release-001 spacex-chart -n infra
命令解释 helm install [release-name] [chart-name] -n infra,此时我们用helm ls可以查看安装chart后的一些对应关系
在这里插入图片描述
ok, 此时应用已经部署到GKE中了,你可以通过ip访问你的应用了。Happy Helming!

安装完后,如果发现需要修改values.yaml里面的值.在修改完values.yaml文件后,用如下命令更新。
helm upgrade boot-chart-release-001 spacex-chart -f spacex-chart/values.yaml -n infra
查看更新后的值
helm get values boot-chart-release-001 -n infra

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1278923.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

代码随想录算法训练营第三十四天|62.不同路径,63. 不同路径 II

62. 不同路径 - 力扣(LeetCode) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” &#…

Git版本管理配置说明 - Visual Studio

一、 Git服务端配置 在源代码管理服务器新建文件夹,并配置共享访问权限Everyone(读取/写入)。 在本地访问这台服务器共享目录,确保正确打开。 在VS中打开项目,点选Git更改,点击“创建Git仓库”,创建项目初始版本。 弹出如下对话框: 因为我们只是在局域网中开发项…

某60区块链安全之Create2实战一学习记录

区块链安全 文章目录 区块链安全Create2实战一实验目的实验环境实验工具实验原理实验内容Create2实战一 实验步骤分析合约源代码漏洞Create2实战一 实验目的 学会使用python3的web3模块 学会分析以太坊智能合约Create2引发的漏洞及其利用 找到合约漏洞进行分析并形成利用 实…

初识金融市场

文章目录 前言第一章 金融市场体系考点二、金融市场分类考点三、金融市场的功能考点四、直接融资与间接融资的特点考点五、全球金融市场的形成及发展趋势考点六、国际资金的流动方式考点七、金融体系的主要参与者考点八、国际金融监管体系 第二章 中国的金融体系与多层次资本市…

爬虫学习-基础(HTTP原理)

目录 一、URL和URI 二、HTTP和HTTPS (1)HTTP (2)HTTPS (3)HTTP与HTTPS区别 (4)HTTPS对HTTP的改进:双问的身份认证 三、TCP协议 (1)TCP三次握手…

Redis——某马点评day02——商铺缓存

什么是缓存 添加Redis缓存 添加商铺缓存 Controller层中 /*** 根据id查询商铺信息* param id 商铺id* return 商铺详情数据*/GetMapping("/{id}")public Result queryShopById(PathVariable("id") Long id) {return shopService.queryById(id);} Service…

XIAO ESP32S3之SenseCraft 模型助手部署

sipeed教程:SenseCraft 模型助手部署 | Seeed Studio Wiki 一、安装ESP-IDF 鉴于我的电脑之前安装过esp-idf v4.3版本,而ESP32-S3需要v4.4及以上版本才支持,所以将esp-idf更新到最新5.1版本。 1、启动mingw32.exe应用 2、进入esp-idf目录 …

docker+jmeter+influxdb+granfana

centos7国内阿里源安装docker 1、安装必要的系统工具 sudo yum install -y yum-utils device-mapper-persistent-data lvm2 2添加官方仓库 sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.reposudo sed -i sdownload.doc…

Opencv拖动条控制均值滤波卷积核大小,拖动条控制是否保存(涉及知识点:cv2.createTrackbar和cv2.getTrackbarPos的使用)

带拖动条的均值滤波import timeimport cv2 import numpy as npdef callback(int):passcv2.namedWindow(dst,cv2.WINDOW_AUTOSIZE)# 创建trackbar (trackbarname,winname,value,count,callback,userdata) cv2.createTrackbar(ksize, dst, 3, 30, callback) cv2.createTrackbar(s…

QT 中 QTimer 类 备查

基础 // 指定了父对象, 创建的堆内存可以自动析构 QTimer::QTimer(QObject *parent nullptr);// 根据指定的时间间隔启动或者重启定时器, 需要调用 setInterval() 设置时间间隔 void QTimer::start();// 启动或重新启动定时器,超时间隔为msec毫秒。 void QTimer::…

【NeurIPS 2023】PromptIR: Prompting for All-in-One Blind Image Restoration

PromptIR: Prompting for All-in-One Blind Image Restoration, NeurIPS 2023 论文:https://arxiv.org/abs/2306.13090 代码:https://github.com/va1shn9v/promptir 解读:即插即用系列 | PromptIR:MBZUAI提出一种基…

node.js express路由和中间件

目录 路由 解释 使用方式 中间件 解释 使用方式 中间件类型 路由注册和中间件注册 代码 app全局路由接口请求以及代码解析 示例1 示例2 示例3 示例4 中间件req继承 嵌套子路由 解释 代码 示例1 路由 解释 在 Express 中,路由(Route&…

Spring | Spring的基本应用

目录: 1.什么是Spring?2.Spring框架的优点3.Spring的体系结构 (重点★★★) :3.1 Core Container (核心容器) ★★★Beans模块 (★★★) : BeanFactoryCore核心模块 (★★★) : IOCContext上下文模块 (★★★) : ApplicationContextContext-support模块 (★★★)SpE…

Linux中文件的打包压缩、解压,下载到本地——zip,tar指令等

目录 1 .zip后缀名: 1.1 zip指令 1.2 unzip指令 2 .tar后缀名 3. sz 指令 4. rz 指令 5. scp指令 1 .zip后缀名: 1.1 zip指令 语法:zip [namefile.zip] [namefile]... 功能:将目录或者文件压缩成zip格式 常用选项&#xff1a…

《YOLOv8原创自研》专栏介绍 CSDN独家改进创新实战专栏目录

YOLOv8原创自研 https://blog.csdn.net/m0_63774211/category_12511737.html?spm1001.2014.3001.5482 💡💡💡全网独家首发创新(原创),适合paper !!! 💡&a…

QT 中 QProgressDialog 进度条窗口 备查

基础API //两个构造函数 QProgressDialog::QProgressDialog(QWidget *parent nullptr, Qt::WindowFlags f Qt::WindowFlags());QProgressDialog::QProgressDialog(const QString &labelText, const QString &cancelButtonText, int minimum, int maximum, QWidget *…

java源码-类与对象

1、类与对象的初步认知 在了解类和对象之前我们先了解一下什么是面向过程和面向对象。 1)面向过程编程: C语言就是面向过程编程的,关注的是过程,分析出求解问题的步骤,通过函数调用逐步解决问题。 2)面向对…

【数据结构】最短路径(Dijskra算法)

一.引例 计算机网络传输的问题: 怎样找到一种最经济的方式,从一台计算机向网上所有其他计算机发送一条消息。 抽象为: 给定带权有向图G(V,E)和源点v,求从v到G中其余各顶点的最短路径。 即&…

23.Python 图形化界面编程

目录 1.认识GUI和使用tkinter2.使用组件2.1 标签2.2 按钮2.3 文本框2.4 单选按钮和复选按钮2.5 菜单和消息2.6 列表框2.7 滚动条2.8 框架2.9 画布 3. 组件布局4.事件处理 1.认识GUI和使用tkinter 人机交互是从人努力适应计算机,到计算机不断适应人的发展过程&#…

Redis中的数据结构

文章目录 第1关:Redis中的数据结构 第1关:Redis中的数据结构 这是上篇文章的第一关,只不过本篇是代码按行做的,方便一下大家使用。 代码如下: redis-cliset hello redislpush educoder-list hellorpush educoder-lis…