Skip to content

Latest commit

 

History

History
231 lines (132 loc) · 12.4 KB

Cloud deployment.md

File metadata and controls

231 lines (132 loc) · 12.4 KB

Cloud deployment

The target cloud service for the project is Amazon web services (AWS). Free tier version of the AWS will be used.

For more information regarding AWS free Tier please visit the official AWS website

Terraform will be used to setup the infrastructure on AWS.

Please follow the steps to setup the infrastructure on AWS

pre-requisites

The code was tested on Apple mac m2 chip. Install the required tools

  • Terraform version : v1.5.5
  • Create a AWS account
  • awscli version : 2.13.9

To check the pre installed version of the tools

For terraform execute the command in terminal : terraform version

Screenshot 2023-08-23 at 17.05.37.png

For awscli execute : aws --version

Screenshot 2023-08-23 at 17.06.44.png

Sock shop itself provides the documentation for AWS deployment but has been already outdated as most of the underlying infrastructure has changed and the old terraform files require new security parameters to be included. Furthermore deploying the sock shop web application, requires necessary packages to be pre installed in order to run kubernetes application. The installation process of necessary packages has too changed. Hence a new terraform installation file is available under /cnae-sockshop/microservices-demo/deploy/kubernetes/AWS_deployment/

We have a combination of Terraform and shell scripts to automate the whole process for setting up the infrastructure and deploy sock shop smoothly.

💡 Note : As mentioned earlier we will be working with the free tier of AWS. Hence we don’t have access to [EKS,](https://docs.aws.amazon.com/eks/latest/userguide/what-is-eks.html) which is a managed kubernetes service by AWS and it saves a load of hazzle for the developer in order to deploy the application.

We will be working with t2.micro EC2 instances of ubuntu having 1vCPU and with 1GB of memory.

Screenshot 2023-08-23 at 17.24.33.png

Step-by-step guide

  • Creating access key on AWS

    login to your AWS account

    1. In the navigation bar on the upper right, choose your user name, and then choose Security credentials.

      https://docs.aws.amazon.com/images/IAM/latest/UserGuide/images/security-credentials-user.shared.console.png

    Follow the steps :[source : AWS official website]

    1. In the Access keys section, choose Create access key. If you already have two access keys, this button is deactivated and you must delete an access key before you can create a new one.
    2. On the Access key best practices & alternatives page, choose your use case to learn about additional options which can help you avoid creating a long-term access key. If you determine that your use case still requires an access key, choose Other and then choose Next.
    💡 Copy the access key and secret key on clipboard to be used later to generate key value pair.
    1. (Optional) Set a description tag value for the access key. This adds a tag key-value pair to your IAM user. This can help you identify and rotate access keys later. The tag key is set to the access key id. The tag value is set to the access key description that you specify. When you are finished, choose Create access key.
    2. On the Retrieve access keys page, choose either Show to reveal the value of your user's secret access key, or Download .csv file. This is your only opportunity to save your secret access key. After you've saved your secret access key in a secure location, choose Done.
  • Generating key value pair

    We will start by exporting some envirnomental variables on the local system and not save them in a file due to security reasons. As your free tier resources can be used by some free loaders and get your account suspended.

    Next execute the command :

    aws configure

    Design ohne Titel.png

    Your default region can be seen on top right corner . For the usage in germany Frankfurt is the region to go, which is “eu-central-1”

    Screenshot 2023-08-23 at 18.11.10.png

    Once setup of the variable is finished run the following commands to generate key value pair and store in the folder ~/.ssh/ . Next change the file permission which allows the owner of the file to have read and write permission.

    aws ec2 create-key-pair --key-name deploy-docs-k8s --query 'KeyMaterial' --output text > ~/.ssh/deploy-docs-k8s.pem
    
    chmod 600 ~/.ssh/deploy-docs-k8s.pem

    Furthermore add the following environmental variable for terraform. Simply execute in the terminal window.

    export TF_VAR_access_key=AKI*********Z***A
    export TF_VAR_secret_key=CVZ**********************9
  • Create infrastructure on AWS

    Navigate to the /cnae-sockshop/microservices-demo/deploy/kubernetes/AWS\ deployment

    Execute the following commands:

    terraform init
    
    terraform plan 
    
    terraform apply

    Screenshot 2023-08-24 at 01.17.26.png

    AWS instances will be created and will be seen in the aws console. From the above masterIP is available to us

    Master node configuration :

    Now ssh into your master node using terminal :

    for master_public_ip use the IP adress from the above

    ssh -i ~/.ssh/deploy-docs-k8s.pem ubuntu@<master_public_ip>

    Master public ip can be seen either from terminal itself in the outputs of the terraform or from the AWS console

    add image for AWS console

    now as you are in the master instance console. Make sure kubeadm is installed by using kubeadm verison

    Execute the following comand to setup configuration for kubernetes master node :

    chmod +x /tmp/master_setup.sh && /tmp/master_setup.sh

    once executed the result will be as follows

    Screenshot 2023-08-24 at 01.26.36.png

    copy the last two lines as marked as it will be used to connect worker node to the master in order to form a k8s cluster.

    In order to check the status of kubernetes master node :

    kubectl get nodes

    Screenshot 2023-08-24 at 01.30.20.png

    It should show the status ready which means the weavenet cli plugin is successfully installed and is up and running.

    Next we will be joining the worker/slave nodes to our master node

    Screenshot 2023-08-24 at 12.41.32.png

    ssh -i ~/.ssh/deploy-docs-k8s.pem ubuntu@<worker_public_ip> # Here the
     # above node-addresses can be used to ssh into the instance. This are dns names of all the worker instance 

    and execute the kubeadm join command which was copied from before and run with sudo and one more flag --ignore-preflight-errors=all. As we don’t have enough resources required by kubernetes officially to run nodes. Do the following for all the worker nodes.

    sudo kubeadm join <ip address> --token <token> --discovery-token-ca-cert-hash <hash token> --ignore-preflight-errors=all

    Screenshot 2023-08-24 at 12.50.15.png

    Now to check if the worker are connected to master ssh into your master instance

    and execute

    kubectl get nodes

    Screenshot 2023-08-24 at 12.54.43.png

    One can see different nodes connected and the status should be ready if not then delete the particular instance and generate one more. Otherwise restart the whole process.

  • Deployment

    Now we are all setup with our k8s cluster. To deploy the sock shop

    ssh into your master node and execute the following in the shell

    chmod +x /tmp/deploy_sockshop.sh && /tmp/deploy_sockshop.sh

    Screenshot 2023-08-24 at 13.00.58.png

    Some similar information can be seen. It might take a while to set it up and running as some workarounds have been added to deploy the application due to less resources available in free tier of AWS.

    At the end execute kubectl get nodes to check if all the nodes are still working and haven’t overloaded or closed due to some errors.

    Next execute kubectl get pods -A to see if all the pods are up and running

    Screenshot 2023-08-25 at 10.18.50.png

    Name space can be easily changed to see kubectl get pods -n sock-shop to see all the pods running in the namepsace sock-shop

    NOTE: it might take a bit of time till all pods are running.

    NOTE: it might take a bit of time till all pods are running.

    Once all are up and running, lets see the hosted sock-shop application. Execute

     kubectl describe svc front-end -n sock-shop

    Screenshot 2023-08-24 at 13.13.47.png

    The website is hosted at http://<master_node_public_ip>:

    In our case it is http://18.193.110.84:30001

    Screenshot 2023-08-24 at 23.33.51.png

  • Deployment with new solution HPA+autoscaling

    navigate to the folder : /cnae-sockshop/microservices-demo/deploy/kubernetes/AWS_optimized_deployment

    Follow the same steps as in the above section create infrastructure and Deployment

    Screenshot 2023-08-25 at 20.05.50.png

    Check that all the pods are up and running. It might take around 12-15 minutes to run on t2.micro instances. As suggested if possible use t2.medium

    Screenshot 2023-08-25 at 20.06.06.png