The basic issue is that no right is absolute, and that people have obligations as well as rights. It also reflects the fact that often it is impossible to prevent all grave harm or death so that we have to find some lawful policy that will, on average, minimize the lives lost or the aggregate harm done.
In the military context, individuals have a right to not be deprived of their lives, but countries also have an obligation to protect its people and interests from the use of military force by state and non-state actors. The obligation of the state to protect its people and interests often requires the use of military force by the state in circumstances whose connection to protecting particular people or interests is very indirect.
In the U.S. we delegate the task of deciding if military force by the state is necessary to protect its people and interests to Congress, through its war powers and its power to appropriate funds for military purpose and to establish regulations for the military, to the President as commander-in-chief of the U.S. military, and to subordinate military officers who decide who to implement the direction they receive from Congress and the President at an operational level associated with a particular military officer's responsibilities.
In these situations, we sometimes have to place the needs of the many, now or in the future, to be safe, above the life of an individual soldier, because the President, Congress, and the military officers involved determine through the chain of command and legal process that we can't figure out a way to have both. The military justice system, the rules of engagement for a particular conflict, and the military orders transmitted through the chain of command from the President and authorized by Congress determine when this can be done.
In the case of a soldier who volunteers, the soldier has voluntarily assumed the risk that this might happen and thus made that soldier's life matter less than the lives of civilians and other people who might be in peril, now or in the future, if the soldier doesn't make the sacrifice.
In the case of a military draft, Congress has made the further determination that not only is military force necessary, but also that this military force is so crucial to the security of the country and its people to have soldiers to apply military force at possible risk to their own lives, that Congress will involuntarily put these soldiers in harm's way according to a conscription process that affords some due process to the people subject to it.
Obviously there are also other circumstances outside the military context where there is also a balancing of rights. The law allows the use of deadly force for the protection of oneself or others to interrupt the commission of serious crimes against a more blameless victim, when it is necessary to do so. The law allows the execution of murderers and offenders who commit a handful of other equally weighty crimes because their conduct forfeited their right to live and they had fair warning in advance that this could happen to them. The law allows quarantines and rationing of medical supplies to minimize the total lives and physical harm most rationally since all deaths cannot be avoided. And so on.
Almost always, when the law authorizes the taking of a life there is an implicit balancing of the life taken against other competing interests which are equally or more weighty.
Of course, sometimes, it will turn out that with 20-20 hindsight, a soldier's life was sacrificed despite the fact that this sacrifice did not advance anyone's security because the military officers making the call, or the President, or Congress, in the course of exercising their judgment about the right thing to do in good faith, were mistaken about what the right thing to do was in that case. But since the risk of not making any decisions is, on average, greater than the risk of making decisions in good faith which are sometimes the wrong decisions in hindsight, we don't hold the decision-makers legally accountable in those circumstances.
Situations where a soldiers life must be sacrificed almost always reflect the fact that human decision making by people in positions of power, and the options presented by a situation, are not optimal and a "best" solution to the situation that leaves no one with a life lost often doesn't exist.