The Reds will take on the Greek champions on August 3 in their only pre-season match to be played at Anfield.
Proceeds from the event will go to the Steven Gerrard Foundation, while Olympiacos will donate their appearance fee to local charities in Greece.
Gerrard scored one of his most famous goals against Olympiacos in 2004, helping Liverpool come from behind to win 3-1 and keep them in the Champions League. They went on to win the competition that season.
Gerrard, who made his Liverpool debut almost 15 years ago, told the club's website: "I am delighted to be honoured by Liverpool with a testimonial match and particularly thrilled that Olympiacos have accepted my invitation to play the fixture. Last time we met proved to be such a memorable game both for me personally and Liverpool Football Club.
"I am also especially grateful that Olympiacos have truly embraced the aim of this match and chosen to donate their fee to charity. I am looking forward to what I know will be a fantastic game and a wonderful occasion."