Devops系列二(使用helm chart,将java应用发布部署至k8s的示例)

这篇具有很好参考价值的文章主要介绍了Devops系列二(使用helm chart,将java应用发布部署至k8s的示例)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、接着上一篇的话

docker镜像已经有了,本文我们将接着演示如何使用helm部署应用到k8s。
分为两大部分:

  • 制作helm chart,推送到私有仓库nexus
  • helm拉取chart,部署到k8s

二、制作helm chart

要求你先安装helm,随便一台linux机器即可,不要求你要有k8s或者docker环境。

xxx@local:~/Downloads$ wget https://get.helm.sh/helm-v3.12.1-linux-amd64.tar.gz
--2023-06-30 10:49:03--  https://get.helm.sh/helm-v3.12.1-linux-amd64.tar.gz
正在解析主机 get.helm.sh (get.helm.sh)... 152.199.39.108, 2606:2800:247:1cb7:261b:1f9c:2074:3c
正在连接 get.helm.sh (get.helm.sh)|152.199.39.108|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度: 16036346 (15M) [application/x-tar]
正在保存至: “helm-v3.12.1-linux-amd64.tar.gz”

helm-v3.12.1-linux- 100%[===================>]  15.29M  1.19MB/s    用时 13s   

2023-06-30 10:49:18 (1.14 MB/s) - 已保存 “helm-v3.12.1-linux-amd64.tar.gz” [16036346/16036346])

xxx@local:~/Downloads$ tar -xf helm-v3.12.1-linux-amd64.tar.gz 

xxx@local:~/Downloads$ sudo mv linux-amd64/helm /usr/local/bin/helm
[sudo] xxx 的密码: 

xxx@local:~/Downloads$ helm version
version.BuildInfo{Version:"v3.12.1", GitCommit:"f32a527a060157990e2aa86bf45010dfb3cc8b8d", GitTreeState:"clean", GoVersion:"go1.20.4"}

xxx@local:~/Downloads$ helm repo list
NAME       URL                               
bitnami    https://charts.bitnami.com/bitnami

1、创建helm模板

helm creta java

Devops系列二(使用helm chart,将java应用发布部署至k8s的示例),java,kubernetes这里,我删除了没用到的一些文件,最后保留的见下:
Devops系列二(使用helm chart,将java应用发布部署至k8s的示例),java,kubernetes
templates下的三个文件就是对应K8S容器的三个yaml,没有什么好说的,helm这里额外增加了一个values.yaml文件,它是用来替换templates下的yaml文件里的变量。

那有人要问了,values.yaml文件里的变量可以替换吗?

在helm install的时候,你仍然可以替换values.yaml中的变量。所以我这设计的所有java应用,不同的环境,都使用同一个helm chart。

不同应用存在环境的差异,这个问题也就因此解决了。

2. deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.appName }}
  namespace: {{ .Values.namespace }}
  labels:
    app: {{ .Values.appName }}
spec:
  progressDeadlineSeconds: 600
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: {{ .Values.appName }}
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        app: {{ .Values.appName }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets: 
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: {{ .Values.appName }}
          env:
            - name: TZ
              value: Asia/Shanghai
            - name: APPNAME
              value: {{ .Values.appName }}
            - name: CONFIG_SERVICE_ADDR
              value: {{ .Values.env.configServiceAddr }}
            - name: CONFIG_SERVICE_NAMESPACE
              value: {{ .Values.env.configServiceNameSpace }}
            - name: CONFIG_SERVICE_GROUP
              value: {{ .Values.env.configServiceGroup }}
            - name: CONFIG_EPHEMERAL
              value: 'false'
            - name: CONFIG_SERVICE_ENABLED
              value: '{{ .Values.env.configServiceEnabled }}'
            - name: spring.cloud.nacos.config.accessKey
              value: {{ .Values.env.configAccessKey }}
            - name: spring.cloud.nacos.config.secretKey
              value: {{ .Values.env.configSecretKey }}
            - name: spring.cloud.nacos.discovery.accessKey
              value: {{ .Values.env.configAccessKey }}
            - name: spring.cloud.nacos.discovery.secretKey
              value: {{ .Values.env.configSecretKey }}
            - name: JAVA_OPTS
              value: {{ .Values.env.javaOpts }}

          image: "{{ .Values.image.repository }}{{ .Values.appName }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /mgm/health
              port: {{ .Values.service.port }}
              scheme: HTTP
            initialDelaySeconds: 1
            periodSeconds: 5
            successThreshold: 1
            timeoutSeconds: 3
          startupProbe:
            failureThreshold: 22
            httpGet:
              path: /mgm/health
              port: {{ .Values.service.port }}
              scheme: HTTP
            initialDelaySeconds: 25
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5
          resources:
            limits:
              cpu: {{ .Values.resource.limitCpu }}
              memory: {{ .Values.resource.limitMemory }}
            requests:
              cpu: {{ .Values.resource.requestCpu }}
              memory: {{ .Values.resource.requestMemory }}
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}

3、values.yaml

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

namespace: java-service

appName: devops-service

replicaCount: 1

env:
  javaOpts: 
  # consul
  configServiceEnabled: true
  configServiceHost: 192.168.10.19
  configServicePort: 8500
  # nacos
  configServiceAddr: 192.168.5.28:8848
  configServiceNameSpace: a5fdc665-4267-462e-9b03-d363984d9963
  configServiceGroup: DEFAULT_GROUP
  configAccessKey:
  configSecretKey:

image:
  repository: 192.168.5.6:8086/xxx/
  pullPolicy: IfNotPresent
  # Overrides the image tag whose default is the chart appVersion.
  tag: 1.0.7

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


podAnnotations: {}

podSecurityContext: {}
  # fsGroup: 2000

securityContext: {}
  # capabilities:
  #   drop:
  #   - ALL
  # readOnlyRootFilesystem: true
  # runAsNonRoot: true
  # runAsUser: 1000

service:
  type: NodePort
  port: 8085

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

resource:
  limitCpu: '2'
  limitMemory: 2Gi
  requestCpu: 250m
  requestMemory: 2Gi

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

nodeSelector: {}

tolerations: []

affinity: {}

4. service.yaml

apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.appName }}
  namespace: {{ .Values.namespace }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: {{ .Values.service.port }}
      protocol: TCP
      name: {{ .Values.appName }}
  selector:
    app: {{ .Values.appName }}
  sessionAffinity: None

5、 ingress.yaml

{{- if .Values.ingress.enabled -}}
{{- $fullName := .Values.appName -}}
{{- $svcPort := .Values.service.port -}}
{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }}
  {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }}
  {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}}
  {{- end }}
{{- end }}
{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1
{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: extensions/v1beta1
{{- end }}
kind: Ingress
metadata:
  name: {{ $fullName }}
  namespace: {{ .Values.namespace }}
spec:
  {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }}
  ingressClassName: {{ .Values.ingress.className }}
  {{- end }}
  {{- if .Values.ingress.tls }}
  tls:
    {{- range .Values.ingress.tls }}
    - hosts:
        {{- range .hosts }}
        - {{ . | quote }}
        {{- end }}
      secretName: {{ .secretName }}
    {{- end }}
  {{- end }}
  rules:
    {{- range .Values.ingress.hosts }}
    - host: {{ .host | quote }}
      http:
        paths:
          {{- range .paths }}
          - path: {{ .path }}
            {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }}
            pathType: {{ .pathType }}
            {{- end }}
            backend:
              {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }}
              service:
                name: {{ $fullName }}
                port:
                  number: {{ $svcPort }}
              {{- else }}
              serviceName: {{ $fullName }}
              servicePort: {{ $svcPort }}
              {{- end }}
          {{- end }}
    {{- end }}
{{- end }}

6、Chart.yaml

version是helm chart的版本号,应用的版本号见values.yaml文件中的image.tag(每次构建版本号都会追加1)

apiVersion: v2
name: java
description: java demo

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.8

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"

三、推送chart至helm私库

1、添加 helm repo

xxx@local:~$ helm repo list
NAME    URL                               
bitnami https://charts.bitnami.com/bitnami

# 新增helm仓库
xxx@local:~$ helm repo add nexus http://150xxxx9916:123456@192.168.5.6:8081/repository/xh-helm/
"nexus" has been added to your repositories

xxx@local:~$ helm repo list
NAME    URL                                                           
bitnami https://charts.bitnami.com/bitnami                            
nexus   http://150xxxx9916:123456@192.168.5.6:8081/repository/xh-helm/

2、安装helm推送插件

xxx@local:~$ helm plugin install --version master https://gitee.com/mirrors_sonatype-nexus-community/helm-nexus-push.git
Installed plugin: nexus-push

3、构建并推送

# 构建chart
xxx@local:~/Downloads$  helm package java
Successfully packaged chart and saved it to: /home/xxx/Downloads/java-0.1.8.tgz

# 推送chart到私有仓库
xxx@local:~/Downloads$ helm nexus-push nexus java-0.1.8.tgz -u 150xxxx9916 -p 123456
Pushing java-0.1.8.tgz to repo http://150xxxx9916:123456@192.168.5.6:8081/repository/xh-helm//...
  HTTP/1.1 100 Continue
  
  HTTP/1.1 200 OK
  Date: Fri, 30 Jun 2023 10:16:44 GMT
  Server: Nexus/3.37.3-02 (OSS)
  X-Content-Type-Options: nosniff
  Content-Security-Policy: sandbox allow-forms allow-modals allow-popups allow-presentation allow-scripts allow-top-navigation
  X-XSS-Protection: 1; mode=block
  Content-Length: 0
  
Done

四、总结

登录nexus,查看上传的chart。

Devops系列二(使用helm chart,将java应用发布部署至k8s的示例),java,kubernetes
Devops系列二(使用helm chart,将java应用发布部署至k8s的示例),java,kubernetes
可以看到,chart上传成功。
接下里,就是我们在k8s的控制台,将在下一篇文章进行描述。文章来源地址https://www.toymoban.com/news/detail-524482.html

到了这里,关于Devops系列二(使用helm chart,将java应用发布部署至k8s的示例)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • k8s实战3-使用Helm在AKS上发布应用

    AKS(Azure Kubenetes Service)是微软云azure上的K8s服务。 主要分为三步 1 连接到AKS 2 用kubectl发布应用 3 用Helm发布应用 1 登录 az login 2 连接 dp-npr-dsm-aks(Dsm项目的AKS) az account set --subscription {{subID}} az aks get-credentials --resource-group {{resource-group-name}} --name {{aks-name}} --admin 3 测试是否连接成

    2024年02月13日
    浏览(24)
  • Helm Chart三分钟轻松掌握

    ​ 我们的日常工作中需创建、修改和部署Helm Chart,以管理应用程序的部署。Helm是Kubernetes的应用程序包管理器,它负责协调应用程序的下载、安装和部署。 chart就是一个描述Kubernetes相关资源的文件集合。 ​ 那么为什么会有人使用 Helm 呢? Helm 通过模板化方法在 Kubernetes 中更

    2023年04月09日
    浏览(26)
  • Helm-从0手动创建charts

    创建 chart 目录结构: 创建 Chart.yaml : 创建 templates 目录: 创建 deployment.yaml: 以上完成后就可以使用 helm 部署,部署名为 my-nginx 的应用: 查看创建的应用: 查看创建的实际资源: 继续创建 service.yaml: 创建 values.yaml: 修改 deployment.yaml,注入变量,改为模板形式: 修改

    2024年02月16日
    浏览(24)
  • Helm Chart安装EFK并验证功能

    本文介绍如何通过Helm Chart方式快速在Kubernetes环境中搭建EFK(Elasticsearch,Filebeat,Kibana)V8.5.1 日志收集系统并验证其功能。如果仅对安装有兴趣请直接食用“EFK(Elasticsearch,Filebeat,Kibana)V8.5.1 安装”章节。 日志收集系统背景需求 随着现在各种软件系统的复杂度越来越高,

    2024年02月09日
    浏览(27)
  • 【笔记】Helm-3 主题-6 Chart仓库指南

    Chart仓库指南 本节介绍如何创建和使用chart仓库。在高层级中,chart仓库是打包的chart存储和分享的位置。 社区的Helm chart仓位于 Artifact Hub ,欢迎加入。不过Helm也可以创建并运行您自己的chart仓库。该指南将介绍如何操作。 Artifact Hub 先决条件 先阅读 快速开始 阅读 Charts 文档

    2024年01月18日
    浏览(28)
  • DevOps系列文章 之 Java使用jgit管理git仓库

    最近设计基于gitops新的CICD方案,需要通过java读写git仓库,这里简单记录下。 在jgit中,存在最核心的三个组件:Git类,Repository类。Git类中包含了push commit之类的常见git操作,而Repository则实现了仓库的初始化和基本的管理功能。 Git类的实例都会持有一个Repository实例。 Repositor

    2024年02月12日
    浏览(27)
  • 云原生Kubernetes:简化K8S应用部署工具Helm

    目录 一、理论 1.HELM ​编辑 2.部署HELM2 3.部署HELM3(2to3方式) 4.部署HELM3(单独安装) 二、实验 1.部署 HELM2 2.部署HELM3(2to3方式) 3.部署HELM3(单独安装) 三、问题 1.api版本过期 2.helm初始化报错 3.pod状态为ImagePullBackOff 4.helm 命令显示 no repositories to show 的错误 5.Helm安装报错

    2024年02月07日
    浏览(45)
  • 什么是Helm?它是如何提升云原生应用私有化部署效率的

    转载至我的博客 ,公众号:架构成长指南 试想一下,如果有一个项目有50 个微服务,每个微服务都有service、deployment、ingress、pvc等 yaml 文件,算下来大概有 200 个文件,然后这个项目需要基于k8s进行私有化交付,如果是你会怎么快速部署应用? 首先让我们先思考一下 200 个文

    2024年02月03日
    浏览(37)
  • DevOps系列文章 之 Gitlab+Docker自动部署SpringBoot

    以下服务器的操作系统均为Centos7 服务器A:Gitlab 服务器B:GitlabRunner、Docker、docker-compose、Java1.8、maven3.6.3、git ps:这里可以把服务器B的GitlabRunner、Java1.8、maven3.6.3、git单独提出来,独立部署,需要java的原因是maven,maven用于打包。 应用服务器B就只需要docker和docker-compose就可以

    2024年02月13日
    浏览(40)
  • DevOps系列文章之 GitlabCICD自动化部署SpringBoot项目

    本文主要记录如何通过Gitlab CI/CD自动部署SpringBoot项目jar包。 准备三台 CentOS7服务器,分别部署以下服务: 序号 系统 IP 服务 1 CentOS7 192.168.56.10 Gitlab 2 CentOS7 192.168.56.11 Runner (安装Docker) 3 CentOS7 192.168.56.12 SpringBoot 项目 jar 包(安装jdk、maven等) 上述服务也可以只用一台CentOS

    2024年02月13日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包