Skip to main content

Kubernetes deployment within an ec2 instance

Kubernetes within an EC2 instance,


We have to follow these steps:-

  1. Set up the EC2 instance with Kubernetes.
  2. Create a Kubernetes Deployment YAML file.
  3. Apply the deployment using kubectl.

Below is a guide and code to accomplish this.

Step 1: Set Up EC2 Instance with Kubernetes

  1. Launch an EC2 Instance:

    • Choose an Amazon Linux 2 AMI or Ubuntu AMI.
    • Select an instance type (t2.micro is fine for small projects).
    • Configure security groups to allow SSH, HTTP, HTTPS, and any required Kubernetes ports.
  2. Install Docker: SSH into your instance and install Docker.

    sudo yum update -y
    sudo amazon-linux-extras install docker -y sudo service docker start sudo usermod -aG docker ec2-user

    For Ubuntu:

    sudo apt-get update
    sudo apt-get install -y docker.io sudo systemctl start docker sudo usermod -aG docker ubuntu
  3. Install Kubernetes (kubectl, kubeadm, kubelet):s

    sudo apt-get update && sudo apt-get install -y apt-transport-https curl curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list sudo apt-get update sudo apt-get install -y kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubect
  4. Initialize Kubernetes (Master Node):

    • This is usually done on the master node, but for simplicity, we'll assume a single-node setup.
    sudo kubeadm init --pod-network-cidr=192.168.0.0/16
  5. Set up kubectl for your user:

    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
  6. Install a Pod Network (Weave, Flannel, etc.):

    • For example, with Flannel:
    kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master

Step 2: Create a Kubernetes Deployment YAML File

Below is a sample YAML file for deploying a simple Nginx application.

apiVersion: apps/v1
kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.2 ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 type: LoadBalancer

Step 3: Deploy the Application

  1. Apply the Deployment: Save the above YAML content to a file named nginx-deployment.yaml.

    kubectl apply -f nginx-deployment.yaml
  2. Verify the Deployment:

    kubectl get deployments
    kubectl get pods kubectl get services
  3. Access the Application:

    • If you have set the Service type to LoadBalancer, Kubernetes will provision a public IP through your cloud provider. Use kubectl get services to find the external IP and access your application via a browser or curl.

Additional Considerations:

  • Scaling: You can scale the number of replicas easily with:

    kubectl scale deployment nginx-deployment --replicas=5
  • Monitoring: Consider setting up monitoring for your Kubernetes cluster using tools like Prometheus and Grafana.

This process will give you a basic setup to deploy an application on Kubernetes running on an EC2 instance. For production, you should explore multi-node clusters, proper security configurations, and advanced networking setups.

Comments

Popular posts from this blog

What is Fuzzy Logic?

 Title: Demystifying Fuzzy Logic: A Primer for Engineering Students Introduction In the world of engineering, precise calculations and binary decisions often reign supreme. However, there are real-world scenarios where the classical "yes" or "no" approach falls short of capturing the nuances of human thought and the complexity of certain systems. This is where fuzzy logic comes into play. Fuzzy logic is a powerful tool that allows engineers to handle uncertainty and vagueness in a more human-like way. In this article, we'll explore the basics of fuzzy logic, its applications, and how it can benefit engineering students. Understanding Fuzzy Logic Fuzzy logic, developed by Lotfi Zadeh in the 1960s, is a mathematical framework that deals with reasoning and decision-making in the presence of uncertainty and imprecision. Unlike classical binary logic, which relies on "true" or "false" values, fuzzy logic works with degrees of truth, allowing for a...

Unlocking the Power of CGI-BIN: A Dive into Common Gateway Interface for Dynamic Web Content

 CGI-BIN What is CGI-BIN? The Common Gateway Interface (CGI) is a standard protocol for enabling web servers to execute programs that generate web content dynamically. CGI scripts are commonly written in languages such as Perl, Python, and PHP, and they allow web servers to respond to user input and generate customized web pages on the fly. The CGI BIN directory is a crucial component of this process, serving as the location where these scripts are stored and executed. The CGI BIN directory is typically found within the root directory of a web server, and it is often named "cgi-bin" or "CGI-BIN". This directory is designated for storing executable scripts and programs that will be run by the server in response to requests from web clients. When a user interacts with a web page that requires dynamic content, the server will locate the appropriate CGI script in the CGI BIN directory and execute it to generate the necessary output. One of the key advantages of using ...

Machine Learning: The Power , Pros and Potential.

 **Title: Machine Learning: The Power, Pros, and Potential Pitfalls** **Introduction** Machine Learning (ML) stands as one of the most transformative technologies of our time, offering a glimpse into a future where data-driven decisions and automation redefine how we live and work. In this blog, we'll delve into the world of machine learning, exploring its myriad benefits, potential drawbacks, and the exciting possibilities it holds for the future. **Understanding Machine Learning** Machine learning is a subset of artificial intelligence that equips computers with the ability to learn and improve from experience without being explicitly programmed. It relies on algorithms and statistical models to make predictions or decisions based on data, a process often described as "training" a model. **The Benefits of Machine Learning** 1. **Automation and Efficiency**: ML can automate repetitive tasks, freeing up human resources for more creative and complex endeavors. This boosts...