|
| 1 | +# Azure CNS Windows Service |
| 2 | + |
| 3 | +Azure CNS (Container Networking Service) can be registered and run as a Windows service, enabling automatic startup on system boot and automatic restart on failure. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Automatic Startup**: CNS starts automatically when the Windows system boots |
| 8 | +- **Automatic Recovery**: Service automatically restarts on failure (with 5-second delays) |
| 9 | +- **Event Logging**: Service lifecycle events are logged to Windows Event Log |
| 10 | +- **Service Management**: Easy installation and uninstallation via command-line flags |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +To install Azure CNS as a Windows service, run the following command with administrator privileges: |
| 15 | + |
| 16 | +```powershell |
| 17 | +azure-cns.exe --service install |
| 18 | +``` |
| 19 | + |
| 20 | +or using the short form: |
| 21 | + |
| 22 | +```powershell |
| 23 | +azure-cns.exe -s install |
| 24 | +``` |
| 25 | + |
| 26 | +This will: |
| 27 | +1. Register the service with the Windows Service Control Manager |
| 28 | +2. Configure the service to start automatically on system boot |
| 29 | +3. Set up automatic restart on failure |
| 30 | +4. Create an event log source for CNS |
| 31 | + |
| 32 | +After installation, you can start the service using: |
| 33 | + |
| 34 | +```powershell |
| 35 | +net start azure-cns |
| 36 | +``` |
| 37 | + |
| 38 | +or via the Services management console (`services.msc`). |
| 39 | + |
| 40 | +## Uninstallation |
| 41 | + |
| 42 | +To uninstall the Azure CNS Windows service, run the following command with administrator privileges: |
| 43 | + |
| 44 | +```powershell |
| 45 | +azure-cns.exe --service uninstall |
| 46 | +``` |
| 47 | + |
| 48 | +or using the short form: |
| 49 | + |
| 50 | +```powershell |
| 51 | +azure-cns.exe -s uninstall |
| 52 | +``` |
| 53 | + |
| 54 | +This will: |
| 55 | +1. Stop the service if it's running |
| 56 | +2. Remove the service from the Windows Service Control Manager |
| 57 | +3. Remove the event log source |
| 58 | + |
| 59 | +## Service Configuration |
| 60 | + |
| 61 | +The service is registered with the following configuration: |
| 62 | + |
| 63 | +- **Service Name**: `azure-cns` |
| 64 | +- **Display Name**: `Azure Container Networking Service` |
| 65 | +- **Description**: `Provides container networking services for Azure` |
| 66 | +- **Start Type**: Automatic |
| 67 | +- **Service Account**: LocalSystem |
| 68 | +- **Recovery Actions**: |
| 69 | + - First failure: Restart the service after 5 seconds |
| 70 | + - Second failure: Restart the service after 5 seconds |
| 71 | + - Subsequent failures: Restart the service after 5 seconds |
| 72 | + - Reset failure count after: 24 hours |
| 73 | + |
| 74 | +## Running as a Service |
| 75 | + |
| 76 | +Once installed, the service will automatically detect when it's being started by the Windows Service Control Manager and will run in service mode. No additional command-line flags are needed when the service is started by Windows. |
| 77 | + |
| 78 | +For testing purposes, you can explicitly run in service mode using: |
| 79 | + |
| 80 | +```powershell |
| 81 | +azure-cns.exe --service run |
| 82 | +``` |
| 83 | + |
| 84 | +## Event Logging |
| 85 | + |
| 86 | +Service lifecycle events are logged to the Windows Event Log under the Application log with the source name `azure-cns`. You can view these logs using: |
| 87 | + |
| 88 | +```powershell |
| 89 | +Get-EventLog -LogName Application -Source azure-cns -Newest 20 |
| 90 | +``` |
| 91 | + |
| 92 | +or via the Event Viewer (`eventvwr.msc`). |
| 93 | + |
| 94 | +## Troubleshooting |
| 95 | + |
| 96 | +### Service fails to install |
| 97 | + |
| 98 | +Ensure you are running the command prompt or PowerShell as Administrator. The service installation requires elevated privileges. |
| 99 | + |
| 100 | +### Service fails to start |
| 101 | + |
| 102 | +1. Check the Windows Event Log for error messages: |
| 103 | + ```powershell |
| 104 | + Get-EventLog -LogName Application -Source azure-cns -Newest 10 |
| 105 | + ``` |
| 106 | + |
| 107 | +2. Verify that all required configuration files are present |
| 108 | + |
| 109 | +3. Check that the executable path is correct |
| 110 | + |
| 111 | +4. Try running the executable directly (not as a service) to identify any configuration issues: |
| 112 | + ```powershell |
| 113 | + azure-cns.exe |
| 114 | + ``` |
| 115 | + |
| 116 | +### Service doesn't restart on failure |
| 117 | + |
| 118 | +The service is configured to restart automatically up to 3 times within a 24-hour period. If the service continues to fail, it will remain stopped. Check the Event Log for the root cause and fix the underlying issue before restarting the service. |
| 119 | + |
| 120 | +## Command-Line Reference |
| 121 | + |
| 122 | +``` |
| 123 | + -s, --service= Windows service action: install, uninstall, or run as service {install,uninstall,run,} |
| 124 | +``` |
| 125 | + |
| 126 | +**Available actions:** |
| 127 | +- `install`: Install Azure CNS as a Windows service |
| 128 | +- `uninstall`: Uninstall the Azure CNS Windows service |
| 129 | +- `run`: Explicitly run in service mode (typically not needed) |
| 130 | +- ` ` (empty): Normal execution mode (auto-detects if running as service) |
| 131 | + |
| 132 | +## Examples |
| 133 | + |
| 134 | +### Install and start the service |
| 135 | + |
| 136 | +```powershell |
| 137 | +# Install the service |
| 138 | +azure-cns.exe --service install |
| 139 | +
|
| 140 | +# Start the service |
| 141 | +net start azure-cns |
| 142 | +
|
| 143 | +# Verify the service is running |
| 144 | +Get-Service azure-cns |
| 145 | +``` |
| 146 | + |
| 147 | +### Stop and uninstall the service |
| 148 | + |
| 149 | +```powershell |
| 150 | +# Stop the service |
| 151 | +net stop azure-cns |
| 152 | +
|
| 153 | +# Uninstall the service |
| 154 | +azure-cns.exe --service uninstall |
| 155 | +``` |
| 156 | + |
| 157 | +### Check service status |
| 158 | + |
| 159 | +```powershell |
| 160 | +# Using PowerShell |
| 161 | +Get-Service azure-cns |
| 162 | +
|
| 163 | +# Using sc.exe |
| 164 | +sc query azure-cns |
| 165 | +``` |
| 166 | + |
| 167 | +### View service logs |
| 168 | + |
| 169 | +```powershell |
| 170 | +# View recent events |
| 171 | +Get-EventLog -LogName Application -Source azure-cns -Newest 20 | Format-Table -AutoSize |
| 172 | +
|
| 173 | +# View only errors |
| 174 | +Get-EventLog -LogName Application -Source azure-cns -EntryType Error -Newest 10 |
| 175 | +``` |
| 176 | + |
| 177 | +## Notes |
| 178 | + |
| 179 | +- This feature is only available on Windows platforms. On Linux, use systemd or other init systems to manage the service. |
| 180 | +- The service must be installed with administrator privileges. |
| 181 | +- The service runs under the LocalSystem account, which has high privileges. Ensure the executable and configuration files are properly secured. |
| 182 | +- Service configuration (command-line flags, config files) should be set via the Windows Registry or by using command-line parameters in the service's "Image Path" in the service properties. |
0 commit comments