In conventional DC microgrid control schemes, optimization and real-time control are usually performed at different time scales. It is hard for such control schemes to achieve real-time optimization. Even a small disturbance can result in deviations of bus voltages and output currents from their optimal operating points. In addition, most real-time controllers cannot guarantee the satisfaction of pre-defined constraints on individual bus voltages due to the disconnection between steady-state optimization and real-time control. In this paper, a distributed optimal control scheme is presented for DC microgrid. The objectives are to simultaneously realize generation cost minimization and individual bus voltage regulation. First, the optimal control problem is formulated, based on which a necessary and sufficient optimality condition is derived that characterizes the optimal operating points. The distributed optimal controller is then proposed to dynamically drive the system to operate at the optimality condition. Convergence to the optimal operating points that minimizes generation cost under constraints is guaranteed through rigorous Lyapunov synthesis. Each individual bus voltage can also be maintained within bounds in both transient- and steady-state. The performance of the proposed controller is validated through simulations based on a switch-level microgrid model.