项目作者: ericchiang

项目描述 :
Kubernetes Terraform provider with support for raw manifests
高级语言: Go
项目地址: git://github.com/ericchiang/terraform-provider-k8s.git
创建时间: 2018-02-03T06:25:57Z
项目社区:https://github.com/ericchiang/terraform-provider-k8s

开源协议:MIT License

下载


This project has been archived and is unmaintained.

Kubernetes Terraform Provider

The k8s Terraform provider enables Terraform to deploy Kubernetes resources. Unlike the official Kubernetes provider it handles raw manifests, leveraging kubectl directly to allow developers to work with any Kubernetes resource natively.

Usage

Use go get to install the provider:

  1. go get -u github.com/ericchiang/terraform-provider-k8s

Register the plugin in ~/.terraformrc:

  1. providers {
  2. k8s = "/$GOPATH/bin/terraform-provider-k8s"
  3. }

The provider takes the following optional configuration parameters:

  • If you have a kubeconfig available on the file system you can configure the provider as:
  1. provider "k8s" {
  2. kubeconfig = "/path/to/kubeconfig"
  3. }
  • If you content of the kubeconfig is available in a variable, you can configure the provider as:
  1. provider "k8s" {
  2. kubeconfig_content = "${var.kubeconfig}"
  3. }

WARNING: Configuration from the variable will be recorded into a temporary file and the file will be removed as
soon as call is completed. This may impact performance if the code runs on a shared system because
and the global tempdir is used.

The k8s Terraform provider introduces a single Terraform resource, a k8s_manifest. The resource contains a content field, which contains a raw manifest.

  1. variable "replicas" {
  2. type = "string"
  3. default = 3
  4. }
  5. data "template_file" "nginx-deployment" {
  6. template = "${file("manifests/nginx-deployment.yaml")}"
  7. vars {
  8. replicas = "${var.replicas}"
  9. }
  10. }
  11. resource "k8s_manifest" "nginx-deployment" {
  12. content = "${data.template_file.nginx-deployment.rendered}"
  13. }

In this case manifests/nginx-deployment.yaml is a templated deployment manifest.

  1. apiVersion: apps/v1beta2
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. labels:
  6. app: nginx
  7. spec:
  8. replicas: ${replicas}
  9. selector:
  10. matchLabels:
  11. app: nginx
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx
  16. spec:
  17. containers:
  18. - name: nginx
  19. image: nginx:1.7.9
  20. ports:
  21. - containerPort: 80

The Kubernetes resources can then be managed through Terraform.

  1. $ terraform apply
  2. # ...
  3. Apply complete! Resources: 1 added, 1 changed, 0 destroyed.
  4. $ kubectl get deployments
  5. NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
  6. nginx-deployment 3 3 3 3 1m
  7. $ terraform apply -var 'replicas=5'
  8. # ...
  9. Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
  10. $ kubectl get deployments
  11. NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
  12. nginx-deployment 5 5 5 3 3m
  13. $ terraform destroy -force
  14. # ...
  15. Destroy complete! Resources: 2 destroyed.
  16. $ kubectl get deployments
  17. No resources found.