So the title duke originally comes from the Roman dux, which was a general that led two or more legions – often the governor of a province.
As we move into the post-Roman world, they become the war chieftains, the rulers of the old Roman provinces. Some of the early literature around King Arthur describes him as the dux bellorum (duke of battles or war chief) of the Romano-Britons. The point is that – at least in the early middle ages – the title of duke had a strong military connotation. In the Frankish regions, the dukes were the highest ranking officials in the realm and were the men the Merovingian kings would choose their generals from. They met once a year in May to set policy for the coming year.
Dukes could be king’s men or they could be sovereign, as with the Dukes of Luxembourg and Burgundy – those duchies weren’t part of a kingdom per se. Dukes came from these old families that had served as war chiefs, but increasingly it became a title granted to sons of the monarch and their families (royal dukes). Some of these titles had an associated duchy, others were simply titles without estates.
Note also that there were ecclesiastical ducal titles and secular ducal titles. The most important ecclesiastical dukes were the Archbishop of Reims (archevêque-duc pair de France) and the two Suffragan Bishops (evêque-duc pair de France, specifically: bishop-duc de Laon and bishop-duc de Langres).
Anyway, dukes held vast estates and controlled significant military powers that in some cases rivaled the forces the king could muster from his own estates. In many cases, the king is just the first among equals in the council of dukes.
A count/earl could be a baron, but wasn’t necessarily. If the count holds a county directly from the king, he’s a baron too. But he could hold a county subinfeudated from a duke’s lands, for instance.
Remember that a nobleman might hold a county or two from the king, another county or two from a duke within the kingdom, and possibly more estates from another sovereign altogether. In such cases, the nobleman must decide which will be his liege-lord such that if conflicts arise between his various lords (which was not an infrequent occurrence), he knows which side to back.