And, for this week's topic, how common was it for Cossaks to fight on both sides of a war and Cossacks fighting against Russia come to do so?
Answering this question first requires understanding who the cossacks were, and how their political and social identity changed over time.
The traditional narrative of the origin of the cossacks goes something like this:
The Cossack originate as "nomadicized Slavs". Starting sometime in the 11th century, there is a series of social developments in what is today Russia and Ukraine that see the nobility try to tie the peasants to the land via various legal and social obligations that are today grouped togeather as "serfdom".
Within the existing medieval Rus social system, it's very hard for serfs to permanantly change their legal status, harder than it is in Germany or France at the same time. This state of affairs is mainly because the elites have a tremendous incentive to tie the peasnts to the land -- in this era, there's a surplus of land relative to the number of people, but much of that land is not very economically productive for intensive agriculture, which makes it hard to create and capture the kinds of surpluses the nobility craves.
So there's this constant stream of peasants, male and female, who are running away to the east to to escape a social system that denies them both geographic and social mobility. These former serfs join up, volunatarily or otherwise, with various nomadic groups inhabiting large parts of the black-earth steppes in what is today Eastern Ukraine and Southern Russia. The nomadic to semi-nomadic peoples of this region are initially probably mostly speaking Indo-Iranian or Turkic languages. The flow of "migrants" from the agricultural societies to the West is constant but not overwhelming -- these people get partially assimilated into the semi-nomadic societies they encounter on the steppe.
The results is that the Slavic peoples adopt some of the social structures (certain kinds of decentralized cheifdoms) and economic systems (like nomadic pastoralism) from the societies they interact with on the Eurasian steppe. But, the Slavic steppe inhabitants keep their language and religion. This is the ethogenesis of the groups that would become known as the "Cossack Hosts" from the 16th century onward.
The above is one model of Cossack ethnogenesis was described in 19th and 20th century scholarship.
There's a problem with the narrative above. The entire story is extremely poorly documented by surviving contemporary sources. Instead, what scholars are certain of is mostly from later chronicles and some historical linguistics.
There's been some considerable challenges in more recent works, especially Brian Boeck's 2009 work Imperial Boundries: Cossack Communities and Empire-Building in the Age of Peter the Great. Boeck instead sees the "nomadic" origins of the Cossacks as basicallyt later mythology. Instead, Boeck says that a Cossack identity arises really only in the 16th century as the emerging Russian state co-opts informal fraternal militias organizations (who are focused in raiding their neighbors, independent of state and religion). Local strongmen are given recognition as leaders of these "Cossack Hosts" so that they can be effectively weilded as instruments of foreign policy against the Ottomans and later the central Asian Khanates.
Thus the creation or recognition of the Cossack hosts under the Romanove Dynasty is both to get the unruly peasnt communities that exist beyond the effective reach of the state to stop raiding the agriculturla estate to the Northwest, but also to use these "ungovnerable" communitiies as buffers against military incursion from neighboring foreign entities, which includes both nomially "nomadic" groups like the Tarters but also more neighboring state-like entities, namely the Ottomans and the Polish-Lithuanian Commonwealth. In this view, the Cossacks emerge as a "third option" as the Slavic communities of the lower Don watershed find themselves forced to become agents of the Czar if they are to avoid becoming subjects of the Czar. Becoming "Cossacks" allows these borderland groups the ability to remain more self-governing than if they were properly part of the regular Russian beurogratic state, but also at the same time means they constantly have to negotiate their status as servants of the same state they don't want to be part of.
If this sounds confusing, welcome to the Historiography of Eastern Europe.