Terraform與Kubernetes

看到Terraform能夠替代kubectl管理k8s資源的生命週期,因而調研了下它的使用場景,並對比Terraform和Helm的區別html

一.Terraform介紹

Terraform是一款開源工具,出自HashiCorp公司,著名的Vagrant、Consul也出自於該公司。其主要做用是:讓用戶更輕鬆地管理、配置任何基礎架構,管理公有和私有云服務,也能夠管理外部服務,如GitHub,Nomad。nginx

區別於ansible和puppet等傳統的配置管理工具,Terraform趨向於更上層的一個組裝者。git

Terraform使用模板來定義基礎設施,經過指令來實現資源建立/更新/銷燬的全生命週期管理,實現「基礎設施即代碼」,具體示例以下:github

resource "alicloud_instance" "web" {
    # cn-beijing
    availability_zone = "cn-beijing-b"
    image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"

    system_disk_category = "cloud_ssd"

    instance_type = "ecs.n1.small"
    internet_charge_type = "PayByBandwidth"
    security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
    instance_name = "test_foo"
    io_optimized = "optimized"
}

這是阿里雲的一個Terraform邏輯,執行terraform apply,就能夠建立一個ECS實例web

Terraform AliCloud provider: terraform-providerubuntu

二.Terraform支持K8S

17年9月,Terraform官方宣佈支持Kubernetes,提供Kubernetes應用程序的完整生命週期管理,包含Pod的建立、刪除以及副本控制等功能(經過調用API)。api

如下是操做示例:架構

1.安裝kubernete集羣

當前k8s的installer列表,已經不少了...app

使用Terraform在阿里雲上安裝k8s集羣:kubernetes-exampleside

2.建立應用:

  • 1.初始化k8s-provider
由於是調用apiserver,因此須要指定k8s集羣的鏈接方式

provider "kubernetes" {} // 默認~/.kube/config
或:
provider "kubernetes" {
  host = "https://104.196.242.174"

  client_certificate     = "${file("~/.kube/client-cert.pem")}"
  client_key             = "${file("~/.kube/client-key.pem")}"
  cluster_ca_certificate = "${file("~/.kube/cluster-ca-cert.pem")}"
}
  • 2.初始化terraform
$ terraform init

Initializing provider plugins...
- Downloading plugin for provider "kubernetes"...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
  • 3.建立RC
// Terraform不支持Deployment
// issue:https://github.com/terraform-providers/terraform-provider-kubernetes/issues/3

resource "kubernetes_replication_controller" "nginx" {
  metadata {
    name = "scalable-nginx-example"
    labels {
      App = "ScalableNginxExample"
    }
  }

  spec {
    replicas = 2
    selector {
      App = "ScalableNginxExample"
    }
    template {
      container {
        image = "nginx:1.7.8"
        name  = "example"

        port {
          container_port = 80
        }

        resources {
          limits {
            cpu    = "0.5"
            memory = "512Mi"
          }
          requests {
            cpu    = "250m"
            memory = "50Mi"
          }
        }
      }
    }
  }
}
  • 4.建立service
resource "kubernetes_service" "nginx" {
  metadata {
    name = "nginx-example"
  }
  spec {
    selector {
      App = "${kubernetes_replication_controller.nginx.metadata.0.labels.App}"
    }
    port {
      port = 80
      target_port = 80
    }

    type = "LoadBalancer"
  }
}
  • 4.查看和執行
以上的步驟均爲執行計劃的定義
執行操做:terraform apply
查看當前執行幾乎:terraform plan

三.爲何使用Terraform

  • 1.若是你的基礎設施(虛機、BLB等)是用Terraform來管理,那麼你無需任何成本,能夠用一樣的配置語言,來管理k8s集羣
  • 2.完整的生命週期管理
  • 3.每一個執行的同步反饋
  • 4.關係圖譜:好比PVC和PV,若是PV建立失敗,則不會去執行PVC的流程

四.與Helm的對比

若是是對K8S作上層的資源管理,大多數人會想到用Helm:參考

如下是Helm與Terraform都建立一個APP的操做對比:https://dzone.com/articles/te...

Terraform的優點:

  • 1.若是你的基礎設施已經用了Terraform,那麼k8s集羣管理也能夠直接用這個,沒有學習成本
  • 2.Terraform不須要在k8s集羣中安裝任何組件,它經過APISERVER管理資源

Terraform的缺點:

  • 1.對K8S的支持還比較弱,並且17年9月纔開始支持,項目還在初期
  • 2.嚴重依賴Terraform的基礎設施provider,好比外部磁盤、consul等沒有支持的話,k8s中沒法使用
  • 2.不支持beta資源,這個是硬傷,如:Deployment/StatefulSet/Daemonset不支持
  • 3.沒有生態和市場的概念,好比helm中的倉庫,共享你們的應用倉庫

五.吐槽

對於Terraform,不支持Deployment這一條,足以讓不少人放棄這個方案,而issue中對於這個的討論,也有點不太樂觀

必須在v1中的資源纔會支持。對於Deployment你們只能用RC代替、或者kube exec加進去(尬

但對於kubernetes而言,beta階段的不少資源,已經被你們普遍使用(Deployment、Daemonset),並且新版本的Deployment已經變成了apps/v1。

k8s各類版本(v一、apps/v1)的區別:參考文章

不知道後續Terraform有沒有更多的支持,觀望下~

相關文章
相關標籤/搜索