It was told Joab, “Behold, the king is weeping and mourning for Absalom.” So the victory that day was turned into mourning for all the people, for the people heard that day, “The king is grieving for his son.” And the people stole into the city that day as people steal in who are ashamed when they flee in battle. The king covered his face, and the king cried with a loud voice, “O my son Absalom, O Absalom, my son, my son!” Then Joab came into the house to the king and said, “You have today covered with shame the faces of all your servants, who have this day saved your life and the lives of your sons and your daughters and the lives of your wives and your concubines, because you love those who hate you and hate those who love you. For you have made it clear today that commanders and servants are nothing to you, for today I know that if Absalom were alive and all of us were dead today, then you would be pleased. Now therefore arise, go out and speak kindly to your servants, for I swear by the Lord, if you do not go, not a man will stay with you this night, and this will be worse for you than all the evil that has come upon you from your youth until now.” Then the king arose and took his seat in the gate. And the people were all told, “Behold, the king is sitting in the gate.” And all the people came before the king. 2 Samuel 18:1-8.
When Joab learns that David is weeping over his son, the traitor, who sought his life, he has had enough. He gives David a classic tongue-lashing, and it seems to work. But the king gets the last word and replaces Joab with Absalom’s commander. And before David dies, he will tell his son, Solomon, to execute Joab for the things he has done.
Still, Joab saved David’s bacon on several occasions, and was loyal to him when he thought his king was in danger. Even if he had to disobey a direct order, he made hard decisions that would cost him his life.