Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Deploy Microservices

Table of contents
  1. Deploy Microservices
    1. Deploy First Backend Service
      1. Deploy flask backend
    2. Deploy Express backend
    3. Deploy Frontend Service
      1. Deploy React Frontend

In this lab, you will learn how to deploy the backend, frontend to Amazon EKS, which makes up the web service. The order in which each service is deployed is as follows.

1

  • Download source code from git repository
  • Create a repository for each container image in Amazon ECR
  • Build container image from source code location, including Dockerfile, and push to repository
  • Create and deploy Deployment, Service, Ingress manifest files for each service.

The figure below shows the order in which end users access the web service.

2

Deploy First Backend Service

The architecture from now on would be shown below.

3

Deploy flask backend

To proceed with this lab, Upload container image to Amazon ECR part must be preceded.

  1. Move on to manifests folder(/home/ec2-user/environment/manifests).

     cd ~/environment/manifests/
    
  2. Create deploy manifest.

     cat <<EOF> flask-deployment.yaml
     ---
     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: demo-flask-backend
       namespace: default
     spec:
       replicas: 3
       selector:
         matchLabels:
           app: demo-flask-backend
       template:
         metadata:
           labels:
             app: demo-flask-backend
         spec:
           containers:
             - name: demo-flask-backend
               image: $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/demo-flask-backend:latest
               imagePullPolicy: Always
               ports:
                 - containerPort: 8080
     EOF
    

    5

  3. Next, create service manifest.

     cat <<EOF> flask-service.yaml
     ---
     apiVersion: v1
     kind: Service
     metadata:
       name: demo-flask-backend
       annotations:
         alb.ingress.kubernetes.io/healthcheck-path: "/contents/aws"
     spec:
       selector:
         app: demo-flask-backend
       type: NodePort
       ports:
         - port: 8080
           targetPort: 8080
           protocol: TCP
     EOF
    

    6

  4. Finally, create ingress manifest.

     cat <<EOF> flask-ingress.yaml
     ---
     apiVersion: networking.k8s.io/v1
     kind: Ingress
     metadata:
         name: "flask-backend-ingress"
         namespace: default
         annotations:
           kubernetes.io/ingress.class: alb
           alb.ingress.kubernetes.io/scheme: internet-facing
           alb.ingress.kubernetes.io/target-type: ip
           alb.ingress.kubernetes.io/group.name: eks-demo-group
           alb.ingress.kubernetes.io/group.order: '1'
     spec:
         rules:
         - http:
             paths:
               - path: /contents
                 pathType: Prefix
                 backend:
                   service:
                     name: "demo-flask-backend"
                     port:
                       number: 8080
     EOF
    

    7

  5. Deploy the manifest created above in the order shown below. Ingress provisions Application Load Balancer(ALB).

     kubectl apply -f flask-deployment.yaml
     kubectl apply -f flask-service.yaml 
     kubectl apply -f flask-ingress.yaml
    

    8

  6. Paste the results of the following command into the Web browser or API platform(like Postman) to check:

     echo http://$(kubectl get ingress/flask-backend-ingress -o jsonpath='{.status.loadBalancer.ingress[*].hostname}')/contents/aws
    

    9

Deploy Express backend

The architecture from now on would be shown below.

4

  1. Move on to manifests folder(/home/ec2-user/environment/manifests).

     cd ~/environment/manifests/
    
  2. Create deploy manifest which contains built pre-built container image.

     cat <<EOF> nodejs-deployment.yaml
     ---
     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: demo-nodejs-backend
       namespace: default
     spec:
       replicas: 3
       selector:
         matchLabels:
           app: demo-nodejs-backend
       template:
         metadata:
           labels:
             app: demo-nodejs-backend
         spec:
           containers:
             - name: demo-nodejs-backend
               image: public.ecr.aws/y7c9e1d2/joozero-repo:latest
               imagePullPolicy: Always
               ports:
                 - containerPort: 3000
     EOF
    

    10

  3. And then, create service manifest file.

     cat <<EOF> nodejs-service.yaml
     ---
     apiVersion: v1
     kind: Service
     metadata:
       name: demo-nodejs-backend
       annotations:
         alb.ingress.kubernetes.io/healthcheck-path: "/services/all"
     spec:
       selector:
         app: demo-nodejs-backend
       type: NodePort
       ports:
         - port: 8080
           targetPort: 3000
           protocol: TCP
     EOF
    

    11

  4. create ingress manifest.

     cat <<EOF> nodejs-ingress.yaml
     ---
     apiVersion: networking.k8s.io/v1
     kind: Ingress
     metadata:
       name: "nodejs-backend-ingress"
       namespace: default
       annotations:
         kubernetes.io/ingress.class: alb
         alb.ingress.kubernetes.io/scheme: internet-facing
         alb.ingress.kubernetes.io/target-type: ip
         alb.ingress.kubernetes.io/group.name: eks-demo-group
         alb.ingress.kubernetes.io/group.order: '2'
     spec:
       rules:
       - http:
             paths:
               - path: /services
                 pathType: Prefix
                 backend:
                   service:
                     name: "demo-nodejs-backend"
                     port:
                       number: 8080
     EOF
    

    12

  5. Deploy the manifest files.

     kubectl apply -f nodejs-deployment.yaml
     kubectl apply -f nodejs-service.yaml
     kubectl apply -f nodejs-ingress.yaml
    

    13

  6. Paste the results of the following command into the Web browser or API platform(like Postman) to check.

     echo http://$(kubectl get ingress/nodejs-backend-ingress -o jsonpath='{.status.loadBalancer.ingress[*].hostname}')/services/all
    

    14

Deploy Frontend Service

Deploy React Frontend

Once you have deployed two backend services, you will now deploy the frontend to configure the web page’s screen.

  1. Download the source code to be containerized through the command below.

     cd /home/ec2-user/environment
     git clone <https://github.com/joozero/amazon-eks-frontend.git>
    

    15

  2. Through AWS CLI, create an image repository. In this lab, we will set the repository name to demo-frontend.

     aws ecr create-repository \
     --repository-name demo-frontend \
     --image-scanning-configuration scanOnPush=true \
     --region ${AWS_REGION}
    

    16

  3. To spray two backend API data on the web screen, we have to change source code. Change the url values in App.js file and page/upperPage.js file from the frontend source code(location: /home/ec2-user/environment/amazon-eks-frontend/src).

    18

    Environment > amazon-eks-frontend > src > App.js

    17 19

    In the above source code, paste the values derived from the result (ingress addresses) below.

    Environment > amazon-eks-frontend > src > page > UpperPage.js

    20

    22

     echo http://$(kubectl get ingress/flask-backend-ingress -o jsonpath='{.status.loadBalancer.ingress[*].hostname}')/contents/'${search}'
    

    21

  4. Execute the following command in the location of the amazon-eks-frontend folder.

     cd /home/ec2-user/environment/amazon-eks-frontend
     npm install
     npm run build
    

    23 24

  5. Refer Upload container image to Amazon ECR guide and proceed to create container image repository and push image. In this lab, set the image repository name to demo-frontend.

     docker build -t demo-frontend .
    
     docker tag demo-frontend:latest $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/demo-frontend:latest
    

    25

     docker push $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/demo-frontend:latest
    

    26

    During applying above CLI, if you receive message which said denied: Your authorization token has expired. Reauthenticate and try again., then applying bottom command line and do this again

     aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
    
  6. Move to manifests folder. At this point, type image value to demo-frontend repository URI.

     cd /home/ec2-user/environment/manifests
    
     cat <<EOF> frontend-deployment.yaml
     ---
     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: demo-frontend
       namespace: default
     spec:
       replicas: 3
       selector:
         matchLabels:
           app: demo-frontend
       template:
         metadata:
           labels:
             app: demo-frontend
         spec:
           containers:
             - name: demo-frontend
               image: $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/demo-frontend:latest
               imagePullPolicy: Always
               ports:
                 - containerPort: 80
     EOF
    
     cat <<EOF> frontend-service.yaml
     ---
     apiVersion: v1
     kind: Service
     metadata:
       name: demo-frontend
       annotations:
         alb.ingress.kubernetes.io/healthcheck-path: "/"
     spec:
       selector:
         app: demo-frontend
       type: NodePort
       ports:
         - protocol: TCP
           port: 80
           targetPort: 80
     EOF
    
     cat <<EOF> frontend-ingress.yaml
     ---
     apiVersion: networking.k8s.io/v1
     kind: Ingress
     metadata:
       name: "frontend-ingress"
       namespace: default
       annotations:
         kubernetes.io/ingress.class: alb
         alb.ingress.kubernetes.io/scheme: internet-facing
         alb.ingress.kubernetes.io/target-type: ip
         alb.ingress.kubernetes.io/group.name: eks-demo-group
         alb.ingress.kubernetes.io/group.order: '3'
     spec:
       rules:
         - http:
             paths:
               - path: /
                 pathType: Prefix
                 backend:
                   service:
                     name: "demo-frontend"
                     port:
                       number: 80
     EOF
    
     kubectl apply -f frontend-deployment.yaml
     kubectl apply -f frontend-service.yaml
     kubectl apply -f frontend-ingress.yaml