Wikipedia says, the president is the commander-in-chief, if I understand correctly, he delegates to the secretary of defense, who commands the military.
This certainly sounds like there is nothing the military can (legally) do against the president. They basically have to obey him.
Is there a legal way for the military to disobey? Have there been any precedences of the military going against the president, apart from outright coup?
Only the highest ranks of the military have to deal with treasonous presidents, etc. Guess what? There are laws and regulations to deal with that situation as well. Hopefully, the top military leaders are in the process of protecting the US constitution from an illegitimate, belligerent civilian government right now - and are following the rules and regulations that apply. Otherwise, it would be a military coup.
The point is - everyone else except those top ranking leaders just follow the orders in their chain of command - as long as those orders are lawful. In practice, they are lawful pretty much 100% of the time.
In the years that I served, I never heard of anyone refusing to follow unlawful orders, for example. That does not mean it never happens, but it is very rare.