Skip to content

Commit

Permalink
feat: Add Kubernetes resources for demo-app namespace, api-node servi…
Browse files Browse the repository at this point in the history
…ce, api-golang service, client-react-nginx service, and strip-api-prefixes middleware

- Added Namespace.yaml for demo-app namespace
- Added Service.yaml for api-node service
- Added Service.yaml for api-golang service
- Added Service.yaml for client-react-nginx service
- Added Middleware.yaml for strip-api-prefixes middleware
  • Loading branch information
Pradumnasaraf committed Sep 10, 2024
1 parent 526ff75 commit 24fdbf1
Show file tree
Hide file tree
Showing 21 changed files with 527 additions and 3 deletions.
27 changes: 24 additions & 3 deletions docs/docker/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,20 @@ Docker is an open-source platform designed to simplify the development, deployme
- `CMD` (command to run when the container starts)
- `WORKDIR` (create a directory where all the files will be copied and used)

To build an image from the **Dockerfile**, use this command:
### Docker Build Architecture

We can build the image in two ways single architecture or multi-architecture. In the single architecture, we can build the image for a specific architecture, and in multi-architecture, we can build the image for multiple architectures.

#### Single Architecture

```bash
docker build -t <image-name> .
```

#### Multi-Architecture

```bash
docker build <path>
// docker build .
docker buildx build --platform linux/amd64,linux/arm64 -t <image-name> .
```

**Good Practice**
Expand Down Expand Up @@ -236,6 +245,18 @@ HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost/ || exit 1
```

### Container Registry

A repo - a collection of repositories. Use to store and access container images.

Some popular registries are:

- Docker Hub
- GitHub Container Registry (ghcr.io)
- Google Container Registry (gcr.io)
- Amazon Elastic Container Registry (ECR)
- Azure Container Registry (ACR)

### Private Docker Registry

We can create a registry with the official [Registry image](https://hub.docker.com/_/registry).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: demo-app
165 changes: 165 additions & 0 deletions docs/kubernetes/demo-apps/go-node-react-postgres/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
## Multi Service App

This is a simple multi-service app that consists of a React Client, a Node API, and a Golang API. The app uses a Postgresql database for data storage.

### Prerequisites

- Kubernetes Cluster
- Helm v3
- kubectl

## Layout

The following shows the layout of this directory:

```markdown
├── api-golang
│ ├── Deployment.yaml
│ ├── IngressRoute.yaml
│ ├── Secret.yml
│ └── Service.yaml
├── api-node
│ ├── Deployment.yaml
│ ├── IngressRoute.yaml
│ ├── Secret.yaml
│ └── Service.yaml
├── client-react
│ ├── ConfigMap.yaml
│ ├── Deployment.yaml
│ ├── IngressRoute.yaml
│ └── Service.yaml
├── common
│ ├── Middleware.yaml
│ ├── Namespace.yaml
└── postgresql
├── Job.db-migrator.yaml
└── Secret.db-password.yaml
```

### Setting up the APP

Make sure you are in `go-node-react-postgres` directory.

1. Create a Namespace

```bash
kubectl apply -f Namespace.yaml
```

switch to the namespace:

```bash
kubectl config set-context --current --namespace=demo-app
```

2. Install Postgresql Database using Helm

Add the bitnami repo:

```bash
helm repo add bitnami https://charts.bitnami.com/bitnami
```

Install the chart:

```bash
helm upgrade --install \
-n postgres \
postgres bitnami/postgresql \
--set auth.postgresPassword=foobarbaz \
--version 15.3.2 \
--values ./postgresql/values.yaml \
--create-namespace
```

3. Database Migration

First we need to create a secret for the database password:

```bash
kubectl apply -f postgresql/Secret.db-password.yaml
```

Then we can run the migration job:

```bash
kubectl apply -f postgresql/Job.db-migrator.yaml
```

5. Install traffic Ingress Controller

Add the traefik repo:

```bash
helm repo add traefik https://traefik.github.io/charts
```

Install the chart:

```bash
helm upgrade --install \
-n traefik \
traefik traefik/traefik \
--version 20.8.0 \
--create-namespace
```

6. Deploy a Middleware to Strop of the prefix from incoming requests

```bash
kubectl apply -f common/Middleware.yaml
```

7. Deploy the Golang API.

Before deploying, update the `IngressRoute.yaml` file to match the correct host.

```yaml
routes:
- kind: Rule
match: Host(`host/domain`) && PathPrefix(`/api/golang`)
```
Then deploy the API:
```bash
kubectl apply -f api-golang
```

8. Deploy the Node API.

Before deploying, update the `IngressRoute.yaml` file to match the correct host.

```yaml
routes:
- kind: Rule
match: Host(`host/domain`) && PathPrefix(`/api/node`)
```
Then deploy the API:
```bash
kubectl apply -f api-node
```

9. Deploy the React Client.

Before deploying, update the `IngressRoute.yaml` file to match the correct host.

```yaml
routes:
- kind: Rule
match: Host(`host/domain`) # This will root with no prefix
```
Then deploy the client:
```bash
kubectl apply -f client-react
```

Now you should be able to access the app using the host/domain you have set in the IngressRoute files.

- For Node API, you can access it using `<host>/api/node`.
- For Golang API, you can access it using `<host>/api/golang`.
- For React Client, you can access it using `<host>`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-golang
namespace: demo-app
labels:
app: api-golang
spec:
replicas: 1
selector:
matchLabels:
app: api-golang
template:
metadata:
labels:
app: api-golang
spec:
containers:
- name: api-golang
image: sidpalas/devops-directive-docker-course-api-golang:foobarbaz
env:
- name: PORT
value: "8000"
envFrom:
- secretRef:
name: api-golang-database-url
ports:
- containerPort: 8000
protocol: TCP
readinessProbe:
httpGet:
path: /ping
port: 8000
resources:
limits:
memory: "100Mi"
requests:
memory: "100Mi"
cpu: "50m"
securityContext:
allowPrivilegeEscalation: false
privileged: false
securityContext:
seccompProfile:
type: RuntimeDefault
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: api-golang
namespace: demo-app
spec:
entryPoints:
- web
routes:
- kind: Rule
match: Host(`d11ae95f-7695-4635-9637-13829bf55c83.lb.civo.com`) && PathPrefix(`/api/golang`)
middlewares:
- name: strip-api-prefixes
services:
- kind: Service
name: api-golang
port: 8000
scheme: http
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ⛔️ DONT PUT SECRET FILES IN VCS
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: api-golang-database-url
namespace: demo-app
stringData:
DATABASE_URL: postgres://postgres:[email protected]:5432/postgres
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: api-golang
namespace: demo-app
spec:
selector:
app: api-golang
ports:
- protocol: TCP
port: 8000
targetPort: 8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-node
namespace: demo-app
labels:
app: api-node
spec:
replicas: 1
selector:
matchLabels:
app: api-node
template:
metadata:
labels:
app: api-node
spec:
containers:
- name: api-node
image: sidpalas/devops-directive-docker-course-api-node:foobarbaz
env:
- name: PORT
value: "3000"
envFrom:
- secretRef:
name: api-node-database-url
ports:
- containerPort: 3000
protocol: TCP
readinessProbe:
httpGet:
path: /ping
port: 3000
resources:
limits:
memory: "100Mi"
requests:
memory: "100Mi"
cpu: "50m"
securityContext:
allowPrivilegeEscalation: false
privileged: false
securityContext:
seccompProfile:
type: RuntimeDefault
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: api-node
namespace: demo-app
spec:
entryPoints:
- web
routes:
- kind: Rule
match: Host(`d11ae95f-7695-4635-9637-13829bf55c83.lb.civo.com`) && PathPrefix(`/api/node`)
middlewares:
- name: strip-api-prefixes
services:
- kind: Service
name: api-node
port: 3000
scheme: http
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ⛔️ DONT PUT SECRET FILES IN VCS
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: api-node-database-url
namespace: demo-app
stringData:
DATABASE_URL: postgres://postgres:[email protected]:5432/postgres
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: api-node
namespace: demo-app
spec:
selector:
app: api-node
ports:
- protocol: TCP
port: 3000
targetPort: 3000
Loading

0 comments on commit 24fdbf1

Please sign in to comment.