当前位置: 首页 > 知识库问答 >
问题:

无法使用NodePort服务连接到kubernetes中的redis pod

华锦
2023-03-14

我对kubernetes相当陌生,我正试图在MacBook上使用minikube来编排我的rails应用程序。我的应用程序包括MySQL、Redis和Sidekiq。我在独立的豆荚里运行webapp、sidekiq、redis和数据库。Sidekiq吊舱没有连接到redis吊舱。

sidekiq吊舱的库贝特尔日志是这样说的:

2020-09-15T14:01:16.978Z 1 TID-gnaz4yzs0 INFO: Booting Sidekiq 4.2.10 with redis options {:url=>"redis://redis:6379/0"}
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 `<top (required)>'
/usr/local/bundle/bin/sidekiq:29:in `load'
/usr/local/bundle/bin/sidekiq:29:in `<main>'

我的webapp.yaml

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: ["rails", "s", "-p", "3001", "-b", "0.0.0.0", "-e", "PRODUCTION"]
          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: "6379"
  selector:
    matchLabels:
      app: railsapp

webapp-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  ports:
  - port: 3001
    protocol: TCP
  type: NodePort
  selector:
    app: railsapp

Sidekiq.Yaml

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: ["sidekiq", "-C", "config/sidekiq.yml"]
          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: "6379"
          ports:
            - name: redis-port
              containerPort: 6379
  selector:
    matchLabels:
      instance: sidekiq

Redis.Yaml

apiVersion: v1
kind: Pod
metadata:
  name: redis-pod
spec:
  containers:
  - name: redis
    image: redis:alpine
    command: ["redis-server"]
    ports:
      - containerPort: 6379

---
apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  selector:
    name: redis-pod
    instance: sidekiq
    app: railsapp
  type: NodePort
  ports:
  - port: 6379

这是我的rails应用程序中的sidekiq.yml

Sidekiq.configure_server do |config|
    config.redis = { url: "redis://#{ENV['REDIS_URL']}:#{ENV['REDIS_PORT']}/0"}
end

Sidekiq.configure_client do |config|
    config.redis = { url: "redis://#{ENV['REDIS_URL']}:#{ENV['REDIS_PORT']}/0"}
end
FROM ubuntu:16.04

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

ENV PATH="$BUNDLE_BIN:$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH"

USER root
RUN apt-get update && \
      apt-get -y install sudo
RUN echo "%sudo ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers && \
    addgroup --gid 1024 stars && \
    useradd -G stars,sudo -d /home/user --shell /bin/bash -m user
RUN mkdir -p /usr/local/etc \
    && echo 'install: --no-document' >> /usr/local/etc/gemrc \
    && echo 'update: --no-document' >> /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/* \
    && sudo curl -fSL -o ruby.tar.gz "http://cache.ruby-lang.org/pub/ruby/$RUBY_MAJOR/ruby-$RUBY_VERSION.tar.gz" \
    && sudo mkdir -p /usr/src/ruby \
    && sudo tar -xzf ruby.tar.gz -C /usr/src/ruby --strip-components=1 \
    && sudo rm ruby.tar.gz

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

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

RUN sudo mkdir -p "$GEM_HOME" "$BUNDLE_BIN" \
    && sudo chmod 777 "$GEM_HOME" "$BUNDLE_BIN" \
    && sudo gem install rails --version "$RAILS_VERSION"
RUN mkdir -p ~/.ssh && \
    chmod 0700 ~/.ssh && \
    ssh-keyscan github.com > ~/.ssh/known_hosts
ARG ssh_pub_key
ARG ssh_prv_key
RUN echo "$ssh_pub_key" > ~/.ssh/id_rsa.pub && \
    echo "$ssh_prv_key" > ~/.ssh/id_rsa && \
    chmod 600 ~/.ssh/id_rsa.pub && \
    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 && sh ./config/docker/prepare-db.sh
ENTRYPOINT ["bundle", "exec"]
CMD ["sh", "./config/docker/startup.sh"]
➜  checklist kubectl describe svc redis
Name:                     redis
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=railsapp,instance=sidekiq,name=redis-pod
Type:                     NodePort
IP:                       10.103.6.43
Port:                     <unset>  6379/TCP
TargetPort:               6379/TCP
NodePort:                 <unset>  31886/TCP
Endpoints:                <none>
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

共有1个答案

景远航
2023-03-14

如您所见,Redis服务的Endpoints部分没有pod IPs,这就是Connection Delection错误的原因。该吊舱需要与服务选择器匹配的标签。用下面的标签更新redis吊舱应该可以解决这个问题。

apiVersion: v1
kind: Pod
metadata:
  name: redis-pod
  labels:
    instance: sidekiq
    app: ailsapp
    name: redis-pod
spec:
  containers:
  - name: redis
    image: redis:alpine
    command: ["redis-server"]
    ports:
      - containerPort: 6379
 类似资料:
  • 在我的 OVH 托管 Kubernetes 集群中,我正在尝试公开 NodePort 服务,但看起来无法通过以下方式

  • 我创建了一个副本zookeeper+kafka集群,使用来自官方孵化器repo的官方kafka图表: 如果我进入动物园管理员舱: 我使用工具测试TCP连通性。我预计会出现通信错误,因为服务器没有使用HTTP,但是如果curl连都不能连接,我不得不用ctrl-C输出,那么TCP连接就不能工作了。 我可以通过访问本地pod: 但是通过服务名连接到Kafka pod不起作用,我必须ctrl-C curl

  • 这是mysql连接php脚本。它不断地显示出它的错误 警告:mysqli_connect():(HY000/1045):用户'user'@'localhost'(使用密码:YES)在第6行的C:\xampp\htdocs\index.php访问被拒绝 注意:尝试在第7行连接成功的C:\xampp\htdocs\index.php中获取非对象的属性 我听不懂,请帮帮我好吗?

  • 首先,我知道有很多类似的问题,但我所看到的似乎都不能解决我的设置(我发现的任何解决方案都不管用)。所以请容忍我。。。 我的服务器主机名是IP地址,而不是域名(即URL看起来像:) 我的服务器有一个真正的证书(即,没有自签名) 我的应用程序的plist条目字典为空(没有任何例外-出厂设置ATS) 这是生产代码,我不能禁用ATS(我也不认为我可以,因为例外只适用于显式域名,而不是IP地址) (iOS9

  • 我有使用graphql的经验,但这是我第一次尝试apollo federation和Kubernetes。 我首先尝试使用apollo Federation将整体graphql服务器拆分为微服务。它工作得很好(下面是工作代码)。现在我试图在kubernetes集群中运行这些微服务,但是apollo网关和其他后端服务之间的网络问题不断。 从apollo网关服务器(当我尝试在kubernetes上运行

  • kubectl对kafka的描述也显示了暴露的节点 我有一个出版商二进制文件,将一些信息发送到Kafka。由于我有一个3节点集群部署,我使用我的主节点IP和Kafka节点端口(30092)与Kafka连接。 但是我的二进制文件正在获得错误。我无法理解为什么即使在nodePort到targetPort转换成功后,它还是被拒绝。随着进一步的调试,我在kafka日志中看到了以下调试日志: