If $d(K,X\backslash U) = 0$ then no uniformly continuous function $1$ on $K$ and$0$ on $X \backslash U$ exists. So you might want some compactness condition.
If $X$ is a subset of some $\mathbb{R}^n$ with the induced metric or a manifold with a Riemannian metric then one can find smooth functions $f$ satisfying the conditions. Smooth functions are locally Lipschitz.
For any metric space $X$ it's easy to write an explicit $f$:
$$f(x) = \frac{d (x, X \backslash U)}{ d(x,K) + d(x,X \backslash U)}$$
Assume that $d(K, X\backslash U) = \delta >0$. Now use the fact that the functions $x \mapsto d(x,K)$, $x \mapsto d(x,X \backslash U)$ are Lipschitz with constant $1$ and the fact that the function $(a,b) \mapsto \frac{a}{a+b}$ is Lipschitz on $\{ (a,b) \ | a,b \ge 0\ \textrm{and } a+ b \ge \delta \}$ to conclude that $f$ is Lipschitz. The Lipschitz constant turns out to be $1/\delta$, the best we can get.
More general, on any subset $A$ on which $d(x,K) + d(x,X \backslash U)$ is bounded below by $\eta>0$ the restriction of $f$ to $A$ is Lipschitz with constant $\eta$.