How to deploy a node.js with redis on kubernetes?

前端 未结 5 1984
长发绾君心
长发绾君心 2021-01-12 18:57

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.

From my understanding, I n

5条回答
  •  天涯浪人
    2021-01-12 19:56

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

    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
    

    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
    

    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
    

    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.

提交回复
热议问题