首页 > 其他分享 >Terraform 部署 Docker

Terraform 部署 Docker

时间:2023-08-01 17:56:39浏览次数:61  
标签:known network terraform 部署 Terraform tf apply Docker docker

Terraform 部署 Docker

文件目录结构

./
├── network                             # network 目录,创建 network
│   ├── main.tf
│   ├── outputs.tf
│   ├── terraform.tfstate
│   ├── terraform.tfstate.backup
│   └── versions.tf
└── service                             # service 目录,创建 service
    ├── nginx.tf
    ├── main.tf
    ├── terraform.tfstate
    ├── terraform.tfstate.backup
    └── versions.tf

3 directories, 10 files

创建 network

查看当前 docker network

evescn@evescndeMacBook-Pro network % docker network ls        
NETWORK ID     NAME      DRIVER    SCOPE
a97ca4e7cded   bridge    bridge    local
43c409a0c3e3   host      host      local
02706eb1b5ba   none      null      local

编写 Terraform 代码

  • 查看 docker provider

https://registry.terraform.io/browse/providers

  • 文件结构
./
└── network                             # network 目录,创建 network
    ├── main.tf                         # 定义 network 的创建信息
    ├── outputs.tf                      # 定义模块输出信息,后续其他模块/服务使用
    └── versions.tf                     # 定义 provider 版本 

文档地址:https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/network#ipam_config

  • 定义 provider 版本
## versions.tf

# 定义使用的 docker provider 版本
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "3.0.2"
    }
  }
}
  • 定义 network
## main.tf

# 定义 docker 服务信息,当前使用本机测试,如果远程连接,需要开启 docker 的远程连接服务
provider "docker" {
  host = "unix:///var/run/docker.sock"
}

# 定义环境变量,后续引用
locals {
  network_settings = [
    {
      name   = "devops1"
      driver = "bridge"
      subnet = "10.10.10.0/24"
    }
  ]
}

# docker network 定义,详细参数,查看官方文档
resource "docker_network" "network" {
  count  = length(local.network_settings)
  name   = local.network_settings[count.index]["name"]
  driver = local.network_settings[count.index]["driver"]
  ipam_config {
    subnet = local.network_settings[count.index]["subnet"]
  }
}
  • 定义 output 后续 docker 容器创建需要使用此次创建的 network
## output.tf

# 定义输出信息
output "network" {
  value = docker_network.network
  # value = [for net in docker_network.network : tomap({ "name" : net.name, "subnet" : tolist(net.ipam_config)[0].subnet })]
}
  • plan 查看服务信息
evescn@evescndeMacBook-Pro network % terraform fmt    
outputs.tf

evescn@evescndeMacBook-Pro network % terraform validate
Success! The configuration is valid.

evescn@evescndeMacBook-Pro network % terraform plan    

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_network.network[0] will be created
  + resource "docker_network" "network" {
      + driver      = "bridge"
      + id          = (known after apply)
      + internal    = (known after apply)
      + ipam_driver = "default"
      + name        = "devops1"
      + options     = (known after apply)
      + scope       = (known after apply)

      + ipam_config {
          + subnet = "10.10.10.0/24"
        }
    }

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

Changes to Outputs:
  + network = [
      + {
          + attachable      = null
          + check_duplicate = null
          + driver          = "bridge"
          + id              = (known after apply)
          + ingress         = null
          + internal        = (known after apply)
          + ipam_config     = [
              + {
                  + aux_address = null
                  + gateway     = ""
                  + ip_range    = ""
                  + subnet      = "10.10.10.0/24"
                },
            ]
          + ipam_driver     = "default"
          + ipam_options    = null
          + ipv6            = null
          + labels          = []
          + name            = "devops1"
          + options         = (known after apply)
          + scope           = (known after apply)
        },
    ]

标签:known,network,terraform,部署,Terraform,tf,apply,Docker,docker
From: https://www.cnblogs.com/evescn/p/17598318.html

相关文章

  • docker 搭建 svn
    1、获取镜像dockerpullgarethflowers/svn-server:1.7.02、启动容器dockerrun-id--restartalways--namesvn-v/data/svn:/var/opt/svn-p3690:3690garethflowers/svn-server:1.7.03、进入容器dockerexec-itsvnsh/var/opt/svn#ls/var/opt/svn......
  • Terraform 语法配置
    Terraform语法配置Terraform语法配置语法Terraform的配置文件都是以.tf为后缀Terraform支持两种模式HCL、JSONProvider插件providers地址:https://registry.terraform.io/browse/providersTerraform通过provider管理基础设施,使用provider与云供应商API......
  • Linux安装部署seata集群模式
    第一步:下载seata安装包下载地址:https://seata.io/zh-cn/blog/download.html解压如下进入conf文件夹编辑registry.conf文件:点击查看代码 registry{#file、nacos、eureka、redis、zk、consul、etcd3、sofatype="nacos"nacos{application="seata-ser......
  • centos8修改yum源、安装docker
    一、示例centos8修改阿里yum源1、将源文件备份cd/etc/yum.repos.d/&&mkdirbackup&&mv*repobackup/2、下载阿里源文件curl-o/etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-8.repo3、更新源里面的地址sed-i-e"s|mirrors.cloud.ali......
  • centos7安装docker-compose
    首先确保系统已经安装上了docker1、下载tar包并上传至服务器解压下载地址:https://package-all-1257309290.cos.ap-beijing.myqcloud.com/docker_compose_install.tar.gztarzxfdocker_compose_install.tar.gz2、安装docker-compose解压后得到一个docker_compose_installcd......
  • odoo自制docker 部署
    1:开一个服务器,开通22, 80,  8069, 8072 端口 2:把文件复制到服务器上, 不一定能够非的/tmp目录,可以是任何一个目录,教程是以/tmp目录为例,没scp,用其他sftp工具也可以,上传到啊服务器上即可scp  docker_odoo16e.tar.gz  [email protected]:/tmp/ ......
  • 项目部署
    项目部署篇本博客记录项目部署的一些案例1、SpringBoot项目如何部署SSL证书(JKS格式)1.获取SSL证书和私钥首先,你需要获取有效的SSL证书和私钥。SSL证书是一种用于加密通信的数字证书,它可以通过购买商业SSL证书或使用免费的Let'sEncrypt证书获得。请确保你拥有证书......
  • Docker-volume
    数据卷操作的基本语法如下:dockervolume[COMMAND]dockervolume命令是数据卷操作,根据命令后跟随的command来确定下一步的操作:create创建一个volumeinspect显示一个或多个volume的信息ls列出所有的volumeprune删除未使用的volumerm删除一个或多个指定的volume......
  • addlespeech使用,docker 安装版
    paddlespeech使用,docker安装版默认已经安装docker#拉取paddlespeech镜像dockerpullregistry.baidubce.com/paddlepaddle/paddle:2.3.2#使用镜像在当前目录构建并进入容器dockerrun--namepaddlespeech_docker-it-v${PWD}/paddleregistry.baidubce.com/paddlepad......
  • [Docker] Docker Node.js project tips
    BasicnodeimageFROMnode:12-stretchCOPYindex.jsindex.jsCMD["node","index.js"]Builddockerimage:dockerbuild-tmy-node-app.--initdockerrunmy-node-app,afterdockerisrunning,youpressCTRL+C,itdoesn'texit......