Sergio Perez is to leave Red Bull with two years left on his contract, after reaching a termination agreement with the team.