Barney left the show over his contract.
These things happen in Hollywood. It’s easy to get wrapped up in the world of Mayberry and forget that the people who populate that town have lives offset. On set, they are part of a business with many moving parts, some of which don’t always come together, like during contract negotiations. The terms of Knott’s contract were not agreeable to him, so five years into the production he resigned.