Skip to content

Commit

Permalink
Merge pull request #68 from Rajdip019/rajdeep/k8s-local
Browse files Browse the repository at this point in the history
Rajdeep/k8s local
  • Loading branch information
Rajdip019 authored Oct 27, 2024
2 parents 2ba08c1 + d284e3e commit 91b3957
Show file tree
Hide file tree
Showing 15 changed files with 519 additions and 97 deletions.
Binary file modified .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ data
.env
.env.local
private_key.pem
.errorviz-version
.errorviz-version
skaffold.generated.yaml
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"chrono",
"deks",
"dotenv",
"flexauth",
"inhouse",
"jsonwebtoken",
"lettre",
Expand Down
52 changes: 11 additions & 41 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

ARG RUST_VERSION=1.77
ARG APP_NAME=inhouse-auth

Expand All @@ -16,19 +10,21 @@ FROM rust:${RUST_VERSION}-alpine AS dev
# Set the working directory
WORKDIR /app

# Install system dependencies
RUN apk add --no-cache musl-dev

# Install OpenSSL development libraries
RUN apk add --no-cache pkgconfig openssl-dev

# -lssl -lcrypto are required for the openssl crate
RUN apk add --no-cache openssl-libs-static
# Install system dependencies and required libraries for the build
RUN apk add --no-cache musl-dev pkgconfig openssl-dev openssl-libs-static

# Install cargo-watch for auto-reloading
RUN cargo install cargo-watch

# Copy the source code into the container
# Copy the Cargo.toml and Cargo.lock files separately to cache dependencies
COPY Cargo.toml Cargo.lock ./

# Create a dummy source file and build dependencies to cache them
RUN mkdir src && echo "fn main() {}" > src/main.rs
RUN cargo build --release || true
RUN rm -rf src

# Copy the actual source code
COPY . .

# Mount the source code into the container
Expand Down Expand Up @@ -95,29 +91,3 @@ EXPOSE ${PORT}

# What the container should run when it is started.
CMD ["/bin/server"]


################################################################################
# Docker File for SMTP Server

FROM ubuntu:latest AS smtp

ARG EMAIL
ARG EMAIL_PASSWORD
ARG MAIL_NAME
ARG SMTP_DOMAIN
ARG SMTP_PORT

RUN apt-get update && \
apt-get install -y mailutils && \
apt install -y postfix

COPY /main.cf /etc/postfix/main.cf

RUN sh -c 'echo "root: ${EMAIL}" >> /etc/aliases' && \
sh -c 'echo "${MAIL_NAME}" >> /etc/mailname' && \
sh -c 'echo "[${SMTP_DOMAIN}]:${SMTP_PORT} ${EMAIL}:${EMAIL_PASSWORD}" >> /etc/postfix/sasl_passwd' && \
postmap /etc/postfix/sasl_passwd && \
chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db

CMD service postfix restart && tail -f /dev/null
22 changes: 22 additions & 0 deletions Dockerfile.smtp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Docker File for SMTP Server
FROM ubuntu:latest AS smtp

ARG EMAIL
ARG EMAIL_PASSWORD
ARG MAIL_NAME
ARG SMTP_DOMAIN
ARG SMTP_PORT

RUN apt-get update && \
apt-get install -y mailutils && \
apt install -y postfix

COPY /main.cf /etc/postfix/main.cf

RUN sh -c 'echo "root: ${EMAIL}" >> /etc/aliases' && \
sh -c 'echo "${MAIL_NAME}" >> /etc/mailname' && \
sh -c 'echo "[${SMTP_DOMAIN}]:${SMTP_PORT} ${EMAIL}:${EMAIL_PASSWORD}" >> /etc/postfix/sasl_passwd' && \
postmap /etc/postfix/sasl_passwd && \
chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db

CMD service postfix restart && tail -f /dev/null
4 changes: 3 additions & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ services:
- mongodb
- smtp-server
environment:
MONGO_URI: mongodb://admin:admin@mongodb:27017
MONGO_URI: mongodb
SERVER_KEK: ${SERVER_KEK}
EMAIL: ${EMAIL}
EMAIL_PASSWORD: ${EMAIL_PASSWORD}
MAIL_NAME: ${MAIL_NAME}
SMTP_DOMAIN: ${SMTP_DOMAIN}
SMTP_PORT: ${SMTP_PORT}
X_API_KEY: ${X_API_KEY}
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
volumes:
- ./src:/app/src

Expand Down
72 changes: 68 additions & 4 deletions docs/local-setup/readme.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,75 @@
## Local Setup
## Local Setup Kubernets

### Step 1: Pre-requisites

- [Rust Basics](https://doc.rust-lang.org/book/)
- [Cargo (Rust package manager)](https://doc.rust-lang.org/cargo/getting-started/installation.html)
- [Docker (For containerization)](https://docs.docker.com/get-docker/)
- [MongoDB Compass (For Visualising DB with GUI)](https://www.mongodb.com/try/download/compass)
- [Kubernets](https://kubernetes.io/)
- [Minikube (For Kubernets)](https://minikube.sigs.k8s.io/docs/start/?arch=/macos/arm64/stable/binary+download)

### Step 2: Environment Variables

Run this command to start setting up the environment👇
```
make setup
```
It will automatically start asking you all the required environment variables it needs and automatically create the environment variables it can.

#### Here are some of the variables you need and here's how to get them 👇
You can generate `SERVER_KEK` by running the command below from the root of your project. ( Make sure you have cargo installed )

```
cargo run --bin create_kek
```
For testing purposes only you can use this SERVER_KEK as well: **9628177f62a03f5db4742273b915bf66.a21a897aa750**

SMTP servers require authentication to ensure that only authorized users can send emails. For generating `EMAIL_PASSWORD`, Visit this [link](https://support.google.com/mail/thread/205453566/how-to-generate-an-app-password?hl=en).

`SMTP_DOMAIN = smtp.gmail.com` as we are using GMAIL as a Mail Provider.

For MongoDB username and password, you can use anything you want. But remember you need the same username and password for Mongo Compass mentioned in step 4.

### Step 3: Spinning up kubernetes

Now it's time to spin up the kubernets cluster by running this following command (Make sure you have Docker installed)

```
make flexauth-up-k8s
```

This command will build all your local docker files using `skaffold` and then spin up the kubernets cluster. Once the pods are up and running it will start streamung the flexauth server logs to the terminal You can see the kubernetes deployment configs in the `k8s/local` file.

**Tunneling** : If you want to tunnel the API and the Mongo express server make sure to run below command there so that you can reach the services by from your localhost.
```
minikube tunnel
```

Then you will be able to see your servers are running at the following addresses:

**Flexauth server address:** `http://127.0.0.1:8080`

**Mongo-express address:** `http://127.0.0.1:8081`

Once done to shut down the cluster we need to run the command below
```
flexauth-down-k8s
```

**Note:** Killing the terminal that serving logs or minikube server doesn't make the kubernetes cluser down. So, make sure to run the command.

Congrats, Your Local Setup is done successfully.


## Local Setup Docker

### Step 1: Pre-requisites

- [Rust Basics](https://doc.rust-lang.org/book/)
- [Cargo (Rust package manager)](https://doc.rust-lang.org/cargo/getting-started/installation.html)
- [Docker (For containerization)](https://docs.docker.com/get-docker/)
- [MongoDB Compass (For Visualizing DB with GUI)](https://www.mongodb.com/try/download/compass)

### Step 2: Environment Variables

Expand Down Expand Up @@ -35,12 +99,12 @@ Now it's time to run the docker container by running this following command (Mak

If you want to build the containers and then want to spin them up use this command 👇
```
make build-run-server
flexauth-build-up-docker
```

Otherwise, you can use
```
make run-server
make flexauth-up-docker
```

This command will start the container and watch the /src folder for any changes. If there are any modifications to the content inside /src, the container will automatically hot reload to reflect those changes.
Expand Down
7 changes: 7 additions & 0 deletions k8s/local/flexauth-config-map.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: flexauth-config
namespace: flexauth
data:
database_url: mongodb-service
94 changes: 94 additions & 0 deletions k8s/local/flexauth-service-depl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: flexauth-server
namespace: flexauth
labels:
app: flexauth-server
spec:
replicas: 1
selector:
matchLabels:
app: flexauth-server
template:
metadata:
labels:
app: flexauth-server
spec:
containers:
- name: flexauth-server
image: flexauth-server:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
env:
- name: MONGO_URI
valueFrom:
configMapKeyRef:
name: flexauth-config
key: database_url
- name: PORT
valueFrom:
secretKeyRef:
name: flexauth-secrets
key: PORT
- name: SERVER_KEK
valueFrom:
secretKeyRef:
name: flexauth-secrets
key: SERVER_KEK
- name: EMAIL
valueFrom:
secretKeyRef:
name: flexauth-secrets
key: EMAIL
- name: EMAIL_PASSWORD
valueFrom:
secretKeyRef:
name: flexauth-secrets
key: EMAIL_PASSWORD
- name: MAIL_NAME
valueFrom:
secretKeyRef:
name: flexauth-secrets
key: MAIL_NAME
- name: SMTP_DOMAIN
valueFrom:
secretKeyRef:
name: flexauth-secrets
key: SMTP_DOMAIN
- name: SMTP_PORT
valueFrom:
secretKeyRef:
name: flexauth-secrets
key: SMTP_PORT
- name: X_API_KEY
valueFrom:
secretKeyRef:
name: flexauth-secrets
key: X_API_KEY
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: flexauth-secrets
key: MONGO_INITDB_ROOT_PASSWORD
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: flexauth-secrets
key: MONGO_INITDB_ROOT_USERNAME

---
apiVersion: v1
kind: Service
metadata:
name: flexauth-service
namespace: flexauth
spec:
type: LoadBalancer
selector:
app: flexauth-server
ports:
- protocol: TCP
port: 8080
targetPort: 8080
46 changes: 46 additions & 0 deletions k8s/local/mongodb-depl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb
namespace: flexauth
labels:
app: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo
ports:
- containerPort: 27017
env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: flexauth-secrets
key: MONGO_INITDB_ROOT_USERNAME
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: flexauth-secrets
key: MONGO_INITDB_ROOT_PASSWORD
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
namespace: flexauth
spec:
selector:
app: mongodb
ports:
- protocol: TCP
port: 27017
targetPort: 27017
Loading

0 comments on commit 91b3957

Please sign in to comment.