本文介绍在 VKE 中通过 terraform 部署无状态工作负载。
在VKE中通过 terraform 部署无状态工作负载。
预计实验时间:30分钟
级别:初级
相关产品:VKE
受众: 通用
如果还没有火山引擎账号,点击此[链接]注册账号
如果还没有VKE集群,参考此[链接]快速创建一个VKE
terraform { required_providers { kubernetes = { source = "hashicorp/kubernetes" version = "2.16.0" } } }
variable "image" { description = "enter the image" type = string } variable "config" { description = "enter the config" type = string }
provider "kubernetes" { config_path = var.config } resource "kubernetes_deployment" "example" { metadata { name = "terraform-deployment-example" labels = { test = "tf-deployment" } } spec { replicas = 1 selector { match_labels = { test = "tf-pod" } } template { metadata { labels = { test = "tf-pod" } } spec { container { image = var.image name = "tf-nginx" resources { limits = { cpu = "0.5" memory = "512Mi" } requests = { cpu = "250m" memory = "50Mi" } } liveness_probe { http_get { path = "/" port = 80 http_header { name = "X-Custom-Header" value = "Awesome" } } initial_delay_seconds = 3 period_seconds = 3 } } } } } }
$ terraform init
# 执行过程中会要求输入config文件、image $ terraform plan
$ terraform apply
$ kubectl get deployment -n default NAME READY UP-TO-DATE AVAILABLE AGE terraform-deployment-example 1/1 1 1 8m10s
$ kubectl get pod -n default NAME READY STATUS RESTARTS AGE terraform-deployment-example-5dc5b4b594-gpc94 1/1 Running 0 8m34s
[1] https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/deployment