Wolfram Language (Mathematica), 45 bytes
Mod[#-{0,##&@@Most@#}&/@Range@#~Tuples~#2,#]&
A port of @ovs's BQN answer.
Wolfram Language (Mathematica), 59 bytes
IntegerDigits[FindHamiltonianPath@GridGraph@Table@##-1,##]&
Finds a Hamiltonian path of the \$\underbrace{N\times\cdots\times N}_{k}\$ grid graph.