The evolution of the Internet of Things (IoT) is imposing many distinct challenges, particularly to guarantee both wide and global systems communication, and to ensure an optimal execution of services. To that end, IoT services must make the most out of both cloud and fog computing (turning into combined fog–cloud scenarios), which indeed requires novel and efficient resource management procedures to handle such diversity of resources in a coordinated way. Most of the related works that can be found in the literature focus on resource mapping for service-specific execution in fog–cloud; however, those works assume a control and management architecture already deployed. Interestingly, few works propose algorithms to set that control architecture, necessary to execute the services and effectively implement services and resource mapping. This paper addresses that challenge by solving the problem of optimal clustering of devices located at the edge of the network offering their resources to support fog computing while defining the control and management role of each of them in the architecture, in order to ensure access to management functions in combined fog–cloud scenarios. In particular, we set out the Fog–Cloud Clustering (FCC) problem as an optimization problem, which is based on multi-objective optimization, including realistic, novel and stringent constraints; e.g., to improve the architecture's robustness by means of a device acting as a backup in the cluster. We model the FCC problem as a Mixed Integer Linear Programming (MILP) formulation, for which a lower and an upper bound on the number of required clusters is derived. We also propose a machine learning-based heuristic that provides scalable and near-optimal solutions in realistic scenarios in which, due to the high number of connected devices, solving the MILP formulation is not viable. By means of a simulation study, we demonstrate the effectiveness of the algorithms comparing its results with those of MILP formulation.