helm 创建 JDK11 模板 并根据模板创建应用

创建模板

mkdir -p /data/helm_app/ && cd /data/helm_app/

helm create jdk11-projects

cd jdk11-projects/templates
# 只保留下面3个文件
deployment.yaml  _helpers.tpl  service.yaml

# deployment.yaml 配置
cat deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: {{ .Values.namespace.name }}
  name: {{ include "jdk11-projects.fullname" . }}
  labels:
    {{- include "jdk11-projects.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "jdk11-projects.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "jdk11-projects.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts: 
          - mountPath: /etc/localtime
            readOnly: false 
            name: time-data
          livenessProbe:      
            tcpSocket:
              port: {{ .Values.container.port }}
            initialDelaySeconds: 30 # 在执行第一次检查之前等待的时间,单位秒
            periodSeconds: 90       # 指定kubelet 每隔多少秒执行一次检查
          readinessProbe:
            tcpSocket:
              port: {{ .Values.container.port }}
            initialDelaySeconds: 60 # 在执行第一次检查之前等待的时间,单位秒
            timeoutSeconds: 1       # 探测的超时时间,默认为1,单位秒
          lifecycle:
              preStop: #  确认删除 pod 和更新网络规则之间的顺序关系。
                exec:
                  command: ["sh", "-c", "sleep 10"]
          env:
            {{- toYaml .Values.envs | nindent 12 }}
      volumes: 
      - name: time-data 
        hostPath: 
          path: /usr/share/zoneinfo/Asia/Shanghai

# _helpers.tpl 配置
cat _helpers.tpl 
{{/*
Expand the name of the chart.
*/}}
{{- define "jdk11-projects.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "jdk11-projects.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "jdk11-projects.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "jdk11-projects.labels" -}}
helm.sh/chart: {{ include "jdk11-projects.chart" . }}
{{ include "jdk11-projects.selectorLabels" . }}
{{- if .Chart.AppVersion }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "jdk11-projects.selectorLabels" -}}
app.kubernetes.io/name: {{ include "jdk11-projects.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "jdk11-projects.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "jdk11-projects.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}


# service.yaml 配置
cat service.yaml 
{{- if .Values.service.enabled  }}
apiVersion: v1
kind: Service
metadata:
  namespace: {{ .Values.namespace.name }}
  name: {{ include "jdk11-projects.fullname" . }}
  labels:
    {{- include "jdk11-projects.labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
  - name: {{ .Values.service.name }}
    port: {{ .Values.service.port }}
    {{- if eq .Values.service.type "NodePort" }}
    nodePort: {{ .Values.service.nodePort }}
    {{- end }}

  selector:
    {{- include "jdk11-projects.selectorLabels" . | nindent 4 }}
{{- end }}

values.yaml 值

cd /data/helm_app/jdk11-projects

cat values.yaml 
namespace: 
  name: default

replicaCount: 1

image:
  repository: nginx
  pullPolicy: IfNotPresent
  # Overrides the image tag whose default is the chart appVersion.
  tag: ""

imagePullSecrets: []
fullnameOverride: ""

service:
  enabled: false
  name: http
  type: NodePort
  nodePort: 30081
  port: 80

resources: 
   limits:
     cpu: 2000m
     memory: 2048Mi
   requests:
     cpu: 100m
     memory: 1024Mi

envs: []
#- name: PROFILES_ACTIVE
#  value: "test"

container:
  port: 80

上传到 harbor

cd /data/helm_app/
helm cm-push ./jdk11-projects devharbor -u admin -p Harbor12345

根据模板创建应用,以nginx为例子

mkdir -p /data/helm_app/nginx && cd /data/helm_app/nginx

cat Chart.yaml 
apiVersion: v2
name: nginx
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: "1.16.0"
# 添加依赖
dependencies:
- name: jdk11-projects
  version: "0.1.0"
  repository: "https://devharbor.junengcloud.com/chartrepo/helm"

# 替换模板的值
cat values.yaml 
jdk11-projects:
  namespace: 
    name: default
  
  replicaCount: 1
  
  image:
    repository: nginx
    pullPolicy: IfNotPresent
    # Overrides the image tag whose default is the chart appVersion.
    tag: "1.23.3"
  
  imagePullSecrets: []
  fullnameOverride: "nginx"
  
  container:
    port: 80

  service:
    enabled: false
    name: http
    type: NodePort
    nodePort: 30081
    port: 80
  
  resources: {}
  #   limits:
  #     cpu: 2000m
  #     memory: 2048Mi
  #   requests:
  #     cpu: 100m
  #     memory: 1024Mi
  
  envs: []
  #- name: PROFILES_ACTIVE
  #  value: "test"

# 更新依赖
helm dependency build

# 启动应用
cd  /data/helm_app/
helm install -g ./nginx/

helm ls

posted @ 2022-12-22 16:45  klvchen  阅读(56)  评论(0)    收藏  举报