如何在Kubernetes上使用Redis部署Node.js? [英] How to deploy a node.js with redis on kubernetes?

查看:67
本文介绍了如何在Kubernetes上使用Redis部署Node.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的node.js应用程序(HTTP服务),可与redis对话".我想创建一个部署并使用minikube运行它.

I have a very simple node.js application (HTTP service), which "talks" to redis. I want to create a deployment and run it with minikube.

根据我的理解,我需要基于docker镜像的我的应用程序的kubernetes Pod.这是我的Dockerfile:

From my understanding, I need a kubernetes Pod for my app, based on the docker image. Here's my Dockerfile:

FROM node:8.9.1
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD ["npm", "start"]

我用docker build -t my-app .

接下来,我为应用程序的Pod创建了Pod定义:

Next, I created a Pod definition for my app's Pod:

apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
  - name: my-app
    image: my-app:latest
    imagePullPolicy: Never
    ports:
    - containerPort: 8080

到目前为止,太好了.但是从现在开始,我还不清楚如何进行Redis:

So far, so good. But from now on, I have no clear idea how to proceed with redis:

  1. 应该重新使用另一个Pod还是服务(就Kubernetes而言)?

  1. should redis be another Pod, or a Service (in terms of Kubernetes kind)?

如何从我的应用程序内部引用Redis?根据是否将Redis定义为Pod/服务,如何获取连接URL和端口?我读到有关Kubernetes正在创建的环境变量的信息,但是我不确定这些变量是否适用于Pod或Services.

How do I reference redis from inside my app? Based on whether redis will be defined as a Pod/Service, how do I obtain a connection URL and port? I read about environment variables being created by Kubernetes, but I am not sure whether these work for Pods or Services.

如何在单一配置下同时汇总这两个(我的应用程序和Redis)?如何确保Redis首先启动,然后确保我的应用程序启动(需要运行Redis实例),以及如何将HTTP端点暴露给外部世界"?我读过有关部署的文章,但不确定如何将这些部分连接在一起.

How do I aggregate both (my app & redis) under single configuration? How do I make sure that redis starts first, then my app (which requires running redis instance), and how do I expose my HTTP endpoints to the "outside world"? I read about Deployments, but I am not sure how to connect these pieces together.

理想情况下,我希望将所有配置都包含在YAML文件中,以便最终可以通过单个命令启动整个基础架构.

Ideally, I would like to have all configurations inside YAML files, so that at the end of the day the whole infrastructure could be started with a single command.

推荐答案

我同意所有前面的答案.我只是想通过执行一个命令来使事情变得更简单.

I agree with all of the previous answers. I'm just trying to things more simple by executing a single command.

首先,在名为redis.yaml的文件中为redis创建必要的清单,并进行服务以将其公开到外部.

First, create necessary manifests for redis in a file say redis.yaml and service to expose it outside.

apiVersion: v1
kind: Service
metadata:
  name: redis
  labels:
    app: node-redis
spec:
  ports:
  - name: redis
    port: 6379
    targetPort: 6379
  type: NodePort
  selector:
    app: node-redis
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
spec:
  selector:
    matchLabels:
      app: node-redis
  replicas: 1
  template:
    metadata:
      labels:
        app: node-redis
    spec:
      containers:
      - name: redis
        image: redis:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 6379
        # data volume where redis writes data
        volumeMounts:
        - name: data
          mountPath: /data
          readOnly: false
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: redis-data
---
# data volume
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-data
  labels:
    app: node-redis
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi

接下来,将您应用的清单放在另一个文件中,例如my-app.yaml.在这里,我放置了音量字段,以便您可以使用redis存储的数据.

Next put manifests for your app in another file say my-app.yaml. Here i put the volume field so that you can use the data that stored by redis.

apiVersion: v1
kind: Pod
metadata:
  name: my-app
  labels:
    app: node-redis
spec:
  containers:
  - name: my-app
    image: my-app:latest
    ports:
    - containerPort: 8080
    # data volume from where my-app read data those are written by redis
    volumeMounts:
    - name: data
      mountPath: /data
      readOnly: false
  volumes:
  - name: data
    persistentVolumeClaim:
      claimName: redis-data

现在我们可以使用以下bash文件my-app.sh.

Now we can use the following bash file my-app.sh.

#!/bin/bash

kubectl create -f redis.yaml

pod_name=$(kubectl get po -l app=node-redis | grep app-with-redis | awk '{print $1}')

# check whether redis server is ready or not
while true; do
  pong=$(kubectl exec -it $pod_name -c redis redis-cli ping)
  if [[ "$pong" == *"PONG"* ]]; then
    echo ok;
    break
  fi
done

kubectl create -f my-app.yaml

只需运行chmod +x my-app.sh; ./my-app.sh进行部署.要获取URL,请运行minikube service redis --url.您可以类似地获取应用程序的URL.唯一的事情是您需要为应用程序提供nodePort类型的服务,以便从群集外部访问它.

Just run chmod +x my-app.sh; ./my-app.sh to deploy. To get the url run minikube service redis --url. You can similarly get the url for your app. The only thing is you need a nodePort type service for your app to access it from outside of the cluster.

所以,现在一切都在您手中.

So, everything is in your hand now.

这篇关于如何在Kubernetes上使用Redis部署Node.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆