Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
mmohamed committed Apr 7, 2020
2 parents 64eba15 + 727cd0e commit d1005c1
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ mvn install && mvn spring-boot:run -pl service
$ npm -v
6.12.1


$ node -v
v12.13.1
41 changes: 41 additions & 0 deletions k8s/agent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
# nohup sh agent.sh [NODE-NAME] [YOUR-SECURITY-TOKEN] > /tmp/agent.log
if [ -z "$1" ]; then
echo "Node name required !"
exit 1
fi

if [ -z "$2" ]; then
echo "Security Token required !"
exit 1
fi

attempts=0
server="http[s]://[YOUR-API-BACKEN-URL]/k8s/collect/$1/temperature"

while true; do

temperature=$(cat /sys/class/thermal/thermal_zone0/temp)

if [ $? != 0 ] || [ -z "$temperature" ]; then
echo "Unable to determinate CPU temperature value !"
exit 1
fi

url="$server?node=$2&value=$temperature"

responseCode=$(curl --silent --output /dev/null --write-out "%{http_code}" $url)

if [ $? != 0 ] || [ -z "$responseCode" ] || [ $responseCode -ne 200 ]; then
attempts=$((attempts + 1))
echo "[ATTEMP-$attempts] Failed sending data to server : $responseCode"
if [ $attempts = 20 ]; then
echo "Server response error after 20 attempts !"
exit 1
fi;
else
attempts=0
fi

sleep 5
done;
182 changes: 182 additions & 0 deletions k8s/api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: monitoring

---
apiVersion: v1
kind: Secret
metadata:
name: monitoring-tls
namespace: monitoring
data:
tls.crt: {{crt}}
tls.key: {{key}}
type: kubernetes.io/tls

---
apiVersion: v1
kind: Secret
metadata:
name: monitoring-token
namespace: monitoring
type: Opaque
data:
security.token: {{encodedtoken}}

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
name: monitoring-ingress
namespace: monitoring
labels:
app: api
spec:
rules:
- host: {{host}}
http:
paths:
- backend:
serviceName: monitoring-service
servicePort: http
path: /
tls:
- hosts:
- {{host}}
secretName: monitoring-tls

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
namespace: monitoring
labels:
app: api
spec:
replicas: 1
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
commit: '{{commit}}'
spec:
serviceAccountName: api-access
containers:
- name: api
image: medinvention/k8s-monitoring-api:arm
imagePullPolicy: Always
env:
- name: COLLECTOR_TOKEN
valueFrom:
secretKeyRef:
name: monitoring-token
key: security.token
- name: FAN_SERVER_URL
value: 'http://192.168.1.85:5000'
- name: FAN_MAXTEMP
value: '70'
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /k8s/actuator/health
port: 8080
initialDelaySeconds: 60
timeoutSeconds: 2
periodSeconds: 3
failureThreshold: 1
livenessProbe:
httpGet:
path: /k8s/actuator/health
port: 8080
initialDelaySeconds: 300
timeoutSeconds: 5
periodSeconds: 60
failureThreshold: 1

---
apiVersion: v1
kind: Service
metadata:
name: monitoring-service
namespace: monitoring
spec:
ports:
- name: http
port: 80
targetPort: 8080
selector:
app: api

---
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
name: api-access
namespace: monitoring

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: api-access
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: api-access
namespace: monitoring

---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: monitoring-agent
namespace: monitoring
labels:
k8s-app: monitoring-agent
spec:
selector:
matchLabels:
name: monitoring-agent
template:
metadata:
labels:
name: monitoring-agent
commit: '{{commit}}'
spec:
tolerations:
# this toleration is to have the daemonset runnable on master nodes
# remove it if your masters can't run pods
- key: node-role.kubernetes.io/master
effect: NoSchedule
containers:
- name: monitoring-agent
image: busybox
env:
- name: NODE
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: SERVER
value: http://monitoring-service.monitoring.svc.cluster.local/k8s/collect/{{token}}/temperature
command: [ "sh", "-c"]
args:
- while true; do
TEMP=$(cat /sys/class/thermal/thermal_zone0/temp);
URL="$SERVER?node=$NODE&value=$TEMP";
wget -qO- $URL;
sleep 5;
done;
imagePullPolicy: IfNotPresent
74 changes: 74 additions & 0 deletions k8s/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/bin/sh

if [ -z "$CRT" ] || [ -z "$KEY" ]; then
echo "TLS CRT/KEY environment value not found !"
exit 1
fi

if [ -z "$TOKEN" ]; then
echo "Kube Token environment value not found !"
exit 1
fi

if [ -z "$COLLECTORTOKEN" ]; then
echo "Collector Token environment value not found !"
exit 1
fi

echo "Get Kubectl"
curl -s -LO https://storage.googleapis.com/kubernetes-release/release/v1.17.0/bin/linux/arm/kubectl
chmod +x ./kubectl

commitID=$(git log -1 --pretty="%H")

if [ $? != 0 ] || [ -z "$commitID" ]; then
echo "Unable to determinate CommitID !"
exit 1
fi

echo "Deploy for CommitID : ${commitID}"

ENCODEDCOLLECTORTOKEN=$(echo -n $COLLECTORTOKEN | base64)
# create new deploy
sed -i "s|{{crt}}|`echo $CRT`|g" api.yaml
sed -i "s|{{key}}|`echo $KEY`|g" api.yaml
sed -i "s|{{token}}|`echo $COLLECTORTOKEN`|g" api.yaml
sed -i "s|{{encodedtoken}}|`echo $ENCODEDCOLLECTORTOKEN`|g" api.yaml
sed -i "s|{{host}}|api-monitoring.medinvention.dev|g" api.yaml
sed -i "s|{{commit}}|`echo $commitID`|g" api.yaml

./kubectl --token=$TOKEN apply -f api.yaml
if [ $? != 0 ]; then
echo "Unable to deploy API !"
exit 1
fi

# wait for ready
attempts=0
rolloutStatusCmd="./kubectl --token=$TOKEN rollout status deployment/api -n monitoring"
until $rolloutStatusCmd || [ $attempts -eq 60 ]; do
$rolloutStatusCmd
attempts=$((attempts + 1))
sleep 10
done

# create new deploy
sed -i "s|{{crt}}|`echo $CRT`|g" front.yaml
sed -i "s|{{key}}|`echo $KEY`|g" front.yaml
sed -i "s|{{host}}|monitoring.medinvention.dev|g" front.yaml
sed -i "s|{{commit}}|`echo $commitID`|g" front.yaml

./kubectl --token=$TOKEN apply -f front.yaml
if [ $? != 0 ]; then
echo "Unable to deploy Front !"
exit 1
fi

# wait for ready
attempts=0
rolloutStatusCmd="./kubectl --token=$TOKEN rollout status deployment/front -n monitoring"
until $rolloutStatusCmd || [ $attempts -eq 60 ]; do
$rolloutStatusCmd
attempts=$((attempts + 1))
sleep 10
done
74 changes: 74 additions & 0 deletions k8s/front.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
apiVersion: v1
kind: Secret
metadata:
name: front-tls
namespace: monitoring
type: Opaque
data:
tls.crt: {{crt}}
tls.key: {{key}}
type: kubernetes.io/tls

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
name: front
namespace: monitoring
labels:
app: front
spec:
rules:
- host: {{host}}
http:
paths:
- backend:
serviceName: front-service
servicePort: http
path: /
tls:
- hosts:
- {{host}}
secretName: front-tls

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: front
namespace: monitoring
labels:
app: front
spec:
replicas: 1
selector:
matchLabels:
app: front
template:
metadata:
labels:
app: front
commit: '{{commit}}'
spec:
containers:
- name: front
image: medinvention/k8s-monitoring-front:arm
imagePullPolicy: Always
ports:
- containerPort: 80

---
apiVersion: v1
kind: Service
metadata:
name: front-service
namespace: monitoring
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: front
Loading

0 comments on commit d1005c1

Please sign in to comment.