使用terraform和cdk在azure上使用terraform和python在azure上创建kubernetes集群-程序员宅基地

技术标签: kubernetes  python  

介绍与动机 (Intro & Motivation)

Defining infrastructure by writing code has been widely adopted last years. Terraform is one of the leading infrastructure as code tools and known by providing intuitive language, low barrier to get working with, and a lot of providers(integrations to places we are trying to automate).

近年来,通过编写代码定义基础结构已被广泛采用。 Terraform作为代码工具是领先的基础架构之一,以提供直观的语言,较低的使用障碍以及众多的提供程序(集成到我们尝试自动化的地方)而闻名。

Files for Terraform is mostly written in HCL, which is a language used for configuring many HashiCorp products besides Terraform. Terraform’s HCL options are widening day by day and handles many detailed/corner cases. It is also really easy to write and read, even for non-programmers can easily define their infrastructure in it. Still, in some cases, developers still need to be dynamic about the resources we are trying to define as code, define infrastructure needs according to responses of custom integrations in their programming languages. Note that writing custom integrations is possible with Terraform and there are a lot of examples covering that.

Terraform的文件主要是用HCL编写的,该语言是用于配置Terraform之外的许多HashiCorp产品的语言。 Terraform的HCL选项每天都在扩展,并且可以处理许多详细/角落情况。 即使非程序员也可以轻松地在其中定义其基础结构,它的读写也非常容易。 尽管如此,在某些情况下,开发人员仍然需要对我们试图定义为代码的资源保持动态,根据其编程语言中自定义集成的响应来定义基础架构需求。 请注意,使用Terraform可以编写自定义集成,并且有很多示例对此进行了介绍。

Bonus: It is also possible to employ linters/code completion/etc. for Python or TypeScript and they should work!

奖励 :也可以使用lints /代码完成/等。 对于Python或TypeScript,它们应该可以工作!

如何用Python编写Terraform文件? (How to Write Terraform Files in Python?)

As we said before, Terraform takes our definitions in HCL. In addition to HCL, Terraform also accepts JSON formatted files. This means that it is possible to produce JSON files in proper format and Terraform will recognize this files without converting to HCL.

如前所述,Terraform在HCL中采用了我们的定义。 除了HCL,Terraform还接受JSON格式的文件。 这意味着可以生成正确格式的JSON文件,并且Terraform可以识别该文件而无需转换为HCL。

Python and TypeScript are also very common in the last years. So even if a feature is not exists in HCL (yet) or you have a code that should generate infrastructure definitions automatically, integrate with your projects, create SaaS that should create computing resources, … there is a chance to define Terraform resources in Python and TypeScript as well.

过去几年,Python和TypeScript也很常见。 因此,即使HCL中尚不存在某个功能,或者您具有应自动生成基础结构定义,与您的项目集成,创建应创建计算资源的SaaS的代码,也可以在Python中定义Terraform资源,并且以及TypeScript。

Instead of learning Terraform’s JSON format, manually scanning module docs and writing classes/methods to create proper definitions, HashiCorp released CDK for Terraform (details in the link) that automatically does heavy lifting for you by creating classes for which providers you are using in your preferred language (Python or TypeScript).

HashiCorp不再学习Terraform的JSON格式,而是手动扫描模块文档并编写类/方法来创建正确的定义,而是发布了CDK for Terraform (链接中的详细信息),该CDK通过创建要在您的提供程序中使用的提供程序的类为您自动完成繁重的工作首选语言(Python或TypeScript)。

CDK for Terraform also generates the JSON file in the format that Terraform understands.

CDK for Terraform还会以Terraform可以理解的格式生成JSON文件。

Let’s see how to create an Azure Kubernetes Cluster using Terraform CDK.

让我们看看如何使用Terraform CDK创建Azure Kubernetes群集。

安装CDK for Terraform (Installing CDK for Terraform)

In order to use CDK for Terraform, we should install it using via npm (most of it is written in TypeScript). It requires Terraform, Node.js, and Yarn installed on your computer. For up-to-date install instructions, HashiCorp Learn will be a great resource. Just follow the instructions in that link.

为了将CDK用于Terraform,我们应该使用npm来安装它(大多数都是用TypeScript编写的)。 它需要在计算机上安装Terraform,Node.js和Yarn。 有关最新的安装说明, HashiCorp Learn将是一个很好的资源。 只需按照该链接中的说明进行操作即可。

If you use Docker and don’t want to install a package with npm, I prepared a Dockerfile that contains all of them. It is also available as an image on Docker Hub so you may use it directly to try contents of this post without building image. I tagged my image for CDK for Terraform v0.0.14 as guray/tfcdkc:0.0.14. So in that case, there is no requisite other than Docker. Just follow the steps and you should be ready.

如果您使用Docker并且不想使用npm安装软件包,那么我准备了一个包含所有软件包Dockerfile 。 它也可以作为镜像在Docker Hub上使用,因此您可以直接使用它来尝试本文的内容,而无需构建镜像。 我将Terraform v0.0.14的CDK图像标记为guray / tfcdkc:0.0.14 。 因此,在这种情况下,除Docker之外没有其他要求。 只需执行以下步骤,您就应该准备好了。

Run this if you are using Docker

如果您正在使用Docker,请运行此命令

docker pull guray/tfcdkc:0.0.14

使用CDK for Terraform启动新项目 (Starting a New Project with CDK for Terraform)

A project in Terraform-CDK needs include a couple of files to be useful. cdktf command helps us to generate these files automatically.

Terraform-CDK中的一个项目需要包含几个有用的文件。 cdktf命令可帮助我们自动生成这些文件。

If you have installed with npm, go to an empty directory(or create) and run the command:

如果已使用npm安装,请转到一个空目录(或创建)并运行以下命令:

$ mkdir cdktest && cd $_
$ cdktf init --template python

OR if you are using Docker:

或者,如果您使用的是Docker:

$ mkdir cdktest && cd $_
$ docker run -it --rm -v $PWD:/code guray/tfcdkc:0.0.14
(in container)/code # cdktf init --template python

After running the command, it will ask you a couple of questions:

运行命令后,它将询问您几个问题:

If you are using Terraform Cloud and want to store Terraform state there, you can write “yes” and login. In this post, we will store the state locally. So we will say “no” here.

如果您正在使用Terraform Cloud并想在其中存储Terraform状态,则可以输入“是”并登录。 在这篇文章中,我们将状态存储在本地。 因此,我们在这里说“不”。

It will ask a Project Name and Project Description, write answers or just press enter to proceed with the default values. It should look like this:

它将询问项目名称项目描述 ,编写答案或仅按Enter即可继续使用默认值。 它看起来应该像这样:

Image for post
CDK for Terraform asking project name and description
CDK for Terraform询问项目名称和描述

Afterwards, it will create an empty project for you:

之后,它将为您创建一个空项目:

$ lsPipfile       Pipfile.lock  cdktf.json    cdktf.out     help          imports       main.py

发生了什么? 这些文件是什么?为什么在这里? (What Happened? What are These Files and Why They Are Here?)

cdktf command line interface has created a Python virtual environment with pipenv and it installed cdktf module in it using pip.

cdktf命令行界面已使用pipenv创建了一个Python虚拟环境,并使用pip在其中安装了cdktf模块。

By default, it bootstraps the project for AWS(Terraform’s AWS provider). This is defined in cdktf.json and we will change that file soon. Generated Python modules for Terraform’s AWS provider is resides in imports directory.

默认情况下,它会为AWS(Terraform的AWS提供程序)引导项目。 这是在cdktf.json中定义的,我们将尽快更改该文件。 为Terraform的AWS提供程序生成的Python模块位于imports目录中。

Pipfile and Pipfile.lock are for pipenv to record & lock installed module versions & requirements.

PipfilePipfile.lock用于pipenv记录并锁定已安装的模块版本和要求。

help file includes a basic documentation showing useful subcommands of cdktf. Taking a look at that file will be useful.

帮助文件包括基本文档,该文档显示了cdktf的有用子命令。 查看该文件将很有用。

main.py is the vital thing for us here. We will write our definition of Azure resources (AKS in this case) here. It includes a fundamental template to work on, so we will add our resources/providers here.

main.py对我们来说至关重要。 我们将在此处编写Azure资源(在本例中为AKS)的定义。 它包括一个基础模板,因此我们将在此处添加我们的资源/提供者。

cdktf.out is a directory that includes our JSON file (output).

cdktf.out是包含我们的JSON文件(输出)的目录。

使用CDK为Azure编码Azure Kubernetes服务 (Coding Azure Kubernetes Service With CDK for Terraform)

Now we have main.py and a working project.

现在我们有了main.py和一个正在运行的项目。

At this point, you can use your IDE and its features to define resources here. Many IDEs will automatically recogize the virtual environment created by pipenv and use it. This way they will autocomplete your code by using cdktf module and other modules created automatically for defining resources.

此时,您可以使用IDE及其功能在此处定义资源。 许多IDE会自动识别Pipenv创建的虚拟环境并使用它。 这样,他们将使用cdktf模块和为定义资源而自动创建的其他模块自动完成代码。

将Azure定义为CDK的提供者 (Defining Azure as a Provider to CDK)

By default, CDK for Terraform creates a config file that includes AWS as a provider. We can use any number of Terraform providers(there are a lot!) at there. Let’s add Azure to the list. cdktf.json file will look like this(you can also remove AWS provider if you will not use):

默认情况下,CDK for Terraform创建一个包含AWS作为提供程序的配置文件。 我们在那里可以使用任何数量的Terraform提供程序(数量很多!)。 让我们将Azure添加到列表中。 cdktf.json文件将如下所示(如果您不使用,也可以删除AWS提供程序):

{
        
"language": "python",
"app": "pipenv run ./main.py",
"terraformProviders": [
"aws@~> 2.0",
"azurerm@~> 2.22.0"
],
"codeMakerOutput": "imports"
}

After adding the provider, we should ask cdktf to generate Python modules for these providers:

添加提供程序之后,我们应要求cdktf为这些提供程序生成Python模块:

$ cdktf get   
⠴ downloading and generating providers...
... after a while
Generated python constructs in the output directory: imports

It may take some time but you will not need to change it if you are not changing the config file(which will probably be very rare). Now, import directory is updated. Just make sure your code completion saw the new modules in this directory to write code easily.

这可能会花费一些时间,但是如果您不更改配置文件,则无需更改它(这可能非常少见)。 现在, 导入目录已更新。 只需确保您的代码完成情况看到该目录中的新模块即可轻松编写代码。

Note that if you want to use Terraform modules, it is also possible. Just define them in this file and run the ‘cdktf get’ command again.

请注意,如果要使用Terraform模块,也可以。 只需在此文件中定义它们,然后再次运行“ cdktf get”命令即可。

在CDK中导入Terraform资源 (Importing Terraform Resources in CDK)

The complete source code is at the end.

完整的源代码在最后。

Now we have the modules we need. Start with importing them:

现在,我们有了所需的模块。 从导入它们开始:

from imports.azurerm import \
AzurermProvider, \
KubernetesCluster, \
KubernetesClusterDefaultNodePool, \
KubernetesClusterIdentity, ResourceGroupConfig, AzurermProviderFeatures

If you need to know how to decide which Python modules should be used, they are named like Terraform resource names. Also writing a couple of characters from resource name will trigger autocompletion to suggest related modules.

如果您需要了解如何决定使用哪个Python模块,它们的名称就像Terraform资源名称一样 。 另外,从资源名称中写入几个字符将触发自动补全功能以建议相关模块。

We have imported required modules and are ready to define our resources.

我们已经导入了必需的模块,并准备定义我们的资源。

在Python中定义Terraform资源 (Defining Terraform Resources in Python)

There is a comment line sayin # define resources here” in our code. We will add the lines after it, in the same method (which is __init__).

在我们的代码中,有一条注释行“在此处定义资源”。 我们将使用相同的方法(即__init__ )在其后添加行。

Firstly, we should define our provider(Azurerm in our case):

首先,我们应该定义我们的提供者(在我们的例子中是Azurerm):

        features = AzurermProviderFeatures()
provider = AzurermProvider(self, 'azure', features=[features])

Features is empty in our case(required by the provider even if it is empty, see the docs). For details like KeyVault, Terraform docs will help to clarify and write.

在我们的情况下,功能为空(即使它为空,提供者也需要它,请参阅文档)。 有关KeyVault之类的详细信息,Terraform文档将有助于澄清和编写。

Afterwards, create a default node pool for our cluster:

然后,为我们的集群创建一个默认的节点池:

        node_pool = KubernetesClusterDefaultNodePool(
name='default', node_count=1, vm_size='Standard_D2_v2')

In this example, node count is set to 1 and VM size is Standart_D2_v2. You may need to change them according to your needs.

在此示例中,节点计数设置为1,VM大小为Standart_D2_v2。 您可能需要根据需要进行更改。

We will use a resource group which already exists in our Azure subscription. Either select an existing one or create a new one and provide its name:

我们将使用Azure订阅中已经存在的资源组。 选择一个现有的或创建一个新的并提供其名称:

        resource_group = ResourceGroupConfig(name='OUR_RESOURCE_GROUP', location='East US')

Replace OUR_RESOURCE_GROUP and location. This will help Terraform to create the AKS cluster in that resource group & region.

替换OUR_RESOURCE_GROUP和位置。 这将帮助Terraform在该资源组和区域中创建AKS集群。

Kubernetes cluster identity is needed to create the cluster, so let’s define it:

创建集群需要Kubernetes集群标识,因此让我们对其进行定义:

        identity = KubernetesClusterIdentity(type='SystemAssigned')

There is currently nothing to change(only supported type is ‘SystemAssigned’ right now), so we will define it and continue.

当前没有任何更改(现在仅支持的类型为'SystemAssigned'),因此我们将对其进行定义并继续。

Finally, define our cluster:

最后,定义我们的集群:

cluster = KubernetesCluster(
self, 'our-kube-cluster',
name='our-kube-cluster',
default_node_pool=[node_pool],
dns_prefix='test',
location=resource_group.location,
resource_group_name=resource_group.name,
identity=[identity],
tags={"foo": "bar"}
)

Now we can arrange the parameters like cluster name, DNS prefix, and tags to fit our requirements.

现在,我们可以安排参数,例如群集名称,DNS前缀和标签,以满足我们的要求。

At this point we are ready to synthesize JSON file for Terraform. Before continuing, here is the complete source (main.py):

至此,我们准备为Terraform合成JSON文件。 在继续之前,这里是完整的源代码(main.py):

#!/usr/bin/env python
from constructs import Construct
from cdktf import App, TerraformStack, TerraformOutput
from imports.azurerm import \
    AzurermProvider, \
    KubernetesCluster, \
    KubernetesClusterDefaultNodePool, \
    KubernetesClusterIdentity, ResourceGroupConfig, AzurermProviderFeatures




class MyStack(TerraformStack):
    def __init__(self, scope: Construct, ns: str):
        super().__init__(scope, ns)


        # define resources here
        features = AzurermProviderFeatures()
        provider = AzurermProvider(self, 'azure', features=[features])


        node_pool = KubernetesClusterDefaultNodePool(
            name='default', node_count=1, vm_size='Standard_D2_v2')


        resource_group = ResourceGroupConfig(name='OUR_RESOURCE_GROUP', location='East US')


        identity = KubernetesClusterIdentity(type='SystemAssigned')


        cluster = KubernetesCluster(
            self, 'our-kube-cluster',
            name='our-kube-cluster',
            default_node_pool=[node_pool],
            dns_prefix='test',
            location=resource_group.location,
            resource_group_name=resource_group.name,
            identity=[identity],
            tags={"foo": "bar"}
        )




app = App()
MyStack(app, "test")


app.synth()

部署集群 (Deploy the Cluster)

Firstly we will generate the JSON file:

首先,我们将生成JSON文件:

$ cdktf synth
⠼ synthesizing ...

After command is completed, there will be a file named “cdk.tf.json” in cdktf.out.

命令完成后,cdktf.out中将有一个名为“ cdk.tf.json”的文件。

We can give this file to Terraform and use Terraform subcommands like plan/apply/destroy/… using:

我们可以将此文件提供给Terraform并使用Terraform子命令,例如plan / apply / destroy /…,使用:

$ cd cdktf.out
$ terraform init
...
$ terraform apply
...

OR, we can use cdktf’s subcommands to apply the same procedure:

或者,我们可以使用cdktf的子命令来应用相同的过程:

$ cdktf deploy

This command will automatically run cdktf synth and terraform apply.

该命令将自动运行cdktf synthterraform apply

Note that you need to have az installed and logged in, in order to apply this JSON file otherwise it will throw an error.

请注意,您需要先安装az并登录,然后才能应用此JSON文件,否则它将引发错误。

Image for post

翻译自: https://medium.com/@gurayy/creating-kubernetes-cluster-on-azure-with-terraform-and-python-using-cdk-for-terraform-8237ffa15092

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_26755331/article/details/108515883

智能推荐

while循环&CPU占用率高问题深入分析与解决方案_main函数使用while(1)循环cpu占用99-程序员宅基地

文章浏览阅读3.8k次,点赞9次,收藏28次。直接上一个工作中碰到的问题,另外一个系统开启多线程调用我这边的接口,然后我这边会开启多线程批量查询第三方接口并且返回给调用方。使用的是两三年前别人遗留下来的方法,放到线上后发现确实是可以正常取到结果,但是一旦调用,CPU占用就直接100%(部署环境是win server服务器)。因此查看了下相关的老代码并使用JProfiler查看发现是在某个while循环的时候有问题。具体项目代码就不贴了,类似于下面这段代码。​​​​​​while(flag) {//your code;}这里的flag._main函数使用while(1)循环cpu占用99

【无标题】jetbrains idea shift f6不生效_idea shift +f6快捷键不生效-程序员宅基地

文章浏览阅读347次。idea shift f6 快捷键无效_idea shift +f6快捷键不生效

node.js学习笔记之Node中的核心模块_node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是-程序员宅基地

文章浏览阅读135次。Ecmacript 中没有DOM 和 BOM核心模块Node为JavaScript提供了很多服务器级别,这些API绝大多数都被包装到了一个具名和核心模块中了,例如文件操作的 fs 核心模块 ,http服务构建的http 模块 path 路径操作模块 os 操作系统信息模块// 用来获取机器信息的var os = require('os')// 用来操作路径的var path = require('path')// 获取当前机器的 CPU 信息console.log(os.cpus._node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是

数学建模【SPSS 下载-安装、方差分析与回归分析的SPSS实现(软件概述、方差分析、回归分析)】_化工数学模型数据回归软件-程序员宅基地

文章浏览阅读10w+次,点赞435次,收藏3.4k次。SPSS 22 下载安装过程7.6 方差分析与回归分析的SPSS实现7.6.1 SPSS软件概述1 SPSS版本与安装2 SPSS界面3 SPSS特点4 SPSS数据7.6.2 SPSS与方差分析1 单因素方差分析2 双因素方差分析7.6.3 SPSS与回归分析SPSS回归分析过程牙膏价格问题的回归分析_化工数学模型数据回归软件

利用hutool实现邮件发送功能_hutool发送邮件-程序员宅基地

文章浏览阅读7.5k次。如何利用hutool工具包实现邮件发送功能呢?1、首先引入hutool依赖<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.19</version></dependency>2、编写邮件发送工具类package com.pc.c..._hutool发送邮件

docker安装elasticsearch,elasticsearch-head,kibana,ik分词器_docker安装kibana连接elasticsearch并且elasticsearch有密码-程序员宅基地

文章浏览阅读867次,点赞2次,收藏2次。docker安装elasticsearch,elasticsearch-head,kibana,ik分词器安装方式基本有两种,一种是pull的方式,一种是Dockerfile的方式,由于pull的方式pull下来后还需配置许多东西且不便于复用,个人比较喜欢使用Dockerfile的方式所有docker支持的镜像基本都在https://hub.docker.com/docker的官网上能找到合..._docker安装kibana连接elasticsearch并且elasticsearch有密码

随便推点

Python 攻克移动开发失败!_beeware-程序员宅基地

文章浏览阅读1.3w次,点赞57次,收藏92次。整理 | 郑丽媛出品 | CSDN(ID:CSDNnews)近年来,随着机器学习的兴起,有一门编程语言逐渐变得火热——Python。得益于其针对机器学习提供了大量开源框架和第三方模块,内置..._beeware

Swift4.0_Timer 的基本使用_swift timer 暂停-程序员宅基地

文章浏览阅读7.9k次。//// ViewController.swift// Day_10_Timer//// Created by dongqiangfei on 2018/10/15.// Copyright 2018年 飞飞. All rights reserved.//import UIKitclass ViewController: UIViewController { ..._swift timer 暂停

元素三大等待-程序员宅基地

文章浏览阅读986次,点赞2次,收藏2次。1.硬性等待让当前线程暂停执行,应用场景:代码执行速度太快了,但是UI元素没有立马加载出来,造成两者不同步,这时候就可以让代码等待一下,再去执行找元素的动作线程休眠,强制等待 Thread.sleep(long mills)package com.example.demo;import org.junit.jupiter.api.Test;import org.openqa.selenium.By;import org.openqa.selenium.firefox.Firefox.._元素三大等待

Java软件工程师职位分析_java岗位分析-程序员宅基地

文章浏览阅读3k次,点赞4次,收藏14次。Java软件工程师职位分析_java岗位分析

Java:Unreachable code的解决方法_java unreachable code-程序员宅基地

文章浏览阅读2k次。Java:Unreachable code的解决方法_java unreachable code

标签data-*自定义属性值和根据data属性值查找对应标签_如何根据data-*属性获取对应的标签对象-程序员宅基地

文章浏览阅读1w次。1、html中设置标签data-*的值 标题 11111 222222、点击获取当前标签的data-url的值$('dd').on('click', function() { var urlVal = $(this).data('ur_如何根据data-*属性获取对应的标签对象

推荐文章

热门文章

相关标签