<p>I'm fairly new to kubernetes and I'm trying to orchestrate my rails app using minikube on my MacBook. My app includes MySQL, Redis and Sidekiq. I'm running webapp, sidekiq, redis and database in isolated pods. Sidekiq pod is not connecting to redis pod.</p>
<p>kubectl logs of sidekiq pod says this:</p>
<pre><code>2020-09-15T14:01:16.978Z 1 TID-gnaz4yzs0 INFO: Booting Sidekiq 4.2.10 with redis options {:url=&gt;&quot;redis://redis:6379/0&quot;}
2020-09-15T14:01:18.475Z 1 TID-gnaz4yzs0 INFO: Running in ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
2020-09-15T14:01:18.475Z 1 TID-gnaz4yzs0 INFO: See LICENSE and the LGPL-3.0 for licensing details.
2020-09-15T14:01:18.475Z 1 TID-gnaz4yzs0 INFO: Upgrade to Sidekiq Pro for more features and support: http://sidekiq.org
/usr/local/bundle/gems/redis-3.3.1/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
Error connecting to Redis on redis:6379 (Errno::ECONNREFUSED)
/usr/local/bundle/gems/redis-3.3.1/lib/redis/client.rb:345:in `rescue in establish_connection'
/usr/local/bundle/gems/redis-3.3.1/lib/redis/client.rb:330:in `establish_connection'
/usr/local/bundle/gems/redis-3.3.1/lib/redis/client.rb:101:in `block in connect'
/usr/local/bundle/gems/redis-3.3.1/lib/redis/client.rb:293:in `with_reconnect'
/usr/local/bundle/gems/redis-3.3.1/lib/redis/client.rb:100:in `connect'
/usr/local/bundle/gems/redis-3.3.1/lib/redis/client.rb:364:in `ensure_connected'
/usr/local/bundle/gems/redis-3.3.1/lib/redis/client.rb:221:in `block in process'
/usr/local/bundle/gems/redis-3.3.1/lib/redis/client.rb:306:in `logging'
/usr/local/bundle/gems/redis-3.3.1/lib/redis/client.rb:220:in `process'
/usr/local/bundle/gems/redis-3.3.1/lib/redis/client.rb:120:in `call'
/usr/local/bundle/gems/redis-3.3.1/lib/redis.rb:251:in `block in info'
/usr/local/bundle/gems/redis-3.3.1/lib/redis.rb:58:in `block in synchronize'
/usr/local/lib/ruby/2.6.0/monitor.rb:230:in `mon_synchronize'
/usr/local/bundle/gems/redis-3.3.1/lib/redis.rb:58:in `synchronize'
/usr/local/bundle/gems/redis-3.3.1/lib/redis.rb:250:in `info'
/usr/local/bundle/gems/sidekiq-4.2.10/lib/sidekiq.rb:113:in `block in redis_info'
/usr/local/bundle/gems/sidekiq-4.2.10/lib/sidekiq.rb:95:in `block in redis'
/usr/local/bundle/gems/connection_pool-2.2.3/lib/connection_pool.rb:63:in `block (2 levels) in with'
/usr/local/bundle/gems/connection_pool-2.2.3/lib/connection_pool.rb:62:in `handle_interrupt'
/usr/local/bundle/gems/connection_pool-2.2.3/lib/connection_pool.rb:62:in `block in with'
/usr/local/bundle/gems/connection_pool-2.2.3/lib/connection_pool.rb:59:in `handle_interrupt'
/usr/local/bundle/gems/connection_pool-2.2.3/lib/connection_pool.rb:59:in `with'
/usr/local/bundle/gems/sidekiq-4.2.10/lib/sidekiq.rb:92:in `redis'
/usr/local/bundle/gems/sidekiq-4.2.10/lib/sidekiq.rb:106:in `redis_info'
/usr/local/bundle/gems/sidekiq-4.2.10/lib/sidekiq/cli.rb:71:in `run'
/usr/local/bundle/gems/sidekiq-4.2.10/bin/sidekiq:12:in `&lt;top (required)&gt;'
/usr/local/bundle/bin/sidekiq:29:in `load'
/usr/local/bundle/bin/sidekiq:29:in `&lt;main&gt;'
</code></pre>
<p>My webapp.yaml</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: checklist-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: railsapp
    spec:
      containers:
        - name: webapp
          image: masettyabhishek/checklist:latest
          command: [&quot;rails&quot;, &quot;s&quot;, &quot;-p&quot;, &quot;3001&quot;, &quot;-b&quot;, &quot;0.0.0.0&quot;, &quot;-e&quot;, &quot;PRODUCTION&quot;]
          ports:
          - name: checklist-port
            containerPort: 3001
          env:
            - name: MYSQL_HOST
              value: database-service
            - name: MYSQL_USER
              value: root
            - name: MYSQL_PASSWORD
              value: Mission2019
            - name: MYSQL_DATABASE
              value: checklist
            - name: MYSQL_ROOT_PASSWORD
              value: Mission2019
            - name: REDIS_URL
              value: redis
            - name: REDIS_PORT
              value: &quot;6379&quot;
  selector:
    matchLabels:
      app: railsapp
</code></pre>
<p>webapp-service.yaml</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  ports:
  - port: 3001
    protocol: TCP
  type: NodePort
  selector:
    app: railsapp
</code></pre>
<p>sidekiq.yaml</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: sidekiq-deployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        instance: sidekiq
    spec:
      containers:
        - name: sidekiq
          image: masettyabhishek/checklist:latest
          command: [&quot;sidekiq&quot;, &quot;-C&quot;, &quot;config/sidekiq.yml&quot;]
          env:
            - name: MYSQL_HOST
              value: database-service
            - name: MYSQL_USER
              value: root
            - name: MYSQL_PASSWORD
              value: Mission2019
            - name: MYSQL_DATABASE
              value: checklist
            - name: MYSQL_ROOT_PASSWORD
              value: Mission2019
            - name: REDIS_URL
              value: redis
            - name: REDIS_PORT
              value: &quot;6379&quot;
          ports:
            - name: redis-port
              containerPort: 6379
  selector:
    matchLabels:
      instance: sidekiq
</code></pre>
<p>redis.yaml</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: redis-pod
spec:
  containers:
  - name: redis
    image: redis:alpine
    command: [&quot;redis-server&quot;]
    ports:
      - containerPort: 6379

---
apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  selector:
    name: redis-pod
    instance: sidekiq
    app: railsapp
  type: NodePort
  ports:
  - port: 6379
</code></pre>
<p>This is sidekiq.yml in my rails app</p>
<pre><code>Sidekiq.configure_server do |config|
    config.redis = { url: &quot;redis://#{ENV['REDIS_URL']}:#{ENV['REDIS_PORT']}/0&quot;}
end

Sidekiq.configure_client do |config|
    config.redis = { url: &quot;redis://#{ENV['REDIS_URL']}:#{ENV['REDIS_PORT']}/0&quot;}
end
</code></pre>
<p>This is Dockerfile if that helps to answer the question.</p>
<pre><code>FROM ubuntu:16.04

ENV RUBY_MAJOR=&quot;2.6&quot; \
    RUBY_VERSION=&quot;2.6.3&quot; \
    RUBYGEMS_VERSION=&quot;3.0.8&quot; \
    BUNDLER_VERSION=&quot;1.17.3&quot; \
    RAILS_VERSION=&quot;5.2.1&quot; \
    RAILS_ENV=&quot;production&quot; \
    GEM_HOME=&quot;/usr/local/bundle&quot;
ENV BUNDLE_PATH=&quot;$GEM_HOME&quot; \
    BUNDLE_BIN=&quot;$GEM_HOME/bin&quot; \
    BUNDLE_SILENCE_ROOT_WARNING=1 \
    BUNDLE_APP_CONFIG=&quot;$GEM_HOME&quot;

ENV PATH=&quot;$BUNDLE_BIN:$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH&quot;

USER root
RUN apt-get update &amp;&amp; \
      apt-get -y install sudo
RUN echo &quot;%sudo ALL=(ALL) NOPASSWD: ALL&quot; &gt;&gt; /etc/sudoers &amp;&amp; \
    addgroup --gid 1024 stars &amp;&amp; \
    useradd -G stars,sudo -d /home/user --shell /bin/bash -m user
RUN mkdir -p /usr/local/etc \
    &amp;&amp; echo 'install: --no-document' &gt;&gt; /usr/local/etc/gemrc \
    &amp;&amp; echo 'update: --no-document' &gt;&gt; /usr/local/etc/gemrc

USER user
RUN sudo apt-get -y install --no-install-recommends vim make gcc zlib1g-dev autoconf build-essential libssl-dev libsqlite3-dev \
    curl htop unzip mc openssh-server openssl bison libgdbm-dev ruby git libmysqlclient-dev tzdata mysql-client
    
RUN sudo rm -rf /var/lib/apt/lists/* \
    &amp;&amp; sudo curl -fSL -o ruby.tar.gz &quot;http://cache.ruby-lang.org/pub/ruby/$RUBY_MAJOR/ruby-$RUBY_VERSION.tar.gz&quot; \
    &amp;&amp; sudo mkdir -p /usr/src/ruby \
    &amp;&amp; sudo tar -xzf ruby.tar.gz -C /usr/src/ruby --strip-components=1 \
    &amp;&amp; sudo rm ruby.tar.gz

USER root
RUN cd /usr/src/ruby \
    &amp;&amp; { sudo echo '#define ENABLE_PATH_CHECK 0'; echo; cat file.c; } &gt; file.c.new &amp;&amp; mv file.c.new file.c \
    &amp;&amp; autoconf \
    &amp;&amp; ./configure --disable-install-doc

USER user
RUN cd /usr/src/ruby \
    &amp;&amp; sudo make -j&quot;$(nproc)&quot; \
    &amp;&amp; sudo make install \
    &amp;&amp; sudo gem update --system $RUBYGEMS_VERSION \
    &amp;&amp; sudo rm -r /usr/src/ruby
RUN sudo gem install bundler --version &quot;$BUNDLER_VERSION&quot;

RUN sudo mkdir -p &quot;$GEM_HOME&quot; &quot;$BUNDLE_BIN&quot; \
    &amp;&amp; sudo chmod 777 &quot;$GEM_HOME&quot; &quot;$BUNDLE_BIN&quot; \
    &amp;&amp; sudo gem install rails --version &quot;$RAILS_VERSION&quot;
RUN mkdir -p ~/.ssh &amp;&amp; \
    chmod 0700 ~/.ssh &amp;&amp; \
    ssh-keyscan github.com &gt; ~/.ssh/known_hosts
ARG ssh_pub_key
ARG ssh_prv_key
RUN echo &quot;$ssh_pub_key&quot; &gt; ~/.ssh/id_rsa.pub &amp;&amp; \
    echo &quot;$ssh_prv_key&quot; &gt; ~/.ssh/id_rsa &amp;&amp; \
    chmod 600 ~/.ssh/id_rsa.pub &amp;&amp; \
    chmod 600 ~/.ssh/id_rsa
USER root
RUN curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
RUN apt-get install -y nodejs
USER user
WORKDIR /data
RUN sudo mkdir /data/checklist
WORKDIR /data/checklist
ADD Gemfile Gemfile.lock ./
RUN sudo chown -R user /data/checklist
RUN bundle install
ADD . .
RUN sudo chown -R user /data/checklist
EXPOSE 3001
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true
RUN chmod +x ./config/docker/prepare-db.sh &amp;&amp; sh ./config/docker/prepare-db.sh
ENTRYPOINT [&quot;bundle&quot;, &quot;exec&quot;]
CMD [&quot;sh&quot;, &quot;./config/docker/startup.sh&quot;]
</code></pre>
<p>kubectl describe svc redis</p>
<pre><code>➜  checklist kubectl describe svc redis
Name:                     redis
Namespace:                default
Labels:                   &lt;none&gt;
Annotations:              &lt;none&gt;
Selector:                 app=railsapp,instance=sidekiq,name=redis-pod
Type:                     NodePort
IP:                       10.103.6.43
Port:                     &lt;unset&gt;  6379/TCP
TargetPort:               6379/TCP
NodePort:                 &lt;unset&gt;  31886/TCP
Endpoints:                &lt;none&gt;
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   &lt;none&gt;
</code></pre>

<p>As you can see the <code>Endpoints</code> section of the <code>redis</code> service is not having pod IPs which is the reason for <code>Connection refused</code> error. The Pod need to have label matching with selector of service. Updating the redis pod with labels as below should solve the issue.</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: redis-pod
  labels:
    instance: sidekiq
    app: ailsapp
    name: redis-pod
spec:
  containers:
  - name: redis
    image: redis:alpine
    command: [&quot;redis-server&quot;]
    ports:
      - containerPort: 6379
</code></pre>
