Deployment to Docker and Kubernetes of StatsD-Grafana bundle

Real Example

$ git clone https://github.com/xtrmstep/DockerNetSample
$ cd .\DockerNetSample\
$ kubectl apply -f .\src\StatsDServer\k8s-deployment.yaml
$ .\build.ps1
$ .\run.ps1
$ kubectl delete svc stats-tcp
$ kubectl delete svc stats-udp
$ kubectl delete deployment stats

You deployed StatsD, InfluxDb and Grafana locally to Kubernetes

Deployment with Docker-Compose

version: '2'
services:
stats:
image: samuelebistoletti/docker-statsd-influxdb-grafana:latest
ports:
- "3003:3003"
- "3004:8888"
- "8086:8086"
- "8125:8125/udp"
$ docker-compose -f docker-compose.yaml up -d
$ docker-compose stop

Deployment to Kubernetes

Deployment with kubectl

$ kubectl run stats --image=samuelebistoletti/docker-statsd-influxdb-grafana:latest --image-pull-policy=Always
$ kubectl expose deployment stats --type=LoadBalancer --port=3003 --target-port=3003
$ kubectl get all

Extracting YAML configuration

$ kubectl get deployment,service stats -o yaml --export > exported.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: stats
spec:
replicas: 1
selector:
matchLabels:
run: stats
template:
metadata:
labels:
run: stats
spec:
containers:
- image: samuelebistoletti/docker-statsd-influxdb-grafana:latest
imagePullPolicy: Always
name: stats
---
apiVersion: v1
kind: Service
metadata:
name: stats-tcp
spec:
type: LoadBalancer
ports:
- name: grafana
protocol: TCP
port: 3003
targetPort: 3003
- name: influxdb-admin
protocol: TCP
port: 3004
targetPort: 8888
- name: influxdb
protocol: TCP
port: 8086
targetPort: 8086
selector:
run: stats
---
apiVersion: v1
kind: Service
metadata:
name: stats-udp
spec:
type: LoadBalancer
ports:
- name: telegraf
protocol: UDP
port: 8125
targetPort: 8125
selector:
run: stats
$ kubectl delete svc stats
$ kubectl delete deployment stats
$ kubectl apply -f k8s-deployment.yaml

StatsD protocol

counter.name:1|c
timing.name:320|ms
gauge.name:333|g

Metrics in .NET Core Service

public override async Task<FactorialReply> Factorial(FactorialRequest request, ServerCallContext context)
{
// Obtain the number of available threads in ThreadPool
ThreadPool.GetAvailableThreads(out var availableThreads, out _);
// The number of available threads is the example of Gauge metric
// Send gauge metric to StatsD (using JustEat.StatsD nuget)
_stats.Gauge(availableThreads, "GaugeAvailableThreads");

// Increment a counter metric for incoming requests
_stats.Increment("CountRequests");

// The method _stats.Time() will calculate the time while the _semaphoreSlim.WaitAsync() were waiting
// and send the metric to StatsD
await _stats.Time("TimeWait", async f => await _semaphoreSlim.WaitAsync());
try
{
// Again measure time length of calculation and send it to StatsD
var result = await _stats.Time("TimeCalculation", async t => await CalculateFactorialAsync(request.Factor));
// Increment a counter of processed requests
_stats.Increment("CountProcessed");
return await Task.FromResult(new FactorialReply
{
Result = result
});
}
finally
{
_semaphoreSlim.Release();
}
}

See Also

--

--

--

All opinions are my own || Software Developer, learner, perfectionist and entrepreneur-kind person, nonconformist. Always seeks for the order and completeness.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to start with CP ?

Pointers in Go

How To improve Python and Django development skills?

Latest features in Java 16

Deploy a Flask app with AWS API Gateway managed via Zappa and Terraform

Java Concurrency: How to create a thread

Leetcode Q133. Clone Graph

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alexander Goida

Alexander Goida

All opinions are my own || Software Developer, learner, perfectionist and entrepreneur-kind person, nonconformist. Always seeks for the order and completeness.

More from Medium

DevOps for Steampunk aka ABAP Cloud with ReleaseOwl

Tudo sobre APIM( Azure API Management)

Introduction to Docker

Build the Azure SSO login page in Blazor Server application with SAML