Andrew Smith is a 3rd-degree BJJ black belt based out of Richmond, VA (Revolution BJJ). He runs BJJ Path, a video tutorial website.
The kneebar is roughly akin to the armbar with regard to conceptual simplicity: the general idea is to hyper-extend the joint beyond its normal range. However, below-the-waist submissions tend to have something of a mystique, aren't frequently taught at lower levels at many gyms (partly due to tradition and partly due to the rules of the sport), and sometimes aren't well understood. As such, escapes tend to elude even upper belts in BJJ at times. Here are two different approaches to escaping the submission; like with the armbar, it can be useful to break the escape options up into "early" and "late" stage techniques.
The early stage defense definitely involves moving at the right time, right as your partner begins their backstep technique. As you're pummeling for the underhook during a classic knee cut guard pass, your partner responds by overshooting intentionally, then stepping backward, ultimately looking to set up a kneebar (or, alternatively, to pass your guard). This is often in response to the nudge of your knee to their butt, driving them forward. While you are doing this "nudge," your knee should be bent. Keep it bent while your partner starts to step back, and catch their leg with the front of your ankle, right as your shin connects with your foot. Use this "same side" hook to off-balance your partner, attempting a sweep. If you get the sweep, great; if not, you can still use this technique as a guard recovery.
Often times with a kneebar, your partner will get there well before you have the opportunity to hook the back of their leg with your foot, nullifying your early stage defense. In this situation, start by gripping your partner's hip on the same side as they're attacking your leg. Next up, hip out away from the leg they're attacking, ultimately creating a lot of space. Extend your leg as they fall for the kneebar, but be sure to rotate your toes at a right angle (remember that wherever your toes turn, so must your knee). Finish escaping by pushing on their knee with your free leg (not by stepping on their butt, as I've seen taught many times in the past). Use a "row boat" motion with your arms in order to facilitate moving your body away.
This second camera angle makes it clear that this technique is, in essence, in the same direction as the early stage defense. The difference, of course, is that you don't have the hook to fully protect your knee; instead, you must rely on changing the angle at which your partner is able to attack your leg. If they're off at a right angle, they won't be able to hyper-extend your knee. You can also use your free arm to help them commit to this technique, forcing them to either go after the kneebar submission, or else to lose their balance entirely and abandon the position (think of this as "forcing their hand").
In The Gi
Doing this technique in the gi is even easier. You can make a solid grip on the back of your partner's jacket (as opposed to simply on the hip), and on their pants leg, ultimately dictating which way they fall and when. In the gi, you might also find it easier to not only get on top, but also to initiate a guard pass after your partner misses their kneebar attempt.
While the knowledge of leg attacks has expanded exponentially in the past few years, knowledge of good escapes continues to lag. It is during this transitional period that many leg attack specialists have excelled, staying well ahead of the pack. Knowing a few fundamental escapes can set you apart from the herd, allowing you to begin defending fundamental leg attacks much sooner (and much more effectively). Keep in mind that safety should always come first, and if a defense isn't working, you should always have the wherewithal to tap early and tap often!