Consul部署手册(helm版本)
使用 Helm 部署 Consul 作为统一配置管理中心
本文档旨在指导您使用 Helm 在 Kubernetes 集群中部署 Consul,并将其配置为一个专注于统一配置管理的服务。此部署将禁用服务网格、服务同步等非必要功能,以简化架构并专注于配置存储。
1. 前提条件
- 一个正在运行的 Kubernetes 集群 (版本 >= 1.20 推荐)。
kubectl命令行工具已安装并配置好,可以访问您的集群。helm(版本 3+) 命令行工具已安装。- (可选,但推荐) 用于 Consul UI 的 TLS 证书和私钥 (例如
consultest.crt和consultest.key)。 - (可选) 一个支持
networking.k8s.io/v1Ingress API 的 Ingress Controller (如 NGINX Ingress Controller) 已部署并运行。
2. 添加 HashiCorp Helm 仓库
将 HashiCorp 官方 Helm 仓库添加到您的本地 Helm 客户端:
helm repo add hashicorp https://helm.releases.hashicorp.com
helm repo update
3. 准备配置文件
创建一个名为 consul-values.yaml 的文件,内容如下。请根据您的具体环境修改其中的 consul.XXX.com 域名和存储类名称 (nfs-sc)。
重要提示: 此配置禁用了内部 TLS (global.tls.enabled: false) 和 Connect 功能 (global.connect: false, connectInject.enabled: false 等),专注于配置管理。它也移除了可能导致调度问题的 Pod 反亲和性规则,允许 Pod 调度到相同节点(适用于节点数少于副本数的场景)。
# consul-values.yaml - 仅用于 Consul 统一配置管理
# --- 全局设置 ---
global:
# -- 设置所有组件的日志级别
logLevel: "info"
# -- 启用 JSON 格式日志
logJSON: true
# -- Consul Docker 镜像 (请根据需要指定版本)
image: "hashicorp/consul:1.21.2"
# -- Consul K8s Control Plane 镜像 (请根据需要指定版本)
imageK8S: "hashicorp/consul-k8s-control-plane:1.7.2"
# -- Consul 域名 (用于 DNS 查询,即使不用于服务发现也建议设置)
domain: "consul"
# -- 启用 ACLs (强烈推荐用于生产环境,即使只做配置管理)
acls:
manageSystemACLs: true
# bootstrapToken: # 如果预创建了引导令牌 Secret,可以在此指定
# secretName: "consul-bootstrap-acl-token"
# secretKey: "token"
# -- 启用 TLS (强烈推荐用于生产环境)
tls:
enabled: false
# enableAutoEncrypt: false # 可选:加密客户端与服务端通信
# caCert: # 如果使用预创建的 CA 证书,可以在此指定
# secretName: "consul-ca-cert"
# caKey: # 如果使用预创建的 CA 密钥,可以在此指定
# secretName: "consul-ca-key"
# -- Gossip 加密 (强烈推荐)
gossipEncryption:
autoGenerate: true
# -- 节点元数据
nodeMeta:
pod-name: ${HOSTNAME}
host-ip: ${HOST_IP}
# --- 服务端配置 ---
server:
enabled: true
replicas: 3 # 推荐用于高可用 (HA)
bootstrapExpect: 3 # 应与 replicas 匹配以实现 HA
storage: "10Gi" # 持久卷大小
storageClass: "nfs-sc" # 如有需要,指定存储类
resources:
requests:
memory: "512Mi"
cpu: "250m"
limits:
memory: "1Gi"
cpu: "500m"
connect: false # 禁用服务网格功能
extraConfig: | # 自定义配置(可选)
{
"log_level": "INFO",
"disable_update_check": true
}
# -- 为服务端 Pod 设置反亲和性,防止调度到同一节点
# 使用软性反亲和性(优先分散,但不强制)
affinity: |
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchLabels:
app: {{ template "consul.name" . }}
release: "{{ .Release.Name }}"
component: server
topologyKey: kubernetes.io/hostname
#使用硬性反亲和性(强制,推荐使用)
#affinity: |
# podAntiAffinity:
# requiredDuringSchedulingIgnoredDuringExecution:
# - labelSelector:
# matchLabels:
# app: {{ template "consul.name" . }}
# release: "{{ .Release.Name }}"
# component: server
# topologyKey: kubernetes.io/hostname
# --- 客户端配置 ---
client:
enabled: true
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "200m"
# -- 客户端额外配置
extraConfig: |
{
"leave_on_terminate": true,
"skip_leave_on_interrupt": false
}
# --- UI 配置 (可选,方便查看配置) ---
ui:
enabled: true
service:
enabled: true
type: "ClusterIP" # 或 LoadBalancer, NodePort
# Ingress配置(对外暴露UI)
ingress:
enabled: true # 启用Ingress
ingressClassName: "nginx" # 指定Ingress Controller类型
pathType: Prefix
annotations: | # 自定义注解(如配置重定向规则)
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
hosts:
- host: "consul.XXX.com" # 访问域名
paths:
- /
tls: # TLS配置(建议生产环境启用)
- hosts:
- consul.XXX.com
# --- DNS 配置 (通常需要,即使不用于服务发现) ---
dns:
enabled: true # 启用Consul DNS服务
enableRedirection: false # 禁用DNS重定向(仅配置中心不需要)
# --- 其他不需要的组件 ---
# connectInject, syncCatalog, meshGateway, ingressGateways 等默认为 false 或 null,无需显式设置
# 除非您明确需要它们的功能
connectInject:
enabled: false # 禁用自动sidecar注入
meshGateway:
enabled: false # 禁用网格网关
ingressGateways:
enabled: false # 禁用入口网关
terminatingGateways:
enabled: false # 禁用终止网关
syncCatalog:
enabled: false # 禁用K8S服务与Consul服务的同步
4. 部署Consul
helm upgrade --install consul-config \
hashicorp/consul \
-n consul \
--create-namespace \
-f consul-values.yaml \
--version 1.7.2
5. 验证部署
部署后,检查 Pod 和 Service 是否正常运行:
kubectl get pods,svc -n consul
您应该看到 Consul Server Pod (状态为 Running) 和相关的 Service。
检查 Ingress 是否创建:
kubectl get ingress -n consul
6. 访问 Consul UI
- 确保您的 DNS 或 /etc/hosts 文件已将 consul.XXX.com 指向您的 Ingress Controller 的 IP 地址。
- 在浏览器中访问 http://consul.XXX.com (如果配置了 TLS 且 Secret 存在,则访问 https://consul.XXX.com)
- 如果启用了 ACL (global.acls.manageSystemACLs: true),首次访问 UI 时需要提供 ACL Token。引导令牌可以通过以下方式获取:
kubectl get secret <RELEASE_NAME>-consul-bootstrap-acl-token -n <NAMESPACE> -o jsonpath={.data.token} | base64 --decode
(将 <RELEASE_NAME> 和

浙公网安备 33010602011771号