Skip to content

Simple stack playground to experiment with Ansible, K8S, Terraform, VMs and database providers.

License

Notifications You must be signed in to change notification settings

trolit/simple-stack-playground

Repository files navigation

Stack

Preview

          +-----------------+
          |  K8S STATELESS  |
          |    CLUSTER      |
          |                 |
+-----+       +--------+    |
| WWW | <---> | CLIENT |    |
+-----+       +--------+    |
          |       ^         |
          |       |         | +----------+
          |       v         | |  REDIS   |
          |   +-------+       +----------+
          |   |  API  | <---> | POSTGRES |
          |   +-------+       +----------+
          |                 |
          +-----------------+

Running project

  1. Check if ssh is installed (ssh -V)

  2. Create VM/machine with Unix based OS. It should have openssh-server installed and configured ssh connection with your machine

    How to configure SSH connection?
    1. Generate key named id_cass (in case of different name, reflect change in ansible.cfg)
    sh-keygen -t rsa -b 4096 -f ~/.ssh/id_cass
    1. Paste public key (~/.ssh/id_cass.pub) content to ~/.ssh/authorized_keys on VM/machine
  3. Check if current become-directives in setup.yaml match VM/machine OS.

  4. Get host of VM/machine using e.g. ip a and define /database/inventory

    [postgres]
    db1 ansible_host=192.168.0.7

    Use ansible postgres -m ping to test if host is reachable. Note that for ping you might have to specify destination user (ansible_user).

  5. Setup infrastructure

    with script 🔥
    1. Run quick-start.sh script from root dir.
    with step-by-step guide ⚙️
    1. Go to /database and execute setup.yaml playbook for PostgreSQL role
    ansible-playbook setup.yaml --extra-vars "role=postgres" -K
    1. Create .env files (x3) based on .env.example.

    2. Modify DATABASE_URL and SHADOW_DATABASE_URL in /api/.env by replacing address (127.0.0.1) to the one that you've provided in inventory.

    3. Go to root dir of project and build client and api images.

      npm run dc-build
      
    4. Go to /infrastructure-as-code and call:

      terraform apply

      If creating client service fails and you don't want to wait until K8S tries to resurrect it again, re-run it manually after few seconds

    5. Run migrations by accessing api:

      # kubectl get pod
      kubectl exec --stdin --tty <POD-NAME> -- /bin/bash
      
      npm run db:migrate:dev
    6. Access app at http://localhost:31000 or http://127.0.0.1:31000

    7. Destroy stack using terraform destroy