看到Terraform能夠替代kubectl管理k8s資源的生命週期,因而調研了下它的使用場景,並對比Terraform和Helm的區別html
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
17年9月,Terraform官方宣佈支持Kubernetes,提供Kubernetes應用程序的完整生命週期管理,包含Pod的建立、刪除以及副本控制等功能(經過調用API)。api
如下是操做示例:架構
當前k8s的installer列表,已經不少了...app
使用Terraform在阿里雲上安裝k8s集羣:kubernetes-exampleside
由於是調用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")}" }
$ 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.
// 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" } } } } } }
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" } }
以上的步驟均爲執行計劃的定義 執行操做:terraform apply 查看當前執行幾乎:terraform plan
若是是對K8S作上層的資源管理,大多數人會想到用Helm:參考
如下是Helm與Terraform都建立一個APP的操做對比:https://dzone.com/articles/te...
Terraform的優點:
Terraform的缺點:
對於Terraform,不支持Deployment這一條,足以讓不少人放棄這個方案,而issue中對於這個的討論,也有點不太樂觀
必須在v1中的資源纔會支持。對於Deployment你們只能用RC代替、或者kube exec加進去(尬
但對於kubernetes而言,beta階段的不少資源,已經被你們普遍使用(Deployment、Daemonset),並且新版本的Deployment已經變成了apps/v1。
k8s各類版本(v一、apps/v1)的區別:參考文章
不知道後續Terraform有沒有更多的支持,觀望下~