From 22948b65b499c284db8394c58bc8b3e57c28f915 Mon Sep 17 00:00:00 2001 From: Rafael Jardim Date: Wed, 3 Mar 2021 08:27:32 -0300 Subject: [PATCH] Add a sample NodeJs Metric Server client The sample app is a NodeJs app that requests the metrics every second and prints that in the console. In the deployment is created a service account, roles and role binding to allows this application communicate with the api, this token is stored by default on `/var/run/secrets/kubernetes.io/serviceaccount/token`, the application is reading that token and doing the requests on the endpoint `/apis/metrics.k8s.io/v1beta1/pods` that returns all pod metrics and log it on console. Story: 2008457 Task: 41954 Change-Id: Ifd56e6e6ef862515d96d6ca22c2146e382bba78c Signed-off-by: Rafael Jardim --- centos_stable_docker_images.inc | 1 + readme.md | 78 +++++++++++++++++++ sample-app/centos/docker/.dockerignore | 3 + sample-app/centos/docker/Dockerfile | 11 +++ sample-app/centos/docker/src/package.json | 14 ++++ .../centos/docker/src/sample-application.js | 52 +++++++++++++ .../centos/sample-app.stable_docker_image | 2 + sample-app/readme.md | 44 +++++++++++ 8 files changed, 205 insertions(+) create mode 100644 centos_stable_docker_images.inc create mode 100644 readme.md create mode 100644 sample-app/centos/docker/.dockerignore create mode 100644 sample-app/centos/docker/Dockerfile create mode 100644 sample-app/centos/docker/src/package.json create mode 100644 sample-app/centos/docker/src/sample-application.js create mode 100644 sample-app/centos/sample-app.stable_docker_image create mode 100644 sample-app/readme.md diff --git a/centos_stable_docker_images.inc b/centos_stable_docker_images.inc new file mode 100644 index 0000000..0f85471 --- /dev/null +++ b/centos_stable_docker_images.inc @@ -0,0 +1 @@ +sample-app diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..bc4329d --- /dev/null +++ b/readme.md @@ -0,0 +1,78 @@ +# Metrics Server Armada App + +This Armada App is responsible to deliver the metrics server inside the ISO. +>Metrics Server is a scalable, efficient source of container resource metrics for Kubernetes built-in autoscaling pipelines. + +## Structure +``` +metrics-server-armada-app +├── centos_build_layer.cfg +├── centos_iso_image.inc +├── centos_pkg_dirs +├── centos_pkg_dirs_containers +├── centos_stable_docker_images.inc +├── readme.md +├── requirements.txt +├── sample-app +│ ├── centos +│ │ ├── docker +│ │ │ ├── Dockerfile +│ │ │ └── src +│ │ │ ├── package.json +│ │ │ └── sample-application.js +│ │ └── sample-app.stable_docker_image +│ └── readme.md +├── stx-metrics-server-helm +│ ├── centos +│ │ ├── build_srpm.data +│ │ └── stx-metrics-server-helm.spec +│ └── stx-metrics-server-helm +│ ├── files +│ │ ├── index.yaml +│ │ ├── metadata.yaml +│ │ └── repositories.yaml +│ ├── helm-charts +│ │ ├── Makefile +│ │ └── metrics-server +│ │ ├── Chart.yaml +│ │ ├── templates +│ │ │ ├── apiservice.yaml +│ │ │ ├── clusterrole-aggregated-reader.yaml +│ │ │ ├── clusterrolebinding-auth-delegator.yaml +│ │ │ ├── clusterrolebinding.yaml +│ │ │ ├── clusterrole.yaml +│ │ │ ├── deployment.yaml +│ │ │ ├── pdb.yaml +│ │ │ ├── psp.yaml +│ │ │ ├── rolebinding.yaml +│ │ │ ├── serviceaccount.yaml +│ │ │ └── service.yaml +│ │ └── values.yaml +│ └── manifests +│ └── metrics-server_manifest.yaml +├── test-requirements.txt +└── tox.ini +``` +Important files +- metrics-server_manifest.yaml - Armada Manifest +- helm-charts - Metrics Server helm charts +- stx-metrics-server-helm.spec - Steps to generate +- centos_iso_image.inc - It inserts the rpm inside the ISO +- centos_pkg_dirs - Folders to build the pkgs +- sample-app - Sample app application + +## Install +- Navigate to the path `/usr/local/share/applications/helm/` +- The `metrics-server-1.0-1.tgz` will be present +- Run `system application-upload metrics-server-1.0-1.tgz` +- Run `system application-list` to see if it was uploaded +- Run `system helm-override-update + --reuse-values --set sampleApp.create=true + metrics-server metrics-server metrics-server` If you want to deploy the sample app +- Run `system application-apply metrics-server` +- Run` system application-list` to see if it was applied +- Run `kubectl get pods -l app=metrics-server -n metrics-server` to see the pod running + + + + diff --git a/sample-app/centos/docker/.dockerignore b/sample-app/centos/docker/.dockerignore new file mode 100644 index 0000000..799e5bb --- /dev/null +++ b/sample-app/centos/docker/.dockerignore @@ -0,0 +1,3 @@ +node_modules +npm-debug.log +sample-app.yml \ No newline at end of file diff --git a/sample-app/centos/docker/Dockerfile b/sample-app/centos/docker/Dockerfile new file mode 100644 index 0000000..cd4224b --- /dev/null +++ b/sample-app/centos/docker/Dockerfile @@ -0,0 +1,11 @@ +FROM node:14.15.5-alpine3.13 + +WORKDIR /usr/src/app + +COPY src/package*.json ./ + +RUN npm install + +COPY src/ . + +CMD [ "node", "sample-application.js" ] \ No newline at end of file diff --git a/sample-app/centos/docker/src/package.json b/sample-app/centos/docker/src/package.json new file mode 100644 index 0000000..42a8942 --- /dev/null +++ b/sample-app/centos/docker/src/package.json @@ -0,0 +1,14 @@ +{ + "name": "src", + "version": "1.0.0", + "description": "", + "main": "sample-application.js", + "dependencies": { + "node-fetch": "2.6.1" + }, + "devDependencies": {}, + "scripts": {}, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/sample-app/centos/docker/src/sample-application.js b/sample-app/centos/docker/src/sample-application.js new file mode 100644 index 0000000..cb76c0e --- /dev/null +++ b/sample-app/centos/docker/src/sample-application.js @@ -0,0 +1,52 @@ + +/* Copyright (c) 2021 Wind River Systems, Inc. + + SPDX-License-Identifier: Apache-2.0 */ + + +const fetch = require('node-fetch'); +const https = require('https'); +const fs = require('fs'); + +const url = "https://kubernetes.default.svc/apis/metrics.k8s.io/v1beta1/pods" + +const httpsAgent = new https.Agent({ + rejectUnauthorized: false, +}); + +if (process.env.TOKEN_PATH) { + fs.readFile(process.env.TOKEN_PATH, 'utf8', (err, token) => { + if (err) { + console.error(`Error reading TOKEN_PATH: ${TOKEN_PATH}`, err) + return + } + if (!token) { + console.log("Miss the token, stopping the application!") + process.exit(1) + } + setInterval(async () => { + try { + console.log(`Using the token ${token}`); + const response = await fetch(url, { + method: 'GET', + headers: { "Authorization": `Bearer ${token}` }, + agent: httpsAgent + }); + + const body = await response.text(); + console.log(body); + } catch (error) { + console.log(`Error requesting ${url}` + error); + } + }, 1000) + }); +} else { + console.error("Missing the TOKEN_PATH variable. Stopping the application"); + process.exit(1) +} + + + + + + diff --git a/sample-app/centos/sample-app.stable_docker_image b/sample-app/centos/sample-app.stable_docker_image new file mode 100644 index 0000000..8cbe97f --- /dev/null +++ b/sample-app/centos/sample-app.stable_docker_image @@ -0,0 +1,2 @@ +BUILDER=docker +LABEL=stx-metrics-server-sample-app \ No newline at end of file diff --git a/sample-app/readme.md b/sample-app/readme.md new file mode 100644 index 0000000..32c03eb --- /dev/null +++ b/sample-app/readme.md @@ -0,0 +1,44 @@ +# Sample App + +Containerized application that retrieves metrics server data + +## How does it work ? + + The sample app is a Nodejs app that requests the metrics every second and prints that in the console. The deployment creates a service account, roles and role binding to allows this application communicate with the api, this token is stored by default on `/var/run/secrets/kubernetes.io/serviceaccount/token`, the application is reading that token and doing the requests on the endpoint `/apis/metrics.k8s.io/v1beta1/pods` that returns all pod metrics and log it on console. + +## Structure +``` +sample-app +├── centos +│ ├── docker +│ │ ├── Dockerfile +│ │ └── src +│ │ ├── package.json +│ │ └── sample-application.js +│ └── sample-app.stable_docker_image +└── readme.md +``` +Important files +- src - Contains Nodejs application +- Dockerfile - Application Dockerfile + +## Run application +> Deploy tha sample app using `system helm-override-update --reuse-values +--set atribute=value ` folow the steps below: + +- Run `system helm-override-update --reuse-values +--set sampleApp.create=true metrics-server metrics-server metrics-server` +- Run `system application apply metrics-server` to apply the override +- Run `kubectl get pods -n metric-server-test-app` to get the name of the pod +- Run `kubectl logs -n metric-server-test-app pod-name --tail 1 -f` to see the logs and check if the sample application is requesting successfully the metrics server api + +## Endpoints + +All of the following endpoints are GET endpoints and they are under the base path `/apis/metrics.k8s.io/v1beta1`: + - `/nodes` - all node metrics +- `/nodes/{node}` - metrics for a specified node +- `/namespaces/{namespace}/pods` - all pod metrics within namespace with support for all-namespaces +- `/namespaces/{namespace}/pods/{pod}` - metrics for a specified pod +- `/pods` - all pod metrics of all namespaces + +