Terraform(二)

news2024/9/29 11:37:44

Terraform实践

    • 1. Terraform Docker Example
      • 1.1 Install Terraform
      • 1.2 Verify the installation
      • 1.3 Enable tab completion
      • 1.4 Quick start tutorial

在这里插入图片描述
To deploy infrastructure with Terraform:

  • Scope - Identify the infrastructure for your project.
  • Author - Write the configuration for your infrastructure.
  • Initialize - Install the plugins Terraform needs to manage the infrastructure.
  • Plan - Preview the changes Terraform will make to match your configuration.
  • Apply - Make the planned changes.

1. Terraform Docker Example

https://developer.hashicorp.com/terraform/tutorials/docker-get-started

1.1 Install Terraform

yum install -y yum-utils
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
yum -y install terraform

Tip
Now that you have added the HashiCorp repository, you can install Vault, Consul, Nomad and Packer with the same command.

1.2 Verify the installation

[root@node-252 ~]# terraform -help
Usage: terraform [global options] <subcommand> [args]

The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.

Main commands:
  init          Prepare your working directory for other commands
  validate      Check whether the configuration is valid
  plan          Show changes required by the current configuration
  apply         Create or update infrastructure
  destroy       Destroy previously-created infrastructure
...
[root@node-252 ~]# terraform -help plan
Usage: terraform [global options] plan [options]

  Generates a speculative execution plan, showing what actions Terraform
  would take to apply the current configuration. This command will not
  actually perform the planned actions.

  You can optionally save the plan to a file, which you can then pass to
  the "apply" command to perform exactly the actions described in the plan.
...

Troubleshoot
If you get an error that terraform could not be found, your PATH environment variable was not set up properly. Please go back and ensure that your PATH variable contains the directory where Terraform was installed.

1.3 Enable tab completion

If you use either Bash or Zsh, you can enable tab completion for Terraform commands. To enable autocomplete, first ensure that a config file exists for your chosen shell.

touch ~/.bashrc

Then install the autocomplete package.

[root@node-252 ~]# terraform -install-autocomplete

1.4 Quick start tutorial

Now that you’ve installed Terraform, you can provision an NGINX server in less than a minute using Docker on Mac, Windows, or Linux. You can also follow the rest of this tutorial in your web browser.

  1. Create a directory named learn-terraform-docker-container.
mkdir learn-terraform-docker-container
  1. Navigate into the working directory.
cd learn-terraform-docker-container
  1. In the working directory, create a file called main.tf and paste the following Terraform configuration into it.
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 3.0.1"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.image_id
  name  = "tutorial"

  ports {
    internal = 80
    external = 8000
  }
}
  1. Initialize the project, which downloads a plugin called a provider that lets Terraform interact with Docker.
terraform init
  1. Provision the NGINX server container with apply. When Terraform asks you to confirm type yes and press ENTER.
terraform apply
[root@node-135 learn-terraform-docker-container]# terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
  + create

Terraform will perform the following actions:

  # docker_container.nginx will be created
  + resource "docker_container" "nginx" {
      + attach                                      = false
      + bridge                                      = (known after apply)
      + command                                     = (known after apply)
      + container_logs                              = (known after apply)
      + container_read_refresh_timeout_milliseconds = 15000
      + entrypoint                                  = (known after apply)
      + env                                         = (known after apply)
      + exit_code                                   = (known after apply)
      + hostname                                    = (known after apply)
      + id                                          = (known after apply)
      + image                                       = (known after apply)
      + init                                        = (known after apply)
      + ipc_mode                                    = (known after apply)
      + log_driver                                  = (known after apply)
      + logs                                        = false
      + must_run                                    = true
      + name                                        = "tutorial"
      + network_data                                = (known after apply)
      + read_only                                   = false
      + remove_volumes                              = true
      + restart                                     = "no"
      + rm                                          = false
      + runtime                                     = (known after apply)
      + security_opts                               = (known after apply)
      + shm_size                                    = (known after apply)
      + start                                       = true
      + stdin_open                                  = false
      + stop_signal                                 = (known after apply)
      + stop_timeout                                = (known after apply)
      + tty                                         = false
      + wait                                        = false
      + wait_timeout                                = 60

      + ports {
          + external = 8000
          + internal = 80
          + ip       = "0.0.0.0"
          + protocol = "tcp"
        }
    }

  # docker_image.nginx will be created
  + resource "docker_image" "nginx" {
      + id           = (known after apply)
      + image_id     = (known after apply)
      + keep_locally = false
      + name         = "nginx"
      + repo_digest  = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

docker_image.nginx: Creating...
docker_image.nginx: Still creating... [10s elapsed]
docker_image.nginx: Still creating... [20s elapsed]
docker_image.nginx: Still creating... [30s elapsed]
docker_image.nginx: Still creating... [42s elapsed]
docker_image.nginx: Still creating... [52s elapsed]
docker_image.nginx: Creation complete after 54s [id=sha256:eea7b3dcba7ee47c0d16a60cc85d2b977d166be3960541991f3e6294d795ed24nginx]
docker_container.nginx: Creating...
docker_container.nginx: Still creating... [10s elapsed]
docker_container.nginx: Still creating... [21s elapsed]
docker_container.nginx: Creation complete after 22s [id=109baf53333c9a9b71695a1d639eedd29e6cb8afd0ef259b88cd5c998efe6983]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  1. Verify the existence of the NGINX container by visiting localhost:8000 in your web browser or running docker ps to see the container.
docker ps
[root@node-135 learn-terraform-docker-container]# docker ps
CONTAINER ID   IMAGE              COMMAND                  CREATED          STATUS          PORTS                    NAMES
109baf53333c   eea7b3dcba7e       "/docker-entrypoint.…"   59 seconds ago   Up 39 seconds   0.0.0.0:8000->80/tcp     tutorial
  1. To stop the container, run terraform destroy.
terraform destroy

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/920610.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【校招VIP】前端校招考点之行内/块级元素

考点介绍&#xff1a; 行内元素会在一条直线上排列&#xff08;默认宽度只与内容有关&#xff09;&#xff0c;都是同一行的&#xff0c;水平方向排列。块级元素各占据一行&#xff08;默认宽度是它本身父容器的100%&#xff08;和父元素的宽度一致&#xff09;&#xff0c;与内…

抖音书单背景图制作怎么做?分享两个制作小妙招

相信屏幕前的小伙伴今天有刷抖音吧&#xff01;由于迎合受众对碎片化传播的需求&#xff0c;抖音也成为了当下最流行的短视频应用之一&#xff0c;在抖音上分享书单已经成为了一种流行的趋势&#xff0c;而制作一张吸引人的书单背景图是很重要的。在本文中&#xff0c;我们将分…

Python Web开发技巧X

目录 select_related 和 prefetch_related 生成器对象的三种创建方式 classmethod和staticmethod __class__属性 python创建一个类会依次去调用哪些方法 __new__和__init__实现单例模式的饿汉式和懒汉式 select_related 和 prefetch_related select_related 和 prefetch_…

HTML中SCRIPT 标签中的那些属性

在HTML中&#xff0c; <script> 标签用于嵌入或引用JavaScript代码。 在 <script> 标签中&#xff0c;有两个属性可以用来控制脚本的加载和执行方式&#xff1a; async 和 defer 。 当然这也是常见的一道面试题&#xff0c; async 和 defer 的作用和区别。 asy…

【IO流中的字节流(InputStream)(OutputStream)】

字符集 美国人发明计算机 要将他们的字符存入计算机&#xff08;英文字母、数字、标点、特殊字符&#xff09; 给字符进行编号&#xff0c;组成了一张ASCII码表&#xff08;美国信息交换标准代码&#xff09;&#xff0c;一共包含128个字符 该码表以1个字节存储1个字符&#xf…

Java 实现 国密SM4/ECB/PKCS7Padding对称加密解密

Java 实现 国密SM4/ECB/PKCS7Padding对称加密解密&#xff0c;为了演示方便本问使用的是IntelliJ IDEA 2022.1 (Community Edition)来构建代码的 1、pom.xml文件添加需要的jar <?xml version"1.0" encoding"UTF-8"?> <project xmlns"htt…

1002 Business

要解这道题目&#xff0c;感觉最关键的因素就是找到第 件任务最晚开始的时间 。这个看似简单&#xff0c;但是对建模的功底要求相当高。我一开始就把 当成了 &#xff0c;但其实不是的。后者就相当于把第 件任务锁死在最后面完成&#xff0c;但其实是允许放在之前完成的&am…

springboot2+redis 订阅发布,解决接收消息累计线程到内存溢出,使用自定义线程池接收消息

pom 添加redis <!-- redis 缓存操作 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency> 发布消息 import lombok.extern.slf4j.Slf4j; import o…

FPGA原理与结构——FIFO IP核原理学习

一、FIFO概述 1、FIFO的定义 FIFO是英文First-In-First-Out的缩写&#xff0c;是一种先入先出的数据缓冲器&#xff0c;与一般的存储器的区别在于没有地址线&#xff0c; 使用起来简单&#xff0c;缺点是只能顺序读写数据&#xff0c;其数据地址由内部读写指针自动加1完成&…

DC电源模块关于输入电压范围

BOSHIDA DC电源模块关于输入电压范围 DC电源模块是一种常用的电源供应设备&#xff0c;其主要作用是将交流电转化为直流电&#xff0c;以供电子设备使用。在DC电源模块的使用过程中&#xff0c;输入电压范围是一个非常重要的参数&#xff0c;它关系到模块的稳定性、可靠性以及…

driver‘s license exam 1

驾考科目1&#xff0c;都是做题目 driver‘s license exam 1_spencer_tseng的博客-CSDN博客 driver‘s license exam 2_spencer_tseng的博客-CSDN博客 driver‘s license exam 3_spencer_tseng的博客-CSDN博客 driver‘s license exam 4_spencer_tseng的博客-CSDN博客 car …

对1GHz脉冲多普勒雷达进行快速和慢速处理生成5个移动目标的距离多普勒图研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

微信公众号关注/取消订阅事件一文详解

一、背景介绍 这周期的项目需求中需要做一个引导用户关注微信公众号的功能&#xff0c;但是引导用户关注的前提是需要实时获取当前用户是否已经关注微信公众号,如果光看官方文档还是对于一些小伙伴来说比较无从下手&#xff0c;所以我来分享以下我做的过程中遇到的问题以及解决…

Ansible 创建分区

实例&#xff1a; 创建一个名为/home/greg/ansible/partition.yml 的剧本&#xff0c;它在所有托管节点上运行&#xff0c;执行以下操作&#xff1a; 在设备 VDB 上创建大小为 1500 MB 的单个主分区 1 使用 ext4 文件系统格式化分区 在/n…

无涯教程-PHP - 性能优化

根据Zend小组的说明,以下插图显示了PHP 7与PHP 5.6和基于流行的基于PHP的应用程序上的HHVM 3.7。 Magento 1.9 与执行Magento事务的PHP 5.6相比&#xff0c;PHP 7的运行速度证明是其两倍。 Drupal 7 在执行Drupal事务时&#xff0c;与PHP 5.6相比&#xff0c;PHP 7的运行速度…

【Gitee提交pr】

Gitee提交pr 什么是pr怎样提交一个pr嘞&#xff1f; 什么是pr pr:指的是将自己的修改从自己的账号仓库dev下提交到官方账号仓库master下&#xff1b; 通俗来讲就是Gitee线上有属于自己的分支&#xff0c;然后本地在自己地分支修改完代码之后&#xff0c;提交到自己的线上分支&a…

十一、hadoop应用

1.上传数据集 27.19.74.143,2015/3/30 17:38,/static/image/common/faq.gif 110.52.250.126,2015/3/30 17:38,/data/cache/style_1_widthauto.css?y7a 27.19.74.143,2015/3/30 17:38,/static/image/common/hot_1.gif 27.19.74.143,2015/3/30 17:38,/static/image/common/hot_2…

【ESP32】启动电流不足——调试问题记录

【ESP32】启动电流不足——调试问题记录 本文主要记录基于esp32 自开发设备硬件调试过程&#xff0c;解决供电不足的问题&#xff0c;用于新手小白记录 &#x1f4cb; 个人简介 &#x1f496; 作者简介&#xff1a;大家好&#xff0c;我是喜欢记录零碎知识点的小菜鸟。&#x…

善于打仗的人,创造高势能,行动节奏恰当

善于打仗的人&#xff0c;创造高势能&#xff0c;行动节奏恰当 【安志强趣讲《孙子兵法》第18讲】 【原文】 激水之疾&#xff0c;至于漂石者&#xff0c;势也&#xff1b;鸷鸟之疾&#xff0c;至于毁折者&#xff0c;节也。 【注释】 激&#xff0c;阻截水流 节&#xff0c;时…

SensorService中Binder案例

SensorService中Binder案例 1、FWK实际操作在Native层2、Native层中代码实现Bn/Bp端2.1 代码实现Bn端2.2 代码实现Bp端2.2.1 模板interface_cast android12-release 1、FWK实际操作在Native层 SensorService.java实际操作Native层SensorService.cpp&#xff1b;对应Bn服务端。 …