Backend GKE Deploy, access key for backend

- Configs and automation for deploying backend to GKE
- First steps to adding an optional token check for requests to backend
- First steps frontend sending optional token to backend when configured
This commit is contained in:
Hristo 2024-05-10 16:07:58 -04:00
parent 0fedaef537
commit e6c2042df6
17 changed files with 296 additions and 39 deletions

View file

@ -1,21 +1,14 @@
# USAGE:
# 0: Install `docker` and `terraform` (Process specific to your system)
# 1: Copy the sample.env file to .env
# 2: Fillout the GCP info in .env
# 3: Edit GCP_REPO to the correct docker image repo path if you are using something other than Container registry
# 4: Edit the PREFIX if you would like images and GKE entities to be prefixed with something else
# 5: Run `make init` to initialize terraform
# 6: Follow the normal Preplexica configuration steps outlined in the project readme
# 7: Run `make build-deplpy` to build and push the project images to the repo, create a GKE cluster and deploy the app
#
# NOTES/ WARNINGS:
# - The app endpoint is unsecured and exposed to the internet at large, you will need to implement your desired auth
# - No auto scaling is enabled for this cluster and deployments, edit the terraform files accordingly for that
# Adds all the deployment relevant sensitive information about project
include .env
# Adds secrets/ keys we have define for the project locally and deployment
include ../../.env
# Use `location-id-docker.pkg` for artifact registry Ex. west-1-docker.pkg
GCP_REPO=gcr.io
PREFIX=perplexica
SEARCH_PORT=8080
BACKEND_PORT=3001
SEARCH_IMAGE_TAG=$(GCP_REPO)/$(GCP_PROJECT_ID)/$(PREFIX)-searxng:latest
BACKEND_IMAGE_TAG=$(GCP_REPO)/$(GCP_PROJECT_ID)/$(PREFIX)-backend:latest
APP_IMAGE_TAG=$(GCP_REPO)/$(GCP_PROJECT_ID)/$(PREFIX)-app:latest
@ -38,8 +31,10 @@ show_config:
&& echo $(GCP_SERVICE_ACCOUNT_KEY_FILE) \
&& echo $(SEARCH_IMAGE_TAG) \
&& echo $(BACKEND_IMAGE_TAG) \
&& echo $(APP_IMAGE_TAG)
&& echo $(APP_IMAGE_TAG) \
&& echo $(SEARCH_PORT) \
&& echo $(BACKEND_PORT) \
&& echo $(OPENAI)
.PHONY: docker-build-push-searxng
docker-build-push-searxng:
@ -49,14 +44,15 @@ docker-build-push-searxng:
.PHONY: docker-build-push-backend
docker-build-push-backend:
cd ../../ && docker build -f ./backed.dockerfile -t $(BACKEND_IMAGE_TAG) . --platform="linux/amd64"
cd ../../ && docker build -f ./backend.dockerfile -t $(BACKEND_IMAGE_TAG) . --platform="linux/amd64"
docker push $(BACKEND_IMAGE_TAG)
.PHONY: docker-build-push-app
docker-build-push-app:
cd ../../ && docker build -f ./app.dockerfile -t $(APP_IMAGE_TAG) . --platform="linux/amd64"
docker push $(APP_IMAGE_TAG)
#
# cd ../../ && docker build -f ./app.dockerfile -t $(APP_IMAGE_TAG) . --platform="linux/amd64"
# docker push $(APP_IMAGE_TAG)
.PHONY: init
@ -73,6 +69,9 @@ deploy:
&& export TF_VAR_search_image=$(SEARCH_IMAGE_TAG) \
&& export TF_VAR_backend_image=$(BACKEND_IMAGE_TAG) \
&& export TF_VAR_app_image=$(APP_IMAGE_TAG) \
&& export TF_VAR_search_port=$(SEARCH_PORT) \
&& export TF_VAR_backend_port=$(BACKEND_PORT) \
&& export TF_VAR_open_ai=$(OPENAI) \
&& terraform apply

View file

@ -35,6 +35,9 @@ provider "kubernetes" {
)
}
#####################################################################################################
# SearXNG - Search engine deployment and service
#####################################################################################################
resource "kubernetes_deployment" "searxng" {
metadata {
name = "searxng"
@ -60,7 +63,7 @@ resource "kubernetes_deployment" "searxng" {
image = var.search_image
name = "searxng-container"
port {
container_port = 8080
container_port = var.search_port
}
}
}
@ -80,14 +83,88 @@ resource "kubernetes_service" "searxng_service" {
}
port {
port = 8080
target_port = 8080
port = var.search_port
target_port = var.search_port
}
type = "LoadBalancer"
}
}
#####################################################################################################
# Perplexica - backend deployment and service
#####################################################################################################
resource "kubernetes_deployment" "backend" {
metadata {
name = "backend"
labels = {
app = "backend"
}
}
spec {
replicas = 1
selector {
match_labels = {
component = "backend"
}
}
template {
metadata {
labels = {
component = "backend"
}
}
spec {
container {
image = var.backend_image
name = "backend-container"
port {
container_port = var.backend_port
}
env {
# searxng service ip
name = "SEARXNG_API_URL"
value = "http://${kubernetes_service.searxng_service.status[0].load_balancer[0].ingress[0].ip}:${var.search_port}"
}
env {
# openai key
name = "OPENAI"
value = var.open_ai
}
env {
# port
name = "PORT"
value = var.backend_port
}
}
}
}
}
}
resource "kubernetes_service" "backend_service" {
metadata {
name = "backend-service"
namespace = "default"
}
spec {
selector = {
component = "backend"
}
port {
port = var.backend_port
target_port = var.backend_port
}
type = "LoadBalancer"
}
}
#####################################################################################################
# Variable and module definitions
#####################################################################################################
variable "project_id" {
description = "The ID of the project in which the resources will be deployed."
type = string
@ -113,7 +190,7 @@ variable "search_image" {
type = string
}
variable "backed_image" {
variable "backend_image" {
description = "Tag for the Perplexica backend image"
type = string
}
@ -123,6 +200,21 @@ variable "app_image" {
type = string
}
variable "open_ai" {
description = "OPENAI access key"
type = string
}
variable "search_port" {
description = "Port for searxng service"
type = number
}
variable "backend_port" {
description = "Port for backend service"
type = number
}
module "gke-cluster" {
source = "./gke-cluster"