Yes, a 2 kΩ resistor is sufficient to limit the current to about 12 mA through the optocoupler. With a maximum current specification of 60 mA you require at least 400 Ω; any resistor in between the values should be suitable for this optocoupler component when used at 24 V. More information is found below.
Having a glass bed and using an inductive probe may not work optimally. In such cases a 3D touch sensor is a very good alternative. For inductive sensors, remember that these sensors optimally detect steel, aluminium is harder to detect (about 60 % less than iron; a glass sheet in between the bed and the sensor requires you to acquire a sensor with a large detection range, e.g. 8 mm or more. To optimize the detection distance such sensors need to be powered to the maximum voltage they can handle (usually 36 V) or the highest voltage you have available (e.g. power supply 12 or 24 V). To protect the board, that is only allowed to receive up to 5 V, an optocoupler is an excellent way to guarantee the separation of voltage levels.
Considering your questions, it is fair to assume you are not an experienced electronics tinkerer, purchasing an optocoupler module is the best alternative, you just screw the wires into the respective input screw terminals as shown in this answer.
As the optocoupler separates two circuits, you can safely use 24 V on the one side and the 5 V on the other side, you do not need an additional resistor when you use a module board. If you plan to buy separate components to build your own circuit, you need to look at the maximum current that the optocoupler can handle on the input side (that will be 24 V). From the documentation from the optocoupler one reads that it is limited to 60 mA. A maximum current would therefore require a resistor of:
$$ R=\frac{U}{I}=\frac{24}{60\times10^{-3}}=400\ \Omega $$
My 12 V optocoupler module uses a 1 kΩ resistor implying a 12 mA current. In your case allowing 12 mA of current yields a 2 kΩ resistor.