A necessary evil

Meaning: Something unpleasant that must be accepted because it is necessary.

Example: Paying taxes may feel like a necessary evil, but they fund public services.