Add Kubernetes Deploy
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
electricitymap-deploy.yaml
|
||||
electricitymap-svc.yaml
|
||||
service-monitor.yaml
|
||||
@@ -0,0 +1,10 @@
|
||||
FROM python:3.11-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY requirements.txt requirements.txt
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY . .
|
||||
|
||||
CMD ["python", "electricity.py"]
|
||||
@@ -0,0 +1,52 @@
|
||||
from http.server import BaseHTTPRequestHandler
|
||||
from datetime import datetime
|
||||
import socketserver
|
||||
import requests
|
||||
import hashlib
|
||||
|
||||
|
||||
PORT = 8808
|
||||
TOKEN = 'kUp26@Zg4fv$9Pm'
|
||||
ZONE = 'FR'
|
||||
ENDPOINT = f'/v7/details/hourly/{ZONE}'
|
||||
URL = f'https://app-backend.electricitymap.org{ENDPOINT}'
|
||||
|
||||
|
||||
def create_headers():
|
||||
timestamp = str(int(datetime.now().timestamp() * 1000))
|
||||
concat = f'{TOKEN}{ENDPOINT}{timestamp}'
|
||||
signature = hashlib.sha256()
|
||||
signature.update(concat.encode('utf-8'))
|
||||
signature = signature.hexdigest()
|
||||
|
||||
headers = {
|
||||
"electricitymap-token": TOKEN,
|
||||
"x-request-timestamp": timestamp,
|
||||
"x-signature": signature,
|
||||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0"
|
||||
}
|
||||
return headers
|
||||
|
||||
def fetch_intensity():
|
||||
headers = create_headers()
|
||||
electricityMap = requests.get(URL, headers=headers)
|
||||
jsonElectricityMap = electricityMap.json()
|
||||
last_key = list(jsonElectricityMap["data"]["zoneStates"])[-1]
|
||||
|
||||
return jsonElectricityMap["data"]["zoneStates"][last_key]['co2intensity']
|
||||
|
||||
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
|
||||
|
||||
def do_GET(self):
|
||||
data = fetch_intensity()
|
||||
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
self.send_header('Content-type', 'text/plain; version=0.0.4')
|
||||
response_string = f'electricitymap_carbon_intensity{{zone="{ZONE}"}} ' + str(data)
|
||||
self.wfile.write(response_string.encode('utf-8'))
|
||||
|
||||
with socketserver.TCPServer(("", PORT), SimpleHTTPRequestHandler) as httpd:
|
||||
print("serving at port", PORT)
|
||||
httpd.serve_forever()
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: electricitymap-deployment
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app.kubernetes.io/component: exporter
|
||||
app.kubernetes.io/name: electricitymap
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app.kubernetes.io/component: exporter
|
||||
app.kubernetes.io/name: electricitymap
|
||||
spec:
|
||||
containers:
|
||||
- name: electricitymap
|
||||
image: hysio-kub-repos.artifact.hysio.mutu.dc1.smhg.adm.ths/electricitymap:1.0.0
|
||||
ports:
|
||||
- containerPort: 8808
|
||||
name: metrics
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: electricitymap-service
|
||||
labels:
|
||||
app.kubernetes.io/component: exporter
|
||||
app.kubernetes.io/name: electricitymap
|
||||
spec:
|
||||
selector:
|
||||
app.kubernetes.io/component: exporter
|
||||
app.kubernetes.io/name: electricitymap
|
||||
ports:
|
||||
- protocol: TCP
|
||||
port: 8808
|
||||
targetPort: metrics
|
||||
name: metrics
|
||||
@@ -0,0 +1 @@
|
||||
requests == 2.31.0
|
||||
@@ -0,0 +1,19 @@
|
||||
apiVersion: monitoring.coreos.com/v1
|
||||
kind: ServiceMonitor
|
||||
metadata:
|
||||
name: electricitymap-servicemonitor
|
||||
spec:
|
||||
namespaceSelector:
|
||||
matchNames:
|
||||
- kepler
|
||||
selector:
|
||||
matchLabels:
|
||||
app.kubernetes.io/component: exporter
|
||||
app.kubernetes.io/name: electricitymap
|
||||
jobLabel: app.kubernetes.io/name
|
||||
endpoints:
|
||||
- port: metrics
|
||||
interval: 30s
|
||||
path: "/"
|
||||
scheme: http
|
||||
scrapeTimeout: 15s
|
||||
Reference in New Issue
Block a user