If purchasing cruise line insurance you can book it immediately even if you're not sailing for a long time. You need to purchase any insurance prior to the penalty period beginning. With the cruise line it's the only time you can, with outside providers it's the only way to be covered for pre-existing conditions. If you're booking a cruise already in penalty you need to buy it at the time you book.
If you purchase outside insurance (better coverage in most cases and usually lower cost) don't buy it until you near the penalty period. If you need to cancel your cruise outside of penalty there is no need for insurance. If you buy it early and then cancel, the cruise line doesn't have a penalty but most insurance is non-refundable so you've lost the cost of the insurance for no reason.
Be careful of booking with agencies that charge cancellation fees for cancellations outside of penalty. The cruise line does not but some agencies do.