Skip to content

Commit 64acab8

Browse files
committed
Add module configuration
1 parent f5ca06c commit 64acab8

File tree

4 files changed

+202
-33
lines changed

4 files changed

+202
-33
lines changed

README.md

+34-33
Original file line numberDiff line numberDiff line change
@@ -13,60 +13,61 @@ For this project, I used CentOS operating system on hardware and virtual machine
1313
* [CentOS-7-x86_64-Minimal-1810](http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1810.iso) for bare metal
1414
* [CentOS-7-x86_64-GenericCloud-1901](https://cloud.centos.org/centos/7/images/) for virtual machines
1515

16-
### Check CPU virtualization
16+
### Check CPU virtualization
1717

1818
First check if your CPU supports hardware virtualization
1919

20-
```
20+
```bash
2121
cat /proc/cpuinfo | egrep "(vmx|svm)"
2222
```
2323

2424
If the command does not return anything, virtualization is not supported on the server or it is disabled in the BIOS settings. KVM itself can be put on such a server, but when we try to enter the hypervisor management command, we will get the error "WARNING KVM acceleration not available, using 'qemu'".
2525

2626
### Install libvirt and virsh
2727

28-
```
28+
```bash
2929
yum install -y qemu-kvm libvirt virt-install
3030
```
31-
* qemu-kvm - hypervisor;
31+
32+
* qemu-kvm - hypervisor;
3233
* libvirt - virtualization management library;
3334
* virt-install - utility for managing virtual machines.
3435

3536
Allow autorun:
3637

37-
```
38+
```bash
3839
systemctl enable libvirtd
3940
```
4041

4142
Launch KVM:
4243

43-
```
44+
```bash
4445
systemctl start libvirtd
4546
```
4647

4748
### Network configuration
4849

4950
Install the package to work with bridge:
5051

51-
```
52+
```bash
5253
yum install -y bridge-utils
5354
```
5455

5556
Сheck the real network interface with the configured IP address
5657

57-
```
58+
```bash
5859
ip a
5960
```
6061

6162
Edit the settings of the real adapter:
6263

63-
```
64+
```bash
6465
vi /etc/sysconfig/network-scripts/ifcfg-eth0
6566
```
6667

6768
Need to get something like this:
6869

69-
```
70+
```text
7071
TYPE=Ethernet
7172
DEVICE=eth0
7273
#IPADDR=192.168.1.100
@@ -81,13 +82,13 @@ BRIDGE=br0
8182

8283
Create an interface for the network bridge:
8384

84-
```
85+
```bash
8586
vi /etc/sysconfig/network-scripts/ifcfg-br0
8687
```
8788

8889
Need to get something like this:
8990

90-
```
91+
```text
9192
DEVICE=br0
9293
TYPE=Bridge
9394
ONBOOT=yes
@@ -98,58 +99,59 @@ GATEWAY=192.168.1.1
9899
DNS1=8.8.8.8
99100
DNS2=8.8.4.4
100101
```
102+
101103
Restart the network:
102104

103-
```
105+
```bash
104106
systemctl restart network
105107
```
106108

107109
Restart libvirtd:
108110

109-
```
111+
```bash
110112
systemctl restart libvirtd
111113
```
112114

113115
### Install terraform
114116

115117
Ensure wget and unzip are installed
116118

117-
```
119+
```bash
118120
yum install -y wget unzip
119121
```
120122

121123
Then download the terraform archive
122124

123-
```
125+
```bash
124126
wget https://releases.hashicorp.com/terraform/0.11.10/terraform_0.11.10_linux_amd64.zip
125127
unzip terraform_0.11.10_linux_amd64.zip
126128
```
127129

128130
This will create a terraform binary file on your working directory. Move this file to the directory/usr/local/bin
129131

130-
```
132+
```bash
131133
mv terraform /usr/local/bin/
132134
```
133135

134136
Confirm the version installed
135137

136-
```
138+
```bash
137139
terraform -v
138140
```
139141

140142
### Install Terraform KVM provider
141143

142144
Terraform has a number of officially [supported providers](https://www.terraform.io/docs/providers/) available for use. Unfortunately, KVM is not in the list. I will use the [Terraform KVM provider](https://github.com/dmacvicar/terraform-provider-libvirt)
143145

144-
```
146+
```bash
145147
wget https://github.com/dmacvicar/terraform-provider-libvirt/releases/download/v0.5.1/terraform-provider-libvirt-0.5.1.CentOS_7.x86_64.tar.gz
146148
tar xvf terraform-provider-libvirt-0.5.1.CentOS_7.x86_64.tar.gz
147149
mv terraform-provider-libvirt ~/.terraform.d/plugins/
148150
```
149151

150152
Check that libvirt daemon 1.2.14 or newer is running on the hypervisor
151153

152-
```
154+
```bash
153155
yum info libvirt
154156
```
155157

@@ -160,37 +162,37 @@ yum info libvirt
160162
* Terraform - v0.11.10
161163
* terraform-provider-libvirt - 0.5.1.CentOS_7.x86_64
162164

163-
### If all the necessary components and dependencies are installed, you can start writing the infrastructure as code!
165+
### If all the necessary components and dependencies are installed, you can start writing the infrastructure as code
164166

165167
## Terraform
166168

167169
Initialize a Terraform working directory
168170

169-
```
171+
```bash
170172
terraform init
171173
```
172174

173175
Generate and show Terraform execution plan
174176

175-
```
177+
```bash
176178
terraform plan
177179
```
178180

179181
Then build your Terraform infrastructure
180182

181-
```
183+
```bash
182184
terraform apply
183185
```
184186

185187
Check your infrastructure use virsh
186188

187-
```
189+
```bash
188190
virsh list --all
189-
```
191+
```
190192

191193
You can destroy your Terraform infrastructure
192194

193-
```
195+
```bash
194196
terraform destroy
195197
```
196198

@@ -199,26 +201,25 @@ terraform destroy
199201
Terraform v0.11.10 cannot create a storage pool from code. In this example I will use the virsh for create k8s-pool.
200202
"/kvm/images" is the path to a file system directory for storing image files. If this directory does not exist, virsh will create it.
201203

202-
```
204+
```bash
203205
virsh pool-define-as k8s-pool dir - - - - "/kvm/images"
204206
```
205207

206-
Check storage pool
208+
Check storage pool
207209

208-
```
210+
```bash
209211
virsh pool-list --all
210212
```
211213

212214
Build and start k8s-pool
213215

214-
```
216+
```bash
215217
virsh pool-build k8s-pool
216218
virsh pool-start k8s-pool
217219
```
218220

219221
Enable autostart to start the pool after restarting the machine.
220222

221-
```
223+
```bash
222224
virsh pool-autostart k8s-pool
223225
```
224-

kubernetes/main.tf

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
provider "libvirt" {
2+
uri = "qemu:///system"
3+
}
4+
5+
resource "libvirt_volume" "base" {
6+
name = "base"
7+
pool = "k8s-pool"
8+
source = "/kvm/images/base.qcow2"
9+
format = "qcow2"
10+
}
11+
12+
data "template_file" "network_setting" {
13+
template = "${file("${path.module}/network_config.tpl")}"
14+
count = "${var.master_count}"
15+
vars {
16+
addr = "${count.index}"
17+
}
18+
}
19+
20+
resource "libvirt_volume" "master" {
21+
count = "${var.master_count}"
22+
name = "master-${count.index}"
23+
pool = "k8s-pool"
24+
base_volume_id = "${libvirt_volume.base.id}"
25+
format = "qcow2"
26+
size = "${var.master_disk_size}"
27+
}
28+
29+
resource "libvirt_cloudinit_disk" "masterinit" {
30+
count = "${var.master_count}"
31+
name = "masterinit-${count.index}"
32+
pool = "k8s-pool"
33+
user_data = "${data.template_file.user_data.rendered}"
34+
network_config = "${element(data.template_file.network_setting.*.rendered, count.index)}"
35+
}
36+
37+
resource "libvirt_domain" "k8s-master" {
38+
count = "${var.master_count}"
39+
name = "k8s-master-${count.index}"
40+
memory = "${var.master_memory}"
41+
vcpu = "${var.master_cpus}"
42+
autostart = "true"
43+
44+
network_interface {
45+
bridge = "${var.bridge}"
46+
addresses = ["192.168.3.${count.index}"]
47+
}
48+
49+
disk {
50+
volume_id = "${element(libvirt_volume.master.*.id, count.index)}"
51+
}
52+
53+
cloudinit = "${element(libvirt_cloudinit_disk.masterinit.*.id, count.index)}"
54+
55+
graphics {
56+
type = "vnc"
57+
listen_type = "address"
58+
autoport = true
59+
}
60+
}
61+
62+
# -------------------- NODES ------------------------
63+
data "template_file" "node_setting" {
64+
template = "${file("${path.module}/network_config.tpl")}"
65+
count = "${var.node_count}"
66+
vars {
67+
addr = "${var.master_count+count.index}"
68+
}
69+
}
70+
71+
resource "libvirt_volume" "node" {
72+
count = "${var.node_count}"
73+
name = "node-${count.index}"
74+
pool = "k8s-pool"
75+
base_volume_id = "${libvirt_volume.base.id}"
76+
format = "qcow2"
77+
size = "${var.node_disk_size}"
78+
}
79+
80+
resource "libvirt_cloudinit_disk" "nodeinit" {
81+
count = "${var.node_count}"
82+
name = "nodeinit-${count.index}"
83+
pool = "k8s-pool"
84+
user_data = "${data.template_file.user_data.rendered}"
85+
network_config = "${element(data.template_file.node_setting.*.rendered, count.index)}"
86+
}
87+
88+
resource "libvirt_domain" "k8s-node" {
89+
count = "${var.node_count}"
90+
name = "k8s-node-${count.index}"
91+
memory = "${var.node_memory}"
92+
vcpu = "${var.node_cpus}"
93+
autostart = "true"
94+
95+
network_interface {
96+
bridge = "${var.bridge}"
97+
addresses = ["192.168.3.${var.master_count+count.index}"]
98+
}
99+
100+
disk {
101+
volume_id = "${element(libvirt_volume.node.*.id, count.index)}"
102+
}
103+
104+
cloudinit = "${element(libvirt_cloudinit_disk.nodeinit.*.id, count.index)}"
105+
106+
graphics {
107+
type = "vnc"
108+
listen_type = "address"
109+
autoport = true
110+
}
111+
}

kubernetes/output.tf

Whitespace-only changes.

0 commit comments

Comments
 (0)