|
1 | | -# k8s-devmachine |
2 | | -A helm chart for a traditional like dev machine on a vm |
| 1 | +# K8s DevMachine |
| 2 | + |
| 3 | +A containerized development environment for Kubernetes that provides a VM-like dev experience with SSH access. |
| 4 | + |
| 5 | +## ✨ Features |
| 6 | + |
| 7 | +- **Complete development toolkit** with pre-installed tools: |
| 8 | + - Terraform |
| 9 | + - Packer |
| 10 | + - Ansible with Python libraries |
| 11 | + - Git, Vim, SSH utilities |
| 12 | + - MinIO Client |
| 13 | +- **SSH access** on port 2222 |
| 14 | +- **Persistent storage** for home directory |
| 15 | +- **Secure configuration** with non-root user |
| 16 | +- **Python virtual environment** auto-activated |
| 17 | +- **Customizable SSH keys** via ConfigMap |
| 18 | + |
| 19 | +## 🚀 Quick Start |
| 20 | + |
| 21 | +### Prerequisites |
| 22 | + |
| 23 | +- Kubernetes cluster (>= 1.20) |
| 24 | +- Helm 3.x |
| 25 | +- Storage class (Longhorn recommended) |
| 26 | +- LoadBalancer support (for external access) |
| 27 | + |
| 28 | +### Installation |
| 29 | + |
| 30 | +1. **Clone the repository** |
| 31 | +```bash |
| 32 | +git clone https://github.com/syntax3rror404/k8s-devmachine.git |
| 33 | +cd k8s-devmachine |
| 34 | +``` |
| 35 | + |
| 36 | +2. **Configure SSH keys** |
| 37 | +Edit `chart/values.yaml` and add your public SSH keys: |
| 38 | + |
| 39 | +```yaml |
| 40 | +ssh: |
| 41 | + authorizedKeys: | |
| 42 | + ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC... user@hostname |
| 43 | + ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... user@hostname |
| 44 | +``` |
| 45 | +
|
| 46 | +3. **Deploy with Helm** |
| 47 | +```bash |
| 48 | +helm install devmachine ./chart |
| 49 | +``` |
| 50 | + |
| 51 | +4. **Get external IP and connect** |
| 52 | +```bash |
| 53 | +kubectl get svc devmachine-service |
| 54 | +ssh -p 2222 dev@<EXTERNAL-IP> |
| 55 | +``` |
| 56 | + |
| 57 | +## 🔧 Configuration |
| 58 | + |
| 59 | +### values.yaml |
| 60 | + |
| 61 | +```yaml |
| 62 | +replicaCount: 1 |
| 63 | + |
| 64 | +image: |
| 65 | + source: ghcr.io/syntax3rror404/k8s-devmachine@sha256:... |
| 66 | + pullPolicy: IfNotPresent |
| 67 | + |
| 68 | +service: |
| 69 | + type: LoadBalancer |
| 70 | + port: 2222 |
| 71 | + targetPort: 2222 |
| 72 | + |
| 73 | +persistence: |
| 74 | + enabled: true |
| 75 | + size: 10Gi |
| 76 | + storageClass: "longhorn" |
| 77 | + |
| 78 | +ssh: |
| 79 | + authorizedKeys: | |
| 80 | + # Add your SSH public keys here |
| 81 | +``` |
| 82 | +
|
| 83 | +### Resource limits (optional) |
| 84 | +
|
| 85 | +```yaml |
| 86 | +resources: |
| 87 | + requests: |
| 88 | + memory: "512Mi" |
| 89 | + cpu: "250m" |
| 90 | + limits: |
| 91 | + memory: "2Gi" |
| 92 | + cpu: "1000m" |
| 93 | +``` |
| 94 | +
|
| 95 | +## 🔐 Access |
| 96 | +
|
| 97 | +### SSH Connection |
| 98 | +```bash |
| 99 | +# Get external IP |
| 100 | +EXTERNAL_IP=$(kubectl get svc devmachine-service -o jsonpath='{.status.loadBalancer.ingress[0].ip}') |
| 101 | + |
| 102 | +# Connect via SSH |
| 103 | +ssh -p 2222 dev@$EXTERNAL_IP |
| 104 | +``` |
| 105 | + |
| 106 | +### Port Forwarding (alternative) |
| 107 | +```bash |
| 108 | +kubectl port-forward svc/devmachine-service 2222:2222 |
| 109 | +ssh -p 2222 dev@localhost |
| 110 | +``` |
| 111 | + |
| 112 | +## 📁 Container Structure |
| 113 | + |
| 114 | +``` |
| 115 | +/home/dev/ |
| 116 | +├── .ssh/authorized_keys # Your SSH keys |
| 117 | +├── .bashrc # Shell configuration |
| 118 | +├── venv/ # Python virtual environment |
| 119 | +├── ssh_keys/ # SSH host keys (persistent) |
| 120 | +└── run/ # Runtime files |
| 121 | +``` |
| 122 | + |
| 123 | +## 🛠️ Development |
| 124 | + |
| 125 | +### Building the image |
| 126 | + |
| 127 | +The container image is automatically built via GitHub Actions on push to master branch. |
| 128 | + |
| 129 | +For local development: |
| 130 | +```bash |
| 131 | +docker build -t k8s-devmachine . |
| 132 | +docker run -p 2222:2222 k8s-devmachine |
| 133 | +``` |
| 134 | + |
| 135 | +### Installed Tools |
| 136 | + |
| 137 | +- **Infrastructure**: Terraform, Packer, TF-Helper |
| 138 | +- **Configuration Management**: Ansible with extensive Python libraries |
| 139 | +- **Utilities**: Git, Vim, curl, jq, openssh, MinIO client |
| 140 | +- **System Tools**: ping, dig, dmidecode, lshw |
| 141 | + |
| 142 | +## 🐛 Troubleshooting |
| 143 | + |
| 144 | +### Common Issues |
| 145 | + |
| 146 | +**SSH connection refused** |
| 147 | +```bash |
| 148 | +# Check pod status |
| 149 | +kubectl get pods -l app=devmachine |
| 150 | + |
| 151 | +# View logs |
| 152 | +kubectl logs -l app=devmachine |
| 153 | + |
| 154 | +# Debug inside pod |
| 155 | +kubectl exec -it devmachine-0 -- /bin/bash |
| 156 | +``` |
| 157 | + |
| 158 | +**LoadBalancer pending** |
| 159 | +```bash |
| 160 | +# Use NodePort instead |
| 161 | +kubectl patch svc devmachine-service -p '{"spec":{"type":"NodePort"}}' |
| 162 | + |
| 163 | +# Or use port-forwarding |
| 164 | +kubectl port-forward svc/devmachine-service 2222:2222 |
| 165 | +``` |
| 166 | + |
| 167 | +**Storage issues** |
| 168 | +```bash |
| 169 | +# Check storage class |
| 170 | +kubectl get storageclass |
| 171 | + |
| 172 | +# Check PVC status |
| 173 | +kubectl get pvc |
| 174 | +``` |
| 175 | + |
| 176 | +### Debug Commands |
| 177 | + |
| 178 | +```bash |
| 179 | +# Check service endpoints |
| 180 | +kubectl get endpoints devmachine-service |
| 181 | + |
| 182 | +# Describe pod for events |
| 183 | +kubectl describe pod devmachine-0 |
| 184 | + |
| 185 | +# Test SSH service inside pod |
| 186 | +kubectl exec devmachine-0 -- ss -tuln | grep 2222 |
| 187 | +``` |
| 188 | + |
| 189 | +## 📊 Operations |
| 190 | + |
| 191 | +### Backup |
| 192 | +```bash |
| 193 | +# Backup home directory |
| 194 | +kubectl exec devmachine-0 -- tar czf - /home/dev > backup.tar.gz |
| 195 | + |
| 196 | +# Restore |
| 197 | +kubectl exec -i devmachine-0 -- tar xzf - -C / < backup.tar.gz |
| 198 | +``` |
| 199 | + |
| 200 | +### Updates |
| 201 | +```bash |
| 202 | +# Update image tag in values.yaml, then: |
| 203 | +helm upgrade devmachine ./chart |
| 204 | + |
| 205 | +# Rolling restart |
| 206 | +kubectl rollout restart statefulset/devmachine |
| 207 | +``` |
| 208 | + |
| 209 | +### Scaling |
| 210 | +```bash |
| 211 | +# Scale to multiple instances |
| 212 | +helm upgrade devmachine ./chart --set replicaCount=3 |
| 213 | +``` |
| 214 | + |
| 215 | +## 🔒 Security |
| 216 | + |
| 217 | +- Runs as non-root user (UID 1001) |
| 218 | +- Password authentication disabled |
| 219 | +- Key-based SSH authentication only |
| 220 | +- Seccomp profile enabled |
| 221 | +- Read-only root filesystem (where possible) |
| 222 | + |
| 223 | +## 🤝 Contributing |
| 224 | + |
| 225 | +1. Fork the repository |
| 226 | +2. Create a feature branch (`git checkout -b feature/amazing-feature`) |
| 227 | +3. Commit your changes (`git commit -m 'Add amazing feature'`) |
| 228 | +4. Push to the branch (`git push origin feature/amazing-feature`) |
| 229 | +5. Open a Pull Request |
| 230 | + |
| 231 | +## 🙏 Acknowledgments |
| 232 | + |
| 233 | +- Built with Debian Bookworm Slim |
| 234 | +- Uses HashiCorp tools (Terraform, Packer) |
| 235 | +- Ansible automation platform |
| 236 | +- Kubernetes community |
| 237 | + |
| 238 | +--- |
| 239 | + |
| 240 | +> **Note**: This development machine is designed for development and testing purposes. Consider additional security measures for production environments. |
0 commit comments