This commit is contained in:
Victor Barba Martin 2021-11-12 18:31:53 +01:00
parent bb42e2c504
commit 72bb8ff2ec
42 changed files with 322 additions and 362 deletions

View File

@ -0,0 +1,10 @@
FROM python:3
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY pod-node-register.py .
CMD [ "python3", "pod-node-register.py"]

View File

@ -1,26 +1,27 @@
import requests
import json
from kubernetes import client, config
import boto3
import time
import os
HOSTED_ZONE=os.environ['HOSTED_ZONE']
CLUSTER_ID=os.environ['CLUSTER_ID']
POOL_ID=os.environ['POOL_ID']
LINODE_TOKEN=os.environ['LINODE_TOKEN']
TOKEN={"Authorization": "Bearer "+LINODE_TOKEN}
COTURN_DOMAIN_NAME=os.environ['COTURN_DOMAIN_NAME']
COTURN_DOMAIN_NAME=os.environ['COTURN_DOMAIN_NAME']
COTURN_DOMAIN_NAME="coturn.staging.video.jamkazam.com"
config.load_kube_config()
v1 = client.CoreV1Api()
while(True):
r = requests.get("https://api.linode.com/v4/lke/clusters/"+CLUSTER_ID+"/pools/"+POOL_ID, headers=TOKEN)
ips=[]
for node in r.json()['nodes']:
ip = requests.get("https://api.linode.com/v4/linode/instances/"+str(node['instance_id'])+"/ips", headers=TOKEN)
ips.append({'Value': ip.json()['ipv4']['public'][0]['address']})
pods = v1.list_namespaced_pod(namespace="coturn")
for i in pods.items:
node_status = v1.read_node(name=i.spec.node_name)
for adr in node_status.status.addresses:
if adr.type=="ExternalIP":
ips.append(adr.address)
print("Node IPs: "+str(ips))
client = boto3.client('route53')
response = client.change_resource_record_sets(
HostedZoneId=HOSTED_ZONE,
@ -37,4 +38,4 @@ while(True):
}
}]
})
time.sleep(60)
time.sleep(60)

View File

@ -0,0 +1,2 @@
boto3
kubernetes

View File

@ -0,0 +1,3 @@
*
!rootfs/

213
docker/coturn/Dockerfile Normal file
View File

@ -0,0 +1,213 @@
#
# Dockerfile of coturn/coturn:alpine Docker image.
#
ARG alpine_ver=3.14.2
#
# Stage 'dist-libprom' creates prometheus-client-c distribution.
#
# We compile prometheus-client-c from sources, because Alpine doesn't provide
# it as its package yet.
#
# TODO: Re-check this to be present in packages on next Alpine major version update.
# https://hub.docker.com/_/alpine
FROM alpine:${alpine_ver} AS dist-libprom
# Install tools for building.
RUN apk update \
&& apk add --no-cache \
ca-certificates cmake g++ git make curl bash\
&& update-ca-certificates
# Install prometheus-client-c build dependencies.
RUN apk add --no-cache \
libmicrohttpd-dev
# Prepare prometheus-client-c sources for building.
ARG prom_ver=0.1.3
RUN mkdir -p /build/ && cd /build/ \
&& git init \
&& git remote add origin https://github.com/digitalocean/prometheus-client-c \
&& git fetch --depth=1 origin "v${prom_ver}" \
&& git checkout FETCH_HEAD
# Build libprom.so from sources.
RUN mkdir -p /build/prom/build/ && cd /build/prom/build/ \
&& TEST=0 cmake -G "Unix Makefiles" \
-DCMAKE_INSTALL_PREFIX=/usr \
-DCMAKE_SKIP_BUILD_RPATH=TRUE \
-DCMAKE_C_FLAGS="-DPROM_LOG_ENABLE -g -O3" \
.. \
&& make
# Build libpromhttp.so from sources.
RUN mkdir -p /build/promhttp/build/ && cd /build/promhttp/build/ \
# Fix compiler warning: -Werror=incompatible-pointer-types
&& sed -i 's/\&promhttp_handler/(MHD_AccessHandlerCallback)\&promhttp_handler/' \
/build/promhttp/src/promhttp.c \
&& TEST=0 cmake -G "Unix Makefiles" \
-DCMAKE_INSTALL_PREFIX=/usr \
-DCMAKE_SKIP_BUILD_RPATH=TRUE \
-DCMAKE_C_FLAGS="-g -O3" \
.. \
&& make VERBOSE=1
# Install prometheus-client-c.
RUN LIBS_DIR=/out/$(dirname $(find /usr/ -name libc.so)) \
&& mkdir -p $LIBS_DIR/ \
&& cp -rf /build/prom/build/libprom.so \
/build/promhttp/build/libpromhttp.so \
$LIBS_DIR/ \
&& mkdir -p /out/usr/include/ \
&& cp -rf /build/prom/include/* \
/build/promhttp/include/* \
/out/usr/include/ \
# Preserve license file.
&& mkdir -p /out/usr/share/licenses/prometheus-client-c/ \
&& cp /build/LICENSE /out/usr/share/licenses/prometheus-client-c/
#
# Stage 'dist-coturn' creates Coturn distribution.
#
# https://hub.docker.com/_/alpine
FROM alpine:${alpine_ver} AS dist-coturn
ARG coturn_ver=4.5.2
# Install tools for building.
RUN apk update \
&& apk add --no-cache \
autoconf ca-certificates coreutils g++ git libtool make curl bash \
&& update-ca-certificates
# Install Coturn build dependencies.
RUN apk add --no-cache \
linux-headers \
libevent-dev \
openssl-dev \
postgresql-dev mariadb-connector-c-dev sqlite-dev \
hiredis-dev \
mongo-c-driver-dev \
libmicrohttpd-dev
# Install prometheus-client-c distribution.
COPY --from=dist-libprom /out/ /
# Download and prepare Coturn sources.
RUN curl -fL -o /tmp/coturn.tar.gz \
https://github.com/coturn/coturn/archive/${coturn_ver}.tar.gz \
&& tar -xzf /tmp/coturn.tar.gz -C /tmp/ \
&& mv /tmp/coturn-${coturn_ver} /app
WORKDIR /app/
# Use Coturn sources from Git if `coturn_git_ref` is specified.
ARG coturn_git_ref=HEAD
RUN if [ "${coturn_git_ref}" != 'HEAD' ]; then true \
&& rm -rf /app/* \
&& git init \
&& git remote add origin https://github.com/coturn/coturn \
&& git fetch --depth=1 origin "${coturn_git_ref}" \
&& git checkout FETCH_HEAD \
&& true; fi
# Build Coturn from sources.
# TODO: Remove this symlink with next Coturn release detecting MySQL libs correctly.
RUN ln -s /usr/lib/pkgconfig/libmariadb.pc /usr/lib/pkgconfig/mariadb.pc \
&& ./configure --prefix=/usr \
--turndbdir=/var/lib/coturn \
--disable-rpath \
--sysconfdir=/etc/coturn \
# No documentation included to keep image size smaller.
--mandir=/tmp/coturn/man \
--docsdir=/tmp/coturn/docs \
--examplesdir=/tmp/coturn/examples \
&& make
# Install and configure Coturn.
RUN mkdir -p /out/ \
&& DESTDIR=/out make install \
# Remove redundant files.
&& rm -rf /out/tmp/ \
# Preserve license file.
&& mkdir -p /out/usr/share/licenses/coturn/ \
&& cp LICENSE /out/usr/share/licenses/coturn/ \
# Remove default config file.
&& rm -f /out/etc/coturn/turnserver.conf.default
# Install helper tools of Docker image.
COPY docker/coturn/rootfs/ /out/
RUN chmod +x /out/usr/local/bin/docker-entrypoint.sh \
/out/usr/local/bin/detect-external-ip.sh \
/out/usr/local/bin/launch-coturn.sh
RUN ln -s /usr/local/bin/detect-external-ip.sh \
/out/usr/local/bin/detect-external-ip
#RUN chown -R nobody:nogroup /out/var/lib/coturn/
# Re-export prometheus-client-c distribution.
COPY --from=dist-libprom /out/ /out/
#
# Stage 'runtime' creates final Docker image to use in runtime.
#
# https://hub.docker.com/_/alpine
FROM alpine:${alpine_ver} AS runtime
LABEL org.opencontainers.image.source="https://github.com/coturn/coturn"
# Update system packages.
RUN apk update \
&& apk upgrade \
&& apk add --no-cache ca-certificates \
&& update-ca-certificates \
# Install Coturn dependencies.
&& apk add --no-cache \
libevent \
libcrypto1.1 libssl1.1 \
libpq mariadb-connector-c sqlite-libs \
hiredis \
mongo-c-driver \
libmicrohttpd \
# Install `dig` tool for `detect-external-ip.sh`.
&& apk add --no-cache \
bind-tools curl\
# Cleanup unnecessary stuff.
&& rm -rf /var/cache/apk/*
# Install Coturn distribution.
COPY --from=dist-coturn /out/ /
# Allow non-root using privileged ports.
RUN apk add --no-cache libcap bash \
&& setcap CAP_NET_BIND_SERVICE=+ep /usr/bin/turnserver \
# Cleanup unnecessary stuff.
&& apk del libcap \
&& rm -rf /var/cache/apk/*
# COPY docker/coturn/rootfs /
# RUN chmod +x /usr/local/bin/docker-entrypoint.sh \
# /usr/local/bin/detect-external-ip.sh \
# /usr/local/bin/launch-coturn.sh \
# && ln -s /usr/local/bin/detect-external-ip.sh \
# /usr/local/bin/detect-external-ip
RUN chown -R nobody:nogroup /var/lib/coturn/
EXPOSE 3478 3478/udp
VOLUME ["/var/lib/coturn"]
ENTRYPOINT ["/usr/local/bin/launch-coturn.sh"]
# CMD ["--log-file=stdout", "--external-ip=192.168.1.5", "--min-port", "49160", "--max-port", "49200"]

View File

@ -0,0 +1,6 @@
min-port=49152
max-port=65535
lt-cred-mech
user=username:password
realm=jamkazam.com
syslog

View File

@ -0,0 +1,6 @@
#!/bin/sh
export DOCKER_HOST_IP=`dig +short unix.stackexchange.com` > /tmp/ugh.log
# export MY_LOCAL_IP=`hostname -i` >> /tmp/ugh.log
exec echo "$DOCKER_HOST_IP"

View File

@ -0,0 +1,9 @@
#!/bin/sh
# If command starts with an option, prepend with turnserver binary.
if [ "${1:0:1}" == '-' ]; then
echo "$@"
set -- turnserver "$@"
fi
exec $(eval "echo $@")

View File

@ -0,0 +1,9 @@
#!/bin/bash
trap -- '' SIGINT SIGTERM
PUBLIC_IP=`curl --silent checkip.amazonaws.com`
turnserver --prometheus --log-file=stdout --min-port 49160 --max-port 49200 --external-ip=$PUBLIC_IP &
child=$!
wait "$child"

View File

@ -1,3 +0,0 @@
FROM meezaan/linode-k8s-autoscaler
COPY k8s/prd-video-cluster-kubeconfig.yaml /root/.kube/config

View File

@ -1,10 +0,0 @@
FROM python:3
WORKDIR /app
COPY k8s/coturn-dns/requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY k8s/coturn-dns/ .
CMD [ "python3", "register-nodes.py"]

View File

View File

@ -1,17 +0,0 @@
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: argocd
resources:
- apps.yaml
#- haproxy-ingress.yaml
- argocd.yaml
- cert-manager.yaml
- metrics-server.yaml
- external-dns.yaml
- ingress-nginx.yaml
- monitoring.yaml
- alertmanager-slack.yaml
- coturn.yaml
- webrtc-be.yaml

View File

@ -8,6 +8,9 @@ spec:
namespace: argocd
server: 'https://kubernetes.default.svc'
source:
helm:
valueFiles:
- values-{{ .Values.environment }}.yaml
path: k8s/applications
repoURL: 'git@bitbucket.org:jamkazam/video-iac.git'
targetRevision: HEAD

View File

@ -8,7 +8,7 @@ spec:
namespace: argocd
server: 'https://kubernetes.default.svc'
source:
path: k8s/argocd/overlays/staging
path: k8s/argocd/overlays/{{ .Values.environment }}
repoURL: 'git@bitbucket.org:jamkazam/video-iac.git'
targetRevision: HEAD
project: default

View File

@ -0,0 +1,29 @@
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: coturn-dns
spec:
destination:
name: ''
namespace: coturn-dns
server: 'https://kubernetes.default.svc'
source:
helm:
valueFiles:
- values-{{ .Values.environment }}.yaml
path: k8s/coturn-dns
repoURL: 'git@bitbucket.org:jamkazam/video-iac.git'
targetRevision: HEAD
project: default
syncPolicy:
syncOptions:
- CreateNamespace=true
# automated:
# prune: true
# allowEmpty: false
# retry:
# limit: 5
# backoff:
# duration: 5s
# factor: 2
# maxDuration: 3m

View File

@ -0,0 +1 @@
environment: staging

View File

View File

@ -1 +0,0 @@
#TODO

View File

@ -1,2 +0,0 @@
boto3
requests

View File

@ -1,43 +0,0 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: coturn-dns
spec:
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 0
maxUnavailable: 1
selector:
matchLabels:
app: coturn-dns
template:
metadata:
labels:
app: coturn-dns
spec:
containers:
- name: coturn-dns
image: {{coturn_dns_image}}
env:
- name: AWS_ACCESS_KEY_ID
value: "AKIA2SXEHOQFBQRGCSST"
- name: AWS_SECRET_ACCESS_KEY
value: "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"
- name: PYTHONUNBUFFERED
value: "1"
- name: HOSTED_ZONE
value: "Z00156242SK162FEXDPVF"
- name: CLUSTER_ID
value: "29062"
- name: POOL_ID
value: "49934"
- name: LINODE_TOKEN
value: "a821bb97039cbd8b259e19ef9f7ea7a4e295a7399e00709fc27cad2b1f3742f4"
resources:
requests:
memory: 32Mi
limits:
memory: 32Mi

View File

@ -20,24 +20,24 @@ spec:
spec:
containers:
- name: coturn-dns
image: {{coturn_dns_image}}
image: gcr.io/tough-craft-276813/coturn:latest
env:
- name: AWS_ACCESS_KEY_ID
value: "AKIA2SXEHOQFBQRGCSST"
valueFrom:
secretKeyRef:
name: aws-user-external-dns
key: username
- name: AWS_SECRET_ACCESS_KEY
value: "lj85CIIik/83V980VKEPfqlOWtutEM3s7bSqMZNH"
valueFrom:
secretKeyRef:
name: aws-user-external-dns
key: password
- name: PYTHONUNBUFFERED
value: "1"
- name: HOSTED_ZONE
value: "Z00156242SK162FEXDPVF"
- name: CLUSTER_ID
value: "35025"
- name: POOL_ID
value: "53760"
- name: LINODE_TOKEN
value: "a821bb97039cbd8b259e19ef9f7ea7a4e295a7399e00709fc27cad2b1f3742f4"
- name: COTURN_DOMAIN_NAME
value: "coturn.video.jamkazam.com"
value: {{ .Values.coturn-domain-name }}
resources:
requests:
memory: 32Mi

View File

@ -0,0 +1 @@
coturn-domain-name: "coturn.staging.video.jamkazam.com"

3
k8s/coturn/Chart.yaml Normal file
View File

@ -0,0 +1,3 @@
apiVersion: v2
name: coturn
version: '1.0'

View File

@ -1,7 +1,7 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: coturn-deployment
name: coturn
labels:
app: coturn
spec:
@ -14,17 +14,12 @@ spec:
labels:
app: coturn
spec:
nodeSelector:
lke.linode.com/pool-id:
configMapKeyRef:
name: linode-pool
key: pool
hostNetwork: true
imagePullSecrets:
- name: gcr-json-key
containers:
- name: coturn
image: gcr.io/tough-craft-276813/coturn:prod-0.1.83
image: gcr.io/tough-craft-276813/coturn:latest
ports:
- containerPort: 3478
name: coturn

View File

@ -12,7 +12,6 @@
- --provider=aws
- --registry=txt
- --source=ingress
- --source=service
- --txt-prefix=fmifrruf_ # Random string for hardener TXT entries
- op: replace
path: /spec/template/spec/containers/0/env

File diff suppressed because one or more lines are too long

View File

@ -1,61 +0,0 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: coturn-autoscale
namespace: linode-autoscaler
spec:
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: coturn-autoscale
template:
metadata:
labels:
app: coturn-autoscale
spec:
imagePullSecrets:
- name: gcr-json-key
containers:
- name: coturn-autoscale
image: {{linode_autoscaler_image}} ####### CHANGE THIS TO YOUR ACTUAL DOCKER IMAGE
env:
- name: LINODE_PERSONAL_ACCCESS_TOKEN
value: "a821bb97039cbd8b259e19ef9f7ea7a4e295a7399e00709fc27cad2b1f3742f4"
# valueFrom:
# secretKeyRef:
# name: linode-personal-access-token-k8s-autoscaler ####### LINODE PERSONAL ACCESS TOKEN SECRET
# key: token
- name: LINODE_LKE_CLUSTER_ID
value: "35025"
- name: LINODE_LKE_CLUSTER_POOL_ID
value: "53760"
- name: AUTOSCALE_TRIGGER
value: "cpu"
- name: AUTOSCALE_TRIGGER_TYPE
value: "used"
- name: AUTOSCALE_UP_PERCENTAGE
value: "60"
- name: AUTOSCALE_DOWN_PERCENTAGE
value: "30"
- name: AUTOSCALE_QUERY_INTERVAL
value: "10"
- name: AUTOSCALE_THRESHOLD_COUNT
value: "3"
- name: AUTOSCALE_NUMBER_OF_NODES
value: "1"
- name: AUTOSCALE_WAIT_TIME_AFTER_SCALING
value: "180"
- name: LINODE_LKE_CLUSTER_POOL_MINIMUM_NODES
value: "3"
resources:
requests:
memory: 32Mi
limits:
memory: 32Mi

View File

@ -1,60 +0,0 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: webrtc-be-autoscale
namespace: linode-autoscaler
spec:
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: webrtc-be-autoscale
template:
metadata:
labels:
app: webrtc-be-autoscale
spec:
imagePullSecrets:
- name: gcr-json-key
containers:
- name: webrtc-be-autoscale
image: {{linode_autoscaler_image}} ####### CHANGE THIS TO YOUR ACTUAL DOCKER IMAGE
env:
- name: LINODE_PERSONAL_ACCCESS_TOKEN
value: "a821bb97039cbd8b259e19ef9f7ea7a4e295a7399e00709fc27cad2b1f3742f4"
# valueFrom:
# secretKeyRef:
# name: linode-personal-access-token-k8s-autoscaler ####### LINODE PERSONAL ACCESS TOKEN SECRET
# key: token
- name: LINODE_LKE_CLUSTER_ID
value: "29062"
- name: LINODE_LKE_CLUSTER_POOL_ID
value: "53759"
- name: AUTOSCALE_TRIGGER
value: "cpu"
- name: AUTOSCALE_TRIGGER_TYPE
value: "used"
- name: AUTOSCALE_UP_PERCENTAGE
value: "60"
- name: AUTOSCALE_DOWN_PERCENTAGE
value: "30"
- name: AUTOSCALE_QUERY_INTERVAL
value: "10"
- name: AUTOSCALE_THRESHOLD_COUNT
value: "3"
- name: AUTOSCALE_NUMBER_OF_NODES
value: "1"
- name: AUTOSCALE_WAIT_TIME_AFTER_SCALING
value: "180"
- name: LINODE_LKE_CLUSTER_POOL_MINIMUM_NODES
value: "3"
resources:
requests:
memory: 32Mi
limits:
memory: 32Mi

View File

@ -1,60 +0,0 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: coturn-autoscale
namespace: linode-autoscaler
spec:
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: coturn-autoscale
template:
metadata:
labels:
app: coturn-autoscale
spec:
imagePullSecrets:
- name: gcr-json-key
containers:
- name: coturn-autoscale
image: {{linode_autoscaler_image}} ####### CHANGE THIS TO YOUR ACTUAL DOCKER IMAGE
env:
- name: LINODE_PERSONAL_ACCCESS_TOKEN
value: "a821bb97039cbd8b259e19ef9f7ea7a4e295a7399e00709fc27cad2b1f3742f4"
# valueFrom:
# secretKeyRef:
# name: linode-personal-access-token-k8s-autoscaler ####### LINODE PERSONAL ACCESS TOKEN SECRET
# key: token
- name: LINODE_LKE_CLUSTER_ID
value: "35025"
- name: LINODE_LKE_CLUSTER_POOL_ID
value: "47218"
- name: AUTOSCALE_TRIGGER
value: "cpu"
- name: AUTOSCALE_TRIGGER_TYPE
value: "used"
- name: AUTOSCALE_UP_PERCENTAGE
value: "60"
- name: AUTOSCALE_DOWN_PERCENTAGE
value: "30"
- name: AUTOSCALE_QUERY_INTERVAL
value: "10"
- name: AUTOSCALE_THRESHOLD_COUNT
value: "3"
- name: AUTOSCALE_NUMBER_OF_NODES
value: "1"
- name: AUTOSCALE_WAIT_TIME_AFTER_SCALING
value: "180"
- name: LINODE_LKE_CLUSTER_POOL_MINIMUM_NODES
value: "1"
resources:
requests:
memory: 32Mi
limits:
memory: 32Mi

View File

@ -1,60 +0,0 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: webrtc-be-autoscale
namespace: linode-autoscaler
spec:
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: webrtc-be-autoscale
template:
metadata:
labels:
app: webrtc-be-autoscale
spec:
imagePullSecrets:
- name: gcr-json-key
containers:
- name: webrtc-be-autoscale
image: {{linode_autoscaler_image}} ####### CHANGE THIS TO YOUR ACTUAL DOCKER IMAGE
env:
- name: LINODE_PERSONAL_ACCCESS_TOKEN
value: "a821bb97039cbd8b259e19ef9f7ea7a4e295a7399e00709fc27cad2b1f3742f4"
# valueFrom:
# secretKeyRef:
# name: linode-personal-access-token-k8s-autoscaler ####### LINODE PERSONAL ACCESS TOKEN SECRET
# key: token
- name: LINODE_LKE_CLUSTER_ID
value: "29062"
- name: LINODE_LKE_CLUSTER_POOL_ID
value: "49934"
- name: AUTOSCALE_TRIGGER
value: "cpu"
- name: AUTOSCALE_TRIGGER_TYPE
value: "used"
- name: AUTOSCALE_UP_PERCENTAGE
value: "60"
- name: AUTOSCALE_DOWN_PERCENTAGE
value: "30"
- name: AUTOSCALE_QUERY_INTERVAL
value: "10"
- name: AUTOSCALE_THRESHOLD_COUNT
value: "3"
- name: AUTOSCALE_NUMBER_OF_NODES
value: "1"
- name: AUTOSCALE_WAIT_TIME_AFTER_SCALING
value: "180"
- name: LINODE_LKE_CLUSTER_POOL_MINIMUM_NODES
value: "1"
resources:
requests:
memory: 32Mi
limits:
memory: 32Mi