通过 Terraform 安装 Nginx 插件和实例

最近更新时间:2024-01-09 19:58:41

我的收藏

前言

本文示例使用的环境信息如下:
TKE 集群 Kubernetes 版本:v1.26.1
安装 Nginx 插件版本:v1.4.1
安装 Nginx 实例版本:v1.6.4

步骤1:安装 Terraform

您可以通过以下命令下载并安装 Terraform:
wget https://releases.hashicorp.com/terraform/1.6.6/terraform_1.6.6_linux_amd64.zip
v1.4.6版本 Release 地址为 https://releases.hashicorp.com/terraform/1.6.6/,您可以根据系统选择对应安装包。

步骤2:在集群中安装 Nginx Addon

Nginx Addon 插件是一个 Nginx 的安装管理工具。首先安装 Addon 插件,然后再使用插件安装 Nginx 实例,具体操作请参见 腾讯云Terraform 应用指南
provider.tf 示例文件如下:
# 腾讯云 provider
terraform {
required_providers {
tencentcloud = {
source = "tencentcloudstack/tencentcloud"
version = "1.81.61"
}
}
}

# 腾讯云 相关信息(更换密钥对 "secret_id"、"secret_key")
provider "tencentcloud" {
secret_id = "********"
secret_key = "********"
region = "ap-shanghai"
}

# 安装Nginx插件(更换集群ID "cluster_id")
resource "tencentcloud_kubernetes_addon_attachment" "addon_ingressnginx" {
cluster_id = "cls-xxxxxxxx"
name = "ingressnginx"
request_body = "{\\"kind\\":\\"App\\",\\"spec\\":{\\"chart\\":{\\"chartName\\":\\"ingressnginx\\",\\"chartVersion\\":\\"1.4.1\\"}}}"
}

步骤3:声明式安装 Nginx 实例

有关 Kubernetes Provider 的更多配置信息,请参见 官方文档
Nginx 实例的相关配置可以根据需要进行修改。
IngressClass 配置(示例中使用的是 demo)
HPA 配置
requests/limits 配置
provider.tf 示例文件如下:
provider "kubernetes" {
config_path = "~/.kube/config"
}

resource "kubernetes_manifest" "nginxingress_demo" {
manifest = {
"apiVersion" = "cloud.tencent.com/v1alpha1"
"kind" = "NginxIngress"
"metadata" = {
"name" = "demo"
}
"spec" = {
"ingressClass" = "demo"
"service" = {
"annotation" = {
"service.kubernetes.io/service.extensiveParameters" = "{\\"InternetAccessible\\":{\\"InternetChargeType\\":\\"TRAFFIC_POSTPAID_BY_HOUR\\",\\"InternetMaxBandwidthOut\\":10}}"
}
"type" = "LoadBalancer"
}
"workLoad" = {
"hpa" = {
"enable" = true
"maxReplicas" = 2
"metrics" = [
{
"pods" = {
"metricName" = "k8s_pod_rate_cpu_core_used_limit"
"targetAverageValue" = "80"
}
"type" = "Pods"
},
]
"minReplicas" = 1
}
"template" = {
"affinity" = {}
"container" = {
"image" = "ccr.ccs.tencentyun.com/tkeimages/nginx-ingress-controller:v1.6.4"
"resources" = {
"limits" = {
"cpu" = "0.5"
"memory" = "1024Mi"
}
"requests" = {
"cpu" = "0.25"
"memory" = "256Mi"
}
}
}
}
"type" = "deployment"
}
}
}
}