Skip to content

Commit

Permalink
added Kuberenetes setup procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Nov 11, 2021
1 parent 05222af commit aa2ff42
Show file tree
Hide file tree
Showing 3 changed files with 289 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,108 +1,101 @@
# Setup Kubernetes (K8s) Cluster on AWS


1. Create Ubuntu EC2 instance
1. install AWSCLI
```sh
curl https://s3.amazonaws.com/aws-cli/awscli-bundle.zip -o awscli-bundle.zip
apt install unzip python
unzip awscli-bundle.zip
#sudo apt-get install unzip - if you dont have unzip in your system
./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
```

1. Install kubectl on ubuntu instance
```sh
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
```

1. Install kops on ubuntu instance
```sh
curl -LO https://github.com/kubernetes/kops/releases/download/1.15.0/kops-linux-amd64
chmod +x kops-linux-amd64
sudo mv kops-linux-amd64 /usr/local/bin/kops
kops version (it should be 1.15.0)
Note: use below command if you wish to use latest version. For now we could see latest version of kops. So ignore it until further update.
# curl -LO https://github.com/kubernetes/kops/releases/download/$(curl -s https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name | cut -d '"' -f 4)/kops-linux-amd64
```
1. Create an IAM user/role with Route53, EC2, IAM and S3 full access
1. Attach IAM role to ubuntu instance
```sh
# Note: If you create IAM user with programmatic access then provide Access keys. Otherwise region information is enough
aws configure
```
1. Create a Route53 private hosted zone (you can create Public hosted zone if you have a domain)
```sh
Routeh53 --> hosted zones --> created hosted zone
Domain Name: valaxy.net
Type: Private hosted zone for Amazon VPC. Make sure you are chosing right VPC if you have multiple
```
1. create an S3 bucket
```sh
aws s3 mb s3://demo.k8s.valaxy.net
```
1. Expose environment variable:
```sh
export KOPS_STATE_STORE=s3://demo.k8s.valaxy.net
```
1. Create sshkeys before creating cluster
```sh
ssh-keygen
```
1. Create kubernetes cluster definitions on S3 bucket
```sh
kops create cluster --cloud=aws --zones=ap-south-1b --name=demo.k8s.valaxy.net --dns-zone=valaxy.net --dns private
```
1. Create kubernetes cluser
```sh
kops update cluster demo.k8s.valaxy.net --yes
```
1. To cahnge the kubernetes master and worker instance sizes
```sh
kops edit ig --name=<cluster_name> nodes
#kops edit ig --name=demo.k8s.valaxy.net nodes
kops edit ig --name=<cluster_name> master-<zone_name>
#kops edit ig --name=demo.k8s.valaxy.net master-ap-south-1b
```
1. to Delete cluster (try once your lab is done)
```sh
kops delete cluster <cluster_name> --yes
```
1. Validate your cluster
```sh
kops validate cluster
```
1. To list nodes
```sh
kubectl get nodes
```
#### Deploying Nginx pods on Kubernetes
1. Deploying Nginx Container
```sh
kubectl run --generator=run-pod/v1 sample-nginx --image=nginx --replicas=2 --port=80
#kubectl run sample-nginx --image=nginx --replicas=2 --port=80
# kubectl run simple-devops-project --image=yankils/simple-devops-image --replicas=2 --port=8080
kubectl get pods
kubectl get deployments
```
1. Expose the deployment as service. This will create an ELB in front of those 2 containers and allow us to publicly access them.
```sh
kubectl expose deployment sample-nginx --port=80 --type=LoadBalancer
# kubectl expose deployment simple-devops-project --port=8080 --type=LoadBalancer
kubectl get services -o wide
```
# Setup Kubernetes (K8s) Cluster on AWS


1. Create Ubuntu EC2 instance
1. install AWSCLI
```sh
curl https://s3.amazonaws.com/aws-cli/awscli-bundle.zip -o awscli-bundle.zip
sudo apt update
sudo apt install unzip python
unzip awscli-bundle.zip
#sudo apt-get install unzip - if you dont have unzip in your system
./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
```

1. Install kubectl on ubuntu instance
```sh
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
```

1. Install kops on ubuntu instance
```sh
curl -LO https://github.com/kubernetes/kops/releases/download/$(curl -s https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name | cut -d '"' -f 4)/kops-linux-amd64
chmod +x kops-linux-amd64
sudo mv kops-linux-amd64 /usr/local/bin/kops
```
1. Create an IAM user/role with Route53, EC2, IAM and S3 full access

1. Attach IAM role to ubuntu instance
```sh
# Note: If you create IAM user with programmatic access then provide Access keys. Otherwise region information is enough
aws configure
```

1. Create a Route53 private hosted zone (you can create Public hosted zone if you have a domain)
```sh
Routeh53 --> hosted zones --> created hosted zone
Domain Name: valaxy.net
Type: Private hosted zone for Amzon VPC
```

1. create an S3 bucket
```sh
aws s3 mb s3://demo.k8s.valaxy.net
```
1. Expose environment variable:
```sh
export KOPS_STATE_STORE=s3://demo.k8s.valaxy.net
```

1. Create sshkeys before creating cluster
```sh
ssh-keygen
```

1. Create kubernetes cluster definitions on S3 bucket
```sh
kops create cluster --cloud=aws --zones=ap-south-1b --name=demo.k8s.valaxy.net --dns-zone=valaxy.net --dns private
```

1. If you wish to update the cluster worker node sizes use below command
```sh
kops edit ig --name=CHANGE_TO_CLUSTER_NAME nodes
```

1. Create kubernetes cluser
```sh
kops update cluster demo.k8s.valaxy.net --yes
```

1. Validate your cluster
```sh
kops validate cluster
```

1. To list nodes
```sh
kubectl get nodes
```

1. To delete cluster
```sh
kops delete cluster demo.k8s.valaxy.net --yes
```

#### Deploying Nginx pods on Kubernetes
1. Deploying Nginx Container
```sh
kubectl run sample-nginx --image=nginx --replicas=2 --port=80
# kubectl run simple-devops-project --image=yankils/simple-devops-image --replicas=2 --port=8080
kubectl get pods
kubectl get deployments
```

1. Expose the deployment as service. This will create an ELB in front of those 2 containers and allow us to publicly access them.
```sh
kubectl expose deployment sample-nginx --port=80 --type=LoadBalancer
# kubectl expose deployment simple-devops-project --port=8080 --type=LoadBalancer
kubectl get services -o wide
```
125 changes: 125 additions & 0 deletions Kubernetes/Kubernetes_Setup_using_kubeadm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Kubernetes Cluster installation using kubeadm
Follow this documentation to set up a Kubernetes cluster on __CentOS__ 7 machines.

This documentation guides you in setting up a cluster with one master node and two worker nodes.

## Prerequisites:
1. System Requirements
>Master: t2.medium (2 CPUs and 2GB Memory)
>Worker Nodes: t2.micro
1. Open Below ports in the Security Group.
#### Master node:
`6443
32750
10250
4443
443
8080 `

##### On Master node and Worker node:
`179`

### `On Master and Worker:`
1. Perform all the commands as root user unless otherwise specified

Install, Enable and start docker service.
Use the Docker repository to install docker.
> If you use docker from CentOS OS repository, the docker version might be old to work with Kubernetes v1.13.0 and above
```sh
yum install -y -q yum-utils device-mapper-persistent-data lvm2 > /dev/null 2>&1
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo > /dev/null 2>&1
yum install -y -q docker-ce >/dev/null 2>&1
```
1. Start Docker services
```sh
systemctl enable docker
systemctl start docker
```
1. Disable SELinux
```sh
setenforce 0
sed -i --follow-symlinks 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
```
1. Disable Firewall
```sh
systemctl disable firewalld
systemctl stop firewalld
```
1. Disable swap
```sh
sed -i '/swap/d' /etc/fstab
swapoff -a
```
1. Update sysctl settings for Kubernetes networking
```sh
cat >> /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
```
## Kubernetes Setup
1. Add yum repository for kubernetes packages
```sh
cat >>/etc/yum.repos.d/kubernetes.repo<<EOF
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
```
1. Install Kubernetes
```sh
yum install -y kubeadm-1.15.6-0.x86_64 kubelet-1.15.6-0.x86_64 kubectl-1.15.6-0.x86_64
```
1. Enable and Start kubelet service
```sh
systemctl enable kubelet
systemctl start kubelet
```
## `On Master Node:`
1. Initialize Kubernetes Cluster
```sh
kubeadm init --apiserver-advertise-address=<MasterServerIP> --pod-network-cidr=192.168.0.0/16
```
1. Create a user for kubernetes administration and copy kube config file.
``To be able to use kubectl command to connect and interact with the cluster, the user needs kube config file.``
In this case, we are creating a user called `kubeadmin`
```sh
useradd kubeadmin
mkdir /home/kubeadmin/.kube
cp /etc/kubernetes/admin.conf /home/kubeadmin/.kube/config
chown -R kubeadmin:kubeadmin /home/kubeadmin/.kube
```
1. Deploy Calico network as a __kubeadmin__ user.
> This should be executed as a user (heare as a __kubeadmin__ )
```sh
sudo su - kubeadmin
kubectl create -f https://docs.projectcalico.org/v3.9/manifests/calico.yaml
```
1. Cluster join command
```sh
kubeadm token create --print-join-command
```
## `On Worker Node:`
1. Add worker nodes to cluster
> Use the output from __kubeadm token create__ command in previous step from the master server and run here.
1. Verifying the cluster
To Get Nodes status
```sh
kubectl get nodes
```
To Get component status
```sh
kubectl get cs
```
63 changes: 63 additions & 0 deletions Kubernetes/kubernetes_setup_using_eksctl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Setup Kubernetes on Amazon EKS

You can follow same procedure in the official AWS document [Getting started with Amazon EKS – eksctl](https://docs.aws.amazon.com/eks/latest/userguide/getting-started-eksctl.html)

#### Pre-requisites:
- an EC2 Instance

#### AWS EKS Setup
1. Setup kubectl
a. Download kubectl version 1.20
b. Grant execution permissions to kubectl executable
c. Move kubectl onto /usr/local/bin
d. Test that your kubectl installation was successful
```sh
chmod +x ./kubectl
curl -o kubectl https://amazon-eks.s3.us-west-2.amazonaws.com/1.21.2/2021-07-05/bin/linux/amd64/kubectl
mv ./kubectl /usr/local/bin
kubectl version --short --client
```
2. Setup eksctl
a. Download and extract the latest release
b. Move the extracted binary to /usr/local/bin
c. Test that your eksclt installation was successful
```sh
curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
sudo mv /tmp/eksctl /usr/local/bin
eksctl version
```

3. Create an IAM Role and attache it to EC2 instance
`Note: create IAM user with programmatic access if your bootstrap system is outside of AWS`
IAM user should have access to
IAM
EC2
VPC
CloudFormation

4. Create your cluster and nodes
```sh
eksctl create cluster --name cluster-name \
--region region-name \
--node-type instance-type \
--nodes-min 2 \
--nodes-max 2 \
--zones <AZ-1>,<AZ-2>

example:
eksctl create cluster --name valaxy-cluster \
--region ap-south-1 \
--node-type t2.small \
```

5. To delete the EKS clsuter
```sh
eksctl delete cluster valaxy --region ap-south-1
```

6. Validate your cluster using by creating by checking nodes and by creating a pod
```sh
kubectl get nodes
kubectl run pod tomcat --image=tomcat
```

0 comments on commit aa2ff42

Please sign in to comment.