An eight-game winning streak — with the final six against two of the best teams in the American League — had the Yankees riding high.

On Sunday, though, they got a bit of a wake-up call from Justin Verlander and the Astros, who pounded them, 9-4, in The Bronx, preventing a Yankees’ sweep.

“It’s a bad way to end it, but we’ve got to take the positives out of it,’’ Luke Voit said of the seven-game stretch against the Rays and Houston. “Those were [two] potential playoff teams, so to win six of seven is pretty good.’’

Though one loss — especially after their longest stretch of success of the season — is hardly alarming, there were some red flags, as J.A. Happ was pounded in one of the worst starts of his career in another disappointing outing.

Happ, coming off a solid start against the Rays in which he didn’t give up a homer, was bad from the jump Sunday following Mariano Rivera’s debut at Old-Timers’ Day.

The left-hander gave up a blast to Jose Altuve that reached the second deck in left to lead off the game.

The Astros put the game out of reach in the fourth, with three consecutive singles to load the bases before Tyler White launched a grand slam to right, giving Houston a 6-0 lead.

Happ’s day didn’t end until Yordan Alvarez hit a monster two-run homer into the second deck in right in the fifth and he was booed loudly after finally being pulled.

It’s the third time this season has given up at least six runs in an outing. He matched career highs by giving up eight runs, three homers and 11 hits.

And his home ERA rose to 6.29, with 13 of his 20 homers allowed in nine starts in The Bronx.

“Today was a game, if possible, you try to flush,’’ said Happ, speaking for himself, as well as the team.

“We had a good series. I’d hope we’d get the sweep, but I put us in a bad spot. … We’re capable of coming back from anything, but that was a tough one today.”

Voit agreed.

“Today was one of those days,’’ the first baseman said. “It’s difficult to come back from that deficit sometimes.’’

Following DJ LeMahieu’s single to start the bottom of the first, the Yankees didn’t get another hit off Verlander until Aaron Hicks singled with two outs in the fifth, which led to a mock cheer from the angry crowd.

Brett Gardner followed with another single to left and LeMahieu got the Yankees on the board with a three-run homer to make it 9-3. The Yankees have homered in 26 consecutive games, a franchise record and one shy of the MLB mark set by the Rangers in 2002.

LeMahieu’s home run provided the only runs scored off Verlander, who struck out nine in seven innings. In six outings against the Yankees since joining the Astros — including the postseason — Verlander is now 4-0 with a 1.65 ERA.

“His offspeed [stuff] wasn’t really working for him,’’ Voit said. “He relied on his fastball a little more.’’

The Astros — without the injured Carlos Correa and George Springer — snapped a seven-game losing streak, their longest since 2015.

Luis Cessa gave up a homer to Yuli Gurriel, but helped preserve the bullpen by pitching four solid innings in relief of Happ.

And if the two teams end up where they would like, they could face each other down the road.

“They’re obviously a great team,’’ Aaron Boone said. “We may again cross paths — and for a lot more [at stake]. We always feel like we match up well with other clubs. If we’re playing our best, I always love our chances. You know you’ve got to bring it against these guys.’’