For something less bulky ...we have used different options.
#1 . . . A steel closet rod . . . one end goes into a round section, one end into a u shaped channel. You have to instal (screw) the ends into the wall, but the pole can travel under the bunk when not in use. Look at Closet Organizers - The Hardware Hut
to see what I am talking about.
#2 . . . my father made bunk rails for us as kids from 3x3 wood. He went to a metal shop and had brackets made into a shape the would hold the wood. They worked really well, even with 4 rambunctious kids.