使用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

智能推荐

使用nginx解决浏览器跨域问题_nginx不停的xhr-程序员宅基地

文章浏览阅读1k次。通过使用ajax方法跨域请求是浏览器所不允许的,浏览器出于安全考虑是禁止的。警告信息如下:不过jQuery对跨域问题也有解决方案,使用jsonp的方式解决,方法如下:$.ajax({ async:false, url: 'http://www.mysite.com/demo.do', // 跨域URL ty..._nginx不停的xhr

在 Oracle 中配置 extproc 以访问 ST_Geometry-程序员宅基地

文章浏览阅读2k次。关于在 Oracle 中配置 extproc 以访问 ST_Geometry,也就是我们所说的 使用空间SQL 的方法,官方文档链接如下。http://desktop.arcgis.com/zh-cn/arcmap/latest/manage-data/gdbs-in-oracle/configure-oracle-extproc.htm其实简单总结一下,主要就分为以下几个步骤。..._extproc

Linux C++ gbk转为utf-8_linux c++ gbk->utf8-程序员宅基地

文章浏览阅读1.5w次。linux下没有上面的两个函数,需要使用函数 mbstowcs和wcstombsmbstowcs将多字节编码转换为宽字节编码wcstombs将宽字节编码转换为多字节编码这两个函数,转换过程中受到系统编码类型的影响,需要通过设置来设定转换前和转换后的编码类型。通过函数setlocale进行系统编码的设置。linux下输入命名locale -a查看系统支持的编码_linux c++ gbk->utf8

IMP-00009: 导出文件异常结束-程序员宅基地

文章浏览阅读750次。今天准备从生产库向测试库进行数据导入,结果在imp导入的时候遇到“ IMP-00009:导出文件异常结束” 错误,google一下,发现可能有如下原因导致imp的数据太大,没有写buffer和commit两个数据库字符集不同从低版本exp的dmp文件,向高版本imp导出的dmp文件出错传输dmp文件时,文件损坏解决办法:imp时指定..._imp-00009导出文件异常结束

python程序员需要深入掌握的技能_Python用数据说明程序员需要掌握的技能-程序员宅基地

文章浏览阅读143次。当下是一个大数据的时代,各个行业都离不开数据的支持。因此,网络爬虫就应运而生。网络爬虫当下最为火热的是Python,Python开发爬虫相对简单,而且功能库相当完善,力压众多开发语言。本次教程我们爬取前程无忧的招聘信息来分析Python程序员需要掌握那些编程技术。首先在谷歌浏览器打开前程无忧的首页,按F12打开浏览器的开发者工具。浏览器开发者工具是用于捕捉网站的请求信息,通过分析请求信息可以了解请..._初级python程序员能力要求

Spring @Service生成bean名称的规则(当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致)_@service beanname-程序员宅基地

文章浏览阅读7.6k次,点赞2次,收藏6次。@Service标注的bean,类名:ABDemoService查看源码后发现,原来是经过一个特殊处理:当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致public class AnnotationBeanNameGenerator implements BeanNameGenerator { private static final String C..._@service beanname

随便推点

二叉树的各种创建方法_二叉树的建立-程序员宅基地

文章浏览阅读6.9w次,点赞73次,收藏463次。1.前序创建#include<stdio.h>#include<string.h>#include<stdlib.h>#include<malloc.h>#include<iostream>#include<stack>#include<queue>using namespace std;typed_二叉树的建立

解决asp.net导出excel时中文文件名乱码_asp.net utf8 导出中文字符乱码-程序员宅基地

文章浏览阅读7.1k次。在Asp.net上使用Excel导出功能,如果文件名出现中文,便会以乱码视之。 解决方法: fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);_asp.net utf8 导出中文字符乱码

笔记-编译原理-实验一-词法分析器设计_对pl/0作以下修改扩充。增加单词-程序员宅基地

文章浏览阅读2.1k次,点赞4次,收藏23次。第一次实验 词法分析实验报告设计思想词法分析的主要任务是根据文法的词汇表以及对应约定的编码进行一定的识别,找出文件中所有的合法的单词,并给出一定的信息作为最后的结果,用于后续语法分析程序的使用;本实验针对 PL/0 语言 的文法、词汇表编写一个词法分析程序,对于每个单词根据词汇表输出: (单词种类, 单词的值) 二元对。词汇表:种别编码单词符号助记符0beginb..._对pl/0作以下修改扩充。增加单词

android adb shell 权限,android adb shell权限被拒绝-程序员宅基地

文章浏览阅读773次。我在使用adb.exe时遇到了麻烦.我想使用与bash相同的adb.exe shell提示符,所以我决定更改默认的bash二进制文件(当然二进制文件是交叉编译的,一切都很完美)更改bash二进制文件遵循以下顺序> adb remount> adb push bash / system / bin /> adb shell> cd / system / bin> chm..._adb shell mv 权限

投影仪-相机标定_相机-投影仪标定-程序员宅基地

文章浏览阅读6.8k次,点赞12次,收藏125次。1. 单目相机标定引言相机标定已经研究多年,标定的算法可以分为基于摄影测量的标定和自标定。其中,应用最为广泛的还是张正友标定法。这是一种简单灵活、高鲁棒性、低成本的相机标定算法。仅需要一台相机和一块平面标定板构建相机标定系统,在标定过程中,相机拍摄多个角度下(至少两个角度,推荐10~20个角度)的标定板图像(相机和标定板都可以移动),即可对相机的内外参数进行标定。下面介绍张氏标定法(以下也这么称呼)的原理。原理相机模型和单应矩阵相机标定,就是对相机的内外参数进行计算的过程,从而得到物体到图像的投影_相机-投影仪标定

Wayland架构、渲染、硬件支持-程序员宅基地

文章浏览阅读2.2k次。文章目录Wayland 架构Wayland 渲染Wayland的 硬件支持简 述: 翻译一篇关于和 wayland 有关的技术文章, 其英文标题为Wayland Architecture .Wayland 架构若是想要更好的理解 Wayland 架构及其与 X (X11 or X Window System) 结构;一种很好的方法是将事件从输入设备就开始跟踪, 查看期间所有的屏幕上出现的变化。这就是我们现在对 X 的理解。 内核是从一个输入设备中获取一个事件,并通过 evdev 输入_wayland

推荐文章

热门文章

相关标签