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" \
LDFLAGS="-w -s -d"
FROM scratch
FROM alpine
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/group /etc/group
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
ENTRYPOINT ["/go/bin/weather"]

View File

@ -52,9 +52,9 @@ get-root-ca:
docker: docker-build-poller docker-build-weather docker-push
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 -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.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" {
metadata {
name = "poller-application"
name = local.application_name_prefixed
labels = {
app = local.service_match_label
env = local.environment
env = var.environment
}
namespace = kubernetes_namespace.application_namespace.id
namespace = var.kubernetes_namespace.id
}
spec {
dynamic strategy {
for_each = var.expose_application ? [1] : []
content {
type = "Recreate"
}
}
replicas = 1
revision_history_limit = 0
selector {
@ -19,14 +28,14 @@ resource "kubernetes_deployment" "application" {
metadata {
labels = {
app = local.deployment_match_label
env = local.environment
env = var.environment
}
}
spec {
volume {
name = local.config_volume_name
config_map {
name = kubernetes_config_map.weather_config.metadata.0.name
name = var.kubernetes_config_map.name
}
}
volume {
@ -36,9 +45,9 @@ resource "kubernetes_deployment" "application" {
}
}
container {
image = format("%s:%s", var.application_image_tag, var.application_version)
name = "poller-application"
args = ["-filename", "/conf/config.hcl", "-logLevel", "info", "-logOutput", "/logs/weather.log", "-check-interval", "1h"]
image = var.application_image
name = local.application_name_prefixed
args = var.application_args
volume_mount {
mount_path = "/conf"
name = local.config_volume_name
@ -57,6 +66,28 @@ resource "kubernetes_deployment" "application" {
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" {
metadata {
namespace = kubernetes_namespace.application_namespace.id
name = "log-weather-pvc"
namespace = var.kubernetes_namespace.id
name = format("log-%s-pvc", var.application_name)
}
spec {
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

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