一、方式二:下载并分析 WordPress Chart

1、下载WordPress chart

执行以下命令将WordPress chart下载到本地

[root@master01 ~]# cd /root/3/
[root@master01 3]# helm fetch bitnami/wordpress

2、解压WordPress chart

执行以下命令解压WordPress chart

[root@master01 3]# tar -zxvf wordpress-*.tgz

注意:这里解压会覆盖掉方式一的wordpress目录,请事先备份

3、修改values.yaml文件

在wordpress目录下,打开values.yaml文件,并根据需要修改WordPress的配置参数,以下是一个示例

[root@master01 ~]# vim /root/3//wordpress/values.yaml
global:
  imageRegistry: ""
  imagePullSecrets: []
  storageClass: "nfs-storage"
wordpressPassword: "wppwd"
wordpressUsername: wpuser
wordpressBlogName: ZHDYA's Blog!
wordpressScheme: http
wordpressSkipInstall: true

replicaCount: 1
updateStrategy:
  type: RollingUpdate
hostAliases:
initContainers: []
pod/node亲和相关:
resources:
livenessProbe:
readinessProbe:
startupProbe:
service:
ingress:
...
...

4、Deployment.yaml文件

在wordpress/templates目录下,查看Deployment.yaml的文件:

[root@master01 ~]# more  /root/3/wordpress/templates/deployment.yaml
apiVersion: {{ include "common.capabilities.deployment.apiVersion" .
}}
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value".Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value".Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  {{- if .Values.updateStrategy }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
...
...

在这个文件中,使用include函数引用了其他资源的名称,例如使用include"common.names.fullname"来引用charts包( charts/common/templates )中定义的Deployment资源的名称。这样可以使不同的资源之间产生关联,从而实现更加复杂的应用程序部署。

5、Service.yaml文件

在wordpress/templates目录下:

[root@master01 ~]# more  /root/3/wordpress/templates/svc.yaml
...
...
spec:
  type: {{ .Values.service.type }}
  {{- if and .Values.service.clusterIP (eq .Values.service.type"ClusterIP") }}
  clusterIP: {{ .Values.service.clusterIP }}
  {{- end }}
  {{- if or (eq .Values.service.type "LoadBalancer") (eq.Values.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.service.type "LoadBalancer") (not (empty.Values.service.loadBalancerSourceRanges))}}
  loadBalancerSourceRanges: {{.Values.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.service.type "LoadBalancer") (not (empty.Values.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.service.loadBalancerIP }}
  {{- end }}
  {{- if .Values.service.sessionAffinity }}
  sessionAffinity: {{ .Values.service.sessionAffinity }}
...
...

6、Ingress.yaml文件

在wordpress/templates目录下,其中在这个文件中,使用了 if 的小开关模式;

[root@master01 ~]# more  /root/3/wordpress/templates/ingress.yaml
{{- if .Values.ingress.enabled }}
apiVersion: {{ include "common.capabilities.ingress.apiVersion" . }}
kind: Ingress
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value".Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  annotations:
    {{- if .Values.ingress.annotations }}
    {{- include "common.tplvalues.render" (dict "value".Values.ingress.annotations "context" $) | nindent 4 }}
    {{- end }}
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value".Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
...
...
    {{- end }}

7、PersistentVolumeClaim.yaml文件

在wordpress/templates目录下:

[root@master01 ~]# more  /root/3/wordpress/templates/pvc.yaml
{{- if and .Values.persistence.enabled (not.Values.persistence.existingClaim) }}
{{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }}
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  {{- if or .Values.persistence.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.persistence.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  accessModes:
  {{- if not (empty .Values.persistence.accessModes) }}
  {{- range .Values.persistence.accessModes }}
    - {{ . | quote }}
  {{- end }}
  {{- else }}
    - {{ .Values.persistence.accessMode | quote }}
  {{- end }}
  resources:
    requests:
      storage: {{ .Values.persistence.size | quote }}
  {{- include "common.storage.class" (dict "persistence" .Values.persistence "global" .Values.global) | nindent 2 }}
  {{- if .Values.persistence.selector }}
  selector: {{- include "common.tplvalues.render" (dict "value" .Values.persistence.selector "context" $) | nindent 4 }}
  {{- end -}}
  {{- if .Values.persistence.dataSource }}
  dataSource: {{- include "common.tplvalues.render" (dict "value" .Values.persistence.dataSource "context" $) | nindent 4 }}
  {{- end }}
{{- end }}

在这个文件中,使用了PersistentVolumeClaim资源来定义WordPress的持久化存储配置。大量使用了 if , include , .Release 等

8、Secret.yaml文件

在wordpress/templates目录下:

[root@master01 ~]# more  /root/3/wordpress/templates/secrets.yaml
{{- if or (not .Values.existingSecret) (and (not .Values.smtpExistingSecret) .Values.smtpPassword) }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: Opaque
data:
  {{- if not .Values.existingSecret }}
  wordpress-password: {{ include "common.secrets.passwords.manage" (dict "secret" (include "common.names.fullname" .) "key" "wordpress-password" "providedValues" (list "wordpressPassword") "context" $) }}
  {{- end }}
  {{- if and .Values.smtpPassword (not .Values.smtpExistingSecret) }}
  {{- if .Values.smtpPassword }}
  smtp-password: {{ .Values.smtpPassword | b64enc | quote }}
  {{- end }}
  {{- end }}
{{- end }}

在这个文件中,使用了Secret资源来存储MySQL的密码信息。

在这个示例中, Values.smtpPassword 是一个字符串类型的变量,使用 b64enc 函数将其编码为 Base64 格式,并将其存储在名为 password 的 Secret 数据中。在使用这个Secret 数据时,可以使用 base64decode 函数将其解码为原始字符串格式。

注意, b64enc 函数只能用于字符串类型的变量,如果需要编码其他类型的变量,需要先将其转换为字符串类型。

9、定义Chart.yaml文件

在wordpress目录下

[root@master01 ~]# more  /root/3/wordpress/Chart.yaml
annotations:
  category: CMS
  images: |
    - name: apache-exporter
      image: docker.io/bitnami/apache-exporter:1.0.3-debian-11-r1
    - name: os-shell
      image: docker.io/bitnami/os-shell:11-debian-11-r91
    - name: wordpress
      image: docker.io/bitnami/wordpress:6.4.1-debian-11-r4
  licenses: Apache-2.0
apiVersion: v2
appVersion: 6.4.1
dependencies:
- condition: memcached.enabled
  name: memcached
  repository: oci://registry-1.docker.io/bitnamicharts
  version: 6.x.x
- condition: mariadb.enabled
  name: mariadb
  repository: oci://registry-1.docker.io/bitnamicharts
  version: 14.x.x
- name: common
  repository: oci://registry-1.docker.io/bitnamicharts
  tags:
  - bitnami-common
  version: 2.x.x
description: WordPress is the world's most popular blogging and content management
  platform. Powerful yet simple, everyone from students to global corporations use
  it to build beautiful, functional websites.
home: https://bitnami.com
icon: https://bitnami.com/assets/stacks/wordpress/img/wordpress-stack-220x234.png
keywords:
- application
- blog
- cms
- http
- php
- web
- wordpress
maintainers:
- name: VMware, Inc.
  url: https://github.com/bitnami/charts
name: wordpress
sources:
- https://github.com/bitnami/charts/tree/main/bitnami/wordpress
version: 18.1.14

在这个文件中,定义了一个名称模板,用于生成应用程序的名称。同时定义了wordpress依赖Chart的信息,以及维护者和源代码信息。

注意:这边我们使用到了dependencies(作为应用的启动基础依赖项),如果手动开发Chart文件在运行前,我们需要运行 helm dependency update将依赖下载到chart目录中。

以下是一个包含你所需的模板函数的 _helpers.tpl 文件示例:

[root@master01 ~]# more  /root/3/wordpress/templates/_helpers.tpl
...
...
{{/* vim: set filetype=mustache: */}}
{{/*
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).
*/}}
{{- define "wordpress.mariadb.fullname" -}}
{{- include "common.names.dependency.fullname" (dict "chartName"
"mariadb" "chartValues" .Values.mariadb "context" $) -}}
{{- 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).
*/}}
{{- define "wordpress.memcached.fullname" -}}
{{- include "common.names.dependency.fullname" (dict "chartName"
"memcached" "chartValues" .Values.memcached "context" $) -}}
{{- end -}}
...
...

这个文件定义了很多模板函数和变量:

  • wordpress.mariadb.fullname :用于定义 Mariadb Chart 的完整名称,可以在其他模板文件中引用。

你可以将这个文件放在 WordPress Chart 的 templates 目录下,然后在其他模板文件中使用 {{ include "wordpress.mariadb.fullname" . }}的方式来引用wordpress.mariadb.fullname变量。

10、安装Chart

使用以下命令安装Chart

[root@master01 ~]# cd /root/3/
[root@master01 3]# helm install --dry-run my-wordpress ./wordpress

helm --dry-run 选项用于模拟 Helm 安装过程,但不会实际执行任何操作。它会输出Helm 安装过程中生成的 Kubernetes YAML 文件,以便用户可以检查这些文件是否正确。使用 --dry-run 选项时,Helm 不会创建任何 Kubernetes 资源,也不会更新状态,因此它是一个非常安全的选项,可以用于测试 Helm Chart 的正确性。这个命令会使用values.yaml文件中定义的配置参数,安装一个名为my-wordpress的新Chart实例。

11、正式部署

其中,my-wordpress是部署的名称,可以根据需要进行修改。

[root@master01 3]# helm install my-wordpress ./wordpress

12、查看部署状态

#查看pvc
[root@master01 wordpress]# kg pvc
NAME                                          STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
data-my-wordpress-mariadb-0                   Bound    pvc-22dbb823-8293-40b3-817a-5597b9e2263f   8Gi        RWO            nfs-storage    5m54s
my-wordpress                                  Bound    pvc-88c0ab77-a9b8-4738-9373-9d251b3055db   10Gi       RWO            nfs-storage    5m54s

#查看svc
[root@master01 wordpress]# kg svc
NAME                   TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)                      AGE
kubernetes             ClusterIP      10.0.0.1       <none>        443/TCP                      13d
my-wordpress           LoadBalancer   10.0.187.53    <pending>     80:32711/TCP,443:30159/TCP   6m12s
my-wordpress-mariadb   ClusterIP      10.0.127.162   <none>        3306/TCP                     6m12s

#查看pod
[root@master01 wordpress]# kgp
NAME                            READY   STATUS    RESTARTS   AGE
my-wordpress-58db4b7dfc-jfksc   1/1     Running   0          6m29s
my-wordpress-mariadb-0          1/1     Running   0          6m29s

12、访问WordPress

节点内任意 宿主机IP:32088 即可访问;

[root@master01 3]# curl 10.0.0.60:32711 -I
HTTP/1.1 200 OK
Date: Tue, 14 Nov 2023 12:11:46 GMT
Server: Apache
Link: <http://10.0.0.60:32711/wp-json/>; rel="https://api.w.org/"
Content-Type: text/html; charset=UTF-8

13、如果需要升级WordPress,可以使用以下命令

$ helm upgrade my-wordpress bitnami/wordpress -f values.yaml

如果需要删除WordPress,可以使用以下命令:

$ helm uninstall my-wordpress

二、环境清理

第三章节学完后,进行资源清理

1、清理pod

#清理wordpress下的pod
[root@master01 3]# k delete po  --all -n wordpress

#清理default下的pod
[root@master01 3]# k delete po  --all

查看

#查看default下的pod
[root@master01 3]# kgp -n wordpress
No resources found in wordpress namespace.

#查看wordpress下的pod
[root@master01 3]# kgp

2、清理PVC

[root@master01 3]# k delete pvc  --all