Introduction
Kubernetes (K8s) is a powerful container orchestration platform that simplifies application deployment and scaling. In this guide, we’ll set up Kubernetes on an AWS EC2 instance, install the Nginx Ingress Controller, and configure Ingress rules to expose multiple services (app1 and app2).
Step 1: Setting Up Kubernetes on an EC2 Instance
1.1 Launch an EC2 Instance
Choose an instance with enough resources (e.g., t3.medium or larger) and install Ubuntu 20.04 or Amazon Linux 2.
1.2 Update Packages
sudo apt update && sudo apt upgrade -y # For Ubuntu
1.3 Install Docker
sudo apt install -y docker.io
sudo systemctl enable --now docker
1.4 Install Kubernetes (kubectl, kubeadm, kubelet)
sudo apt install -y apt-transport-https ca-certificates curl
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
1.5 Initialize Kubernetes
sudo kubeadm init --pod-network-cidr=192.168.0.0/16
Follow the output instructions to 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
1.6 Install a Network Plugin (Calico)
For Calico:
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Now, Kubernetes is ready!
Step 2: Install Nginx Ingress Controller
Nginx Ingress Controller helps manage external traffic to services inside the cluster.
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
Wait until the controller is running:
kubectl get pods -n ingress-nginx
You should see ingress-nginx-controller running.
Step 3: Deploy Two Applications (app1 and app2)
3.1 Deploy app1
Create app1-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: app1
spec:
replicas: 2
selector:
matchLabels:
app: app1
template:
metadata:
labels:
app: app1
spec:
containers:
- name: app1
image: nginx
ports:
- containerPort: 80
Create app1-service.yaml:
apiVersion: v1
kind: Service
metadata:
name: app1-service
spec:
selector:
app: app1
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
Apply the resources:
kubectl apply -f app1-deployment.yaml
kubectl apply -f app1-service.yaml
3.2 Deploy app2
Create app2-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: app2
spec:
replicas: 2
selector:
matchLabels:
app: app2
template:
metadata:
labels:
app: app2
spec:
containers:
- name: app2
image: nginx
ports:
- containerPort: 80
Create app2-service.yaml:
apiVersion: v1
kind: Service
metadata:
name: app2-service
spec:
selector:
app: app2
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
Apply the resources:
kubectl apply -f app2-deployment.yaml
kubectl apply -f app2-service.yaml
Step 4: Configure Ingress for app1 and app2
Create nginx-ingress.yaml:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: app1.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app1-service
port:
number: 80
- host: app2.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app2-service
port:
number: 80
Apply the Ingress rule:
kubectl apply -f nginx-ingress.yaml
Step 5: Verify Everything
5.1 Get Ingress External IP
kubectl get ingress
5.2 Update /etc/hosts (Local Testing Only)
If you’re testing on a local machine, add this to /etc/hosts:
app1.example.com
app2.example.com
Replace with the actual external IP of your Ingress Controller.
5.3 Test in Browser or Curl
curl http://app1.example.com
curl http://app2.example.com
If everything is set up correctly, you should see the default Nginx welcome page for both applications.
Conclusion
In this guide, we:
- Installed Kubernetes on an EC2 instance
- Set up Nginx Ingress Controller
- Deployed two services (app1 and app2)
- Configured Ingress to expose them via domain names
Now, you can easily manage multiple applications in your cluster using a single Ingress resource.
Follow for more . Happy learning 🙂