|
| 1 | +# Configuration Best Practices |
| 2 | + |
| 3 | +This guide covers configuration management strategies across deployment models (VM-based, containerized, Kubernetes) and CI/CD pipelines. Core principle: configuration is injected at runtime, never embedded in artifacts. |
| 4 | + |
| 5 | +## VM-based deployments |
| 6 | + |
| 7 | +### Centralized approach |
| 8 | + |
| 9 | +All services share a single configuration management point. |
| 10 | + |
| 11 | +#### Structure |
| 12 | +- Base configuration: `/opt/integrations/shared/Config.toml` |
| 13 | +- Environment overrides: `/opt/integrations/environments/{env}/Config.{env}.toml` |
| 14 | +- Endpoints: `/opt/integrations/environments/{env}/endpoints.{env}.toml` |
| 15 | +- Secrets: `/opt/integrations/environments/{env}/secrets.{env}.toml` |
| 16 | + |
| 17 | +#### Loading |
| 18 | +Create a loader script that combines files in precedence order: secrets → environment-specific → base. |
| 19 | + |
| 20 | +#### Management |
| 21 | +All services use the same configuration loader mechanism. Update configurations in one place. |
| 22 | + |
| 23 | +### Decentralized approach |
| 24 | + |
| 25 | +Each service maintains its own configuration independently. |
| 26 | + |
| 27 | +#### Structure |
| 28 | +- Per service: `/opt/integrations/{service-name}/conf/base.toml`, `production.toml`, `secrets.production.toml` |
| 29 | + |
| 30 | +#### Loading |
| 31 | +Each service's systemd file references its own configuration via `BAL_CONFIG_FILES`. |
| 32 | + |
| 33 | +#### Management |
| 34 | +Services are deployed and configured independently. Good for teams owning individual services. |
| 35 | + |
| 36 | +## Containerized deployments |
| 37 | + |
| 38 | +???+ Warning |
| 39 | + Do NOT include `Config.toml` in the container image. |
| 40 | + |
| 41 | +### Configuration injection methods |
| 42 | + |
| 43 | +1. Volume mounts: `-v /path/to/Config.toml:/home/ballerina/conf/Config.toml` |
| 44 | +2. Environment variables: `-e BAL_CONFIG_VAR_PORT=9090` |
| 45 | +3. Docker Compose: Reference external config files via `.env` files |
| 46 | + |
| 47 | +### Docker Compose pattern |
| 48 | + |
| 49 | +Use environment files (`.env.development`, `.env.production`) to define environment-specific values. Mount configuration files as read-only volumes. Each environment has a separate config directory. |
| 50 | + |
| 51 | +### Docker Swarm |
| 52 | + |
| 53 | +Use Docker Secrets for sensitive data, Docker Configs for non-sensitive configuration. Reference them in service definitions. |
| 54 | + |
| 55 | +## Kubernetes deployments |
| 56 | + |
| 57 | +### Configuration resources |
| 58 | + |
| 59 | +#### ConfigMap |
| 60 | +Non-sensitive configuration |
| 61 | + |
| 62 | +- Base application config |
| 63 | +- Endpoint definitions |
| 64 | +- Feature flags |
| 65 | + |
| 66 | +#### Secrets |
| 67 | +Sensitive data |
| 68 | + |
| 69 | +- Database passwords |
| 70 | +- API keys |
| 71 | +- JWT secrets |
| 72 | + |
| 73 | +### Configuration delivery |
| 74 | + |
| 75 | +1. ConfigMap volumes mount as read-only files at `/etc/config/` |
| 76 | +2. Secret volumes mount with restricted permissions at `/etc/secrets/` |
| 77 | +3. Individual values are injected as environment variables |
| 78 | + |
| 79 | +### Environment management with kustomize |
| 80 | + |
| 81 | +Use kustomize overlays for environment progression: |
| 82 | + |
| 83 | +- `base/`: Common manifests |
| 84 | +- `overlays/development/`: Dev-specific ConfigMaps and patches |
| 85 | +- `overlays/staging/`: Staging overrides |
| 86 | +- `overlays/production/`: Production overrides |
| 87 | + |
| 88 | +Deploy with: `kubectl apply -k overlays/production/` |
| 89 | + |
| 90 | +## CI/CD configuration handling |
| 91 | + |
| 92 | +### Build process |
| 93 | +Build WITHOUT the configuration files. Create deployment artifacts independent of environment. |
| 94 | + |
| 95 | +### Deployment process |
| 96 | +Each stage applies environment-specific configuration before updating the application: |
| 97 | + |
| 98 | +1. Dev deployment: Apply dev ConfigMaps/Secrets → Update image |
| 99 | +2. Staging deployment: Apply staging ConfigMaps/Secrets → Update image |
| 100 | +3. Production deployment: Apply production ConfigMaps/Secrets → Update image (with manual approval) |
| 101 | + |
| 102 | +### Configuration storage |
| 103 | +- Keep config files in repository (`config/` or `k8s/` directories) |
| 104 | +- Store secrets in platform secret storage (never in repo) |
| 105 | +- Each environment has separate secrets |
| 106 | + |
| 107 | +### Pattern across platforms |
| 108 | +GitHub Actions, GitLab CI, and Jenkins follow the same: build once, configure per environment, deploy many times. |
| 109 | + |
| 110 | +## Configuration promotion |
| 111 | + |
| 112 | +Configuration flows through environments: development → staging → production. |
| 113 | + |
| 114 | +### Promotion workflow |
| 115 | + |
| 116 | +1. Validate source environment configuration |
| 117 | +2. Backup existing target configuration |
| 118 | +3. Copy with environment-specific transformations |
| 119 | +4. Validate transformed configuration |
| 120 | +5. Apply to the target environment |
| 121 | +6. Audit log all changes |
| 122 | + |
| 123 | +### Transformations |
| 124 | + |
| 125 | +#### Dev to staging |
| 126 | + |
| 127 | +- Change: `dev.example.com` → `staging.example.com` |
| 128 | +- Keep: Authentication details unchanged |
| 129 | +- Adjust: Resource limits if needed |
| 130 | + |
| 131 | +#### Staging to production |
| 132 | + |
| 133 | +- Change: `staging.example.com` → `prod.example.com` |
| 134 | +- Increase: Log level requirements |
| 135 | +- Enable: Additional monitoring/security |
| 136 | + |
| 137 | +### Automation tools |
| 138 | + |
| 139 | +Automate promotion scripts that: |
| 140 | + |
| 141 | +- Validate before proceeding |
| 142 | +- Log all changes |
| 143 | +- Enable rollback |
| 144 | +- Handle transformation rules |
| 145 | + |
| 146 | +## Endpoint promotion |
| 147 | + |
| 148 | +External and internal service endpoints change across environments. |
| 149 | + |
| 150 | +### Endpoint configuration structure |
| 151 | + |
| 152 | +```toml |
| 153 | +[external_services.development] |
| 154 | +auth_service = "https://auth-dev.example.com" |
| 155 | + |
| 156 | +[external_services.staging] |
| 157 | +auth_service = "https://auth-staging.example.com" |
| 158 | + |
| 159 | +[external_services.production] |
| 160 | +auth_service = "https://auth.example.com" |
| 161 | +``` |
| 162 | + |
| 163 | +### Endpoint resolution |
| 164 | + |
| 165 | +During application startup: |
| 166 | + |
| 167 | +- Read `endpoints.toml` |
| 168 | +- Select the correct endpoint block based on the `ENVIRONMENT` variable |
| 169 | +- Use the selected endpoint for all outbound connections |
| 170 | + |
| 171 | +### Endpoint promotion |
| 172 | + |
| 173 | +Promote independently from the general configuration: |
| 174 | + |
| 175 | +1. Extract source environment endpoint block |
| 176 | +2. Transform domain/URLs to target pattern |
| 177 | +3. Apply to the target environment |
| 178 | +4. Validate connectivity to new endpoints |
| 179 | +5. Document changes in the audit log |
| 180 | + |
| 181 | +## Common best practices |
| 182 | + |
| 183 | +### Separation |
| 184 | + |
| 185 | +Configuration should be separate from code and artifacts. |
| 186 | + |
| 187 | +### Precedence management |
| 188 | + |
| 189 | +Leverage configuration precedence order correctly: |
| 190 | + |
| 191 | +1. Environment variables (highest priority) |
| 192 | +2. Command-line arguments |
| 193 | +3. TOML files |
| 194 | +4. Embedded defaults (lowest priority) |
| 195 | + |
| 196 | +### Secrets security |
| 197 | + |
| 198 | +- Never commit secrets to version control |
| 199 | +- Use platform secret management (Vault, K8s Secrets, CI/CD secrets) |
| 200 | +- Rotate secrets regularly |
| 201 | +- Audit all secret access |
| 202 | + |
| 203 | +### Validation |
| 204 | + |
| 205 | +Validate the configuration at startup. |
| 206 | + |
| 207 | +- Check required values present |
| 208 | +- Validate value ranges and formats |
| 209 | +- Fail fast on invalid configuration |
| 210 | + |
| 211 | +### Documentation |
| 212 | + |
| 213 | +Maintain the endpoint and the configuration documentation. |
| 214 | + |
| 215 | +- Example configuration files (Config.toml.example) |
| 216 | +- Endpoint registry showing all external/internal endpoints |
| 217 | +- Document transformation rules for promotions |
| 218 | + |
| 219 | +### Auditability |
| 220 | + |
| 221 | +Log all configuration changes. |
| 222 | + |
| 223 | +- Who changed what |
| 224 | +- When changes occurred |
| 225 | +- Source and target environments |
| 226 | +- Rollback actions |
| 227 | + |
| 228 | +## References |
| 229 | + |
| 230 | +- [Managing Configurations](/deploy/managing-configurations) |
| 231 | +- [Kubernetes ConfigMaps & Secrets](https://kubernetes.io/docs/tasks/configure-pod-container/) |
| 232 | +- [Docker Compose Documentation](https://docs.docker.com/compose/) |
| 233 | +- [Kustomize](https://kustomize.io/) |
| 234 | +- [HashiCorp Vault](https://www.vaultproject.io/) |
0 commit comments