It all started with King Roderic’s rise to power in the year of our Lord 710. The partisans of the former Visigoth King, along with Jews and Arians fleeing forced conversion, sought refuge in the North African domain of the disloyal Visigoth vassal Julian, the count of Ceuta. Contacted by the Muslim governor of Africa, the count offered his ships to help transport an invading army to Iberia, in exchange for lands in the newly conquered country.
The Muslim invaders disembarked near Gibraltar, and won a crushing victory at the battle of Guadalete, in July 711. Under the command of Tariq ibn-Ziyad, the following seven-year campaign brought most of the Iberian peninsula under Umayyad rule. When the Umayyad dynasty gave way to the Abbasid in 750, Abd al-Rahman I, an Umayyad exile, established himself as the Emir of Córdoba in 756. For the next century and a half, his descendants continued as emirs of Córdoba, with nominal control over the rest of al-Andalus (and sometimes parts of western North Africa) but with real control over the marches along the Christian border. Abdallah ibn Muhammad, who was emir around 900, had very little control beyond the area immediately around Córdoba.
Abdallah's grandson Abd-al-Rahman III, who succeeded him in 912, not only rapidly restored Ummayad power throughout al-Andalus but extended it into western North Africa as well. In 929 he proclaimed himself Caliph, elevating the emirate to a position competing in prestige not only with the Abbasid Caliph in Baghdad but also the Shi'ite Caliph in Tunis — with whom he was competing for control of North Africa.
Meanwhile, the Northern part of Iberia was still controlled by the Christians, and it was there that the Visigoth noble Pelayo established the Kingdom of Asturias. The Asturian Mountains provided a good defendable natural boundary to the South, and the Christians successfully defended themselves from attacks. Pelayo founded a dynasty in Asturias that survived for centuries and gradually expanded the kingdom's boundaries until all of northwest Iberia was included by roughly 775. The reign of Alfonso II from 791-842 saw further expansion of the kingdom to the south, almost as far as Lisbon, Portugal. The kingdom was known as Asturias until 924, when it became the Kingdom of León.
Almost immediately, León began to expand to the south and east, securing the newly gained territory with numerous castles. The newly added area was the County of Burgos until the 930s, at which time count Fernan Gonzalez of Castile began a campaign to expand Burgos and make it independent and hereditary. He took upon himself the title King of Castile, after the numerous castles in the area, and continued expanding his kingdom at the expense of León by allying with the Caliphate of Cordoba, until 966, when he was stopped by Sancho.
Constant rivalry between the two kingdoms opened rifts that could be exploited by outsiders, and Sancho III "the Great" of Navarre (1004–1035) absorbed Castile in the 1020s, and added León in the last year of his life, leaving Galicia to temporary independence. In the division of lands which followed his death, his son Fernando succeeded to the county of Castile.
The situation in 1030: