This guide will walk you through the process of setting up and deploying a full-stack TypeScript application on a local Kubernetes (k3s) cluster, using Terraform for infrastructure management.
Ensure you have the following installed on your local machine:
Create the following directory structure:
project-root/
├── frontend/
├── backend/
├── terraform/
└── scripts/
curl -sfL https://get.k3s.io | sh -
mkdir ~/.kube
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chown $USER:$USER ~/.kube/config
npx create-react-app frontend --template typescript
cd frontend
npm install axios
src/App.tsx with the provided React component code.mkdir backend && cd backend
npm init -y
npm install express pg cors
npm install --save-dev typescript @types/express @types/pg @types/cors ts-node
npx tsc --init
Update tsconfig.json with the provided configuration.
Create src/server.ts with the provided Node.js server code.
Update package.json scripts as shown in the original document.
We’ll use a PostgreSQL container in our k3s cluster. The setup will be handled by Terraform in later steps.
Create a Dockerfile in the frontend directory with the provided Dockerfile content.
Create a Dockerfile in the backend directory with the provided Dockerfile content.
Build the Docker images:
cd frontend
docker build -t frontend:latest .
cd ../backend
docker build -t backend:latest .
terraform directory, create main.tf with the provided Terraform configuration.Navigate to the project root directory.
Initialize Terraform:
cd terraform
terraform init
terraform apply
kubectl exec -it -n myapp $(kubectl get pods -n myapp -l app=postgres -o jsonpath='{.items[0].metadata.name}') -- psql -U postgres -d myapp -c "CREATE TABLE IF NOT EXISTS messages (id SERIAL PRIMARY KEY, text TEXT NOT NULL);"
Access the frontend:
Open a web browser and navigate to http://localhost:30080
Test the interaction:
If you encounter issues:
kubectl get pods -n myapp
kubectl logs -n myapp <pod-name>
kubectl port-forward -n myapp service/backend 3000:3000
Then, in another terminal:
curl http://localhost:3000/api/message
kubectl exec -it -n myapp <postgres-pod-name> -- psql -U postgres -d myapp -c "SELECT * FROM messages;"
cd terraform && terraform show
cd frontend && npm run build
cd ../backend && npm run build
cat tsconfig.json
Remember, without Ansible, you’ll need to perform these steps manually and ensure all prerequisites are installed on your system. This process requires more direct interaction but gives you more control over each step of the deployment.