<p>I have a kubernetes namespace that hosts both my springboot application and the redis service. However when I run the springboot app I get error</p>
<pre><code>org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
...
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to localhost:6379
</code></pre>
<p>Ill post both the manifest yaml for the application and redis server below.</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: spring-deployment
  namespace: spring-poc
  labels:
    app: spring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: spring
  template:
    metadata:
      labels:
        app: spring
    spec:
      containers:
        - name: spring
          image: spring-application
          resources:
            requests:
              memory: 1G
              cpu: &quot;500m&quot;
            limits:
              memory: 1G
              cpu: 1
          ports:
            - name: http
              containerPort: 8080
          securityContext:
            runAsUser: 1000
      imagePullSecrets:
        - name: prod.secret
---
apiVersion: v1
kind: Service
metadata:
  name: spring-service
  namespace: spring-poc
  labels:
    app: spring-service
spec:
  selector:
    app: spring
  type: ClusterIP
  ports:
    - name: http
      port: 8080
      targetPort: 8080
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: spring-ingress
  namespace: spring-poc
spec:
  rules:
    - host: spring.url.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: spring-service
                port:
                  number: 8080
  tls:
    - hosts:
        - spring.url.com
</code></pre>
<p>This manifest successfully deploys my app to the namespace and I can access it from the url.</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-deployment
  namespace: spring-poc
  labels:
    app: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis
          image: redis
          resources:
            requests:
              memory: 1G
              cpu: &quot;500m&quot;
            limits:
              memory: 1G
              cpu: 1
          ports:
            - name: redis
              containerPort: 6379
          securityContext:
            runAsUser: 1000
      imagePullSecrets:
        - name: prod.secret
---
apiVersion: v1
kind: Service
metadata:
  name: redis-service
  namespace: spring-poc
  labels:
    app: redis
spec:
  selector:
    app: redis
  type: ClusterIP
  ports:
    - name: redis
      port: 6379
      targetPort: 6379
</code></pre>
<p>This manifest successfully deploys a redis server to my namespace. If I <code>kubectl exec -it redis-deployment -- redis-cli</code> and run <code>ping</code> it returns <code>pong</code>.</p>
<p>Below is my configuration file for the springboot application</p>
<pre><code>import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * The type Cache config.
 */
@Configuration
@EnableCaching
public class CacheConfig {

    /**
     * Redis connection factory jedis connection factory.
     *
     * @return the jedis connection factory
     */
    @Bean
    public JedisConnectionFactory redisConnectionFactory() {
        return new JedisConnectionFactory(new RedisStandaloneConfiguration(&quot;127.0.0.1&quot;, 6379));
    }

    /**
     * Redis template.
     *
     * @param jedisConnectionFactory the jedis connection factory
     * @return the redis template
     */
    @Bean
    public RedisTemplate&lt;String, Object&gt; redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate&lt;String, Object&gt; redisTemplate = new RedisTemplate&lt;&gt;();
        redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        return redisTemplate;
    }

}
</code></pre>
<p>Is my redis server deployed correctly? Do I need to bind the springboot application to my redis server somehow?</p>

Endman888
<p>You're creating a <code>RedisStandaloneConfiguration</code> with localhost (<code>127.0.0.1</code>). The pods (redis and your spring app) are separated and can't communicate like that.</p>
<p>So you need to replace <code>127.0.0.1</code> with <code>redis-service.spring-poc</code>, like this:</p>
<pre><code>[...]
@Bean
    public JedisConnectionFactory redisConnectionFactory() {
        return new JedisConnectionFactory(new RedisStandaloneConfiguration(&quot;redis-service.spring-poc&quot;, 6379));
    }
[...]
</code></pre>
<p>Best practice would be to get that from environment variables or application config, so you can still use <code>localhost</code> for testing on your workstation.</p>
<p>Explanation:</p>
<p>Kubernetes provides out-of-the-box dns. You can query every pod with <code>&lt;service-name&gt;.&lt;namespace-name&gt;</code>. You can think of it similar to a domain like <code>google.de</code>. So you can connect to every pod via it's service by that. In your example the redis service is called <code>redis-service</code> and it needs to be in the same namespace as the deployment, which is <code>spring-poc</code> (though your manifest says <code>namespace: 48713-poc</code> which wouldn't work). So to connect from spring to redis you need to connect to <code>redis-service.spring-poc:6379</code>.</p>
<p>Technically <code>redis-service:6379</code> would also work, but I'd recommend to be verbose. Also check out the docs about <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/" rel="nofollow noreferrer">dns for pod to pod communication</a>.</p>
