Kiev and Moscow have agreed to extend the lease allowing Russia's Black Sea Fleet to be stationed in Ukraine by 25 years in return for cheaper gas.
The deal was signed by Ukrainian President Viktor Yanukovych and his Russian counterpart Dmitry Medvedev.
The lease of Russia's naval base in Sevastopol, Crimea, had been due to expire in 2017.
It was not immediately clear if the extension would be effective from now or from 2017.
Mr Yanukovych's pro-Western predecessor, Viktor Yushchenko, had opposed any extension of the lease.
Mr Yushchenko, who was ousted in presidential elections earlier this year, had wanted Ukraine to join Nato - a move strongly opposed by the Kremlin.
Mr Yanukovych and Mr Medvedev signed the agreement after talks in Ukraine's eastern city of Kharkiv.
Mr Yanukovych, who had pledged to deal with all issues causing tensions between Kiev and Moscow during the Yushchenko administration, said the deal was agreed because "it was important for our Russian colleagues and friends".
He said that the move would have a positive impact for stability in the Black Sea region and Europe.
The deal also has a clause allowing the lease to be extended for a further five years.
In return, Russia agreed to grant Ukraine a discount on imports of Russian gas.
The complicated formula would give Ukraine a discount of $100 for every 1,000 cubic metres of gas providing the benchmark rate was above $330 or a discount of 30% if the rate was lower than that.
Ukraine currently pays $305 per 1,000 cubic metres.
In the past few years, tensions between Moscow and Kiev have led Gazprom, the Russian state-owned energy giant, to switch off the taps on a number of occasions, affecting a number of European countries.
Europe gets a quarter of its gas from Russia, and most of that arrives via an extensive network of pipelines running through Ukraine.