terraform管理本地的k8s创建pod
provider "kubernetes" {
  config_path   = "/etc/kubernetes/admin.conf"
}
resource "kubernetes_namespace" "test" {
  metadata {
    name = "mynginx1"
  }
}
resource "kubernetes_deployment" "test" {
  metadata {
    name      = "nginx"
    namespace = kubernetes_namespace.test.metadata.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.name
  }
  spec {
    selector = {
      app = kubernetes_deployment.test.spec.template.metadata.labels.app
    }
    type = "NodePort"
    port {
      node_port   = 30201
      port        = 80
      target_port = 80
    }
  }
}

import os

if __name__ == '__main__':
    print("this is terrform test")
    userName = "cf"
    namespase = "mynginx1"
    image = "nginx"
    name = "Mytest"
    os.system("mkdir " + userName)
    os.system("cd " + userName)
    os.system("cp copy/copyk8sPyTest.tf " + userName + "/" + userName + ".tf")

    os.system("sed -i 's/myNameSpace/" + namespase + "/' " + userName + "/" + userName + ".tf")
    os.system("sed -i 's/myimage/" + image + "/' " + userName + "/" + userName + ".tf")
    os.system("sed -i 's/MyMyMytest/" + name + "/' " + userName + "/" + userName + ".tf")
    os.system("terraform -chdir=./" + userName + " init")
    os.system("terraform -chdir=./" + userName + " plan")

terraform通过IP远程管理k8s创建pod
provider "kubernetes" {
  host = "https://192.168.100.4:6443"
  client_certificate     = base64decode("LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURJVENDQWdtZ0F3SUJBZ0lJUHhJWHYvWjZMbEl3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB5TWpBNU1qZ3dOekkxTVRkYUZ3MHlNekE1TWpnd056STFNVGhhTURReApGekFWQmdOV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JDQ1dOclpXNFZtYi8rSGpjUmU2bjRxVU5ZcGl6a0ZUNFZSbk1WTE9BWVkxSDl1K3VLUkdibStsNDdJbDRWQwpBOWV0WWcyOGxaWEpkU3pVamhFQ0F3RUFBYU5aTUZjd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0hRWURWUjBPQkJZRUZLRU9wVnBkVkpJYytaSEhqTkZrdytTK05mWmZNQlVHQTFVZEVRUU8KTUF5Q0NtdDFZbVZ5Ym1WMFpYTXdEUVlKS29aSWh2Y05BUUVMQlFBRGdnRUJBS05FNUNieDUrUWpXOUpzeWJHSQpoS1RKOEFSUUlEeC9CektHQkt5ZGx1TDRHQUpMYmFpYjBpREZQOXhxTXdTcVZJR1EyenA4TmdXZmtZT0pEQlZpCmNXT29oRU14bis4VlB2Y2R6eFA4My95S0VSendPNzN4K01YWE05VDhhYXBWRjFkL0hBSDM3QTE4dytPUERORHoKOXRPVFVwc2Vtc1J5M3dudU5UdDN0anlRaHExSDQ0SC9UblhXN1BwN2JnMkNZZEpqWnJueTlBY09QTTlHSGUyMgo3Nk9LSVhNbDI0OUpFUkdKZ3hqOENWNE5hRSt4SmFjWXd1NzVhODBuK09Wa01lWm5heW5KY2daZThOYllYd25kCjkrcWtIMHBBR3hqcmhjL2JsdUdwSW53cnZScHlCckE3UVJpZHhBMHVtYXNSM3N3cWs1bldibE1VK0Q0dXQ5MEkKQ3JZPQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==")
}
resource "kubernetes_namespace" "Mytest" {
  metadata {
    name = "mynginx"
  }
}
resource "kubernetes_deployment" "Mytest" {
  metadata {
    name      = "nginx"
    namespace = kubernetes_namespace.Mytest.metadata.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" "Mytest" {
  metadata {
    name      = "nginx"
    namespace = kubernetes_namespace.Mytest.metadata.name
  }
  spec {
    selector = {
      app = kubernetes_deployment.Mytest.spec.template.metadata.labels.app
    }
    type = "NodePort"
    port {
      node_port   = 30201
      port        = 80
      target_port = 80
    }
  }
}

使用命令kubectl config view获取host

vim /etc/kubernetes/admin.conf文件里

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐