• init = (known after apply)

  • ip_address = (known after apply)

  • ip_prefix_length = (known after apply)

  • ipc_mode = (known after apply)

  • log_driver = “json-file”

  • logs = false

  • must_run = true

  • name = “tutorial”

  • network_data = (known after apply)

  • read_only = false

  • remove_volumes = true

  • restart = “no”

  • rm = false

  • security_opts = (known after apply)

  • shm_size = (known after apply)

  • start = true

  • stdin_open = false

  • tty = false

  • healthcheck {

  • interval = (known after apply)

  • retries = (known after apply)

  • start_period = (known after apply)

  • test = (known after apply)

  • timeout = (known after apply)

}

  • labels {

  • label = (known after apply)

  • value = (known after apply)

}

  • ports {

  • external = 8000

  • internal = 80

  • ip = “0.0.0.0”

  • protocol = “tcp”

}

}

docker_image.nginx will be created

  • resource “docker_image” “nginx” {

  • id = (known after apply)

  • keep_locally = false

  • latest = (known after apply)

  • name = “nginx:latest”

  • output = (known after apply)

}

Plan: 2 to add, 0 to change, 0 to destroy.

执行变更:

$ terraform apply

docker_image.nginx: Creating…

docker_image.nginx: Still creating… [10s elapsed]

docker_image.nginx: Still creating… [20s elapsed]

docker_image.nginx: Creation complete after 28s [id=sha256:d1a364dc548d5357f0da3268c888e1971bbdb957ee3f028fe7194f1d61c6fdeenginx:latest]

docker_container.nginx: Creating…

docker_container.nginx: Creation complete after 1s [id=0dac86e383366959bd976cc843c88395a17c5734d729f62f07106caf604b466f]

它自动帮我们下载了镜像和启动了容器。通过以下命令查看nginx的主页:

$ curl http://localhost:8000

现在我不想要这些资源了,通过以下命令删除:

$ terraform destroy

docker_container.nginx: Destroying… [id=0dac86e383366959bd976cc843c88395a17c5734d729f62f07106caf604b466f]

docker_container.nginx: Destruction complete after 0s

docker_image.nginx: Destroying… [id=sha256:d1a364dc548d5357f0da3268c888e1971bbdb957ee3f028fe7194f1d61c6fdeenginx:latest]

docker_image.nginx: Destruction complete after 1s

4 部署Kubernetes资源


创建目录:

$ mkdir terraform-kubernetes-demo && cd $_

创建 main.tf 文件:

terraform {

required_providers {

kubernetes = {

source = “hashicorp/kubernetes”

version = “>= 2.0.0”

}

}

}

provider “kubernetes” {

config_path = “~/.kube/config”

}

resource “kubernetes_namespace” “test” {

metadata {

name = “nginx”

}

}

resource “kubernetes_deployment” “test” {

metadata {

name = “nginx”

namespace = kubernetes_namespace.test.metadata.0.name

}

spec {

replicas = 2

selector {

match_labels = {

app = “MyTestApp”

}

}

template {

metadata {

labels = {

app = “MyTestApp”

}

}

spec {

container {

image = “nginx”

name = “nginx-container”

port {

container_port = 80

}

}

}

}

}

}

resource “kubernetes_service” “test” {

metadata {

name = “nginx”

namespace = kubernetes_namespace.test.metadata.0.name

}

spec {

selector = {

app = kubernetes_deployment.test.spec.0.template.0.metadata.0.labels.app

}

type = “NodePort”

port {

node_port = 30201

port = 80

target_port = 80

}

}

}

直接执行:

terraform init

terraform apply

检查结果:

$ kubectl -n nginx get deployment

NAME READY UP-TO-DATE AVAILABLE AGE

nginx 2/2 2 2 2m

$ kubectl -n nginx get service

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE

nginx NodePort 10.98.213.164 80:30201/TCP 71s

$ curl http://localhost:30201

测试完成,删除:

terraform destroy

5 总结

Terraform 在云计算领域还是有自己的一席之地的,值得了解学习。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐