Moses has been speaking of the era in which judges will rule in Israel. But a day is coming when the people will prefer a king to judges, saying, "Appoint a king to lead us, such as all the other nations have." (1 Samuel 8:5b) Israel's request for a king will not take place until about four hundred years after Moses provides the instructions regarding a king here in Deuteronomy 17. The Lord knew Israel would someday ask for a king so He provided the rules for choosing one and the rules for the king to live by.
It has been debated by scholars whether or not the Lord ever wanted Israel to have a king. On the one hand, when the prophet Samuel comes to the Lord in distress because the people have rejected him as judge (due to his age) and because they have rejected his sons as judges (due to their questionable morals), the Lord says to Samuel, "It is not you they have rejected, but they have rejected Me as their king." (I Samuel 8:7) This statement suggests that the Lord is not pleased with Israel's desire for a king. On the other hand, the Lord said many centuries earlier to Jacob, "Kings will be among your descendants," (Genesis 35:11b) and indicated that the family line of Jacob's son Judah would be the royal line of Israel. (Genesis 49:10) Whether this means it was God's perfect will for Israel to have kings or whether this means it was His permissive will for Israel to have kings, I cannot say.
Because the Lord knows all things, four hundred years before Israel requested a king He told Moses what the people must look for in a king. He told Moses how the king must behave after he assumes the throne. Moses passed this information on to the congregation of Israel.
"When you enter the land the Lord your God is giving you and have taken possession of it and settled in it, and you say, 'Let us set a king over us like all the nations around us,' be sure to appoint over you a king the Lord your God chooses." (Deuteronomy 17:14-15a) Just as the Lord often does with us, He's going to give Israel what she wants. You and I may want a particular thing that's not necessarily the best plan for us but we'll plead for it and plead for it until the Lord lets us have it. A lot of times we'll realize after we get it that we were better off without it but we are stubborn children who sometimes have to be shown that the Lord knew best all along. The Lord is going to allow Israel to have a king, and we can't be sure whether this was the best plan for the nation or not, but if Israel is going to have a king the people must consult the Lord before making their choice.
Israel's first king, Saul of the tribe of Benjamin, was anointed by Samuel upon the Lord's request in 1 Samuel 9:15-16. Does this mean it was the Lord's will for Israel to have a king or that it was His will for Saul the Benjamite to be elected? I think yes and no. The Lord knew Israel intended to have herself a king and would eventually select one with or without His permission, He knew Saul was capable of doing some good for a while (He said that Israel, who was crying out to Him for help from the Philistines, would find relief from the Philistines through the military prowess of King Saul), and He was granting their displeasing request to have a king "like all the nations around us" perhaps as a form of discipline for acting against their own best interests in rejecting Him as King. Saul did not turn out to be a spiritually righteous man or even a man of good worldly morals in the end. In that sense he was just like the kings of other nations---heathen kings who did not acknowledge the Lord or live by His holy precepts.
Saul was anointed king by Samuel upon instruction of the Lord in 1 Samuel 9 and then in 1 Samuel 10 the prophet called the entire nation together so lots could be cast to choose the man to lead Israel. At this assembly the Lord said through Samuel, "I brought Israel up out of Egypt, and I delivered you from the power of Egypt and all the kingdoms that have oppressed you. But you have now rejected your God, who saves you out of all your disasters and calamities. And you have said, 'No, appoint a king over us.' So now present yourselves before the Lord by your tribes and clans." (1 Samuel 10:18b-19) The Lord was in charge of how the lot fell, for He'd already pointed Saul out to Samuel, but the Lord was giving Israel a king like that of other nations: an outwardly impressive man. Saul was from an influential family. 1 Samuel 9:1 tells us that Saul's father Kish was "a man of standing". Saul was the most handsome man in all Israel and was of unusually tall stature, standing a head taller than anyone else. (1 Samuel 9:2) To the human way of thinking, he was the perfect man for the job. He came from a background of wealth and influence. His looks were capable of making anyone stop and stare at him. He was tall enough to look intimidating to other kings and other armies, and as we've already learned in the Old Testament, some of the kings and some of the soldiers Israel would fight in battle were so tall they were called "giants". Yet where was this perfect man for the job when the lot fell on him? He was hiding! (1 Samuel 10:21-22) He knew the lot would fall on him because he'd already been anointed by Samuel but already he was resisting the will of the Lord, which would become a more and more obvious pattern of behavior throughout his reign.
When presenting Israel's first king to her, Samuel said of Saul, "There is no one like him among the people." (1 Samuel 10:24a) No one in Israel on that day had a more impressive outward appearance or a more impressive resume than Saul, even if he did have to be dragged out from behind sacks of grain and jars of water to be presented to the people. They shouted, "Long live the king!" (1 Samuel 10:24b) And then Samuel "explained to the people the rights and duties of kingship". (1 Samuel 10:25a) Samuel explained to the people the rights and duties Moses is discussing in Deuteronomy 17, which we are studying today and tomorrow.
Whether or not it was ever God's perfect plan for Israel to have a king, He knew Israel would ask for a king and He provided the rights and duties of kingship for the nation to follow. Israel's first king, Saul the Benjamite, was not a godly man but God doesn't always put a man in power who will be a blessing to a nation in every way. Sometimes God allows a leader to rise to power in order to discipline a nation. Saul did some good things for Israel and Saul did some bad things for Israel. By the time his reign was finished, I think the people longed for a king who had a heart for the Lord. They longed for a godly man who would be a blessing upon them. The Lord already had a man picked out, a man whom He said had a heart like His. (1 Samuel 13:14) This man will be David of Bethlehem, of the line of Judah.
I do not believe the Israelites would have accepted David as king if he had been presented to them first. He was still a teenager when Saul was crowned king. He was not overly tall or impressive; he was a young skinny shepherd boy. He had a heart for the Lord but, when requesting a king like all other nations had, the people weren't looking at hearts. They were looking at the outward appearance. Saul fit the bill and David didn't. But by the time Saul's reign ends, David will be a battle-hardened grown man whose troubles and trials have made him not only physically strong but also spiritually strong. David's primary qualification for the kingship will be that he has a heart like the Lord's and that the Lord intends to use him to bless Israel, but he will also possess the power and reputation of a mighty man by the time he ascends to the throne. He will fit the bill in every way, both in the outward appearance and in the inward places of the heart.
It's always best for us to consult the Lord and obey His instructions in everything we do. It's best to do things in His way and in His timing. But even when we mess up and get out of sync with Him, He is merciful to us. It may not have been His will for Israel to ask for a king. Or it may have been His will eventually but they asked for a king at the wrong time. Or it may have been the right time but they wanted the wrong kind of man. And yet all was not lost. The Lord didn't let the nation fall. When Saul was at the pinnacle of his wickedness, the Lord removed him and installed a better man in his place. In so doing He helped the people to see where they had gone wrong but at the same time He blessed them. I think they probably repented of wanting a king like other nations had. I think they regretted thinking a man like Saul would be a blessing to them. This put them in a position to receive blessings from the Lord.
There have been times when I've wanted something so badly that I've gone against the Lord's will. There have been other times when what I wanted was also His will for me but not yet. I got ahead of Him. In both these types of situations I made a mess out of things. But God has been gracious to me. He keeps helping me get back on track. This isn't a license for us to sin and get outside of the will of God or get ahead of God. We always bring hardship on ourselves when we do that, so even if the Lord helps us out of our troubles and keeps us from ruining our lives, we're still going to have to face some consequences of our poor decisions. That's why the best thing we can do is seek His will right from the start. We'll avoid a lot of heartbreak if we do.