You need to enable JavaScript to run this app.
导航
在VKE中通过 terraform 部署无状态工作负载
最近更新时间:2024.01.09 10:16:06首次发布时间:2024.01.09 10:16:06

本文介绍在 VKE 中通过 terraform 部署无状态工作负载。

前言

在VKE中通过 terraform 部署无状态工作负载。

关于实验

预计实验时间:30分钟
级别:初级
相关产品:VKE
受众: 通用

实验说明

如果还没有火山引擎账号,点击此[链接]注册账号
如果还没有VKE集群,参考此[链接]快速创建一个VKE

第一步、环境说明

1.安装terraform CLI

参考链接

2.编写terraform文件

a.编写versions.tf

terraform {
  required_providers {
    kubernetes = {
      source = "hashicorp/kubernetes"
      version = "2.16.0"
    }
  }
}

b.编写variables.tf

variable "image" {
    description = "enter the image"
    type = string
}

variable "config" {
    description = "enter the config"
    type = string
}

c.编写main.tf

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
          }
        }
      }
    }
  }
}

3.初始化工作目录

$ terraform init

4.创建工作计划

# 执行过程中会要求输入config文件、image
$ terraform plan

5.创建工作负载

$ terraform apply

6.查看工作负载

$ kubectl  get deployment -n default
NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
terraform-deployment-example   1/1     1            1           8m10s

7.查看pod

$ 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