diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4bdc8a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +**/node_modules +**/dist +infrastructure/data/certbot diff --git a/frontend/.gitignore b/frontend/.gitignore deleted file mode 100644 index e0a57b2..0000000 --- a/frontend/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -**/node_modules -**/dist diff --git a/infrastructure/data/nginx/app.conf b/infrastructure/data/nginx/app.conf new file mode 100644 index 0000000..194d2a8 --- /dev/null +++ b/infrastructure/data/nginx/app.conf @@ -0,0 +1,31 @@ +server { + listen 80; + server_name decla.red; + server_tokens off; + + location /.well-known/acme-challenge/ { + root /var/www/certbot; + } + + location / { + return 301 https://$host$request_uri; + } +} + +server { + listen 443 ssl; + server_name decla.red; + server_tokens off; + + ssl_certificate /etc/letsencrypt/live/decla.red/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/decla.red/privkey.pem; + include /etc/letsencrypt/options-ssl-nginx.conf; + ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; + + location / { + proxy_pass http://frontend; + proxy_set_header Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } +} diff --git a/infrastructure/docker-compose.yml b/infrastructure/docker-compose.yml new file mode 100644 index 0000000..13b10ff --- /dev/null +++ b/infrastructure/docker-compose.yml @@ -0,0 +1,24 @@ +version: '3' + +services: + ingress: + image: nginx:alpine + restart: unless-stopped + ports: + - "80:80" + - "443:443" + command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'" + volumes: + - ./data/nginx:/etc/nginx/conf.d + - ./data/certbot/conf:/etc/letsencrypt + - ./data/certbot/www:/var/www/certbot + frontend: + image: schmelczera/declared-frontend + restart: unless-stopped + certbot: + image: certbot/certbot + restart: unless-stopped + entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'" + volumes: + - ./data/certbot/conf:/etc/letsencrypt + - ./data/certbot/www:/var/www/certbot diff --git a/infrastructure/frontend/.helmignore b/infrastructure/frontend/.helmignore deleted file mode 100644 index 0e8a0eb..0000000 --- a/infrastructure/frontend/.helmignore +++ /dev/null @@ -1,23 +0,0 @@ -# Patterns to ignore when building packages. -# This supports shell glob matching, relative path matching, and -# negation (prefixed with !). Only one pattern per line. -.DS_Store -# Common VCS dirs -.git/ -.gitignore -.bzr/ -.bzrignore -.hg/ -.hgignore -.svn/ -# Common backup files -*.swp -*.bak -*.tmp -*.orig -*~ -# Various IDEs -.project -.idea/ -*.tmproj -.vscode/ diff --git a/infrastructure/frontend/Chart.yaml b/infrastructure/frontend/Chart.yaml deleted file mode 100644 index 8d385a0..0000000 --- a/infrastructure/frontend/Chart.yaml +++ /dev/null @@ -1,5 +0,0 @@ -apiVersion: v2 -name: frontend -description: A Helm chart for Kubernetes -version: 0.0.0 -appVersion: 0.0.0 diff --git a/infrastructure/frontend/templates/_helpers.tpl b/infrastructure/frontend/templates/_helpers.tpl deleted file mode 100644 index 2dd9b88..0000000 --- a/infrastructure/frontend/templates/_helpers.tpl +++ /dev/null @@ -1,52 +0,0 @@ -{{/* vim: set filetype=mustache: */}} -{{/* -Expand the name of the chart. -*/}} -{{- define "frontend.name" -}} -{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} -{{- end }} - -{{/* -Create a default fully qualified app name. -We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). -If release name contains chart name it will be used as a full name. -*/}} -{{- define "frontend.fullname" -}} -{{- if .Values.fullnameOverride }} -{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} -{{- else }} -{{- $name := default .Chart.Name .Values.nameOverride }} -{{- if contains $name .Release.Name }} -{{- .Release.Name | trunc 63 | trimSuffix "-" }} -{{- else }} -{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} -{{- end }} -{{- end }} -{{- end }} - -{{/* -Create chart name and version as used by the chart label. -*/}} -{{- define "frontend.chart" -}} -{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} -{{- end }} - -{{/* -Common labels -*/}} -{{- define "frontend.labels" -}} -helm.sh/chart: {{ include "frontend.chart" . }} -{{ include "frontend.selectorLabels" . }} -{{- if .Chart.AppVersion }} -app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} -{{- end }} -app.kubernetes.io/managed-by: {{ .Release.Service }} -{{- end }} - -{{/* -Selector labels -*/}} -{{- define "frontend.selectorLabels" -}} -app.kubernetes.io/name: {{ include "frontend.name" . }} -app.kubernetes.io/instance: {{ .Release.Name }} -{{- end }} diff --git a/infrastructure/frontend/templates/deployment.yaml b/infrastructure/frontend/templates/deployment.yaml deleted file mode 100644 index 825b15e..0000000 --- a/infrastructure/frontend/templates/deployment.yaml +++ /dev/null @@ -1,57 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: {{ include "frontend.fullname" . }} - labels: - {{- include "frontend.labels" . | nindent 4 }} -spec: - selector: - matchLabels: - {{- include "frontend.selectorLabels" . | nindent 6 }} - template: - metadata: - {{- with .Values.podAnnotations }} - annotations: - {{- toYaml . | nindent 8 }} - {{- end }} - labels: - {{- include "frontend.selectorLabels" . | nindent 8 }} - spec: - {{- with .Values.imagePullSecrets }} - imagePullSecrets: - {{- toYaml . | nindent 8 }} - {{- end }} - securityContext: - {{- toYaml .Values.podSecurityContext | nindent 8 }} - containers: - - name: {{ .Chart.Name }} - securityContext: - {{- toYaml .Values.securityContext | nindent 12 }} - image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" - imagePullPolicy: IfNotPresent - ports: - - name: http - containerPort: 80 - protocol: TCP - livenessProbe: - httpGet: - path: / - port: http - readinessProbe: - httpGet: - path: / - port: http - resources: - {{- toYaml .Values.resources | nindent 12 }} - {{- with .Values.nodeSelector }} - nodeSelector: - {{- toYaml . | nindent 8 }} - {{- end }} - {{- with .Values.affinity }} - affinity: - {{- toYaml . | nindent 8 }} - {{- end }} - {{- with .Values.tolerations }} - tolerations: - {{- toYaml . | nindent 8 }} - {{- end }} diff --git a/infrastructure/frontend/templates/hpa.yaml b/infrastructure/frontend/templates/hpa.yaml deleted file mode 100644 index 49f1c39..0000000 --- a/infrastructure/frontend/templates/hpa.yaml +++ /dev/null @@ -1,22 +0,0 @@ -apiVersion: autoscaling/v2beta1 -kind: HorizontalPodAutoscaler -metadata: - name: {{ include "frontend.fullname" . }} - labels: - {{- include "frontend.labels" . | nindent 4 }} -spec: - scaleTargetRef: - apiVersion: apps/v1 - kind: Deployment - name: {{ include "frontend.fullname" . }} - minReplicas: {{ .Values.autoscaling.minReplicas }} - maxReplicas: {{ .Values.autoscaling.maxReplicas }} - metrics: - - type: Resource - resource: - name: cpu - targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} - - type: Resource - resource: - name: memory - targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} diff --git a/infrastructure/frontend/templates/service.yaml b/infrastructure/frontend/templates/service.yaml deleted file mode 100644 index 708b0ed..0000000 --- a/infrastructure/frontend/templates/service.yaml +++ /dev/null @@ -1,15 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: {{ include "frontend.fullname" . }} - labels: - {{- include "frontend.labels" . | nindent 4 }} -spec: - type: {{ .Values.service.type }} - ports: - - port: {{ .Values.service.port }} - targetPort: http - protocol: TCP - name: http - selector: - {{- include "frontend.selectorLabels" . | nindent 4 }} diff --git a/infrastructure/frontend/templates/tests/test-connection.yaml b/infrastructure/frontend/templates/tests/test-connection.yaml deleted file mode 100644 index d95398c..0000000 --- a/infrastructure/frontend/templates/tests/test-connection.yaml +++ /dev/null @@ -1,15 +0,0 @@ -apiVersion: v1 -kind: Pod -metadata: - name: "{{ include "frontend.fullname" . }}-test-connection" - labels: - {{- include "frontend.labels" . | nindent 4 }} - annotations: - "helm.sh/hook": test-success -spec: - containers: - - name: wget - image: busybox - command: ['wget'] - args: ['{{ include "frontend.fullname" . }}:{{ .Values.service.port }}'] - restartPolicy: Never diff --git a/infrastructure/frontend/values.yaml b/infrastructure/frontend/values.yaml deleted file mode 100644 index de32314..0000000 --- a/infrastructure/frontend/values.yaml +++ /dev/null @@ -1,44 +0,0 @@ -image: - repository: schmelczera/declared-frontend - tag: "" - -imagePullSecrets: [] -nameOverride: "" -fullnameOverride: "" - -podAnnotations: {} - -podSecurityContext: {} - # fsGroup: 2000 - -securityContext: {} - # capabilities: - # drop: - # - ALL - # readOnlyRootFilesystem: true - # runAsNonRoot: true - # runAsUser: 1000 - -service: - type: ClusterIP - port: 80 - -resources: - limits: - cpu: 100m - memory: 128Mi - requests: - cpu: 100m - memory: 128Mi - -autoscaling: - minReplicas: 2 - maxReplicas: 100 - targetCPUUtilizationPercentage: 70 - targetMemoryUtilizationPercentage: 80 - -nodeSelector: {} - -tolerations: [] - -affinity: {} diff --git a/infrastructure/ingress.md b/infrastructure/ingress.md new file mode 100644 index 0000000..c91ff1b --- /dev/null +++ b/infrastructure/ingress.md @@ -0,0 +1,25 @@ +# How to configure ingress from digital ocean docker one-click app + +## iptables (remote) + +```sh +iptables -L ufw-user-input +iptables -D ufw-user-input 4 +iptables -D ufw-user-input 4 +iptables -I ufw-user-input -p tcp --dport 80 -j ACCEPT +iptables -I ufw-user-input -p tcp --dport 443 -j ACCEPT +``` +> Disable Docker ports and enable HTTP(S) ports + +## Other (remote) + +```sh +apt update && apt upgrade -y +rm -rf /etc/update-motd.d/99-one-click +``` + +## Copy (local) + +```sh +scp -r infrastructure root@164.90.220.12:/root/infra +``` \ No newline at end of file diff --git a/infrastructure/init-letsencrypt.sh b/infrastructure/init-letsencrypt.sh new file mode 100644 index 0000000..6a0138a --- /dev/null +++ b/infrastructure/init-letsencrypt.sh @@ -0,0 +1,85 @@ +#!/bin/bash + +# Source https://medium.com/@pentacent/nginx-and-lets-encrypt-with-docker-in-less-than-5-minutes-b4b8a60d3a71 + +DOCKER_HOST=ssh://root@decla.red + +if ! [ -x "$(command -v docker-compose)" ]; then + echo 'Error: docker-compose is not installed.' >&2 + exit 1 +fi + +domains=(decla.red) +rsa_key_size=4096 +data_path="./data/certbot" +email="schmelczerandras@gmail.com" # Adding a valid address is strongly recommended +staging=0 # Set to 1 if you're testing your setup to avoid hitting request limits + +if [ -d "$data_path" ]; then + read -p "Existing data found for $domains. Continue and replace existing certificate? (y/N) " decision + if [ "$decision" != "Y" ] && [ "$decision" != "y" ]; then + exit + fi +fi + + +if [ ! -e "$data_path/conf/options-ssl-nginx.conf" ] || [ ! -e "$data_path/conf/ssl-dhparams.pem" ]; then + echo "### Downloading recommended TLS parameters ..." + mkdir -p "$data_path/conf" + curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf > "$data_path/conf/options-ssl-nginx.conf" + curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem > "$data_path/conf/ssl-dhparams.pem" + echo +fi + +echo "### Creating dummy certificate for $domains ..." +path="/etc/letsencrypt/live/$domains" +mkdir -p "$data_path/conf/live/$domains" +docker-compose run --rm --entrypoint "\ + openssl req -x509 -nodes -newkey rsa:1024 -days 1\ + -keyout '$path/privkey.pem' \ + -out '$path/fullchain.pem' \ + -subj '/CN=localhost'" certbot +echo + + +echo "### Starting nginx ..." +docker-compose up --force-recreate -d ingress +echo + +echo "### Deleting dummy certificate for $domains ..." +docker-compose run --rm --entrypoint "\ + rm -Rf /etc/letsencrypt/live/$domains && \ + rm -Rf /etc/letsencrypt/archive/$domains && \ + rm -Rf /etc/letsencrypt/renewal/$domains.conf" certbot +echo + + +echo "### Requesting Let's Encrypt certificate for $domains ..." +#Join $domains to -d args +domain_args="" +for domain in "${domains[@]}"; do + domain_args="$domain_args -d $domain" +done + + +# Select appropriate email arg +case "$email" in + "") email_arg="--register-unsafely-without-email" ;; + *) email_arg="--email $email" ;; +esac + +# Enable staging mode if needed +if [ $staging != "0" ]; then staging_arg="--staging"; fi + +docker-compose run --rm --entrypoint "\ + certbot certonly --webroot -w /var/www/certbot \ + $staging_arg \ + $email_arg \ + $domain_args \ + --rsa-key-size $rsa_key_size \ + --agree-tos \ + --force-renewal" certbot +echo + +echo "### Reloading nginx ..." +docker-compose exec ingress ingress -s reload