Skip to content

Commit

Permalink
Merge pull request #157 from 0xEmma/master
Browse files Browse the repository at this point in the history
Add K8S Deployment & GHCR Actions to Auto-Build
  • Loading branch information
sergiotapia committed Apr 15, 2023
2 parents 3146b81 + fbd50da commit fbc0baf
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 1 deletion.
50 changes: 50 additions & 0 deletions .github/workflows/ghcr-image.yml
@@ -0,0 +1,50 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.

name: Create and publish a Docker image

on:
push:
branches: ['master']

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -41,6 +41,23 @@ SECRET_KEY_BASE=YourKeyGoesHere
docker compose up -d
```

**Kubernetes**
```
# Generate a secret_key with either
openssl rand -base64 64
mix phx.gen.secret
Edit the k8s/deployment.yaml file, and replace the ENV SECRET_KEY_BASE with your generated key.
Edit k8s/ingress.yaml & the PHX_HOST in k8s/deployment.yaml and replace the host with your domain name.
The Postgres database is configured to use the DNS name assuming it is being deployed to the default namespace, update this in the enviroment variables if you are deploying to a different namespace.
Finaly, deploy the application to your cluster.
kubectl apply -f k8s/pvc.yaml
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/svc.yaml
kubectl apply -f k8s/ingress.yaml
```
**Local Development**

```
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Expand Up @@ -14,7 +14,7 @@ services:
- "pgdata:/var/lib/postgresql/data"
magnetissimo:
container_name: magnetissimo
build: https://github.com/sergiotapia/magnetissimo.git
image: ghcr.io/sergiotapia/magnetissimo:master
restart: unless-stopped
environment:
- DATABASE_URL=postgres://postgres:postgres@postgres:5432/magnetissimo
Expand Down
82 changes: 82 additions & 0 deletions k8s/deploy/deployment.yaml
@@ -0,0 +1,82 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: magnetissimo
namespace: default
labels:
app.kubernetes.io/name: magnetissimo
spec:
revisionHistoryLimit: 3
replicas: 1
strategy:
type: RollingUpdate
selector:
matchLabels:
app.kubernetes.io/name: magnetissimo
template:
metadata:
labels:
app.kubernetes.io/name: magnetissimo
spec:
dnsPolicy: ClusterFirst
enableServiceLinks: true
containers:
- image: ghcr.io/sergiotapia/magnetissimo:master
imagePullPolicy: Always
name: magnetissimo
ports:
- containerPort: 4000
readinessProbe:
httpGet:
path: /
port: 4000
initialDelaySeconds: 5
periodSeconds: 10
env:
- name: DATABASE_URL
value: "postgresql://postgres:postgres@postgres-svc.default.svc.cluster.local:5432/magnetissimo"\
- name: SECRET_KEY_BASE
value: "SECRET_KEY_HERE"
- name: PHX_HOST
value: "magnet.example.com"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: magnetissimo-postgressql
namespace: default
labels:
app.kubernetes.io/name: magnetissimo-postgressql
spec:
revisionHistoryLimit: 3
replicas: 1
strategy:
type: RollingUpdate
selector:
matchLabels:
app.kubernetes.io/name: magnetissimo-postgressql
template:
metadata:
labels:
app.kubernetes.io/name: magnetissimo-postgressql
spec:
dnsPolicy: ClusterFirst
enableServiceLinks: true
containers:
- image: postgres:15.2-alpine
name: magnetissimo-postgressql
env:
- name: POSTGRES_USER
value: postgres
- name: POSTGRES_PASSWORD
value: postgres
- name: POSTGRES_DB
value: magnetissimo
volumeMounts:
- name: magnetissimo-db
mountPath: /var/lib/postgresql/data
volumes:
- name: magnetissimo-db
persistentVolumeClaim:
claimName: magnetissimo-db
size: 5Gi
23 changes: 23 additions & 0 deletions k8s/deploy/ingress.yaml
@@ -0,0 +1,23 @@
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: magnet
namespace: default
labels:
app.kubernetes.io/name: magnet
annotations:
traefik.ingress.kubernetes.io/router.middlewares: "kube-system-traefik-forward-auth@kubernetescrd"
traeifk.ingress.kubernetes.io/router.entrypoints: "web"
spec:
rules:
- host: "magnet.example.com"
http:
paths:
- path: "/"
pathType: Prefix
backend:
service:
name: magnetissimo-svc
port:
number: 4000
10 changes: 10 additions & 0 deletions k8s/deploy/pvc.yaml
@@ -0,0 +1,10 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: magnetissimo-db
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
19 changes: 19 additions & 0 deletions k8s/deploy/svc.yaml
@@ -0,0 +1,19 @@
kind: Service
apiVersion: v1
metadata:
name: magnetissimo-svc
spec:
selector:
app.kubernetes.io/name: magnetissimo
ports:
- port: 4000
---
kind: Service
apiVersion: v1
metadata:
name: postgres-svc
spec:
selector:
app.kubernetes.io/name: magnetissimo-postgressql
ports:
- port: 5432

0 comments on commit fbc0baf

Please sign in to comment.