近期文章:
昨天安装配置了Helm,尝试从阿里云chart拉取memcached进行部署,接着昨天的内容今天在使用自定义的Chart,部署Pod
自定义 Charts 模板
Helm create创建
[root@master01 ~]# helm create myapp Creating myapp [root@master01 ~]# cd myapp/ [root@master01 myapp]# ll 总用量 8 drwxr-xr-x 2 root root 6 8月 31 00:24 charts -rw-r--r-- 1 root root 1149 8月 31 00:24 Chart.yaml drwxr-xr-x 3 root root 162 8月 31 00:24 templates -rw-r--r-- 1 root root 1880 8月 31 00:24 values.yaml [root@master01 myapp]# tree ./ ./ ├── charts #用于存放所依赖的子 chart ├── Chart.yaml # 描述这个 Chart 的相关信息、包括名字、描述信息、版本等 ├── templates # 模板目录,保留创建 k8s 的资源清单文件 │ ├── deployment.yaml #deployment 资源的 go 模板文件 │ ├── _helpers.tpl # 模板助手文件,定义的值可在模板中使用 │ ├── hpa.yaml #水平 pod 自动扩缩容 go 模板文件 │ ├── ingress.yaml #七层代理 go 模板文件 │ ├── NOTES.txt #chart的帮助文本。会在用户执行helm install时展示 │ ├── serviceaccount.yaml │ ├── service.yaml #service 的 go 模板文件 │ └── tests │ └── test-connection.yaml └── values.yaml #模板的值文件,这些值会在安装时应用到 GO 模板生成部署文件 3 directories, 10 files
Chart.yaml字段解释
[root@master01 myapp]# cat Chart.yaml apiVersion: v2 name: myapp description: A Helm chart for Kubernetes # 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.0 # 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.yaml 文件主要用来描述对应 chart 的相关属性信息, #apiVersion 字段用于描述对应 chart 使用的 api 版本,默认是 v2 版本; #name 字段用于描述对应 chart 的名称; #description 字段用于描述对应 chart 的说明简介; #type 字段用户描述对应 chart 是应用程序还是库文件,应用程序类型的chart,它可以运行为一个 release,但库类型的 chart 不能运行为 release,它只能作为依赖被 #application 类型的 chart 所使用; #version 字段用于描述对应 chart 版本; #appVersion 字段用于描述对应 chart 内部程序的版本信息;
deployment.yaml字段解释
[root@master01 myapp]# cat templates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: {{ include "myapp.fullname" . }} labels: {{- include "myapp.labels" . | nindent 4 }} spec: {{- if not .Values.autoscaling.enabled }} replicas: {{ .Values.replicaCount }} {{- end }} selector: matchLabels: {{- include "myapp.selectorLabels" . | nindent 6 }} template: metadata: {{- with .Values.podAnnotations }} annotations: {{- toYaml . | nindent 8 }} {{- end }} labels: {{- include "myapp.selectorLabels" . | nindent 8 }} spec: {{- with .Values.imagePullSecrets }} imagePullSecrets: {{- toYaml . | nindent 8 }} {{- end }} serviceAccountName: {{ include "myapp.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 }} {{- with .Values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 8 }} {{- end }} {{- with .Values.affinity }} affinity: {{- toYaml . | nindent 8 }} {{- end }} {{- with .Values.tolerations }} tolerations: {{- toYaml . | nindent 8 }} {{- end }}
解释:
#该部署清单模板文件,主要用 go 模板语言来写的,其中{{ include "myapp.fullname" . }}就表示取 myapp 的全名; #{{ .Values.image.repository }}这段代码表示读取当前目录下的 values.yaml 文件中的image.repository 字段的值; #{{ .Values.image.tag | default .Chart.AppVersion }}表示对于 values文件中 image.tag 的值或者读取 default.chart 文件中的 AppVersion 字段的值; #简单讲 go 模板就是应用对应 go 模板语法来定义关属性的的值; #一般都是从 values.yaml 文件中加载对应字段的值作为模板文件相关属性的值。 #nindent 4:表示首行缩进 4 个字母 #TRUNC(NUMBER)表示截断数字 #if/else, 用来创建条件语句
values.yaml字段解释
[root@master01 myapp]# cat values.yaml # Default values for myapp. # This is a YAML-formatted file. # Declare variables to be passed into your templates. replicaCount: 1 image: repository: nginx pullPolicy: IfNotPresent #指定镜像拉取策略 # Overrides the image tag whose default is the chart appVersion. tag: "latest" #指定镜像版本 imagePullSecrets: [] nameOverride: "" fullnameOverride: "" serviceAccount: # Specifies whether a service account should be created create: true # Annotations to add to the service account annotations: {} # The name of the service account to use. # If not set and create is true, a name is generated using the fullname template name: "" podAnnotations: {} podSecurityContext: {} # fsGroup: 2000 securityContext: {} # capabilities: # drop: # - ALL # readOnlyRootFilesystem: true # runAsNonRoot: true # runAsUser: 1000 service: type: ClusterIP port: 80 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: 100 targetCPUUtilizationPercentage: 80 # targetMemoryUtilizationPercentage: 80 nodeSelector: {} tolerations: [] affinity: {}
解释:
#比如我们要引用 values.yaml 文件中的 image 字段下的 tag 字段的值,我们可以在模板文件中写成{{ .Values.image.tag }}; #如果在命令行使用--set 选项来应用我们可以写成 image.tag;修改对应的值可以直接编辑对应 values.yaml 文件中对应字段的值,也可以直接使用--set 指定对应字段的对应值即可; #默认情况在命令行使用--set 选项给出的值,都会直接被替换,没有给定的值,默认还是使用values.yaml 文件中给定的默认值;
Helm install部署
#部署myapp [root@master01 myapp]# helm install myapp ./ NAME: myapp LAST DEPLOYED: Thu Aug 31 07:50:39 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=myapp,app.kubernetes.io/instance=myapp" -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 #查看pod [root@master01 myapp]# kubectl get pods NAME READY STATUS RESTARTS AGE myapp-5d9bf9545c-kdrrs 1/1 Running 0 34s #查看deploy [root@master01 myapp]# kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE myapp 1/1 1 1 2m26s #查看svc [root@master01 myapp]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 44h myapp ClusterIP 10.100.221.23 <none> 80/TCP 2m38s
访问svc地址
![image-1 - zhpengfei.com](https://zhpengfei.com/wp-content/uploads/2023/09/image-1-1024x436.jpg)
可以看出,已经成功部署了自定义chart
#查看pod详细信息 [root@master01 myapp]# kubectl describe pod myapp-5d9bf9545c-kdrrs
![image-1 - zhpengfei.com](https://zhpengfei.com/wp-content/uploads/2023/09/image-1-1024x418.png)
Helm 打包
[root@master01 ~]# helm package myapp Successfully packaged chart and saved it to: /root/myapp-0.1.0.tgz
![image - zhpengfei.com](https://zhpengfei.com/wp-content/uploads/2023/09/image-1024x118.png)
打包之后就可以分享给其他人了,也可以使用helm push推送到自己的仓库
官方文档