Gus Fring initially hired Walter for 3 months for his service, which he later extended it to 1 year. Of course, he wanted Gale to learn Walter's formula and replicate it eventually since Walter was more like a consultant and he had cancer.
But the way I see it was Gus wanted to have a back up chemist, since the cartels wanted Gus' chemist to cook for them, and this demand coincided with Walter killing his two dealers. Walter then panicked when Gale started to ask too much details about his methods and had him killed and he then wanted Gus killed. I mean Gus did tell Walter that Jesse should have let him deal with the dealers regarding the murder of the boy, So maybe Gus was not without reason.
Gus may have wanted to have a back up chemist since the last time he took a cook there, he was shot in the head. So hence he wanted Gale to learn the Walter's method ASAP.
After Gale's death, Gus went to Jesse, since Jesse had been with Walter from the start and he was right to assume that Jesse would be able to replicate Walter's product. He took Jesse to the Cartels, and Cartel being a prick tried to keep Jesse and Gus had to kill them all to keep Jesse and for revenge.
Did Gus really wanted to kill Walter and have him replaced with Gale or was this all Walter's fear?