Skip to content

Commit

Permalink
Markdown Formatting Run1
Browse files Browse the repository at this point in the history
First run through of the markdown formatting for days 60 through 90 and Readme
  • Loading branch information
TheAutisticTechie committed Jun 26, 2022
1 parent efa9d1c commit 73cb256
Show file tree
Hide file tree
Showing 32 changed files with 1,168 additions and 1,137 deletions.
73 changes: 37 additions & 36 deletions Days/day60.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
---
title: '#90DaysOfDevOps - Docker Containers, Provisioners & Modules - Day 60'
title: "#90DaysOfDevOps - Docker Containers, Provisioners & Modules - Day 60"
published: false
description: '90DaysOfDevOps - Docker Containers, Provisioners & Modules'
tags: 'devops, 90daysofdevops, learning'
description: "90DaysOfDevOps - Docker Containers, Provisioners & Modules"
tags: "devops, 90daysofdevops, learning"
cover_image: null
canonical_url: null
id: 1049052
---
## Docker Containers, Provisioners & Modules

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 Containers, Provisioners & Modules

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.
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 {
Expand Down Expand Up @@ -42,21 +43,21 @@ resource "docker_container" "nginx" {
}
```

The first task is to use `terraform init` command to download the provider to our local machine.
The first task is to use `terraform init` command to download the provider to our local machine.

![](Images/Day60_IAC1.png)

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

![](Images/Day60_IAC2.png)

If we then open a browser we can navigate to http://localhost:8000/ and you will see we have access to our NGINX 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.

![](Images/Day60_IAC3.png)

You can find out more information on the [Docker Provider](https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/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.
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)

Expand Down Expand Up @@ -120,26 +121,25 @@ resource "docker_container" "wordpress" {
}
```

We again put this is in a new folder and then run our `terraform init` command to pull down our provisioners required.
We again put this is in a new folder and then run our `terraform init` command to pull down our provisioners required.

![](Images/Day60_IAC4.png)

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 then run our `terraform apply` command and then take a look at our docker ps output we should see our newly created containers.

![](Images/Day60_IAC5.png)

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.
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.

![](Images/Day60_IAC6.png)

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.

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.
Provisioners are there so that if something cannot be declarative 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.
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" {
Expand All @@ -152,40 +152,41 @@ resource "docker_container" "db" {
```

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.
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
- local-exec
- remote-exec
- vendor
- ansible
- chef
- puppet
- local-exec
- remote-exec
- vendor
- ansible
- chef
- puppet

### Modules
### Modules

Modules are containers for multiple resources that are used together. A module consists of a collection of .tf files in the same directory.
Modules are containers for multiple resources that are used together. A module consists of a collection of .tf files in the same directory.

Modules are a good way to separate your infrastructure resources as well as being able to pull in third party modules that have already been created so you do not have to re invent the wheel.
Modules are a good way to separate your infrastructure resources as well as being able to pull in third party modules that have already been created so you do not have to re invent the wheel.

For example if we wanted to use the same project to build out some VMs, VPCs, Security Groups and then also a Kubernetes cluster we would likely want to split our resources out into modules to better define our resources and where they are grouped.
For example if we wanted to use the same project to build out some VMs, VPCs, Security Groups and then also a Kubernetes cluster we would likely want to split our resources out into modules to better define our resources and where they are grouped.

Another benefit to modules is that you can take these modules and use them on other projects or share publicly to help the community.
Another benefit to modules is that you can take these modules and use them on other projects or share publicly to help the community.

We are breaking down our infrastructure into components, components are known here as modules.

## 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.
## 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.

- [What is Infrastructure as Code? Difference of Infrastructure as Code Tools ](https://www.youtube.com/watch?v=POPP2WTJ8es)
- [What is Infrastructure as Code? Difference of Infrastructure as Code Tools](https://www.youtube.com/watch?v=POPP2WTJ8es)
- [Terraform Tutorial | Terraform Course Overview 2021](https://www.youtube.com/watch?v=m3cKkYXl-8o)
- [Terraform explained in 15 mins | Terraform Tutorial for Beginners ](https://www.youtube.com/watch?v=l5k1ai_GBDE)
- [Terraform explained in 15 mins | Terraform Tutorial for Beginners](https://www.youtube.com/watch?v=l5k1ai_GBDE)
- [Terraform Course - From BEGINNER to PRO!](https://www.youtube.com/watch?v=7xngnjfIlK4&list=WL&index=141&t=16s)
- [HashiCorp Terraform Associate Certification Course](https://www.youtube.com/watch?v=V4waklkBC38&list=WL&index=55&t=111s)
- [Terraform Full Course for Beginners](https://www.youtube.com/watch?v=EJ3N-hhiWv0&list=WL&index=39&t=27s)
- [KodeKloud - Terraform for DevOps Beginners + Labs: Complete Step by Step Guide!](https://www.youtube.com/watch?v=YcJ9IeukJL8&list=WL&index=16&t=11s)
- [KodeKloud - Terraform for DevOps Beginners + Labs: Complete Step by Step Guide!](https://www.youtube.com/watch?v=YcJ9IeukJL8&list=WL&index=16&t=11s)
- [Terraform Simple Projects](https://terraform.joshuajebaraj.com/)
- [Terraform Tutorial - The Best Project Ideas](https://www.youtube.com/watch?v=oA-pPa0vfks)
- [Awesome Terraform](https://github.com/shuaibiyy/awesome-terraform)
Expand Down
79 changes: 42 additions & 37 deletions Days/day61.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
---
title: '#90DaysOfDevOps - Kubernetes & Multiple Environments - Day 61'
title: "#90DaysOfDevOps - Kubernetes & Multiple Environments - Day 61"
published: false
description: 90DaysOfDevOps - Kubernetes & Multiple Environments
tags: "devops, 90daysofdevops, learning"
cover_image: null
canonical_url: null
id: 1048743
---
## Kubernetes & Multiple Environments

## Kubernetes & Multiple Environments

So far during this section on Infrastructure as code we have looked at deploying virtual machines albeit to virtualbox but the premise is the same really as we define in code what we want our virtual machine to look like and then we deploy. The same for Docker containers and in this session we are going to take a look at how Terraform can be used to interact with resources supported by Kubernetes.

I have been using Terraform to deploy my Kubernetes clusters for demo purposes across the 3 main cloud providers and you can find the repository [tf_k8deploy](https://github.com/MichaelCade/tf_k8deploy)

However you can also use Terraform to interact with objects within the Kubernetes cluster, this could be using the [Kubernetes provider](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs) or it could be using the [Helm provider](https://registry.terraform.io/providers/hashicorp/helm/latest) to manage your chart deployments.
However you can also use Terraform to interact with objects within the Kubernetes cluster, this could be using the [Kubernetes provider](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs) or it could be using the [Helm provider](https://registry.terraform.io/providers/hashicorp/helm/latest) to manage your chart deployments.

Now we could use `kubectl` as we have showed in previous sections. But there are some benefits to using Terraform in your Kubernetes environment.
Now we could use `kubectl` as we have showed in previous sections. But there are some benefits to using Terraform in your Kubernetes environment.

- Unified workflow - if you have used terraform to deploy your clusters, you could use the same workflow and tool to deploy within your Kubernetes clusters

- Lifecycle management - Terraform is not just a provisioning tool, its going to enable change, updates and deletions.
- Lifecycle management - Terraform is not just a provisioning tool, its going to enable change, updates and deletions.

### Simple Kubernetes Demo

Much like the demo we created in the last session we can now deploy nginx into our Kubernetes cluster, I will be using minikube here again for demo purposes. We create our Kubernetes.tf file and you can find this in the [folder](/Days/IaC/Kubernetes/kubernetes.tf)

In that file we are going to define our Kubernetes provider, we are going to point to our kubeconfig file, create a namespace called nginx, then we will create a deployment which contains 2 replicas and finally a service.
In that file we are going to define our Kubernetes provider, we are going to point to our kubeconfig file, create a namespace called nginx, then we will create a deployment which contains 2 replicas and finally a service.

```
terraform {
Expand Down Expand Up @@ -93,73 +94,77 @@ resource "kubernetes_service" "test" {
}
```

The first thing we have to do in our new project folder is run the `terraform init` command.
The first thing we have to do in our new project folder is run the `terraform init` command.

![](Images/Day61_IAC1.png)

And then before we run the `terraform apply` command, let me show you that we have no namespaces.
And then before we run the `terraform apply` command, let me show you that we have no namespaces.

![](Images/Day61_IAC2.png)

When we run our apply command this is going to create those 3 new resources, namespace, deployment and service within our Kubernetes cluster.
When we run our apply command this is going to create those 3 new resources, namespace, deployment and service within our Kubernetes cluster.

![](Images/Day61_IAC3.png)

We can now take a look at the deployed resources within our cluster.
We can now take a look at the deployed resources within our cluster.

![](Images/Day61_IAC4.png)

Now because we are using minikube and you will have seen in the previous section this has its own limitations when we try and play with the docker networking for ingress. But if we simply issue the `kubectl port-forward -n nginx svc/nginx 30201:80` command and open a browser to http://localhost:30201/ we should see our NGINX page.
Now because we are using minikube and you will have seen in the previous section this has its own limitations when we try and play with the docker networking for ingress. But if we simply issue the `kubectl port-forward -n nginx svc/nginx 30201:80` command and open a browser to `http://localhost:30201/` we should see our NGINX page.

![](Images/Day61_IAC5.png)

If you want to try out more detailed demos with Terraform and Kubernetes then the [HashiCorp Learn site](https://learn.hashicorp.com/tutorials/terraform/kubernetes-provider) is fantastic to run through.
If you want to try out more detailed demos with Terraform and Kubernetes then the [HashiCorp Learn site](https://learn.hashicorp.com/tutorials/terraform/kubernetes-provider) is fantastic to run through.

### Multiple Environments

### Multiple Environments
If we wanted to take any of the demos we have ran through but wanted to now have specific production, staging and development environments looking exactly the same and leveraging this code there are two approaches to achieve this with Terraform

If we wanted to take any of the demos we have ran through but wanted to now have specific production, staging and development environments looking exactly the same and leveraging this code there are two approaches to achieve this with Terraform
- `terraform workspaces` - multiple named sections within a single backend

- `terraform workspaces` - multiple named sections within a single backend
- file structure - Directory layout provides separation, modules provide reuse.

- file structure - Directory layout provides separation, modules provide reuse.
Each of the above do have their pros and cons though.

Each of the above do have their pros and cons though.
### terraform workspaces

### terraform workspaces
Pros

Pros
- Easy to get started
- Convenient terraform.workspace expression
- Minimises code duplication
- Easy to get started
- Convenient terraform.workspace expression
- Minimises code duplication

Cons

- Prone to human error (we were trying to eliminate this by using TF)
- State stored within the same backend
- Codebase doesnt unambiguously show deployment configurations.
- State stored within the same backend
- Codebase doesn't unambiguously show deployment configurations.

### File Structure

### File Structure
Pros

Pros
- Isolation of backends
- improved security
- decreased potential for human error
- Isolation of backends
- improved security
- decreased potential for human error
- Codebase fully represents deployed state

Cons
- Multiple terraform apply required to provision environments
- More code duplication, but can be minimised with modules.
Cons

- Multiple terraform apply required to provision environments
- More code duplication, but can be minimised with 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.

- [What is Infrastructure as Code? Difference of Infrastructure as Code Tools ](https://www.youtube.com/watch?v=POPP2WTJ8es)
- [What is Infrastructure as Code? Difference of Infrastructure as Code Tools](https://www.youtube.com/watch?v=POPP2WTJ8es)
- [Terraform Tutorial | Terraform Course Overview 2021](https://www.youtube.com/watch?v=m3cKkYXl-8o)
- [Terraform explained in 15 mins | Terraform Tutorial for Beginners ](https://www.youtube.com/watch?v=l5k1ai_GBDE)
- [Terraform explained in 15 mins | Terraform Tutorial for Beginners](https://www.youtube.com/watch?v=l5k1ai_GBDE)
- [Terraform Course - From BEGINNER to PRO!](https://www.youtube.com/watch?v=7xngnjfIlK4&list=WL&index=141&t=16s)
- [HashiCorp Terraform Associate Certification Course](https://www.youtube.com/watch?v=V4waklkBC38&list=WL&index=55&t=111s)
- [Terraform Full Course for Beginners](https://www.youtube.com/watch?v=EJ3N-hhiWv0&list=WL&index=39&t=27s)
- [KodeKloud - Terraform for DevOps Beginners + Labs: Complete Step by Step Guide!](https://www.youtube.com/watch?v=YcJ9IeukJL8&list=WL&index=16&t=11s)
- [KodeKloud - Terraform for DevOps Beginners + Labs: Complete Step by Step Guide!](https://www.youtube.com/watch?v=YcJ9IeukJL8&list=WL&index=16&t=11s)
- [Terraform Simple Projects](https://terraform.joshuajebaraj.com/)
- [Terraform Tutorial - The Best Project Ideas](https://www.youtube.com/watch?v=oA-pPa0vfks)
- [Awesome Terraform](https://github.com/shuaibiyy/awesome-terraform)
Expand Down
Loading

0 comments on commit 73cb256

Please sign in to comment.