Why is it needed?

VLANs are isolated from each other. Layer 3 routing is required for communication between them. Router-on-a-stick uses a trunk between a router and a switch.

interface g0/0
 no shutdown
interface g0/0.10
 encapsulation dot1Q 10
 ip address 192.168.10.1 255.255.255.0
interface g0/0.20
 encapsulation dot1Q 20
 ip address 192.168.20.1 255.255.255.0

The switch port connected to the router must be a trunk. Each subinterface IP is used as the gateway for its VLAN.

Verification

Use show ip interface brief, show interfaces trunk and ping.