|
| 1 | +--- |
| 2 | +display_name: VMware vSphere VM (Linux) |
| 3 | +description: Provision VMware vSphere VMs with networking and datastore management |
| 4 | +icon: ../../../../.icons/box-emoji.svg |
| 5 | +maintainer_github: coder |
| 6 | +verified: true |
| 7 | +tags: [vm, linux, vsphere, vmware, enterprise, on-premises, persistent-vm] |
| 8 | +--- |
| 9 | + |
| 10 | +# Remote Development on VMware vSphere VMs (Linux) |
| 11 | + |
| 12 | +Provision VMware vSphere VMs as [Coder workspaces](https://coder.com/docs/workspaces) with this enterprise-ready template. This template provides comprehensive VM configuration, networking setup, and datastore management for on-premises deployments. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +### vSphere Environment |
| 17 | + |
| 18 | +- VMware vSphere 6.7 or later |
| 19 | +- vCenter Server access |
| 20 | +- At least one ESXi host in a cluster |
| 21 | +- A VM template with Linux OS (Ubuntu 20.04+ recommended) |
| 22 | +- Network connectivity between Coder server and vSphere environment |
| 23 | + |
| 24 | +### Authentication |
| 25 | + |
| 26 | +This template authenticates to vSphere using the provider's [authentication methods](https://registry.terraform.io/providers/hashicorp/vsphere/latest/docs#authentication). The recommended approach is via environment variables: |
| 27 | + |
| 28 | +```bash |
| 29 | +export VSPHERE_USER= "[email protected]" |
| 30 | +export VSPHERE_PASSWORD="your-password" |
| 31 | +export VSPHERE_SERVER="vcenter.company.com" |
| 32 | +export VSPHERE_ALLOW_UNVERIFIED_SSL="true" # Only for testing |
| 33 | +``` |
| 34 | + |
| 35 | +Alternatively, configure the provider directly in the template or use a `.terraformrc` file. |
| 36 | + |
| 37 | +### Required vSphere Permissions |
| 38 | + |
| 39 | +The user account needs the following minimum permissions on the relevant vSphere objects: |
| 40 | + |
| 41 | +#### Datacenter Level |
| 42 | +- **Virtual Machine > Configuration > All** |
| 43 | +- **Virtual Machine > Interaction > All** |
| 44 | +- **Virtual Machine > Inventory > All** |
| 45 | +- **Virtual Machine > Provisioning > All** |
| 46 | + |
| 47 | +#### Datastore Level |
| 48 | +- **Datastore > Allocate space** |
| 49 | +- **Datastore > Browse datastore** |
| 50 | +- **Datastore > Low level file operations** |
| 51 | + |
| 52 | +#### Network Level |
| 53 | +- **Network > Assign network** |
| 54 | + |
| 55 | +#### Resource Pool/Cluster Level |
| 56 | +- **Resource > Assign virtual machine to resource pool** |
| 57 | + |
| 58 | +## Configuration Parameters |
| 59 | + |
| 60 | +| Parameter | Description | Default | Required | |
| 61 | +|-----------|-------------|---------|----------| |
| 62 | +| `vsphere_server` | vSphere server hostname or IP | - | ✓ | |
| 63 | +| `datacenter` | vSphere datacenter name | `datacenter1` | ✓ | |
| 64 | +| `cluster` | vSphere cluster name | `cluster1` | ✓ | |
| 65 | +| `datastore` | Datastore for VM storage | - | ✓ | |
| 66 | +| `network` | Network/port group name | `VM Network` | ✓ | |
| 67 | +| `template_name` | VM template to clone from | - | ✓ | |
| 68 | +| `cpu_count` | Number of virtual CPUs | `2` | - | |
| 69 | +| `memory` | Memory in MB | `4096` | - | |
| 70 | +| `disk_size` | Primary disk size in GB | `50` | - | |
| 71 | + |
| 72 | +## Architecture |
| 73 | + |
| 74 | +This template provisions the following resources: |
| 75 | + |
| 76 | +### Infrastructure Components |
| 77 | +- **VMware vSphere Virtual Machine** - Primary compute resource |
| 78 | +- **Virtual Network Interface** - Connected to specified port group |
| 79 | +- **Virtual Disk** - Thin-provisioned storage on specified datastore |
| 80 | +- **Resource Pool Assignment** - VM assigned to cluster resource pool |
| 81 | + |
| 82 | +### Coder Integration |
| 83 | +- **Coder Agent** - Installed automatically via SSH provisioner |
| 84 | +- **Code Server** - Web-based VS Code interface |
| 85 | +- **JetBrains Gateway** - Support for JetBrains IDEs |
| 86 | +- **Workspace Metadata** - VM details displayed in Coder dashboard |
| 87 | + |
| 88 | +### Network Configuration |
| 89 | + |
| 90 | +The template supports various vSphere networking configurations: |
| 91 | + |
| 92 | +- **Standard vSwitches** - Traditional port groups |
| 93 | +- **Distributed vSwitches** - Enterprise networking with advanced features |
| 94 | +- **NSX Networks** - Software-defined networking integration |
| 95 | +- **DHCP or Static IP** - Configurable via guest customization |
| 96 | + |
| 97 | +### Datastore Management |
| 98 | + |
| 99 | +Supports multiple datastore types: |
| 100 | +- **VMFS** - Traditional vSphere datastores |
| 101 | +- **NFS** - Network-attached storage |
| 102 | +- **vSAN** - Software-defined storage |
| 103 | +- **Datastore Clusters** - Storage DRS for automated placement |
| 104 | + |
| 105 | +## VM Template Requirements |
| 106 | + |
| 107 | +Your vSphere VM template should meet these requirements: |
| 108 | + |
| 109 | +### Operating System |
| 110 | +- Ubuntu 20.04 LTS or later (recommended) |
| 111 | +- CentOS 8+ or RHEL 8+ |
| 112 | +- Other Linux distributions with SSH and cloud-init support |
| 113 | + |
| 114 | +### Required Software |
| 115 | +- **SSH server** - For Coder agent installation |
| 116 | +- **Cloud-init** (recommended) - For guest customization |
| 117 | +- **VMware Tools** - For better guest integration |
| 118 | +- **sudo access** - For the default user account |
| 119 | + |
| 120 | +### User Account |
| 121 | +Create a user account (e.g., `coder`) with: |
| 122 | +- sudo privileges without password prompt |
| 123 | +- SSH key-based authentication (optional but recommended) |
| 124 | +- Home directory with appropriate permissions |
| 125 | + |
| 126 | +### Example cloud-init Configuration |
| 127 | +```yaml |
| 128 | +#cloud-config |
| 129 | +users: |
| 130 | + - name: coder |
| 131 | + sudo: ALL=(ALL) NOPASSWD:ALL |
| 132 | + shell: /bin/bash |
| 133 | + groups: sudo, docker |
| 134 | + home: /home/coder |
| 135 | + |
| 136 | +packages: |
| 137 | + - curl |
| 138 | + - wget |
| 139 | + - git |
| 140 | + - vim |
| 141 | + - htop |
| 142 | + - docker.io |
| 143 | + |
| 144 | +runcmd: |
| 145 | + - systemctl enable docker |
| 146 | + - usermod -aG docker coder |
| 147 | +``` |
| 148 | +
|
| 149 | +## Security Considerations |
| 150 | +
|
| 151 | +### Network Security |
| 152 | +- Ensure proper firewall rules between Coder server and vSphere environment |
| 153 | +- Use VPNs or private networks for sensitive environments |
| 154 | +- Configure network segmentation for workspace isolation |
| 155 | +
|
| 156 | +### Access Control |
| 157 | +- Use dedicated service accounts with minimal required permissions |
| 158 | +- Implement vSphere role-based access control (RBAC) |
| 159 | +- Enable audit logging for vSphere operations |
| 160 | +
|
| 161 | +### VM Security |
| 162 | +- Keep VM templates updated with latest security patches |
| 163 | +- Use encrypted datastores for sensitive workloads |
| 164 | +- Implement guest-level security controls |
| 165 | +
|
| 166 | +## Customization Examples |
| 167 | +
|
| 168 | +### Custom VM Specifications |
| 169 | +```hcl |
| 170 | +# Add to data "coder_parameter" blocks for custom sizing |
| 171 | +data "coder_parameter" "custom_cpu" { |
| 172 | + name = "cpu_count" |
| 173 | + display_name = "CPU Count" |
| 174 | + default = "4" |
| 175 | + option { |
| 176 | + name = "High Performance (16 vCPUs)" |
| 177 | + value = "16" |
| 178 | + } |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | +### Multiple Network Interfaces |
| 183 | +```hcl |
| 184 | +# Add additional network interfaces |
| 185 | +network_interface { |
| 186 | + network_id = data.vsphere_network.management.id |
| 187 | + adapter_type = "vmxnet3" |
| 188 | +} |
| 189 | +
|
| 190 | +network_interface { |
| 191 | + network_id = data.vsphere_network.storage.id |
| 192 | + adapter_type = "vmxnet3" |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +### Additional Datastores |
| 197 | +```hcl |
| 198 | +# Add data disk on different datastore |
| 199 | +disk { |
| 200 | + label = "data-disk" |
| 201 | + size = 500 |
| 202 | + thin_provisioned = true |
| 203 | + unit_number = 1 |
| 204 | + datastore_id = data.vsphere_datastore.fast_storage.id |
| 205 | +} |
| 206 | +``` |
| 207 | + |
| 208 | +### GPU Passthrough |
| 209 | +```hcl |
| 210 | +# Enable GPU for AI/ML workloads |
| 211 | +resource "vsphere_virtual_machine" "vm" { |
| 212 | + # ... other configuration ... |
| 213 | + |
| 214 | + pci_device_id = [data.vsphere_pci_device.gpu.id] |
| 215 | + memory_reservation = tonumber(data.coder_parameter.memory.value) |
| 216 | +} |
| 217 | +``` |
| 218 | + |
| 219 | +## Troubleshooting |
| 220 | + |
| 221 | +### Common Issues |
| 222 | + |
| 223 | +#### 1. Template Clone Failures |
| 224 | +``` |
| 225 | +Error: error cloning virtual machine: The operation is not supported on the object |
| 226 | +``` |
| 227 | +**Solution**: Ensure the VM template is properly configured and not powered on. |
| 228 | + |
| 229 | +#### 2. Network Configuration Issues |
| 230 | +``` |
| 231 | +Error: network interface not found |
| 232 | +``` |
| 233 | +**Solution**: Verify the network/port group name exists in the specified datacenter. |
| 234 | + |
| 235 | +#### 3. Insufficient Permissions |
| 236 | +``` |
| 237 | +Error: permission denied |
| 238 | +``` |
| 239 | +**Solution**: Review and assign the required vSphere permissions listed above. |
| 240 | + |
| 241 | +#### 4. Agent Connection Timeouts |
| 242 | +``` |
| 243 | +Error: timeout waiting for agent to connect |
| 244 | +``` |
| 245 | +**Solution**: |
| 246 | +- Check SSH connectivity between Coder and the VM |
| 247 | +- Verify firewall rules allow traffic on required ports |
| 248 | +- Ensure the VM template has SSH server enabled |
| 249 | + |
| 250 | +### Debugging Steps |
| 251 | + |
| 252 | +1. **Verify vSphere Connectivity** |
| 253 | + ```bash |
| 254 | + # Test vSphere API access |
| 255 | + curl -k "https://$VSPHERE_SERVER/rest/com/vmware/cis/session" \ |
| 256 | + -X POST -u "$VSPHERE_USER:$VSPHERE_PASSWORD" |
| 257 | + ``` |
| 258 | + |
| 259 | +2. **Check VM Power State** |
| 260 | + - Verify VMs power on/off correctly based on workspace state |
| 261 | + - Check vSphere events for error messages |
| 262 | + |
| 263 | +3. **Network Troubleshooting** |
| 264 | + - Ping test between Coder server and VM |
| 265 | + - Verify DNS resolution if using hostnames |
| 266 | + - Check vSphere port group configuration |
| 267 | + |
| 268 | +4. **Agent Logs** |
| 269 | + ```bash |
| 270 | + # On the VM, check Coder agent logs |
| 271 | + journalctl -u coder-agent -f |
| 272 | + ``` |
| 273 | + |
| 274 | +## Performance Optimization |
| 275 | + |
| 276 | +### Resource Allocation |
| 277 | +- Enable CPU and memory hot-add for dynamic scaling |
| 278 | +- Use thin-provisioned disks to optimize storage utilization |
| 279 | +- Configure appropriate CPU/memory reservations for guaranteed resources |
| 280 | + |
| 281 | +### Storage Performance |
| 282 | +- Use SSD-backed datastores for better I/O performance |
| 283 | +- Enable Storage DRS for automatic load balancing |
| 284 | +- Consider vSAN for software-defined storage benefits |
| 285 | + |
| 286 | +### Network Performance |
| 287 | +- Use VMXNET3 network adapters for best performance |
| 288 | +- Configure distributed vSwitches for advanced networking features |
| 289 | +- Implement network I/O control for bandwidth management |
| 290 | + |
| 291 | +## Integration Examples |
| 292 | + |
| 293 | +### CI/CD Integration |
| 294 | +This template works well with CI/CD pipelines: |
| 295 | + |
| 296 | +```yaml |
| 297 | +# Example GitHub Actions workflow |
| 298 | +name: Deploy Development Environment |
| 299 | +on: |
| 300 | + push: |
| 301 | + branches: [develop] |
| 302 | + |
| 303 | +jobs: |
| 304 | + deploy: |
| 305 | + runs-on: ubuntu-latest |
| 306 | + steps: |
| 307 | + - uses: actions/checkout@v3 |
| 308 | + - name: Create Coder Workspace |
| 309 | + run: | |
| 310 | + coder create --template=vsphere-linux \ |
| 311 | + --parameter datacenter=prod-dc \ |
| 312 | + --parameter cluster=dev-cluster \ |
| 313 | + --parameter datastore=dev-storage \ |
| 314 | + dev-${{ github.sha }} |
| 315 | +``` |
| 316 | +
|
| 317 | +### Monitoring Integration |
| 318 | +```hcl |
| 319 | +# Add monitoring agent to startup script |
| 320 | +resource "coder_agent" "dev" { |
| 321 | + startup_script = <<-EOT |
| 322 | + # Install monitoring agent |
| 323 | + curl -sSL https://monitoring.company.com/install.sh | bash |
| 324 | + |
| 325 | + # Configure workspace-specific monitoring |
| 326 | + echo "workspace.name=${data.coder_workspace.me.name}" >> /etc/monitoring/config |
| 327 | + EOT |
| 328 | +} |
| 329 | +``` |
| 330 | + |
| 331 | +## Best Practices |
| 332 | + |
| 333 | +1. **Template Management** |
| 334 | + - Regularly update VM templates with security patches |
| 335 | + - Use automation tools like Packer for template creation |
| 336 | + - Maintain separate templates for different use cases |
| 337 | + |
| 338 | +2. **Resource Management** |
| 339 | + - Set appropriate resource limits to prevent over-allocation |
| 340 | + - Use resource pools to organize and limit workspace resources |
| 341 | + - Monitor resource utilization across workspaces |
| 342 | + |
| 343 | +3. **Backup and Recovery** |
| 344 | + - Implement regular VM snapshots for data protection |
| 345 | + - Use vSphere backup solutions for workspace data |
| 346 | + - Document recovery procedures |
| 347 | + |
| 348 | +4. **Cost Optimization** |
| 349 | + - Automatically power off idle workspaces |
| 350 | + - Use thin provisioning to optimize storage usage |
| 351 | + - Implement resource quotas and governance policies |
| 352 | + |
| 353 | +## Support |
| 354 | + |
| 355 | +For issues specific to this template: |
| 356 | +- Check the [Coder documentation](https://coder.com/docs) |
| 357 | +- Visit [Coder Community](https://github.com/coder/coder/discussions) |
| 358 | +- Review [vSphere provider documentation](https://registry.terraform.io/providers/hashicorp/vsphere/latest/docs) |
| 359 | + |
| 360 | +For vSphere-specific issues: |
| 361 | +- Consult VMware documentation |
| 362 | +- Contact your vSphere administrator |
| 363 | +- Check VMware support resources |
0 commit comments