Halo Write

Halo Write

在 Kubernetes 搭建 Halo 博客

766
2023-01-04
在 Kubernetes 搭建 Halo 博客

Kubernetes 也称为 K8s,是用于自动部署、扩缩和管理容器化应用程序的开源系统。如果能够在其上运行 Halo 将会是一件非常有趣地事情。

前置条件

当前文章的重点在于安装 Halo,所以我们需要准备好以下两个环境(或工具):

  1. Kubernetes 集群(版本 >= 1.19)

  2. Helm 包管理工具(版本 >= 3)

目标

  1. 创建 Kubernetes 集群

  2. 安装并运行 Halo 博客

  3. 允许外部访问 Halo

  4. 自动申请域名证书

假设

  1. 域名:halowrite.com

  2. 服务器已准备好

快速开始

搭建 Kubernetes 集群

如果你已经安装过 Kubernetes 集群,可忽略当前步骤。

为了演示简单,这里推荐使用 K3s

  1. 如果你购买的是裸金属服务器,请参考 K3s 安装文档进行安装。

  2. 如果你购买的正好是预装了 K3s 的轻量服务器,可忽略当前步骤。

如果服务器运行在大陆,推荐使用镜像加速器,例如:

cat /etc/rancher/k3s/registries.yaml
mirrors:
  docker.io:
    endpoint:
      - "https://xxxyyyzzz.mirror.aliyuncs.com"

为了解决 Traefik Ingress Controller 无法传递 Client IP 的问题,我们需要自定义 Traefik Service 的 externalTrafficPolicy 为 Local,示例如下:

cat <<EOF | kubectl apply -f -
apiVersion: helm.cattle.io/v1
kind: HelmChartConfig
metadata:
  name: traefik
  namespace: kube-system
spec:
  valuesContent: |-
    service:
      spec:
        externalTrafficPolicy: "Local"
EOF

安装 Helm 包管理工具

为了更方便我们安装和更新证书管理器和 Halo,我们需要提前安装好 Helm 包管理工具。

因运行环境不同,请根据文档选择合适的安装方式。

注意:需要安装 Helm v3 及以上的版本。

安装证书管理工具 cert-manager

推荐使用 Helm 安装,具体操作请参考:https://cert-manager.io/docs/installation/helm/

kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.12.3/cert-manager.crds.yaml
helm repo add jetstack https://charts.jetstack.io
helm install cert-manager --namespace cert-manager --version v1.12.2 jetstack/cert-manager

创建 ClusterIssuer,为后续申请证书做好准备。下面是配置样例,请根据自己的情况合理自定义:

cat <<EOF | kubectl apply -f -
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  name: letsencrypt
spec:
  acme:
    email: johnniang@fastmail.com
    privateKeySecretRef:
      name: cluster-issuer-account-key
    server: https://acme-v02.api.letsencrypt.org/directory
    solvers:
      - http01:
          ingress:
            class: traefik
EOF

解析域名

域名解析可能需要等待一段时间才会生效。

安装 Halo

推荐参考:

在没有任何配置的情况下,默认会安装 PostgreSQL 作为 Halo 的数据库。通过 Helm 安装 Halo 之前,如果想要自定义 Halo 配置,可参考下面的示例:

配置文件位置:~/halo/values.yaml

image:
  tag: "2.7.0"
livenessProbe:
  httpGet:
    path: /actuator/health
readinessProbe:
  httpGet:
    path: /actuator/health
ingress:
  enabled: true
  hostname: halowrite.com
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt
  tls: true

想要了解更多 Helm Charts 自定义参数,请参考:https://github.com/halo-sigs/charts。

添加 Helm Charts 仓库并安装 Halo,具体命令如下:

helm repo add halo https://halo-sigs.github.io/charts/
# 当前命令可重复执行可实现更新操作。
helm upgrade --create-namespace --install -n halo halo halo/halo -f ~/halo/values.yaml

如果一切正常,我们将会看到以下内容:

Release "halo" does not exist. Installing it now.
coalesce.go:223: warning: destination for postgresql.networkPolicy.egressRules.customRules is a table. Ignoring non-table value ([])
NAME: halo
LAST DEPLOYED: Thu Jul 27 11:02:06 2023
NAMESPACE: halo
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: halo
CHART VERSION: 1.1.0
APP VERSION: 2.6.1

** Please be patient while the chart is being deployed **

Your Halo site can be accessed through the following DNS name from within your cluster:

    halo.halo.svc.cluster.local (port 80)

To access your Halo site from outside the cluster follow the steps below:

1. Get the Halo URL and associate Halo hostname to your cluster external IP:

   export CLUSTER_IP=$(minikube ip) # On Minikube. Use: `kubectl cluster-info` on others K8s clusters
   echo "Halo URL: https://halowrite.com/"
   echo "$CLUSTER_IP  halowrite.com" | sudo tee -a /etc/hosts

2. Open a browser and access Halo using the obtained URL.

3. Login with the following credentials below to see your site:

  echo Username: admin
  echo Password: $(kubectl get secret --namespace halo halo -o jsonpath="{.data.halo-password}" | base64 -d)

根据提示,我们可以执行一下命令拿到初始的用户名和密码:

echo Username: admin
echo Password: $(kubectl get secret --namespace halo halo -o jsonpath="{.data.halo-password}" | base64 -d)

至此,SSL 证书会自动创建(自动续期)并应用,我们只需要访问 https://halowrite.com 就可以正常使用 Halo 了。