如何在 kubernetes 上使用 redis 部署 node.js? [英] How to deploy a node.js with redis on kubernetes?
问题描述
我有一个非常简单的 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 构建 docker 镜像.
接下来,我为我的应用程序的 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:
redis 应该是另一个 Pod 还是服务(就 Kubernetes 类型而言)?
should redis be another Pod, or a Service (in terms of Kubernetes kind)?
如何从我的应用程序内部引用 redis?根据redis是否会被定义为Pod/Service,如何获取连接URL和端口?我了解了 Kubernetes 创建的环境变量,但我不确定这些变量是否适用于 Pod 或服务.
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 创建必要的清单,例如 redis.yaml
和服务以将其公开.
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
.这里我放了volume字段,这样你就可以使用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屋!