Skip to content
Snippets Groups Projects
Commit 144c3cb5 authored by František Řezníček's avatar František Řezníček
Browse files

feat: initial terraform use-cases

parent 153815ad
Branches
No related tags found
No related merge requests found
Pipeline #207584 passed
Showing with 416 additions and 84 deletions
# Local .terraform directories
**/.terraform/*
# .tfstate files
*.tfstate
*.tfstate.*
# Crash log files
crash.log
plan
plan1
.terraform.lock.hcl
\ No newline at end of file
stages:
- validate
default:
image:
name: registry.gitlab.ics.muni.cz:443/cloud/container-registry/terraform:latest
entrypoint:
- /usr/bin/env
- "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
before_script:
- terraform init
terraform_validate:
stage: validate
script:
- terraform validate
# Docs Iaas Terraform
# MetaCentrum OpenStack Terraform examples
MetaCentrum OpenStack cloud Terraform use-case examples.
## How to generate infrastructure
## Getting started
1. Load you OpenStack application credentials to environment variables `source ~/conf/prod-meta-cloud-new-openstack-all-roles`, see more details [here](https://docs.cloud.muni.cz/cloud/cli/#getting-credentials).
1. Override any variable if needed in a [main.tf](linux-vm-instances/main.tf). Every variable specified in [common/modules/vm-instances/variables.tf](common/modules/vm-instances/variables.tf)
1. Go to the use-case directory (for example `cd linux-vm-instances/`)
1. Initiate terraform `terraform init`.
1. Validate terraform description `terraform validate`.
1. Plan terraform infrastructure deploy `terraform plan -out plan` (generates terraform plan named `plan`).
1. Deploy infrastructure with `terraform apply plan` with previously generated plan.
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
## How to destroy infrastructure
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
1. Create plan to destroy infrastructure `terraform plan -destroy -out destroy-plan`
1. Destroy infrastructure `terraform apply destroy-plan`
## Add your files
## Use cases
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
### Create Linux VM server instances, each with FIP address `linux-vm-instances`
```
cd existing_repo
git remote add origin https://gitlab.ics.muni.cz/cloud/docs-iaas-terraform.git
git branch -M master
git push -uf origin master
```
## Integrate with your tools
- [ ] [Set up project integrations](https://gitlab.ics.muni.cz/cloud/docs-iaas-terraform/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
### Create Linux VM server instances, each with FIP address `windows-vm-instances`
users:
- default
- name: ubuntu
shell: /bin/bash
ssh_authorized_keys:
# - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIM7qrxujICTwg0TF/u/PuDkl7iPkOSUgqXzn758rMP82 tom-jones
# - ...
####################
# Define instances #
####################
resource "openstack_compute_instance_v2" "instances" {
count = var.instances_count
name = "${var.environment}-${count.index+1}"
image_id = var.instances_ostack_image
flavor_name = var.instances_ostack_flavor
key_pair = openstack_compute_keypair_v2.pubkey.name
security_groups = [openstack_networking_secgroup_v2.secgroup_default.name]
user_data = "#cloud-config\nhostname: ${var.environment}-${count.index+1}.local\n${file("./cloudinit.txt")}"
network {
uuid = openstack_networking_network_v2.instances_network_default.id
port = element(openstack_networking_port_v2.instances_ports.*.id, count.index)
}
}
resource "openstack_compute_keypair_v2" "pubkey" {
name = "${var.environment}-keypair"
public_key = file("${var.public_key}")
}
###############################################################
# Define networking #
# Security group rules are in separate file secgroup_rules.tf #
###############################################################
resource "openstack_networking_network_v2" "instances_network_default" {
name = "${var.environment}_network"
admin_state_up = "true"
}
resource "openstack_networking_subnet_v2" "instances_subnet_default" {
name = "${var.environment}_subnet"
network_id = openstack_networking_network_v2.instances_network_default.id
cidr = var.instances_ostack_network_cidr
ip_version = 4
dns_nameservers = ["1.1.1.1", "8.8.8.8"]
}
data "openstack_networking_network_v2" "instances_external_network" {
name = var.instances_ostack_fip_pool
}
resource "openstack_networking_router_v2" "instances_router_default" {
name = "${var.environment}_router"
admin_state_up = "true"
external_network_id = data.openstack_networking_network_v2.instances_external_network.id
}
resource "openstack_networking_router_interface_v2" "instances_router_interface" {
router_id = openstack_networking_router_v2.instances_router_default.id
subnet_id = openstack_networking_subnet_v2.instances_subnet_default.id
}
# Floating IPs
resource "openstack_networking_floatingip_v2" "fip_addresses" {
count = var.instances_count
pool = var.instances_ostack_fip_pool
}
resource "openstack_networking_floatingip_associate_v2" "fip_to_port_associations" {
count = var.instances_count
floating_ip = openstack_networking_floatingip_v2.fip_addresses[count.index].address
port_id = openstack_networking_port_v2.instances_ports[count.index].id
}
resource "openstack_networking_port_v2" "instances_ports" {
count = var.instances_count
name = "${var.environment}_port_${count.index+1}"
network_id = openstack_networking_network_v2.instances_network_default.id
admin_state_up = "true"
security_group_ids = [openstack_networking_secgroup_v2.secgroup_default.id]
fixed_ip {
subnet_id = openstack_networking_subnet_v2.instances_subnet_default.id
}
}
terraform {
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
version = "~> 1.47.0"
}
}
}
provider "openstack" {
auth_url = "https://identity.cloud.muni.cz"
application_credential_id = ""
application_credential_secret = ""
}
##################################
# Define Network Security Groups #
##################################
resource "openstack_networking_secgroup_v2" "secgroup_default" {
name = "${var.environment}_secgroup"
description = "Security group for instances"
}
# Allow all internal TCP & UDP
resource "openstack_networking_secgroup_rule_v2" "alltcp4" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 1
port_range_max = 65535
remote_ip_prefix = var.instances_ostack_network_cidr
security_group_id = openstack_networking_secgroup_v2.secgroup_default.id
}
resource "openstack_networking_secgroup_rule_v2" "alludp4" {
direction = "ingress"
ethertype = "IPv4"
protocol = "udp"
port_range_min = 1
port_range_max = 65535
remote_ip_prefix = var.instances_ostack_network_cidr
security_group_id = openstack_networking_secgroup_v2.secgroup_default.id
}
# External communication
# HTTP(S)
resource "openstack_networking_secgroup_rule_v2" "https4" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 443
port_range_max = 443
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.secgroup_default.id
}
resource "openstack_networking_secgroup_rule_v2" "http4" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 80
port_range_max = 80
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.secgroup_default.id
}
# ICMP
resource "openstack_networking_secgroup_rule_v2" "icmp4" {
direction = "ingress"
ethertype = "IPv4"
protocol = "icmp"
port_range_min = 0
port_range_max = 0
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.secgroup_default.id
}
# SSH
resource "openstack_networking_secgroup_rule_v2" "ssh4" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 22
port_range_max = 22
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.secgroup_default.id
}
# RDP protocol
resource "openstack_networking_secgroup_rule_v2" "rdp_tcp" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 3389
port_range_max = 3389
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.secgroup_default.id
}
resource "openstack_networking_secgroup_rule_v2" "rdp_udp" {
direction = "ingress"
ethertype = "IPv4"
protocol = "udp"
port_range_min = 3389
port_range_max = 3389
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.secgroup_default.id
}
# LB
resource "openstack_networking_secgroup_rule_v2" "lb4" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 6443
port_range_max = 6443
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.secgroup_default.id
}
variable "environment" {
description = "Name prefix for all resources. Use a-z, 0-9 and the hyphen (-) only."
default = "training"
}
variable "public_key" {
default = "~/.ssh/id_rsa.pub"
}
# user names
variable "ssh_user_name" {
default = "ubuntu"
}
variable "rdp_user_name" {
default = "Admin"
}
##################
# nodes settings #
##################
variable "instances_count" {
default = 3
}
variable "instances_ostack_flavor" {
description = "OpenStack flavor used for instances"
default = "hpc.8core-32ram-ssd-ephem"
}
variable "instances_ostack_image" {
description = "OpenStack image used for instances"
default = "88f8e72a-bbf0-4ccc-8ff2-4f3188cd0d18"
}
variable "instances_ostack_network_cidr" {
description = "Internal OpenStack network address, use CIDR notation"
default = "10.0.0.0/24"
}
variable "instances_ostack_fip_pool" {
description = "FIP pool"
default = "public-cesnet-195-113-167-GROUP"
}
variable "instances_volumes_count" {
description = "Number of volumes to be attached to instances (0 to disable attaching volumes, 1 attach to first instance only, ...)"
default = ""
}
variable "instances_volumes_size" {
description = "Size of volume attached to instances (in GB)"
default = 500
}
resource "openstack_blockstorage_volume_v3" "instances_volumes" {
count = var.instances_volumes_count != "" ? var.instances_volumes_count : var.instances_count
name = "${var.environment}-volume-${count.index+1}"
size = var.instances_volumes_size
}
resource "openstack_compute_volume_attach_v2" "instances_volume_attachments" {
count = var.instances_volumes_count != "" ? var.instances_volumes_count : var.instances_count
instance_id = element(openstack_compute_instance_v2.instances.*.id, count.index)
volume_id = element(openstack_blockstorage_volume_v3.instances_volumes.*.id, count.index)
device = "/dev/sdb"
}
users:
- default
- name: ubuntu
shell: /bin/bash
ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC5fFLKPzxna7fq6kh1CHaIQFnpqXybqLqGs4ZpTDbIrEn7xjCsdyxMm9dcptyS0t6BzXO56BlJyYsR1GWo4rp3g8rMmb9u6/oHmMwgn7G/GLgsaAAO5XHW0A3UEJl3JHfCQLHkN1APQ4dy7gNTG24ahH/pcyr4rV0SsjPUCqFqkSMDZxRgfllNGftxWVHR2fYfPALLrGdhR/SjNSIs3pwBIUXaSfF3aBLsjeGBj4y5YsiR9yI3y2gUmpURROofTvtE7Fp8OIgmWCVqRe70CKDbl17HFbz3FIqYwZLAQHILcp1M45zV8koSOjW5+3C/ZJYzBKOnw/a/1Cw3uHFDrZfRqKLMP/gagnoEPRHjfmUsJ3UJO0eXDCXmnH7F48xBI76CgxYl039/SMmJ2mR0KqAHGnwqVmJI3yBGyK+Z4iEwk+JVDLEB14RHiMp2/I/tYpDWFE1IOigFFNLdfaZrVFY1/fD+yGGyFUO1Wo+CKb8tpndLB4H3Yj2MLRDP/aNpLC4M7Aru7hWnUF81aE/VUAqR6CP2vsHzlAOmH08pOlP9FVITinmJqzBL15l+W7q0Rhh4WBRO4ixlrtRJDNL2wm0vf+GiJnXligFtZ7Cw8bk/LcAe37WqcTl0xLKDyPSw4SvWOC2aE6BVuJjPAhoUUcBaNzoBa7lf4eb+FS4tquTZlQ== secureuser@securehost
terraform {
backend "local" {}
}
terraform {
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
version = "~> 1.47.0"
}
}
}
provider "openstack" {
# all values are loaded from env. variables openstack RC file, see REAME.md
}
module "linux-vm-instances" {
source = "../common/modules/vm-instances"
# Example of variable overrides
instances_count = 3
environment = "linux-vm-farm"
public_key = "~/.ssh/id_rsa.pub"
instances_ostack_fip_pool = "private-muni-10-16-116"
instances_ostack_network_cidr = "10.250.0.0/24"
instances_ostack_flavor = "hpc.8core-128ram"
instances_ostack_image = "88f8e72a-bbf0-4ccc-8ff2-4f3188cd0d18"
# additional volumes
instances_volumes_count = 3 # place volumes
instances_volumes_size = 100 # each 100 GB
}
users: []
terraform {
backend "local" {}
}
terraform {
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
version = "~> 1.47.0"
}
}
}
provider "openstack" {
# all values are loaded from env. variables openstack RC file, see REAME.md
}
module "windows-vm-instances" {
source = "../common/modules/vm-instances"
# Example of variable overrides
instances_count = 3
environment = "win-vm-farm"
instances_ostack_fip_pool = "private-muni-10-16-116"
instances_ostack_network_cidr = "10.250.0.0/24"
instances_ostack_flavor = "hpc.8core-128ram"
instances_ostack_image = "97c8b617-2f82-4ba7-9e4a-4bb5e289acbb" # windows-server-2019-standard-eval-x86_64
# additional volumes
instances_volumes_count = 3 # place volumes
instances_volumes_size = 100 # each 100 GB
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment