The Oath of Enlistment for all military personnel includes the phrase "To support and defend the Constitution of the United States against all enemies, foreign and domestic, to bear true faith and allegiance to the same..."
So the primary allegiance is to the Constitution. However, the Constitution also appoints the President of the United States to be the Commander In Chief of all American military forces.