项目作者: i-sevostyanov

项目描述 :
k8s cluster with Kafka, Grafana, Prometheus and go applications in Vagrant.
高级语言: Go
项目地址: git://github.com/i-sevostyanov/k8s-playground.git
创建时间: 2021-01-31T19:38:31Z
项目社区:https://github.com/i-sevostyanov/k8s-playground

开源协议:MIT License

下载


Latest Release
CI
Go Report Card
codecov
GitHub license

Kubernetes Playground

This project contains a ready-to-use Kubernetes playground. It uses Vagrant and Ansible to start k8s on 3 nodes and deploy
ZooKeeper, Kafka, Prometheus Operator, Kafka Exporter, and two golang applications (Consumer and Producer).

Attention

Ansible playbook is not idempotent, please consider running it only once. The next attempts will fail.

Prerequisites

You need the following installed to use this playground:

  • Vagrant, tested with version 2.2.14
  • VirtualBox, tested with Version 6.1

Structure

  1. k8s-playground
  2. ├─ cmd
  3. ├─ consumer // Main package for consumer
  4. └─ producer // Main package for producer
  5. ├─ deployment // k8s manifests for consumer and producer
  6. ├─ internal // Internal packages for applications (by golang convention)
  7. ├─ bussiness // Business logic of applications
  8. └─ infrastructure // Application infrastructure code
  9. └─ vagrant // Contains a Vagrantfile
  10. └─ provision // Contains a Ansible playbooks for k8s cluster and management machine
  11. └─ manifests // k8s manifests for kafka, zookepeer, prometheus, etc

Producer & Consumer

Business logic

Producer publishes a current timestamp into input topic every 5 seconds (value by default).

Consumer reads timestamp from input topic, converts it into date in RFC3339 format then publishes it into output topic.

CI/CD

Github Actions are invoked on every commit to handle code linting, tests, and code coverage. The release creation triggers build action.
It builds docker image and pushes it to the Github Container Registry.

Bringing up the cluster

To bring up the cluster, clone this repository to a working directory.

  1. git clone https://github.com/i-sevostyanov/k8s-playground.git

Change into the working directory and run vagrant up command:

  1. cd k8s-playground/vagrant
  2. vagrant up

After the command is complete, you can connect to the management machine and wait for a few minutes until all pods are started.
You can check the status by running the following commands:

  1. vagrant ssh management
  2. kubectl get pods -o wide -A

Everything is okay?

To check that the consumer and producer are working properly you need to connect to the management machine and execute the following command:

  1. kubectl exec -i -t kafka-0 -- kafka-console-consumer --bootstrap-server localhost:9092 --topic output --from-beginning

If the screen displays dates in RFC3339 format with an interval of 5 seconds (by default), then everything works correctly:

  1. 2021-01-01T08:49:26Z
  2. 2021-01-01T08:49:31Z
  3. 2021-01-01T08:49:36Z
  4. 2021-01-01T08:49:41Z
  5. 2021-01-01T08:49:46Z
  6. 2021-01-01T08:49:51Z

Monitoring

Grafana

To access the grafana dashboards, you need to set up port forwarding as follows:

  1. vagrant ssh management
  2. kubectl port-forward `kubectl get pods -l app=grafana -n monitoring -o name` --address 0.0.0.0 3000:3000 -n monitoring &

After that you can access grafana by going to the following address: http://192.168.50.13:3000.
In addition to Kubernetes dashboards, you will find a dashboard with consumer and producer metrics, as well as a Kafka dashboard.

Login: admin
Password: admin

Prometheus

Same for Prometheus:

  1. vagrant ssh management
  2. kubectl port-forward prometheus-k8s-0 --address 0.0.0.0 9090:9090 -n monitoring &

Prometheus web interface will be available at http://192.168.50.13:9090.

What can be improved

  • Idempotent Ansible Playbooks
  • Linting for Ansible Playbooks (ansible-lint)
  • Split Ansible Playbooks into roles and tasks
  • Testing for Ansible Playbooks (Molecule)
  • Parametrize kubernetes manifests with helm or kustomize
  • Collect metrics from JMX
  • Add more metrics for consumer and producer