The story
Commonly claimed to have come from the King James Version of the Bible, Book of Proverbs, 13:24: “He that spareth his rod hateth his son: but he that loveth him chasteneth him betimes.” Due to the associated “spoil" concept which is not in the Bible, it more likely came from a 17th-century poem by Samuel Butler called Hudibras. In the poem, a love affair is likened to a child, and spanking is mockingly commended as a way to make the love grow stronger. The actual verse reads: : What medicine else can cure the fits Of lovers when they lose their wits? Love is a boy by poets styled Then spare the rod and spoil the child.
Etymology from Wiktionary, CC BY-SA 4.0.