Is there some connection between the Virial theorem and a least action principle? Both involve some 'averaging' over energies (kinetic and potential) and make some prediction about their mean values.  As far as the least action principles, one could think of them as saying that the actual path is one that makes an equipartition between the two kinds of energies.
 A: *

*There is an interesting Hamiltonian counterpart to BebopButUnsteady's nice Lagrangian answer: An infinitesimal canonical transformation (CT)
$$\begin{align} \delta q~=~&\varepsilon q, \cr \delta p~=~&-\varepsilon p,\end{align}\tag{1}$$
[with type-2 generator $F_2 = (1+\varepsilon)q\cdot P$] of the Hamiltonian action
$$\begin{align} S_H~=~&\int\! dt~ L_H,\cr L_H~=~&p\cdot \dot{q}-H,\end{align} \tag{2}$$
leads to the Hamiltonian virial theorem for long time-averages:

$$\langle q\cdot \frac{\partial H}{\partial q}\rangle~=~\langle p\cdot \frac{\partial H}{\partial p}\rangle,\tag{3} $$

under the usual assumption of bounded motion.


*The virial theorem (3) in Hamiltonian mechanics has the same form as the corresponding virial theorem in classical statistical mechanics, with the understanding that the long time-averages $\langle\cdot \rangle$ are replaced with statistical averages $\langle\cdot \rangle$. The latter follows from the (generalized) equipartition theorem
$$ \langle F(z)\frac{\partial H(z)}{\partial z}\rangle ~=~k_BT \langle \frac{\partial F(z)}{\partial z}\rangle, \tag{4}$$
cf. a (currently deleted) answer by Nikolaj-K.


*The (generalized) equipartition theorem (4) in classical statistical mechanics, in turn, is an analogue of the Schwinger-Dyson (SD) equations
$$\langle F[\phi]\frac{\delta S[\phi]}{\delta \phi}\rangle~=~i\hbar\langle  \frac{\delta F[\phi]}{\delta \phi} \rangle\tag{5}$$
in QFT.
A: As that lovely article linked by dfan says  the virial theorem comes from varying the action $S[x]$ by $x\rightarrow(1+\epsilon)x$ 
$$\frac{1}{T}\delta S = \frac{1}{T}\epsilon\int_{0}^{T} dt\{m\dot{x}^2 -x\frac{\partial V}{\partial x}\}$$
This is a variation of the action and therefore must vanish up to some boundary terms if $x$ is a solution of the equations of the motion. But the equation $\delta S=0$ is just the virial theorem:
$$2\langle T\rangle ~=~ \langle x\cdot \frac{\partial V}{\partial x}\rangle~=~- \langle x\cdot F\rangle,$$
where the angle brackets mean time average. 
The only remaining issue is neglect of the boundary terms. This enforces the condition on the virial theorem that the motion be bounded and that I take a long enough time average. If both of these conditions are true, then I can take  $T\rightarrow \infty$. Since everything is bounded the boundary terms remain finite as $T\rightarrow \infty$ and therefore there contribution to $\frac{\delta s}{T}$ goes to zero. Leaving us with the virial theorem.
A: I will address this question in the following manner:
I will first discuss Hamilton's stationary action, showing that while it may appear as if Hamilton's stationary action involves averaging energies over time, that is actually not the case. I will discuss how to recognize that Hamilton's stationary action is truly not about averaging.

After discussing Hamilton's stationary action I will discuss the virial theorem, making use of a discussion of the virial theorem by John Baez.
John Baez shows that the core of the virial theorem already manifests itself in the case of a bound system consisting of two objects, the primary so much heavier than the secondary that the motion of the primary is negligable. That is: to understand the exchanges of kinetic/potential energy it is sufficient to consider only the motion of a single object: the secondary. Starting from there you can step up through stages of generalization. That is: the form of the virial theorem with averaging over multiple states of motion is to be understood as generalization, starting from a core property.


Hamilton's stationary action
the question of averaging energies over time.
Preliminary discussion:
As we know, there are circumstances where integration can be used to obtain a value that we can use as representing an average. For instance, power output of alternating current. We can compute the integral of the sine function with respect to time, and then divide that value by the total time to obtain a value for the average power.
The definition of Hamilton's action:
$$ S = \int (E_k - E_p) dt  \qquad (1.1)  $$
we can write that as two integrals:
$$ S = \int (E_k) dt - \int (E_p) dt  \qquad (1.2)  $$
And the question then is: is the following  expression, which divides the integral with respect to time by the total time a meaningful expression:
$$ \frac{\int_{t_0}^t (E_k) dt}{\Delta t} - \frac{\int_{t_0}^t (E_p) dt}{\Delta t}    \qquad (1.3)  $$
The following discussion has the logical implication that (1.3) is not a meaningful expression.

The following two animated gifs are about tracking change of energy as an object is moving along a trajectory.
(These two animated gifs are composited from screenshots of an interactive diagram. They were uploaded for an answer I posted in oktober 2021, for a question titled 'motivation for the lagrangian formalism')
Comparison of differential calculus and calculus of variations:
In differential calculus the unit of operation is a pair of points, the line through those two points is tangent to the curve you are looking to solve for.
In calculus of variations the unit of operation is a triplet of points.


Diagram 1
In diagram 1 the intermittent line is a parabola. This parabola represents height as a function of time of an object that has been thrown upwards. A uniform force is acting in downward direction. To obtain the simplest possible parabola the value of the (uniform) acceleration is set at 2 $m/s^2$
The outer points ($t_1$ and $t_3$) are treated as fixed; variation is executed by varying the position coordinate of the middle point.
In the diagram two adjacent time intervals are evaluated; $t_{1,2}$ and $t_{2,3}$
The sweet spot is the point where the change of velocity is such that the difference in kinetic energy matches the difference in potential energy.
(In this diagram a uniform acceleration is used, which means that in this diagram the potential increases linear with height, hence $\Delta E_p$ is in this diagram constant. In the general case the force changes as a function of the position coordinate. Then the potential is not linear, and the value of $\Delta E_p$ changes as a function of the position coordinate.)

Diagram 2 - valid down to infinitisimally small intervals
Diagram 2 illustrates that the logic of this unit of operation is valid at any scale, down to the scale of infinitisimally small time intervals.

Crucially:
It is also the case that Hamilton's stationary action itself obtains at infinitisimal scale. This can be recognized in several ways.
Let's say we have used stationary action to solve for the true trajectory between points A and D. We can then take any points B and C that are on the curve that represents the true trajectory (in that order along the trajectory: A, B, C, D), treat the points B and C as fixed end points, and use stationary action to find the true trajectory for motion from B to C.
If the action is stationary along the trajectory from A-to-D then the action is also stationary along the subsection B-to-C. We can position the sub-section B-to-C anywhere along the trajectory, and we can make the distance B-to-C arbritrarily small: the reasoning remains the same. The reasoning is valid at any scale, down to infinitisimally small scale.
Reference:
The fact that Hamilton's stationary action obtains at infinitisimally small scale is discussed in the following article published in 2004:
"Deriving Lagrange's equations using elementary calculus," Jozef Hanc, Edwin F. Taylor, and Slavomir Tuleja. American Journal of Physics, Vol. 72, No. 4, April 2004, pages 510-513.

With the above established:  
The general convention is to declare Hamilton's action as an integral:
$$ S = \int (E_k - E_p) dt  \qquad (1.1)  $$
However, since the stationary property obtains at infinitisimal scale declaring that integration is redundant.
The fact that the integration is not doing anything can also be seen as follows:
The Euler-Lagrange equation is a differential equation. The very nature of a differential equation is that its operation is defined in terms of taking the limit of operating at infinitisimally small scale.
About the derivation of the Euler-Lagrange equation:
The process of deriving the Euler-Lagrange equation has to accomplish a number of things, and among those things is to remove the integration. It's not that the integration was temporarily doing something useful and was removed only after having done its job. No such thing: the integral was redundant every step along the way.
At the start I raised the question: is this expression meaningful?
$$ \frac{\int_{t_0}^t (E_k) dt}{\Delta t} - \frac{\int_{t_0}^t (E_p) dt}{\Delta t}    \qquad (1.3)  $$
(1.3) is not meaningful because the integration is never doing anything.
For further information: I repeat the link to the oktober 2021 answer about Hamilton's stationary action



The virial theorem
As we know, there are two force laws with the property that in the case of circumnavigating motion in two spatial dimensions they give rise to periodic orbits: Hooke's law and inverse square law.

Hooke's law
An example of circumnavigating motion subject to Hooke's law is a setup that is called 'liquid mirror telescope' If a bowl filled with Mercury is rotating then the surface assumes a shape with a parabolic cross section (paraboloid of revolution).
When the surface of the liquid has reached a paraboloid shape the fluids mass has reached a state that is referred to as 'solid body rotation'.
The dynamic state of solid body rotation has the following property: as you move from close to the axis of rotation to farther away the kinetic energy of circumnavitation and the potential energy both increase quadratically as a function of distance to the axis of rotation. The kinetic energe increases quadratically because circumference increases linear with distance to axis of rotation, and the potential energy increases quadratic because with a parabolic cross section the height increases quadratric with distance to the axis of rotation.
In fact, at every distance to the axis of rotation the kinetic and potential energy are in a 1:1 ratio.
Next we examine if that ratio of 1:1 generalizes to beyond circular motion.
It follows from the Work-Energy theorem that in the process of exchanging kinetic and potential energy the rates of change must match each other. So: in the case of non-circular motion (subject to Hooke's law): while at any instant the ratio of kinetic to potential energy will not be 1:1, if you average out the kinetic energy and the potential energy then you do see the 1:1 ratio, at every distance to the axis of rotation.

Inverse square law
The mechanics of orbiting motion subject to an inverse square law is in a number of ways counter-intuitive.
If you are in orbit, and you use retro-firing thrusters to lower your orbit, then at that lower altitude gravity is so much stronger that a faster velocity is necessary to maintain orbit. Not just faster angular velocity: faster velocity general.
Let's say you are orbiting, and you want to overtake another orbiting spacecraft ahead of you. The way to achieve that is to fire retro-firing thrusters so that you descend to a lower orbit. During that descent gravitational potential energy is converted to kinetic energy. Now you are faster than the spacecraft ahead of you and you are closing the distance.
Conversely, if you would fire thrusters such that they increase your velocity then the effect is to  raise the altitude of your spacecraft. The process of climbing to higher altitude converts kinetic energy to gravitational potential energy. At the destination altitude your velocity is slower than it was at the starting altitude.
Next:
Derivation of the virial theorem for the case of a single object in motion around a much heavier primary.
Gravitational potential energy as a function of the distance $R$ to the center-of-mass of the primary
$$ E_p = -\frac{(GM)m}{R} \qquad (2.1)  $$
Magnitude of gravitational force:
$$ F_{gravitational} = \frac{(GM)m}{R^2} \qquad (2.2) $$
magnitude of required centripetal force:
$$ F_{centripetal} = \frac{mv^2}{R}  \qquad (2.3) $$
Combining (2.2) and (2.3):
$$ \frac{mv^2}{R}  =  \frac{(GM)m}{R^2} \qquad (2.4)  $$
(2.4) can be rearranged to an equation that has the exprssion for kinetic energy on the left hand side. Multiply both sides with $R$, divide both sides by 2.
$$ \tfrac{1}{2}mv^2  =  \frac{(GM)m}{2R} \qquad (2.5)  $$
(2.5) is the equation that is informative. (2.5) expresses a relation of orbital velocity to distance to the center of attraction.
(2.5) can also be expressed in the following form, but (2.5) is the form that is actually used in calculations, because (2.5) states the velocity and distance-to-center explicitly, whereas those are implicit in (2.6).
$$ E_k = - \tfrac{1}{2} E_p \qquad  (2.6) $$
(The minus sign is there because as you climb from one altitude to a higher altitude you are increasing your gravitational potentian energy, and decreasing your kinetic energy.)

Returning to (2.5)
$$ \tfrac{1}{2}mv^2  =  \frac{(GM)m}{2R} \qquad (2.5)  $$
Whereas in the case of solid body rotation the kinetic energy and potential energy are in a 1:1 ratio at every distance to the center of attraction, in the case of inverse square force law the ratio of kinetic energy to potential energy changes as a function of distance to the center of attraction.
Inverse square law:
kinetic energy: the lower the orbit the higher the kinetic energy
potential energy: the higher the orbit the higher the potential energy
Astronomers use the virial theorem in the following way:
Astronomers obtain profiles of velocity distribution of the stars of a galaxy. From the velocity profile as a function of distance to the center of attraction the potential can be inferred. If a Galaxy as a whole has a velocity profile such that on average the motion of the stars is similar to solid body rotation then it follows that the distribution of gravitational mass must be such that the Galactic potential energy is according to Hooke's law.
Whatever the velocity profile is, the potential as a function of distance to the center of attraction can be inferred from it.

As mentioned earlier, we have that Hooke's law and inverse square law are the only two force laws with the property that they give rise to orbital motion that loops back onto itself. The first stage of generalization of (2.5) is to generalizes from circular orbit to averages of energies of motion along eccentric orbits.
A further generalization is to a continuum of force laws, where the force law may be any polynomial function of the distance to the center of attraction (including fractional powers).
In that more general case the motion around the center of attraction does not loop back onto itself, but the averages of the kinetic energy and potential energy respectively will still be in a ratio that changes as a function of distance to the center of attraction. That is how astronomers use the virial theorem.


With the above in place: is there a connection between Hamilton's stationary action and the virial theorem?
Well, both are part of the body of knowledge of classical mechanics, and both are expressed in terms of kinetic energy and potential energy, but other than that there is no connection.
