Prove this $f$ has an absolute maximum. I have the following problem:

In a metric space $(X,d)$, we have $f:X\rightarrow[0,+\infty)$ continuous. Suppose that, $\forall \varepsilon>0$, it exists a compact subset $K_\varepsilon\subset X$ such that $f(x)<\varepsilon$ for $x\not\in K_\varepsilon$. Prove that $f$ has an absolute maximum.

I have tried the following: For each $\varepsilon>0$, we can define $f_\varepsilon:K_\varepsilon\rightarrow[0,+\infty)$. Since $f_\varepsilon$ is continuous and $K_\varepsilon$ is compact, $\exists M_\epsilon\in[0,+\infty)$ such that $f_\varepsilon(x)\leq M_\varepsilon \forall x\in K_\varepsilon$. Since $K_\varepsilon$ is compact, $M_\varepsilon$ is actually the maximum of $f(K_\varepsilon)$.
I am not sure if it is correct so far, and how can end up finding $f$ has an absolute maximum.
 A: For $\epsilon=1/n$ $\exists \ \text{compact}  \ K_n \subseteq X $ such that $f(x)<1/n $ $ \forall x\notin K_n$.
Let $M_n=\text{sup}\{f(x) | x\in K_n\}$
,which exists and attained by $f$,by continuity of $f.$
Then we have , $f(x)\leq\text{max}\{1/n,M_n\}  \  \forall x\in X , \ \forall n\in \Bbb N.$
If for some $n_0\in \Bbb N$, $1/n_0 \leq M_{n_0}$ then we will have $f(x)\leq M_{n_0} \  \forall x\in X$, and we are done.
Or else,we will have that $M_n\leq 1/n,  \  \forall n\in \Bbb N $, $\Rightarrow $ $f(x)\leq 1/n$ $\forall x\in X, \forall n\in \Bbb N.$ $\Rightarrow f(x)=0$ $\forall x\in X$.
A: Two cases:
There first one,
There exists $(\epsilon,\eta)\in \mathbb{R^+}^2$ such $ K_\epsilon \cap K_\eta = \emptyset $
So taking $K_\epsilon^{\complement} \cup K_\eta^{\complement} =X $ hence the results (taking $M=\max(M_\epsilon,M_\eta)$ as absolute maximum)
The second case,
There exists $(\epsilon,\eta)\in \mathbb{R^+}^2$ such $K \triangleq K_\epsilon \cap K_\eta \neq \emptyset $
So $K$ is compact as intersection of 2 compacts, so $f$ is bounded on because continous on a compact.
So on  ${K_\epsilon}^{\complement} \cup{K_\eta}^{\complement}$, $f$ is bounded by $M=\max(M_\epsilon,M_\eta)$
And $X=K_{\epsilon}^{\complement} \cup{K_\eta}^{\complement} \cup K$
**
Conclusion
**
$f$ has absolute maximum $M$ defined by the following general formula (for your exercise) $$M=\max(K,M_\epsilon,M_\eta)$$
