|
| 1 | +# Argo CD Agent Setup with mTLS on Kubernetes |
| 2 | + |
| 3 | +This Terraform configuration automates the complete setup of Argo CD with agent deployment on Kubernetes, including mutual TLS (mTLS) authentication between principal and agent components. |
| 4 | + |
| 5 | +## Architecture Overview |
| 6 | + |
| 7 | +### Components |
| 8 | +- **Control Plane Cluster**: Runs the Argo CD Principal (server) |
| 9 | +- **Workload Cluster(s)**: Runs the Argo CD Agent(s) |
| 10 | +- **mTLS**: Secure encrypted communication between Principal and Agent |
| 11 | + |
| 12 | +### TLS Flow |
| 13 | +``` |
| 14 | +Workload Agent (with client cert) |
| 15 | + | |
| 16 | + [mTLS] |
| 17 | + | |
| 18 | + Control Plane Principal (with server cert) |
| 19 | + | |
| 20 | + CA Certificate (validates both) |
| 21 | +``` |
| 22 | + |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +1. **Kubernetes Clusters** |
| 26 | + - At least 2 clusters (control plane + workload) |
| 27 | + - kubectl configured with contexts |
| 28 | + - Helm 3.x installed |
| 29 | + |
| 30 | +2. **Terraform** |
| 31 | + - Terraform 1.0+ |
| 32 | + - Providers: kubernetes, helm, tls, local |
| 33 | + |
| 34 | +3. **Tools** |
| 35 | + - kubectl |
| 36 | + - helm |
| 37 | + - openssl (for certificate verification) |
| 38 | + |
| 39 | +## Configuration Steps |
| 40 | + |
| 41 | +### 1. Prepare Your Environment |
| 42 | + |
| 43 | +```bash |
| 44 | +cd /Users/gis/progl/mapp7/project/observability/argocd/terraform |
| 45 | + |
| 46 | +# Install dependencies |
| 47 | +terraform init |
| 48 | +``` |
| 49 | + |
| 50 | +### 2. Create terraform.tfvars |
| 51 | + |
| 52 | +Copy and customize the example: |
| 53 | + |
| 54 | +```bash |
| 55 | +cp terraform.tfvars.example terraform.tfvars |
| 56 | +``` |
| 57 | + |
| 58 | +Edit `terraform.tfvars` with your cluster details: |
| 59 | + |
| 60 | +```hcl |
| 61 | +control_plane_cluster = { |
| 62 | + name = "control-plane" |
| 63 | + context_name = "your-cp-context" # kubectl context name |
| 64 | + kubeconfig_path = "~/.kube/config" |
| 65 | + server_address = "argocd.example.com" # FQDN or IP |
| 66 | + server_port = 443 |
| 67 | + tls_enabled = true |
| 68 | +} |
| 69 | +
|
| 70 | +workload_clusters = [ |
| 71 | + { |
| 72 | + name = "workload-1" |
| 73 | + context_name = "your-wl-context" |
| 74 | + kubeconfig_path = "~/.kube/config" |
| 75 | + principal_address = "argocd.example.com" |
| 76 | + principal_port = 443 |
| 77 | + agent_name = "agent-1" |
| 78 | + tls_enabled = true |
| 79 | + } |
| 80 | +] |
| 81 | +``` |
| 82 | + |
| 83 | +### 3. Plan the Deployment |
| 84 | + |
| 85 | +```bash |
| 86 | +terraform plan -out=tfplan |
| 87 | +``` |
| 88 | + |
| 89 | +Review the output to ensure all resources will be created correctly. |
| 90 | + |
| 91 | +### 4. Apply the Configuration |
| 92 | + |
| 93 | +```bash |
| 94 | +terraform apply tfplan |
| 95 | +``` |
| 96 | + |
| 97 | +This will: |
| 98 | +- ✅ Generate self-signed certificates for mTLS |
| 99 | +- ✅ Create namespaces on both clusters |
| 100 | +- ✅ Install Argo CD on control plane with TLS enabled |
| 101 | +- ✅ Install Argo CD on workload cluster |
| 102 | +- ✅ Deploy the Argo CD Agent with mTLS configuration |
| 103 | +- ✅ Configure RBAC for agent operations |
| 104 | + |
| 105 | +### 5. Verify the Setup |
| 106 | + |
| 107 | +```bash |
| 108 | +# Check control plane |
| 109 | +terraform output verification_commands | head -20 |
| 110 | + |
| 111 | +# Manual verification |
| 112 | +kubectl get pods -n argocd --context=<control-plane-context> |
| 113 | +kubectl get pods -n argocd --context=<workload-context> |
| 114 | + |
| 115 | +# Check agent connection |
| 116 | +kubectl logs -n argocd -f deployment/argocd-agent --context=<workload-context> |
| 117 | +``` |
| 118 | + |
| 119 | +## Outputs |
| 120 | + |
| 121 | +After successful deployment, Terraform provides: |
| 122 | + |
| 123 | +- **principal_server_address**: Agent connection address |
| 124 | +- **principal_server_port**: Agent connection port |
| 125 | +- **principal_tls_enabled**: Whether mTLS is active |
| 126 | +- **agent_name**: Registered agent identifier |
| 127 | +- **ca_certificate_path**: CA cert location |
| 128 | +- **server_certificate_path**: Server cert location |
| 129 | +- **agent_client_certificate_path**: Client cert location |
| 130 | +- **verification_commands**: Pre-built kubectl commands |
| 131 | + |
| 132 | +### View Outputs |
| 133 | + |
| 134 | +```bash |
| 135 | +terraform output principal_server_address |
| 136 | +terraform output connection_commands |
| 137 | +``` |
| 138 | + |
| 139 | +## Certificate Management |
| 140 | + |
| 141 | +### Certificates Generated |
| 142 | + |
| 143 | +1. **CA Certificate** (`certs/ca.crt`, `certs/ca.key`) |
| 144 | + - Root authority for all certificates |
| 145 | + - Validity: 365 days (configurable) |
| 146 | + |
| 147 | +2. **Server Certificate** (`certs/argocd-server.crt`, `certs/argocd-server.key`) |
| 148 | + - Used by Principal server |
| 149 | + - Includes DNS names for service resolution |
| 150 | + |
| 151 | +3. **Client Certificate** (`certs/agent-client.crt`, `certs/agent-client.key`) |
| 152 | + - Used by Agent for authentication |
| 153 | + - Presented during mTLS handshake |
| 154 | + |
| 155 | +### Rotating Certificates |
| 156 | + |
| 157 | +```bash |
| 158 | +# Update certificate validity |
| 159 | +terraform apply -var 'tls_config.cert_validity_days=730' |
| 160 | + |
| 161 | +# Remove old certificates |
| 162 | +rm -rf certs/ |
| 163 | +terraform apply |
| 164 | +``` |
| 165 | + |
| 166 | +## Troubleshooting |
| 167 | + |
| 168 | +### Agent Connection Issues |
| 169 | + |
| 170 | +```bash |
| 171 | +# Check agent logs |
| 172 | +kubectl logs -n argocd deployment/argocd-agent \ |
| 173 | + --context=<workload-context> -f |
| 174 | + |
| 175 | +# Expected output should show successful connection to principal |
| 176 | +``` |
| 177 | + |
| 178 | +### TLS Verification Issues |
| 179 | + |
| 180 | +```bash |
| 181 | +# Check certificate details |
| 182 | +openssl x509 -in certs/argocd-server.crt -text -noout |
| 183 | + |
| 184 | +# Verify CA chain |
| 185 | +openssl verify -CAfile certs/ca.crt certs/argocd-server.crt |
| 186 | +``` |
| 187 | + |
| 188 | +### Principal Not Reachable |
| 189 | + |
| 190 | +```bash |
| 191 | +# Check service exposure |
| 192 | +kubectl get svc -n argocd --context=<control-plane-context> |
| 193 | + |
| 194 | +# Port-forward for testing |
| 195 | +kubectl port-forward -n argocd svc/argocd-server 8443:443 \ |
| 196 | + --context=<control-plane-context> |
| 197 | +``` |
| 198 | + |
| 199 | +## Customization |
| 200 | + |
| 201 | +### Variables |
| 202 | + |
| 203 | +Edit variables in `variables.tf`: |
| 204 | + |
| 205 | +- **argocd_version**: Argo CD Helm chart version |
| 206 | +- **server_service_type**: LoadBalancer, ClusterIP, or NodePort |
| 207 | +- **controller_replicas**: Number of controller instances |
| 208 | +- **agent_mode**: "autonomous" (default) or "managed" |
| 209 | + |
| 210 | +### Enable High Availability |
| 211 | + |
| 212 | +```bash |
| 213 | +terraform apply \ |
| 214 | + -var 'controller_replicas=3' \ |
| 215 | + -var 'repo_server_replicas=3' |
| 216 | +``` |
| 217 | + |
| 218 | +### Use Existing Certificates |
| 219 | + |
| 220 | +Set `create_certificate_authority = false` in variables and provide: |
| 221 | +- `certs/ca.crt` and `certs/ca.key` |
| 222 | +- `certs/argocd-server.crt` and `certs/argocd-server.key` |
| 223 | +- `certs/agent-client.crt` and `certs/agent-client.key` |
| 224 | + |
| 225 | +## Clean Up |
| 226 | + |
| 227 | +```bash |
| 228 | +# Destroy all resources |
| 229 | +terraform destroy |
| 230 | + |
| 231 | +# Confirm the action when prompted |
| 232 | +``` |
| 233 | + |
| 234 | +This will: |
| 235 | +- Remove Argo CD installations |
| 236 | +- Delete namespaces |
| 237 | +- Clean up certificates (keep in backup) |
| 238 | +- Remove Kubernetes secrets |
| 239 | + |
| 240 | +## Best Practices |
| 241 | + |
| 242 | +### Security |
| 243 | +- ✅ Always use TLS in production |
| 244 | +- ✅ Rotate certificates regularly |
| 245 | +- ✅ Use strong CA keys |
| 246 | +- ✅ Store certificates in secure location |
| 247 | +- ✅ Limit access to kubeconfig files |
| 248 | + |
| 249 | +### Operations |
| 250 | +- ✅ Use meaningful agent names |
| 251 | +- ✅ Tag all resources with labels |
| 252 | +- ✅ Monitor agent connectivity |
| 253 | +- ✅ Backup Terraform state |
| 254 | +- ✅ Test in non-prod first |
| 255 | + |
| 256 | +### Scalability |
| 257 | +- ✅ Adjust replica counts based on load |
| 258 | +- ✅ Use persistent storage |
| 259 | +- ✅ Monitor resource usage |
| 260 | +- ✅ Plan for cluster growth |
| 261 | + |
| 262 | +## Additional Resources |
| 263 | + |
| 264 | +- [Argo CD Documentation](https://argo-cd.readthedocs.io/) |
| 265 | +- [Argo CD Agent Configuration](https://argocd-agent.readthedocs.io/) |
| 266 | +- [TLS Configuration](https://argo-cd.readthedocs.io/en/stable/operator-manual/tls/) |
| 267 | +- [Kubernetes TLS Secrets](https://kubernetes.io/docs/concepts/configuration/secret/#tls-secrets) |
| 268 | + |
| 269 | +## Support |
| 270 | + |
| 271 | +For issues or questions: |
| 272 | +1. Check logs: `kubectl logs -n argocd <pod-name>` |
| 273 | +2. Verify connectivity: Test DNS and network paths |
| 274 | +3. Review Terraform state: `terraform state show` |
| 275 | +4. Check Argo CD issues: https://github.com/argoproj/argo-cd/issues |
0 commit comments