This knocking would appear in an older gas water heater with dirt settling in the bottom. After drawing water the burner would go on to heat the colder water replacing the heated water. The dirt traps water near the metal under the dirt and permits steam bubbles to build until they pop out to the cooler water and collapse. Collapsing water bubbles can be loud. It may be too late to flush the dirt, although it may be possible to stir up some of the sediment after letting the water cool, removing the bottom valve,draining, poking, spraying, and draining the dirt out. It may make more sense to have the water heater replaced.