Joab was already one of David’s main commanders. He had the authority to engage in battle with Abner and his men (even though it was a futile day), and he was leading raids. Later, he would become David’s right hand man.
But he was violent and he held a grudge.
Abner had just negotiated with David to deliver Israel to him, and Joab did this:
2 Samuel 3:27 When Abner arrived back at Hebron, Joab took him aside at the gateway as if to speak with him privately. But then he stabbed Abner in the stomach and killed him in revenge for killing his brother Asahel.
David wasn’t happy.
2 Samuel 3:38-39 Don’t you realize that a great commander has fallen today in Israel? And even though I am the anointed king, these two sons of Zeruiah—Joab and Abishai—are too strong for me to control. So may the Lord repay these evil men for their evil deeds.
He cursed Joab and his family, then went into mourning.
It was partly a political act to show he wasn’t responsible for it, but it must have also been genuine. Joab had just killed the man who was to call an assembly of Israel and convince them to make David their king.