OpenStack Terraform 실습 / Kubernetes 환경 구축

OpenStack Terraform 실습 / Kubernetes 환경 구축

1. 실습, 구축 환경

[Figure 1] OpenStack Terraform 실습, 구축 환경

[Figure 1] OpenStack Terraform 실습, 구축 환경

[Figure 1]은 Terraform을 이용하여 OpenStack 위에 구축하려는 Kubernetes 환경을 나타내고 있다. External Network, externel-router, Ubuntu 18.04 Image는 미리 생성되어 있는 환경에서 진행하였다.

  • Terraform : 0.12.5
  • Node : Ubuntu 18.04
  • OpenStack : Stein
    • User, Tenant, Password : admin
    • Auth URL :
  • Network :
    • Internal Network : Kubernetes Network, 30.0.0.0/24
  • Flavor :
    • Standard : 4vCPU, 4GB RAM, 30GB Disk

2. Terraform 설치

(Deploy)$ apt-get update
(Deploy)$ apt-get install wget unzip
(Deploy)$ wget https://releases.hashicorp.com/terraform/0.12.5/terraform-0.12.5-linux-amd64.zip
(Deploy)$ unzip ./terraform-0.12.5-linux-amd64.zip -d /usr/local/bin/

Terraform을 설치한다.

3. Terraform 설정

1
2
3
4
5
6
provider "openstack" {
  user-name = "admin"
  tenant-name = "admin"
  password  = "admin"
  auth-url  = "http://192.168.0.40:5000/v3"
}
[File 1] ~/terraform/provider.tf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
variable "router-external" {
  default = "[external-router ID]"
}

variable "secgroup-default" {
  default = "[default Security Group ID]"
}

variable "image-ubuntu" {
  default = "[ubuntu-18.04 Image ID]"
}
[File 2] ~/terraform/00-params.tf
1
2
3
4
5
6
resource "openstack-compute-flavor-v2" "flavor" {
  name  = "m1.standard"
  ram   = "4096"
  vcpus = "4"
  disk  = "30"
}
[File 3] ~/terraform/010-flavor.tf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
resource "openstack-networking-network-v2" "network" {
  name = "internal-net"
}

resource "openstack-networking-subnet-v2" "subnet" {
  name = "internal-sub"
  network-id = "${openstack-networking-network-v2.network.id}"
  cidr = "30.0.0.0/24"
  dns-nameservers = ["8.8.8.8"]
}

resource "openstack-networking-router-interface-v2" "interface" {
  router-id = "${var.router-external}"
  subnet-id = "${openstack-networking-subnet-v2.subnet.id}"
}
[File 4] ~/terraform/020-network.tf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
resource "openstack-networking-secgroup-rule-v2" "secgroup-tcp" {
  direction = "ingress"
  ethertype = "IPv4"
  protocol = "tcp"
  port-range-min = 1
  port-range-max = 65535
  remote-ip-prefix = "0.0.0.0/0"
  security-group-id = "${var.secgroup-default}"
}

resource "openstack-networking-secgroup-rule-v2" "secgroup-udp" {
  direction = "ingress"
  ethertype = "IPv4"
  protocol = "upd"
  port-range-min = 1
  port-range-max = 65535
  remote-ip-prefix = "0.0.0.0/0"
  security-group-id = "${var.secgroup-default}"
}
[File 5] ~/terraform/030-secgroup.tf
1
2
3
resource "openstack-networking-floatingip-v2" "fip" {
  pool = "external-net"
}
[File 6] ~/terraform/040-floating.tf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
resource "openstack-compute-instance-v2" "vm01" {
  depends-on = ["openstack-networking-subnet-v2.subnet"]
  name = "vm01"
  flavor-id = "${openstack-compute-flavor-v2.flavor.id}"

  network {
    name = "${openstack-networking-network-v2.network.name}"
  }

  block-device {
    uuid                  = "${var.image-ubuntu}"
    source-type           = "image"
    volume-size           = 30
    boot-index            = 0
    destination-type      = "volume"
    delete-on-termination = true
  }
}

resource "openstack-compute-instance-v2" "vm02" {
  depends-on = ["openstack-networking-subnet-v2.subnet"]
  name = "vm02"
  flavor-id = "${openstack-compute-flavor-v2.flavor.id}"

  network {
    name = "${openstack-networking-network-v2.network.name}"
  }

  block-device {
    uuid                  = "${var.image-ubuntu}"
    source-type           = "image"
    volume-size           = 30
    boot-index            = 0
    destination-type      = "volume"
    delete-on-termination = true
  }
}

resource "openstack-compute-instance-v2" "vm03" {
  depends-on = ["openstack-networking-subnet-v2.subnet"]
  name = "vm03"
  flavor-id = "${openstack-compute-flavor-v2.flavor.id}"

  network {
    name = "${openstack-networking-network-v2.network.name}"
  }

  block-device {
    uuid                  = "${var.image-ubuntu}"
    source-type           = "image"
    volume-size           = 30
    boot-index            = 0
    destination-type      = "volume"
    delete-on-termination = true
  }
}

resource "openstack-compute-instance-v2" "vm09" {
  depends-on = ["openstack-networking-subnet-v2.subnet"]
  name = "vm09"
  flavor-id = "${openstack-compute-flavor-v2.flavor.id}"

  network {
    name = "${openstack-networking-network-v2.network.name}"
  }

  block-device {
    uuid                  = "${var.image-ubuntu}"
    source-type           = "image"
    volume-size           = 30
    boot-index            = 0
    destination-type      = "volume"
    delete-on-termination = true
  }
}

resource "openstack-compute-floatingip-associate-v2" "fip" {
  floating-ip = "${openstack-networking-floatingip-v2.fip.address}"
  instance-id = "${openstack-compute-instance-v2.vm09.id}"
}
[File 7] ~/terraform/050-instance.tf

[File 1 ~ 7]을 작성한다. [File 1,2]는 OpenStack 환경에 맞게 변경해야한다.

4. Terraform 적용, 초기화

(Deploy)$ cd ~/terraform
(Deploy)$ terraform init
(Deploy)$ terraform apply

Terraform을 적용한다.

(Deploy)$ cd ~/terraform
(Deploy)$ terraform destroy

Terraform을 초기화 한다.

5. 참조