66、K8S-部署管理-Helm-自定义helm项目

发布时间 2023-04-13 19:08:57作者: 小粉优化大师

1、自定义helm项目管理-实践

1.1、自定义helm项目

1.1.1、创建存放的目录

mkdir -p /opt/custom_helm && cd /opt/custom_helm

1.1.2、创建helm项目

helm create nginx

1.2.3、目录的解析

custom_helm]# tree nginx/
nginx/                                - 自动生成的空chart,名称就是nginx
├── charts
├── Chart.yaml                        - 声明了当前Chart的名称、版本等基本信息,便于用户在仓库里浏览检索
├── templates                         - 存放各种资源清单文件
│   ├── deployment.yaml
│   ├── _helpers.tpl                  - 定制的模板功能文件,包含各种需要根据值结果进行调整的功能
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml                       - 存放各种定制的变量值,符合yaml格式语法

注意: Chart里面的nginx名称需要和生成的Chart文件夹名称一致。如果修改nginx,则需要一致的修改。

1.2、常见的语法-解析

1.2.1、{{- if  .Values.serviceAccount.create -}}

如果在项目的根目录下存在Values文件,并且里面的serviceAccount部分的create属性为true的话。

执行下面的操作
{{- 表达式 -}}, 横杠(-)表示去掉表达式输出结果前面和后面的空格,可以使用单个{{- 表达式 }}

# 点的说明
最左面的点(.): 代表全局作用域-项目根目录,中间的点:是对象中属性的引用方式。

1.2.2、{{ include "nginx.serviceAccountName" . }}

加载项目的templates/_helpers.tpl文件里面定义好的 nginxhelloworld.serviceAccountName内容


例如:
templates]# cat serviceaccount.yaml 
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ include "nginx.serviceAccountName" . }}
  labels:
    {{- include "nginx.labels" . | nindent 4 }}
  {{- with .Values.serviceAccount.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
{{- end }}

templates]# cat _helpers.tpl
...
{{/*
Create the name of the service account to use
*/}}
{{- define "nginx.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "nginx.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
...

1.2.3、 {{- toYaml . | nindent 4 }}

toYaml 表示将数据转为yaml格式,nindent表示不局限于4行,indent 4代表就是格式化转换为4行

1.2.4、{{- with .Values.imagePullSecrets }}

 with语法代表修改内容

1.3、修改模板-deployment.yaml

1.3.1、查询需要修改的模板

templates]# cat deployment.yaml 
...
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}  # 设置容器数量
  {{- end }}
  selector:
    matchLabels:
      {{- include "nginx.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "nginx.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "nginx.serviceAccountName" . }}
      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: {{ .Values.service.port }}
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
...

1.3.2、修改Value.yaml【这里主要修改下载镜像为我们本地仓库的】

nginx]# vi values.yaml 
image:
  repository: 192.168.10.33:80/k8s/my_nginx
  pullPolicy: IfNotPresent
  # Overrides the image tag whose default is the chart appVersion.
  tag: "v1"
# 这里主要修改两点,仓库地址+版本

1.3.3、修改deployment.yaml【主要增加访问的首页】

nginx]# vi templates/deployment.yaml
...
spec:
...
  template:
    metadata:
...
    spec:
...
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
          - name: htmlBody:
value: {{ .Values.htmlBody }} lifecycle: postStart: exec: command: ["/bin/sh","-c","echo $htmlBody> /usr/share/nginx/html/index.html"
] ports: - name: http containerPort: {{ .Values.service.port }} protocol: TCP

1.3.4、修改Value.yaml【配置htmlBody变量值内容】

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

replicaCount: 1
htmlBody: "my nginx hello world"

1.3.5、检查语法是否正常

]# helm lint --strict /opt/custom_helm/nginx/
==> Linting /opt/custom_helm/nginx/
[INFO] Chart.yaml: icon is recommended

1 chart(s) linted, 0 chart(s) failed

1.4、项目打包

1.4.1、开始打包

]# helm package /opt/custom_helm/nginx/ -d /opt/custom_helm/
Successfully packaged chart and saved it to: /opt/custom_helm/nginx-0.1.0.tgz # 保存的位置

注意:压缩包的版本号来自于Chart.yaml文件中的属性

1.4.2、版本号的修改

]# vi /opt/custom_helm/nginx/Chart.yaml 
...
# 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.0 # 以这个为命名

1.5、本地安装定制镜像文件

1.5.1、注意事项【了解】

注意:必须在k8s的集群中来部署相关的应用,如果当前主机没有k8s的话,可以通过环境变量来进行指定k8s主机的地址位置。

export KUBERNETES_MASTER
=http://master:6443

1.5.2、安装helm-nginx-0.1.0.tgz压缩包

]# helm install my-nginx-helm /opt/custom_helm/nginx-0.1.0.tgz 
NAME: my-nginx-helm
LAST DEPLOYED: Thu Apr 13 18:26:29 2023
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=nginx,app.kubernetes.io/instance=my-nginx-helm" -o jsonpath="{.items[0].metadata.name}")
  export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT

1.5.3、查询运行状态

]# kubectl get pods -o wide
NAME                             READY   STATUS    RESTARTS   AGE   IP            NODE    NOMINATED NODE   READINESS GATES
my-nginx-helm-85b77648c9-m5n2c   1/1     Running   0          95s   10.244.4.16   node2   <none>           <none>

]# kubectl get svc
NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
my-nginx-helm        ClusterIP   10.107.46.245   <none>        80/TCP    21s

~]# helm list
NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
my-nginx-helm   default         1               2023-04-13 18:26:29.102169531 +0800 CST deployed        nginx-0.1.0     1.16.0 

1.5.4、访问SVCIP地址测试

~]# curl 10.107.46.245
my nginx hello world

1.6、使用变量值设置页面内容

1.6.1、设置变量安装helm

helm install my-nginx-helm-add /opt/custom_helm/nginx-0.1.0.tgz --set htmlBody='my nginx test!'

1.6.2、查询svc

~]# kubectl get svc
NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
my-nginx-helm-add    ClusterIP   10.110.16.246   <none>        80/TCP    12s

1.6.3、访问svc地址测试

~]# curl 10.110.16.246
my nginx test!