The port microgrid cluster, which integrates berth allocation and energy scheduling for joint optimization, is a highly interconnected system of logistics and energy closely coupled microgrids. It can reduce port load fluctuations and ensure the stable and reliable operation of the port energy system. A port berth allocation and microgrid cluster joint optimization scheduling method based on master-slave game is proposed to address issues such as large fluctuations in current port loads, vulnerability of port power supply system to the impact of vessel loading and unloading operations, and conflicts of interest among various participants in the power market. To ensure power quality while achieving peak shaving and valley filling, a two-stage optimization model for microgrid cluster was constructed, enabling joint optimization of berth allocation and energy scheduling. In the first stage, considering the impact of random events during vessel navigation, berth allocation is conducted with the objectives of maximizing peak shaving and valley filling rates and minimizing overall costs, thereby achieving preliminary load leveling while ensuring power supply system quality. In the second stage, dispatching is performed for power generation equipment and adjustable loads, aiming to further achieve peak shaving and valley filling while maintaining the economic viability of the microgrids. Addressing conflicting interests among stakeholders in the port's electricity market, a master-slave game strategy has been established. This strategy aims to balance the interests between the electricity trading agent and the microgrid aggregator composed of multiple microgrid models. By solving the second-order partial derivatives of each optimization variable with respect to the objective function, we demonstrate the existence and uniqueness of the equilibrium solution in the master-slave game. In response to the challenge of difficulties in solving the multi-objective optimization model due to conflicting objective functions, a Dung Beetle Optimization (DBO) algorithm incorporating non-dominated sorting strategy and crowding distance was introduced. This resulted in a novel multi-objective optimization algorithm termed Non-Dominated Sorting Dung Beetle Optimizer (NSDBO). The NSDBO algorithm was demonstrated to possess favorable characteristics in terms of distribution, convergence, and uniformity. Using a northern Chinese port as an example, berth allocation and energy scheduling for three port areas were jointly optimized, and various scenarios were compared. The results indicated that, compared to Scenario 1, which did not consider random events, the proposed joint optimization method increased the peak shaving and valley filling rate by 8.46 %, reduced network losses by 42 kW, and increased the revenue of the microgrid aggregation merchant by 2.11 %. Compared to Scenario 2, which only considered energy scheduling, the joint optimization increased the peak shaving and valley filling rate by 36.89 %, reduced network losses by 18 kW, and increased the revenue of the microgrid aggregation merchant by 2.29 %. Compared to Scenario 3, which was individually optimized, the joint optimization increased the peak shaving and valley filling rate by 48.42 %, reduced network losses by 24 kW, and increased the revenue of the microgrid aggregation merchant by 2.73 %. This validates the superiority of the proposed joint optimization method in mitigating the impact of random events, reducing network losses, stabilizing port load fluctuations, and enhancing the revenue of the microgrid cluster.