If you love your job, part of the reason may be that you freely chose the work that you do. You were not forced to do it. But imagine a situation during wartime in which there were some soldiers or guards with guns standing above you, forcing you to do that same job. Suddenly it would not be pleasurable work anymore.
The fear that you might be harmed if you did not work quickly and efficiently would be very stressful. There would be a sense of danger as you worked below the level where the gunmen were standing. Working "under the gun" of a watchful and hostile master would make anyone nervous. Thus, although no one we can see nearby may have an actual gun, we use this idiom in any work situation where the workers are put under strong pressure to perform quickly and efficiently.