I spend on average 4 hours a day underwater in tropical seas loaded with bacteria.
Protective ear gear is available, however it impedes the ears natural ability to equalize pressure under water and can easily lead to permanent barotrauma (pressure injuries). I have been diving recreationally for 20 years and professionally for 4 and have never met a diver who uses them.
Typically they take the form of a mask with "ear muffs" that are connected to the the front of the mask by hollow tubes that allow you to equalize pressure. I tried them once and found them ineffective and borderline dangerous.
The most effective way that I have found to prevent ear infections while diving is to use "ear beer" drops that are equal parts alcohol and white vinegar. Some divers add oil to make a nice vinaigrette. I am being totally serious.
I use the drops for 5 minutes in each ear after I shower before bed, and after every single dive I clean my ears with fresh water.