This project is a microservices architecture built using Django and Docker, integrated with Keycloak for centralized authentication (SSO) and Kerberos for network authentication. The microservices communicate with their respective PostgreSQL databases, and NGINX is used as a reverse proxy.
├── config/
│ ├── kerberos/ # Kerberos KDC configuration and files
│ ├── keycloak/ # Keycloak authentication service configuration
│ ├── keytabs/ # Kerberos keytab files for services
│ ├── nginx/ # NGINX reverse proxy configuration
│ ├── postgres_data/ # PostgreSQL database volumes
│ └── docker-compose.yml # Docker Compose file to orchestrate the setup
├── microservice1/ # Django microservice 1
│ ├── base/ # Django app files
│ ├── Dockerfile # Dockerfile to build microservice1
│ └── requirements.txt # Python dependencies for microservice1
├── microservice2/ # Django microservice 2
│ ├── base/ # Django app files
│ ├── Dockerfile # Dockerfile to build microservice2
│ └── requirements.txt # Python dependencies for microservice2
├── microservice3/ # Django microservice 3
│ ├── base/ # Django app files
│ ├── Dockerfile # Dockerfile to build microservice3
│ └── requirements.txt # Python dependencies for microservice3
├── venv/ # Python virtual environment (local development, ignored by Git)
├── .gitignore # Specifies files and directories to ignore in Git
├── .gitattributes # Defines file attribute settings for Git
├── LICENSE # License for the project
└── README.md # Project documentation (this file)
- Keycloak is an open-source identity and access management tool used for authentication and authorization. It manages user login for all microservices.
- Keycloak Database (
keycloak_db): A PostgreSQL instance that stores Keycloak data.
Each microservice is a separate Django application with its own PostgreSQL database.
-
Microservice 1:
- Runs on port
8001. - Connects to PostgreSQL database
microservice1_db.
- Runs on port
-
Microservice 2:
- Runs on port
8002. - Connects to PostgreSQL database
microservice2_db.
- Runs on port
-
Microservice 3:
- Runs on port
8003. - Connects to PostgreSQL database
microservice3_db.
- Runs on port
- Each microservice has its own dedicated PostgreSQL database.
microservice1_db: For Microservice 1.microservice2_db: For Microservice 2.microservice3_db: For Microservice 3.
- NGINX is used to reverse proxy and route requests to the correct microservice based on the incoming URL.
- Kerberos is used for network authentication, ensuring secure communication and enabling Single Sign-On (SSO) across the system.
- The Kerberos KDC service runs as a container and manages authentication tickets for services.
The docker-compose.yml file orchestrates the following services:
- Keycloak (
keycloak_con) and Keycloak DB (keycloak_db): Manages user authentication. - Microservice 1 (
microservice1_con): Django microservice with its own database. - Microservice 2 (
microservice2_con): Django microservice with its own database. - Microservice 3 (
microservice3_con): Django microservice with its own database. - Kerberos KDC (
kerberos_kdc): Kerberos authentication service. - PostgreSQL for each microservice.
To start all the services, run the following command from the root directory:
docker-compose up --buildThis command will build and start all containers defined in docker-compose.yml.
- Keycloak: http://localhost:8080 - Centralized authentication UI.
- Microservice 1: http://localhost:8001 - Django service for microservice1.
- Microservice 2: http://localhost:8002 - Django service for microservice2.
- Microservice 3: http://localhost:8003 - Django service for microservice3.
Environment variables for each service are defined in the docker-compose.yml file:
-
Keycloak:
KEYCLOAK_USER: The admin user for Keycloak.KEYCLOAK_PASSWORD: The admin password for Keycloak.DB_VENDOR: Database vendor (PostgreSQL).DB_ADDR,DB_DATABASE,DB_USER,DB_PASSWORD: PostgreSQL connection details.
-
Microservices:
DB_HOST,DB_PORT: PostgreSQL host and port.DB_NAME,DB_USER,DB_PASSWORD: PostgreSQL database connection details.
-
Kerberos:
REALM: Kerberos realm name.KDC_DOMAIN: Kerberos domain.KRB5_KEYTAB: Path to the Kerberos keytab file.
- PostgreSQL data is stored in the
config/postgres_data/directory, ensuring that data persists even if the containers are stopped or removed.
To enable Single Sign-On (SSO) with Keycloak, follow these steps:
-
Install and Configure Keycloak:
- Deploy Keycloak using Docker (as per your existing setup in the
docker-compose.yml). - Access the Keycloak admin console at
http://localhost:8080/authand log in using the admin credentials.
- Deploy Keycloak using Docker (as per your existing setup in the
-
Create a Realm:
- In the Keycloak Admin Console, click Add Realm, name it (e.g.,
myrealm), and save.
- In the Keycloak Admin Console, click Add Realm, name it (e.g.,
-
Create Clients for Each Microservice:
- In Keycloak, go to the Clients tab and click Create for each microservice (e.g.,
microservice1,microservice2,microservice3). - Set Client Protocol to
openid-connectand Access Type toconfidential. - Save the client and copy the Client Secret (you will need this in Django settings).
- In Keycloak, go to the Clients tab and click Create for each microservice (e.g.,
-
Create Users:
- Go to the Users section, click Add User, and create a user for each service.
-
Install the necessary Python package for Keycloak integration:
pip install django-keycloak-auth
-
Update the Django settings for each microservice (
settings.py):- Add Keycloak configurations such as:
KEYCLOAK_CONFIG = { 'KEYCLOAK_SERVER_URL': 'http://localhost:8080/auth', 'KEYCLOAK_CLIENT_ID': 'microservice1', 'KEYCLOAK_REALM': 'myrealm', 'KEYCLOAK_CLIENT_SECRET': 'your-client-secret-here', 'KEYCLOAK_OPENID_CONFIG': '/realms/myrealm/.well-known/openid-configuration', }
- Add Keycloak configurations such as:
- Start all services using
docker-compose up. - Visit each microservice URL, and you will be redirected to Keycloak for authentication.
If you encounter any issues, you can check the logs of the individual services:
docker-compose logs <service_name>For example, to check the logs of microservice1:
docker-compose logs microservice1- Implement scaling strategies for microservices and databases.
- Add monitoring and logging tools (such as Prometheus, Grafana, or ELK stack).
- Enhance security configurations for production environments.
![]() Mritunjay Pratap Singh |
![]() Mohd Sakib |
|---|

