Day 60 - Docker Containers, Provisioners & Modules
This commit is contained in:
parent
4d14207527
commit
e2f016776c
57
Days/IaC/Docker-Wordpress/docker-wordpress.tf
Normal file
57
Days/IaC/Docker-Wordpress/docker-wordpress.tf
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
docker = {
|
||||||
|
source = "kreuzwerker/docker"
|
||||||
|
version = "2.16.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
provider "docker" {}
|
||||||
|
|
||||||
|
variable wordpress_port {
|
||||||
|
default = "8080"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "docker_volume" "db_data" {
|
||||||
|
name = "db_data"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "docker_network" "wordpress_net" {
|
||||||
|
name = "wordpress_net"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "docker_container" "db" {
|
||||||
|
name = "db"
|
||||||
|
image = "mysql:5.7"
|
||||||
|
restart = "always"
|
||||||
|
network_mode = "wordpress_net"
|
||||||
|
env = [
|
||||||
|
"MYSQL_ROOT_PASSWORD=wordpress",
|
||||||
|
"MYSQL_PASSWORD=wordpress",
|
||||||
|
"MYSQL_USER=wordpress",
|
||||||
|
"MYSQL_DATABASE=wordpress"
|
||||||
|
]
|
||||||
|
mounts {
|
||||||
|
type = "volume"
|
||||||
|
target = "/var/lib/mysql"
|
||||||
|
source = "db_data"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "docker_container" "wordpress" {
|
||||||
|
name = "wordpress"
|
||||||
|
image = "wordpress:latest"
|
||||||
|
restart = "always"
|
||||||
|
network_mode = "wordpress_net"
|
||||||
|
env = [
|
||||||
|
"WORDPRESS_DB_HOST=db:3306",
|
||||||
|
"WORDPRESS_DB_USER=wordpress",
|
||||||
|
"WORDPRESS_DB_NAME=wordpress",
|
||||||
|
"WORDPRESS_DB_PASSWORD=wordpress"
|
||||||
|
]
|
||||||
|
ports {
|
||||||
|
internal = "80"
|
||||||
|
external = "${var.wordpress_port}"
|
||||||
|
}
|
||||||
|
}
|
BIN
Days/Images/Day60_IAC2.png
Normal file
BIN
Days/Images/Day60_IAC2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 MiB |
BIN
Days/Images/Day60_IAC3.png
Normal file
BIN
Days/Images/Day60_IAC3.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 118 KiB |
BIN
Days/Images/Day60_IAC4.png
Normal file
BIN
Days/Images/Day60_IAC4.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 513 KiB |
BIN
Days/Images/Day60_IAC5.png
Normal file
BIN
Days/Images/Day60_IAC5.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 660 KiB |
BIN
Days/Images/Day60_IAC6.png
Normal file
BIN
Days/Images/Day60_IAC6.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 191 KiB |
BIN
Days/Images/Screenshot from 2022-02-28 21-03-27.png
Normal file
BIN
Days/Images/Screenshot from 2022-02-28 21-03-27.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 538 KiB |
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Before we start making stuff with Terraform we have to dive a little into HashiCorp Configuration Language (HCL). So far during our challenge we have looked at a few different scripting and programming languages and here is another one. We touched on the [Go programming language](Days/day07.md) then [bash scripts](Days/day19.md) we even touched on a little python when it came to [network automation](Days/day27.md)
|
Before we start making stuff with Terraform we have to dive a little into HashiCorp Configuration Language (HCL). So far during our challenge we have looked at a few different scripting and programming languages and here is another one. We touched on the [Go programming language](Days/day07.md) then [bash scripts](Days/day19.md) we even touched on a little python when it came to [network automation](Days/day27.md)
|
||||||
|
|
||||||
Now we must cover HashiCorp Configuration Language (HCL) if this is the first time you are seeing the language it might look a little daunting but its quite simple and ver powerful.
|
Now we must cover HashiCorp Configuration Language (HCL) if this is the first time you are seeing the language it might look a little daunting but its quite simple and very powerful.
|
||||||
|
|
||||||
As we move through this section, we are going to be using examples that we can run locally on our system regardless of what OS you are using, we will be using virtualbox albeit not the infrastructure platform you would be using with Terraform it is free and will allow us to achieve what we are looking to do we can also do this with Docker and Kubernetes.
|
As we move through this section, we are going to be using examples that we can run locally on our system regardless of what OS you are using, we will be using virtualbox albeit not the infrastructure platform you would be using with Terraform it is free and will allow us to achieve what we are looking to do we can also do this with Docker and Kubernetes.
|
||||||
|
|
||||||
|
@ -117,4 +117,4 @@ I have listed a lot of resources down below and I think this topic has been cove
|
|||||||
- [Terraform Tutorial - The Best Project Ideas](https://www.youtube.com/watch?v=oA-pPa0vfks)
|
- [Terraform Tutorial - The Best Project Ideas](https://www.youtube.com/watch?v=oA-pPa0vfks)
|
||||||
- [Awesome Terraform](https://github.com/shuaibiyy/awesome-terraform)
|
- [Awesome Terraform](https://github.com/shuaibiyy/awesome-terraform)
|
||||||
|
|
||||||
See you on [Day 57](day57.md)
|
See you on [Day 60](day60.md)
|
143
Days/day60.md
143
Days/day60.md
@ -2,17 +2,151 @@
|
|||||||
|
|
||||||
On [Day 59](day59.md) we provisioned a virtual machine using Terraform to our local FREE virtualbox environment. In this section we are going to be deploy a Docker container with some configuration to our local Docker environment.
|
On [Day 59](day59.md) we provisioned a virtual machine using Terraform to our local FREE virtualbox environment. In this section we are going to be deploy a Docker container with some configuration to our local Docker environment.
|
||||||
|
|
||||||
|
### Docker Demo
|
||||||
|
|
||||||
|
First up we are going to use the code block below, the outcome of the below is that we would like a simple web app to be deployed into docker and to publish this so that it is available to our network. We will be using nginx and we will make this available externally on our laptop over localhost and port 8000. We are using a docker provider from the community and you can see the docker image we are using also stated in our configuration.
|
||||||
|
|
||||||
|
```
|
||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
docker = {
|
||||||
|
source = "kreuzwerker/docker"
|
||||||
|
version = "2.16.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Docker demo
|
provider "docker" {}
|
||||||
|
|
||||||
`terraform init`
|
resource "docker_image" "nginx" {
|
||||||
|
name = "nginx:latest"
|
||||||
|
keep_locally = false
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "docker_container" "nginx" {
|
||||||
|
image = docker_image.nginx.latest
|
||||||
|
name = "tutorial"
|
||||||
|
ports {
|
||||||
|
internal = 80
|
||||||
|
external = 8000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The first task is to use `terraform init` command to download the provider to our local machine.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
We then run our `terraform apply` followed by `docker ps` and you can see we have a running container.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
If we then open a browser we can navigate to http://localhost:8000/ and you will see we have access to our NGINX container.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
You can find out more information on the [Docker Provider](https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/container)
|
||||||
|
|
||||||
|
The above is a very simple demo of what can be done with Terraform plus Docker and how we can now manage this under the Terraform state. We covered docker compose in the containers section and there is a little crossover in a way between this, infrastructure as code as well as then Kubernetes.
|
||||||
|
|
||||||
|
For the purpose of showing this and how Terraform can handle a little more complexity, we are going to take the docker compose file for wordpress and mysql that we created with docker compose and we will put this to Terraform. You can find the [docker-wordpress.tf](/Days/IaC/Docker-Wordpress/docker-wordpress.tf)
|
||||||
|
|
||||||
|
```
|
||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
docker = {
|
||||||
|
source = "kreuzwerker/docker"
|
||||||
|
version = "2.16.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
provider "docker" {}
|
||||||
|
|
||||||
|
variable wordpress_port {
|
||||||
|
default = "8080"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "docker_volume" "db_data" {
|
||||||
|
name = "db_data"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "docker_network" "wordpress_net" {
|
||||||
|
name = "wordpress_net"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "docker_container" "db" {
|
||||||
|
name = "db"
|
||||||
|
image = "mysql:5.7"
|
||||||
|
restart = "always"
|
||||||
|
network_mode = "wordpress_net"
|
||||||
|
env = [
|
||||||
|
"MYSQL_ROOT_PASSWORD=wordpress",
|
||||||
|
"MYSQL_PASSWORD=wordpress",
|
||||||
|
"MYSQL_USER=wordpress",
|
||||||
|
"MYSQL_DATABASE=wordpress"
|
||||||
|
]
|
||||||
|
mounts {
|
||||||
|
type = "volume"
|
||||||
|
target = "/var/lib/mysql"
|
||||||
|
source = "db_data"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "docker_container" "wordpress" {
|
||||||
|
name = "wordpress"
|
||||||
|
image = "wordpress:latest"
|
||||||
|
restart = "always"
|
||||||
|
network_mode = "wordpress_net"
|
||||||
|
env = [
|
||||||
|
"WORDPRESS_DB_HOST=db:3306",
|
||||||
|
"WORDPRESS_DB_USER=wordpress",
|
||||||
|
"WORDPRESS_DB_NAME=wordpress",
|
||||||
|
"WORDPRESS_DB_PASSWORD=wordpress"
|
||||||
|
]
|
||||||
|
ports {
|
||||||
|
internal = "80"
|
||||||
|
external = "${var.wordpress_port}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
We again put this is in a new folder and then run our `terraform init` command to pull down our provisioners required.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
We then run our `terraform apply` command and then take a look at our docker ps output we should see our newly created containers.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
We can then also navigate to our WordPress front end. Much like when we went through this process with docker-compose in the containers section we can now run through the setup and our wordpress posts would be living in our MySQL database.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Obviously now we have covered containers and Kubernetes in some detail, we probably know that this is ok for testing but if you were really going to be running a website you would not do this with containers alone and you would look at using Kubernetes to achieve this, Next up we are going to take a look using Terraform with Kubernetes.
|
||||||
|
|
||||||
|
|
||||||
### Provisioners
|
### Provisioners
|
||||||
|
|
||||||
|
Provisioners are there so that if something cannot be declartive we have a way in which to parse this to our deployment.
|
||||||
|
|
||||||
|
If you have no other alternative and adding this complexity to your code is the place to go then you can do this by running something similar to the following block of code.
|
||||||
|
|
||||||
|
```
|
||||||
|
resource "docker_container" "db" {
|
||||||
|
# ...
|
||||||
|
|
||||||
|
provisioner "local-exec" {
|
||||||
|
command = "echo The server's IP address is ${self.private_ip}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
The remote-exec provisioner invokes a script on a remote resource after it is created. This could be used for something OS specific or it could be used to wrap in a configuration management tool. Although notice that we have some of these covered in their own provisioners.
|
||||||
|
|
||||||
|
[More details on provisioners](https://www.terraform.io/language/resources/provisioners/syntax)
|
||||||
|
|
||||||
- file
|
- file
|
||||||
- local-exec
|
- local-exec
|
||||||
- remote-exec
|
- remote-exec
|
||||||
@ -33,9 +167,6 @@ Another benefit to modules is that you can take these modules and use them on ot
|
|||||||
|
|
||||||
We are breaking down our infrastructure into components, components are known here as modules.
|
We are breaking down our infrastructure into components, components are known here as modules.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Resources
|
## Resources
|
||||||
I have listed a lot of resources down below and I think this topic has been covered so many times out there, If you have additional resources be sure to raise a PR with your resources and I will be happy to review and add them to the list.
|
I have listed a lot of resources down below and I think this topic has been covered so many times out there, If you have additional resources be sure to raise a PR with your resources and I will be happy to review and add them to the list.
|
||||||
|
|
||||||
@ -50,4 +181,4 @@ I have listed a lot of resources down below and I think this topic has been cove
|
|||||||
- [Terraform Tutorial - The Best Project Ideas](https://www.youtube.com/watch?v=oA-pPa0vfks)
|
- [Terraform Tutorial - The Best Project Ideas](https://www.youtube.com/watch?v=oA-pPa0vfks)
|
||||||
- [Awesome Terraform](https://github.com/shuaibiyy/awesome-terraform)
|
- [Awesome Terraform](https://github.com/shuaibiyy/awesome-terraform)
|
||||||
|
|
||||||
See you on [Day 57](day57.md)
|
See you on [Day 61](day61.md)
|
@ -58,4 +58,4 @@ I have listed a lot of resources down below and I think this topic has been cove
|
|||||||
- [Terraform Tutorial - The Best Project Ideas](https://www.youtube.com/watch?v=oA-pPa0vfks)
|
- [Terraform Tutorial - The Best Project Ideas](https://www.youtube.com/watch?v=oA-pPa0vfks)
|
||||||
- [Awesome Terraform](https://github.com/shuaibiyy/awesome-terraform)
|
- [Awesome Terraform](https://github.com/shuaibiyy/awesome-terraform)
|
||||||
|
|
||||||
See you on [Day 57](day57.md)
|
See you on [Day 62](day62.md)
|
@ -48,4 +48,4 @@ I have listed a lot of resources down below and I think this topic has been cove
|
|||||||
- [Terraform Tutorial - The Best Project Ideas](https://www.youtube.com/watch?v=oA-pPa0vfks)
|
- [Terraform Tutorial - The Best Project Ideas](https://www.youtube.com/watch?v=oA-pPa0vfks)
|
||||||
- [Awesome Terraform](https://github.com/shuaibiyy/awesome-terraform)
|
- [Awesome Terraform](https://github.com/shuaibiyy/awesome-terraform)
|
||||||
|
|
||||||
See you on [Day 57](day57.md)
|
See you on [Day 63](day63.md)
|
Loading…
Reference in New Issue
Block a user