Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix outdated k8s.ts File #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 24 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Terraform Kubernetes Digital Ocean Deploy

Source repository for Digital Ocean deploy conference. This repository contains an example showing
how to create a Kubernetes cluster in Digital Ocean, and how to deploy applications to it using Terraform.

## Resources Created
* 1x DOKS cluster, single node `s-2vcpu-2gb`
* Minecraft running as a pod on the clsuter
* 1x External load balancer pointed at the Minecraft instance
* 1x 10GB volume attached to the Minecraft pod used for storing config

- 1x DOKS cluster, single node `s-2vcpu-2gb`
- Minecraft running as a pod on the clsuter
- 1x External load balancer pointed at the Minecraft instance
- 1x 10GB volume attached to the Minecraft pod used for storing config
and data.

## Setup
Expand All @@ -17,19 +19,19 @@ Terraform can be installed from the following link: [https://www.terraform.io/do

### Obtain your Digital Ocean API Key and set it as an environment variable

* Visit the page:
[https://cloud.digitalocean.com/account/api/tokens](https://cloud.digitalocean.com/account/api/tokens)
- Visit the page:
[https://cloud.digitalocean.com/account/api/tokens](https://cloud.digitalocean.com/account/api/tokens)

* Click Generate New Token:
![](images/generate.png)
- Click Generate New Token:
![](images/generate.png)

* Give the token a name, ensure Read and Write is set and click generate
![](images/dialog.png)
- Give the token a name, ensure Read and Write is set and click generate
![](images/dialog.png)

* Copy the the token (long string beneath the name) to your clip board
![](images/token.png)
- Copy the the token (long string beneath the name) to your clip board
![](images/token.png)

* Create an environment variable using the token
- Create an environment variable using the token

```shell
export DIGITALOCEAN_TOKEN=c789eb2af98226de4f31582016b3eb83298a7a3baeb608310880936899a4a4d9
Expand All @@ -39,8 +41,8 @@ Terraform will automaticaly read the environment variable `DIGITALOCEAN_TOKEN` a
to communicate with the Digital Ocean API. NOTE: Ensure to keep this token private
do not add it to any files which may be uploaded to public souce code repositories like GitHub.


## Initialize Terraform

Before creating resources with Terraform you need to initialize the configuration, this downloads
any necessary dependencies and should only need to be done once. To do this run `terraform init`
in the current folder.
Expand Down Expand Up @@ -72,7 +74,7 @@ commands will detect it and remind you to do so if necessary.

## Terraform apply

To create resources you can run a `terraform apply`, this will create the cluster and
To create resources you can run a `terraform apply`, this will create the cluster and
the application. Before Terraform creates the cluster it will inform you of the changes
it is about to make. This is known as the Terraform plan. Before Terraform will actually
create the resources you need to answer `yes` that you approve the plan.
Expand Down Expand Up @@ -164,15 +166,16 @@ lb_address = 188.166.134.43
```

## Connecting to the cluster

To connect to the cluster you need to fetch a Kubernetes config file, Terraform allows the definition of output
variables which contain data from the created resources. The `k8s_config` variable contains the config needed
to connect to the cluster. You can output this to a file using the following command:

```
terraform output k8s_config > kubeconfig.yaml
terraform output -raw k8s_config > kubeconfig.yaml
```

Then you can use `kubectl` as normal to connect to the cluster, running get pods will show you a single pod
Then you can use `kubectl` as normal to connect to the cluster, running get pods will show you a single pod
running.

```
Expand All @@ -182,7 +185,8 @@ minecraft-66988b7999-wgm5k 1/1 Running 0 7m39s
```

## Connecting to the Minecraft server
As an example application a Minecraft server has been deployed as a pod to your Kuberenetes cluster. To

As an example application a Minecraft server has been deployed as a pod to your Kuberenetes cluster. To
access the server an external loadbalancer has been created. The address for this load balancer can be
retrieved by again querying Terraform output variables.

Expand All @@ -202,7 +206,7 @@ can add authorization to access the server.

![](images/minecraft_2.png)

You can add access by using the `rcon-cli` inside the Minecraft container, use the following command
You can add access by using the `rcon-cli` inside the Minecraft container, use the following command
to get shell access to the container.

```
Expand All @@ -229,8 +233,8 @@ Once you have whitelisted your user you can now log into the server.

![](images/minecraft_3.png)


## Cleaning up and Destroying resources

To clean up all resource created you can use the `teraform destroy` command.

```
Expand Down
66 changes: 36 additions & 30 deletions k8s.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
provider "digitalocean" {
version = "1.22.2"
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = "~> 2.0"
}
}
}

provider "digitalocean" {}

resource "digitalocean_kubernetes_cluster" "minecraft" {
name = var.name
region = var.region
# Grab the latest version slug from `doctl kubernetes options versions`
version = "1.19.3-do.2"
version = "1.28.2-do.0"

node_pool {
name = "worker-pool"
Expand All @@ -16,19 +26,14 @@ resource "digitalocean_kubernetes_cluster" "minecraft" {
}

provider "kubernetes" {
version = "1.13.2"

load_config_file = false
host = digitalocean_kubernetes_cluster.minecraft.endpoint
token = digitalocean_kubernetes_cluster.minecraft.kube_config[0].token
cluster_ca_certificate = base64decode(
digitalocean_kubernetes_cluster.minecraft.kube_config[0].cluster_ca_certificate
)
host = digitalocean_kubernetes_cluster.minecraft.endpoint
token = digitalocean_kubernetes_cluster.minecraft.kube_config[0].token
cluster_ca_certificate = base64decode(digitalocean_kubernetes_cluster.minecraft.kube_config[0].cluster_ca_certificate)
}

resource "kubernetes_deployment" "minecraft" {
metadata {
name = "minecraft"
name = "minecraft"
labels = {
app = "minecraft"
}
Expand All @@ -52,20 +57,20 @@ resource "kubernetes_deployment" "minecraft" {

spec {
container {
image = var.image
name = "minecraft"
image = var.image
name = "minecraft"
image_pull_policy = "Always"

port {
container_port = var.port
name = "minecraft"
name = "minecraft"
}

dynamic "env" {
for_each = var.envs

content {
name = env.key
name = env.key
value = env.value
}
}
Expand All @@ -74,36 +79,35 @@ resource "kubernetes_deployment" "minecraft" {
for_each = var.mounts

content {
name = kubernetes_persistent_volume_claim.minecraftdata.metadata.0.name
sub_path = volume_mount.value.source
name = kubernetes_persistent_volume_claim.minecraftdata.metadata[0].name
sub_path = volume_mount.value.source
mount_path = volume_mount.value.destination
}
}

}

volume {
name = kubernetes_persistent_volume_claim.minecraftdata.metadata.0.name
name = kubernetes_persistent_volume_claim.minecraftdata.metadata[0].name

persistent_volume_claim {
claim_name = kubernetes_persistent_volume_claim.minecraftdata.metadata.0.name
}
claim_name = kubernetes_persistent_volume_claim.minecraftdata.metadata[0].name
}
}

}
}
}
}


resource "kubernetes_service" "minecraft" {
metadata {
name = "minecraft"
}

spec {
selector = {
app = kubernetes_deployment.minecraft.metadata.0.labels.app
app = "minecraft"
}

port {
port = var.port
target_port = var.port
Expand All @@ -117,6 +121,7 @@ resource "kubernetes_persistent_volume_claim" "minecraftdata" {
metadata {
name = var.volume
}

spec {
access_modes = ["ReadWriteOnce"]
resources {
Expand All @@ -128,9 +133,10 @@ resource "kubernetes_persistent_volume_claim" "minecraftdata" {
}

output "k8s_config" {
value = digitalocean_kubernetes_cluster.minecraft.kube_config.0.raw_config
value = digitalocean_kubernetes_cluster.minecraft.kube_config[0].raw_config
sensitive = true
}

output "lb_address" {
value = kubernetes_service.minecraft.load_balancer_ingress.0.ip
value = kubernetes_service.minecraft.status[0].load_balancer[0].ingress[0].ip
}