Cities of the second class and villages shall have power to provide for the erection of all needful pens and pounds within or without their corporate limits, to appoint and compensate keepers of such pens and pounds, and to establish and enforce rules governing such pens and pounds.