|
| 1 | +# Kubernetes Service Account |
| 2 | + |
| 3 | +This building block creates a Kubernetes service account in your namespace with role-based access to cluster resources. Service accounts are used for automated authentication and authorization in CI/CD pipelines, applications, and automation scripts running within or outside the cluster. |
| 4 | + |
| 5 | +## 🚀 Usage Examples |
| 6 | + |
| 7 | +- A development team creates a service account to **automate deployments** from their CI/CD pipelines to Kubernetes resources. |
| 8 | +- A DevOps engineer sets up service accounts with **view access** for monitoring tools that need to observe cluster state. |
| 9 | +- A team configures a service account with **edit permissions** for their application to manage resources within their namespace. |
| 10 | +- An operations team uses the generated **kubeconfig** to configure external tools like ArgoCD or Flux. |
| 11 | + |
| 12 | +## 🔄 Shared Responsibility |
| 13 | + |
| 14 | +| Responsibility | Platform Team | Application Team | |
| 15 | +|----------------|---------------|------------------| |
| 16 | +| Create service account | ✅ | ❌ | |
| 17 | +| Assign ClusterRole to service account | ✅ | ❌ | |
| 18 | +| Provide kubeconfig credentials | ✅ | ❌ | |
| 19 | +| Store kubeconfig securely | ❌ | ✅ | |
| 20 | +| Use service account in pipelines/applications | ❌ | ✅ | |
| 21 | +| Monitor service account usage | ✅ | ✅ | |
| 22 | +| Use least privilege roles | ⚠️ | ✅ | |
| 23 | +| Request removal of unused service accounts | ❌ | ✅ | |
| 24 | + |
| 25 | +## 💡 Best Practices |
| 26 | + |
| 27 | +### Service Account Naming |
| 28 | + |
| 29 | +**Why**: Clear names help identify purpose and ownership. |
| 30 | + |
| 31 | +**Recommended Patterns**: |
| 32 | +- Include application/service name: `myapp-production-sa` |
| 33 | +- Include purpose: `myapp-cicd-sa`, `myapp-monitoring-sa` |
| 34 | +- Include environment: `myapp-dev-sa`, `myapp-prod-sa` |
| 35 | + |
| 36 | +**Examples**: |
| 37 | +- ✅ `ecommerce-prod-deployment-sa` |
| 38 | +- ✅ `analytics-monitoring-sa` |
| 39 | +- ✅ `backup-automation-sa` |
| 40 | +- ❌ `sa1` |
| 41 | +- ❌ `test-service-account` |
| 42 | + |
| 43 | +### Role Selection |
| 44 | + |
| 45 | +**Why**: Follow least privilege principle to minimize security risks. |
| 46 | + |
| 47 | +| Role | Use Case | |
| 48 | +|------|----------| |
| 49 | +| `view` | Read-only access for monitoring, dashboards, debugging | |
| 50 | +| `edit` | Deploy and manage applications within namespace | |
| 51 | +| `admin` | Full namespace administration including RBAC | |
| 52 | + |
| 53 | +**Recommendations**: |
| 54 | +- Start with `view` and escalate only if needed |
| 55 | +- Use `edit` for CI/CD pipelines that deploy applications |
| 56 | +- Reserve `admin` for tools that need to manage RBAC or other privileged resources |
| 57 | + |
| 58 | +### Kubeconfig Management |
| 59 | + |
| 60 | +**Why**: The kubeconfig contains sensitive credentials that grant access to your namespace. |
| 61 | + |
| 62 | +**Best Practices**: |
| 63 | +- Store kubeconfig in a secure secrets manager (HashiCorp Vault, AWS Secrets Manager, etc.) |
| 64 | +- Never commit kubeconfig to version control |
| 65 | +- Rotate service account tokens periodically |
| 66 | +- Use short-lived tokens where possible |
| 67 | + |
| 68 | +**Example Usage**: |
| 69 | +```bash |
| 70 | +# Save kubeconfig to a file |
| 71 | +echo "$KUBECONFIG_CONTENT" > kubeconfig |
| 72 | + |
| 73 | +# Use with kubectl |
| 74 | +kubectl --kubeconfig kubeconfig get pods |
| 75 | + |
| 76 | +# Or set KUBECONFIG environment variable |
| 77 | +export KUBECONFIG=./kubeconfig |
| 78 | +kubectl get pods |
| 79 | +``` |
| 80 | + |
| 81 | +### CI/CD Integration |
| 82 | + |
| 83 | +**GitHub Actions Example**: |
| 84 | +```yaml |
| 85 | +steps: |
| 86 | + - name: Configure kubectl |
| 87 | + run: | |
| 88 | + echo "${{ secrets.KUBECONFIG }}" > kubeconfig |
| 89 | + kubectl --kubeconfig kubeconfig apply -f k8s/ |
| 90 | +``` |
| 91 | +
|
| 92 | +**GitLab CI Example**: |
| 93 | +```yaml |
| 94 | +deploy: |
| 95 | + script: |
| 96 | + - echo "$KUBECONFIG" > kubeconfig |
| 97 | + - kubectl --kubeconfig kubeconfig apply -f k8s/ |
| 98 | +``` |
| 99 | +
|
| 100 | +## ⚠️ Security Considerations |
| 101 | +
|
| 102 | +### Token Security |
| 103 | +
|
| 104 | +- The service account token provides **long-lived** access to the cluster |
| 105 | +- Anyone with the kubeconfig can access resources according to the assigned role |
| 106 | +- Treat the kubeconfig as a secret credential |
| 107 | +
|
| 108 | +### Namespace Isolation |
| 109 | +
|
| 110 | +- The role binding is **namespace-scoped** |
| 111 | +- The service account cannot access resources in other namespaces |
| 112 | +- Cross-namespace access requires additional configuration |
| 113 | +
|
| 114 | +### Audit and Monitoring |
| 115 | +
|
| 116 | +- All actions performed by the service account are **logged in Kubernetes audit logs** |
| 117 | +- Work with your platform team to set up **alerting** for suspicious activity |
| 118 | +- Regularly review service account usage and remove unused accounts |
| 119 | +
|
| 120 | +## 📋 Troubleshooting |
| 121 | +
|
| 122 | +### Common Issues |
| 123 | +
|
| 124 | +**"Forbidden" errors when using kubectl**: |
| 125 | +- Verify the assigned ClusterRole has the required permissions |
| 126 | +- Check if you're operating in the correct namespace |
| 127 | +- Ensure the kubeconfig context is set correctly |
| 128 | +
|
| 129 | +**Token not working**: |
| 130 | +- Verify the secret was created correctly |
| 131 | +- Check if the service account exists |
| 132 | +- Ensure the cluster CA certificate is correct |
| 133 | +
|
| 134 | +**Unable to connect to cluster**: |
| 135 | +- Verify the cluster endpoint is reachable from your network |
| 136 | +- Check firewall rules and network policies |
| 137 | +- Ensure the cluster CA certificate matches the cluster |
| 138 | +
|
| 139 | +### Getting Help |
| 140 | +
|
| 141 | +Contact your platform team if you: |
| 142 | +- Need elevated permissions beyond `edit` |
| 143 | +- Require access to cluster-wide resources |
| 144 | +- Experience persistent authentication issues |
| 145 | +- Need to rotate or regenerate credentials |
0 commit comments