Deploy Microservices
Table of contents
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.
- 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.
Deploy First Backend Service
The architecture from now on would be shown below.
Deploy flask backend
To proceed with this lab, Upload container image to Amazon ECR part must be preceded.
Move on to manifests folder(/home/ec2-user/environment/manifests).
cd ~/environment/manifests/
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
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
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
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
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
Deploy Express backend
The architecture from now on would be shown below.
Move on to manifests folder(/home/ec2-user/environment/manifests).
cd ~/environment/manifests/
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
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
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
Deploy the manifest files.
kubectl apply -f nodejs-deployment.yaml kubectl apply -f nodejs-service.yaml kubectl apply -f nodejs-ingress.yaml
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
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.
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>
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}
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).
Environment > amazon-eks-frontend > src > App.js
In the above source code, paste the values derived from the result (ingress addresses) below.
Environment > amazon-eks-frontend > src > page > UpperPage.js
echo http://$(kubectl get ingress/flask-backend-ingress -o jsonpath='{.status.loadBalancer.ingress[*].hostname}')/contents/'${search}'
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
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
docker push $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/demo-frontend:latest
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
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