This paper investigates the dynamic deployment of the 5G core network user plane functions (UPFs) in the edge network under time-varying traffic conditions. The objective is to minimize the total cost, including energy consumption, user plane latency, and UPF deployment costs by jointly optimizing UPF placement and traffic routing over the entire planning horizon. We formulate the optimization problem as a mixed-integer linear programming (MILP) problem, which is proven to be NP-hard. To obtain the optimal solution for the problem, we propose a Benders decomposition-based algorithm. However, the high computational complexity of this algorithm limits its practical applicability for large-scale networks. Therefore, we propose a low-complexity algorithm based on nested Benders decomposition that can solve the problem suboptimally. The simulation results demonstrate the convergence and computational efficiency of the proposed algorithm and show that it outperforms two existing algorithms. Moreover, we demonstrate that cost-saving can be achieved by using more power-efficient servers or deploying more servers. Additionally, we show that adjusting the cost parameters can achieve a good tradeoff between energy consumption and user plane latency.