feat: refactor to terraform module to deploy weather server appplication

This commit is contained in:
RouxAntoine 2021-03-13 04:02:00 +01:00
parent 81c20103bc
commit 635d412397
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
7 changed files with 115 additions and 26 deletions

View File

@ -43,7 +43,7 @@ RUN make build-weather \
GOBUILDFLAGS="-a -tags netgo -installsuffix netgo" \ GOBUILDFLAGS="-a -tags netgo -installsuffix netgo" \
LDFLAGS="-w -s -d" LDFLAGS="-w -s -d"
FROM scratch FROM alpine
USER appuser:appuser USER appuser:appuser
@ -52,7 +52,7 @@ COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /etc/passwd /etc/passwd COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group COPY --from=builder /etc/group /etc/group
COPY --from=builder --chown=appuser:appuser /data/bin/* /go/bin/weather COPY --from=builder --chown=appuser:appuser /data/bin/* /go/bin/weather
COPY --from=builder --chown=appuser:appuser /data/certs/out/weather* /go COPY --from=builder --chown=appuser:appuser /data/certs/out/weather* /go/certs/out/
WORKDIR /go WORKDIR /go
ENTRYPOINT ["/go/bin/weather"] ENTRYPOINT ["/go/bin/weather"]

View File

@ -52,9 +52,9 @@ get-root-ca:
docker: docker-build-poller docker-build-weather docker-push docker: docker-build-poller docker-build-weather docker-push
docker-build-poller: docker-build-poller:
docker build -t docker.registry:5000/weather/poller:$(POLLER_VERSION) -f DockerfilePoller . docker build --force-rm -t docker.registry:5000/weather/poller:$(POLLER_VERSION) -f DockerfilePoller .
docker-build-weather: docker-build-weather:
docker build -t docker.registry:5000/weather/server:$(WEATHER_VERSION) -f DockerfileWeather . docker build --force-rm -t docker.registry:5000/weather/server:$(WEATHER_VERSION) -f DockerfileWeather .
docker-push: docker-push:
docker push docker.registry:5000/weather/poller:$(POLLER_VERSION) docker push docker.registry:5000/weather/poller:$(POLLER_VERSION)

View File

@ -1,13 +1,22 @@
locals {
application_name_prefixed = format("%s-application", var.application_name)
}
resource "kubernetes_deployment" "application" { resource "kubernetes_deployment" "application" {
metadata { metadata {
name = "poller-application" name = local.application_name_prefixed
labels = { labels = {
app = local.service_match_label app = local.service_match_label
env = local.environment env = var.environment
} }
namespace = kubernetes_namespace.application_namespace.id namespace = var.kubernetes_namespace.id
} }
spec { spec {
dynamic strategy {
for_each = var.expose_application ? [1] : []
content {
type = "Recreate"
}
}
replicas = 1 replicas = 1
revision_history_limit = 0 revision_history_limit = 0
selector { selector {
@ -19,14 +28,14 @@ resource "kubernetes_deployment" "application" {
metadata { metadata {
labels = { labels = {
app = local.deployment_match_label app = local.deployment_match_label
env = local.environment env = var.environment
} }
} }
spec { spec {
volume { volume {
name = local.config_volume_name name = local.config_volume_name
config_map { config_map {
name = kubernetes_config_map.weather_config.metadata.0.name name = var.kubernetes_config_map.name
} }
} }
volume { volume {
@ -36,9 +45,9 @@ resource "kubernetes_deployment" "application" {
} }
} }
container { container {
image = format("%s:%s", var.application_image_tag, var.application_version) image = var.application_image
name = "poller-application" name = local.application_name_prefixed
args = ["-filename", "/conf/config.hcl", "-logLevel", "info", "-logOutput", "/logs/weather.log", "-check-interval", "1h"] args = var.application_args
volume_mount { volume_mount {
mount_path = "/conf" mount_path = "/conf"
name = local.config_volume_name name = local.config_volume_name
@ -57,6 +66,28 @@ resource "kubernetes_deployment" "application" {
memory = "50Mi" memory = "50Mi"
} }
} }
dynamic port {
for_each = var.expose_application ? [1] : []
content {
container_port = 8080
host_port = 8080
}
}
dynamic liveness_probe {
for_each = var.expose_application ? [1] : []
content {
http_get {
path = "/api/health"
port = 8080
scheme = "HTTPS"
http_header {
name = "X-Custom-Header"
value = "kube-liveness-probe"
}
}
}
}
} }
} }
} }
@ -70,8 +101,8 @@ resource "kubernetes_deployment" "application" {
resource "kubernetes_persistent_volume_claim" "log_volume_claim" { resource "kubernetes_persistent_volume_claim" "log_volume_claim" {
metadata { metadata {
namespace = kubernetes_namespace.application_namespace.id namespace = var.kubernetes_namespace.id
name = "log-weather-pvc" name = format("log-%s-pvc", var.application_name)
} }
spec { spec {
storage_class_name = "dx30-nfs" storage_class_name = "dx30-nfs"

View File

View File

@ -0,0 +1,37 @@
variable "kubernetes_namespace" {
description = "kubernetes namespace containing deployed resource"
type = object({
id: string
})
}
variable "kubernetes_config_map" {
description = "kubernetes config map use to configure deployed application"
type = object({
name: string
})
}
variable "environment" {
description = "application environment"
}
variable "application_name" {
description = "prefix name of application to deploy"
}
variable "application_image" {
description = "docker registry image/version to deploy"
}
variable "application_args" {
description = "args list to specify at application container"
type = list(string)
default = []
}
variable "expose_application" {
default = false
type = bool
}
locals {
config_volume_name = format("config-%s-volume", var.application_name)
log_volume_name = format("log-%s-volume", var.application_name)
service_match_label = format("%s-service", var.application_name)
deployment_match_label = format("%s-deployment", var.application_name)
}

View File

@ -21,3 +21,24 @@ s3 {
EOF EOF
} }
} }
// deploy poller application
module "poller_application" {
source = "./application"
environment = var.environment
application_name = "poller"
kubernetes_namespace = kubernetes_namespace.application_namespace
application_image = format("docker.registry/weather/poller:%s", var.poller_version)
kubernetes_config_map = kubernetes_config_map.weather_config.metadata.0
application_args = ["-filename", "/conf/config.hcl", "-logLevel", "info", "-logOutput", "/logs/weather.log", "-check-interval", "1h"]
}
// deploy weather server application
module "weather_server_application" {
source = "./application"
environment = var.environment
application_name = "weather-server"
kubernetes_namespace = kubernetes_namespace.application_namespace
application_image = format("docker.registry/weather/server:%s", var.poller_version)
kubernetes_config_map = kubernetes_config_map.weather_config.metadata.0
expose_application = true
}

View File

@ -1,8 +1,14 @@
variable "environment" {
default = "prod"
}
variable "openweather_secret" { variable "openweather_secret" {
description = "open weather api secret" description = "open weather api secret"
sensitive = true
} }
variable "S3_key_secret" { variable "S3_key_secret" {
description = "S3 backend key secret" description = "S3 backend key secret"
sensitive = true
} }
variable "S3_endpoint" { variable "S3_endpoint" {
default = "s3.localdomain" default = "s3.localdomain"
@ -16,18 +22,12 @@ variable "S3_key_id" {
default = "antoine" default = "antoine"
description = "S3 backend key id" description = "S3 backend key id"
} }
variable "application_image_tag" {
default = "docker.registry/weather/poller" variable "poller_version" {
description = "container tag deployed"
}
variable "application_version" {
default = "latest" default = "latest"
description = "container tag version deployed" description = "poller container version"
} }
locals { variable "weather_version" {
service_match_label = "weather-service" default = "latest"
deployment_match_label = "poller-deployment" description = "poller container version"
environment = "prod"
config_volume_name = "config-weather-volume"
log_volume_name ="log-weather-volume"
} }