Contradiction in maximum entropy principle The maximum entropy principle states that at equilibrium entropy is maximized. However I found this principle paradoxical because in pure thermodynamics entropy is defined only for equilibrium states. How is it possible?
Edit:
Some answers below suggest that entropy can be defined also for state out of equilibrium. This remove the contradiction, the equilibrium is just the state of maximum entropy.
However i'm not convinced that this is the the answer i was looking for. Consider, for example, the free expansion of a gas.  Will the gas occupy the entire available volume? of course yes but let's prove it using the maximum entropy principle: because the energy is constant $dU=TdS-PdV=0$ from which $dS/dV=P/T>0$. Then the maximum entropy is reached when the volume is maximized.
Why is this different from defining entropy for a system out of equilibrium? Because when we write $dU=TdS-PdV$ we are writing differences between equilibrium states.  $dS=S(V+dV,U)-S(V,U)$ is the difference between the entropy if the system is in equilibrium at volume $V+dV$ and the entropy if the system is in equilibrium at volume $V$. If we would define the entropy $S'$ for the system out of the equilibrium, then $dS'\neq dS$.
Attempt of an answer: I think that when a parameter (for example volume) can freely change we have to consider the system in equilibrium for every possible value of the parameter and then choose as the true physical equilibrium the value of the parameter in which entropy is maximized
 A: There are many thermodynamic quantities that can be well-defined for out-of-equilibrium as well as equilibrium states. Internal energy, volume and entropy are a few.
We define entropy in general by asserting that it is the sum of the entropies of all the parts of a system. Then you can take the parts small enough that each is in internal equilibrium, yet large enough that thermodynamics still applies (I am giving a thermodynamic answer here). The thermodynamic limit allows both requirements to be satisfied simultaneously.
Note that when each part is in internal equilibrium we don't necessarily have an equilibrium state overall because the different parts may not be in equilibrium with each other.
There is a difficulty with cases where a thermodynamic limit may be unclear or too great an approximation. For example in a highly irregular case such as turbulent flow it may not be possible to find parts that are large enough to be treated accurately in a thermodynamic limit (the limit of large numbers of microstates). In such cases one may adopt the statistical definition $S = -k_{\rm B} \sum p_i \ln p_i$. The standard approach is to assert that this is a definition, and one then gives arguments to show that such a definition agrees with the thermodynamic statement $dS = dQ_{\rm rev}/T$ in the thermodynamic limit.
A: The following is from a thermodynamics perspective, rather than statistical mechanics.

I've read that equilibrium is the state at which entropy has a
maximum.

I believe you are referring to the maximum entropy principle. If so, from a thermodynamics perspective, the maximum entropy principle states that for an isolated system (a system for which there is no energy or mass transfer with the surroundings) the entropy is maximized at equilibrium. (See https://en.wikipedia.org/wiki/Principle_of_minimum_energy#:~:text=The%20maximum%20entropy%20principle%3A%20For,energy%20is%20minimized%20at%20equilibrium.)

However I found this definition paradoxical because on books entropy
is usually defined only for equilibrium states

Yes, entropy is a (equilibrium) thermodynamics state property. But I don't believe there is a paradox.
For an isolated system if the system is not in internal equilibrium, entropy is not maximized. Only when it comes to internal equilibrium is entropy maximized. And if it is not in internal equilibrium it can only be brought to internal equilibrium as a result of an irreversible process, which by definition generates (increases) entropy.
Take the often used example of the free expansion of an ideal gas into a vacuum. The system consists of an ideal gas and a vacuum separated by a partition bounded by a rigid, thermally insulated vessel. So we have an isolated system (no work, heat, or mass transfer with the surroundings). The gas (one part of the system) is initially in internal equilibrium and the vacuum (the other part of the system) is in internal equilibrium, but the gas and the vacuum are not in equilibrium with each other due to the pressure and temperature difference (temperature of a perfect vacuum being zero).
In order to bring the two parts of the system into equilibrium, an opening in the partition is created and the gas allowed to freely expand into the vacuum until equilibrium is established. The process is irreversible and entropy is generated (entropy increases). For the process, $\Delta U=0$ and for an ideal gas $\Delta T=0$. The change in entropy can be calculated by assuming a reversible isothermal path connecting the initial and final states of the system. The entropy of the new state of the system is now maximized.
Hope this helps.
A: From the paper Entropy and Time by Arieh Ben-Naim:

Unfortunately, there is no general definition of equilibrium which
applies to all systems. Callen [35*,36**], introduced the existence of
the equilibrium state as a postulate. He also emphasized that any
definition of an equilibrium state is necessarily circular.

I don't know if that view is universal, but certainly you can talk about the entropy of a nonequilibrium system in the way that Andrew Steane said (summing up the entropies of the parts, with the assumption that each test volume is well-described by whatever distribution and with the assumption that the entropy is extensive). So I think your answer is correct and that there are many assumptions baked into the word "equilibrium".

*35. Callen H.B. Thermodynamics. John Wiley and Sons; New York, NY, USA: 1960
**36. Callen H.B. Thermodynamics and an Introduction to Thermostatistics. 2nd ed. John Wiley and Sons; Hoboken, NJ, USA: 1985.
A: Boltezmans entropy equation is what is used for equilibrium situations, thats what the OP has no doubt read in a book, i.e. like this.
However, Boltezeman cannot be used in non-equilibrium systems, for that you need to use Shannons entropy equation. That the resolution to the OP's 'paradox'.
For more on the connection between entropy definitions, see my answer
