‘Night cramps’ are pretty common and get more common as we age. So they seem to be more age-related and are definitely not a problem specific to keto. I suffered them for years prior to eating keto. When they started being a real problem for me several years ago I was shocked to discover their specific cause and cure are unknown. Although, as @PaulL points out the most likely suspects are potassium and/or magnesium deficiency. My layman’s guess is that as we age we tend to ingest less electrolytes in our diet, particularly potassium, and the balance required to maintain healthy muscle function tends to go off kilter more and more.
Sodium in interstitial fluid and potassium within cells have to be in balance for the muscle cells to contract properly. When they’re out of balance, we get the cramps. By the way, not limited only to the feet, ankles and calves either! I often get cramps in my sides and back when pulling my socks on in the morning and even sometimes during the day when I simply stretch my arms to the sides!
Since keto restores normal kidney function, we tend to lose more electrolytes than non-keto folks (along with a lot of other water soluble waste, so that’s a good thing). But it does mean that we have to ingest more to replenish. My opinion is that sodium is prevalent in food and probably requires less supplementation to maintain a healthy level. Whereas potassium is not so and probably requires more supplementation. The daily requirement for magnesium is fairly low, so I suspect it requires least supplementation of the three.
In another topic I linked to a study about the prevalence of hyperkalemia in the US. At least half of the people with hyperkalemia suffered from chronic kidney disease and/or heart failure. Unless you are one of those people, I think you would purposely have to work at supplementing potassium to get there. Keep in mind that sodium seems to be the overall controlling electrolyte, so unneeded excess of any of the three will get excreted quickly as long as you get sufficient sodium.
Finally, calcium is also part of the equation, but I don’t know if its exact role is any better known than that of the other electrolytes. Most people as they age tend to consume less calcium, even losing much of what they already have. If you don’t eat sufficient calcium the metabolism will take it from your bones which is why so many elderly folks suffer not only night cramps but osteoporosis.
In my case, experimenting with electrolyte supplementation I have managed to reduce the severity of my night cramps to the point where if one starts, I can usually stop it by simply relaxing the affected area. Previously, I was not able to do so and had to endure the pain and contractions until they stopped of their own accord. Also, I can actually go for several nights in a row without getting any! This never occurred before I started supplementation. It was a nightly thing, something that often deterred me from going to bed just to avoid what I knew was coming.