2 Samuel 8:18

And Benaiah the son of Jehoiada was over both the Cherethites and the Pelethites; and David’s sons were chief rulers.

The verse belongs to the account of continued wars between Israel and Judah ending in David's triumph as king over all the tribes, presenting God as the one who chooses the rulers of his people.
The chapter shows David extending Israel by conquest eastward to the Euphrates and possessing the Syrian kingdom and Damascus, so that his enemies were subdued, Israel became a powerful kingdom, and David had rest in a settled, peaceful period.

In that settled rest David turned his thoughts to building a permanent house for God and the ark.
God would not let David build that house because he had been a warrior and had shed much blood, reserving the peaceful work for another.
The extent of power and prosperity produced ease and wealth that blinded and hardened David, forming the setting in which his sin and the resulting troubles began.

Benaiah is listed among the chief officers as the one over the Cherethites and the Pelethites, exercising priestly standing with control over divisions of the army.
David's sons being chief rulers means they served as priests, an office interchangeable with that of high priest; they hold the same position in the list that the chief priest holds among Solomon's officers, and the Latin Vulgate renders the term as priests.
The naming of David's sons as chief rulers stands among later biblical examples of Israel practicing things God had prohibited.