Skip to content

Commit

Permalink
v1.28.5
Browse files Browse the repository at this point in the history
  • Loading branch information
berry2012 committed Jan 9, 2024
1 parent 117471e commit c26c2aa
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 82 deletions.
161 changes: 83 additions & 78 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@
- [AWS Account](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/)
- [Key Pair](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/create-key-pairs.html)

# Cluster Details - 4 servers
## Cluster Details - 4 servers

- 1 control plane
- 2 worker nodes
- 1 Kubectl Client
- 1 Kubectl Client

## Default Kubernetes Version

# Default Kubernetes v1.27.5
v1.28.2

## How to specify a different Kubernetes Release Version

**Update 1.XX.X-00 based on Kubernetes release version in deployments/setup.sh**
**Update 1.XX.X-00 based on Kubernetes release version in deployments/deployment.yml for the Kubectl Client**
Be sure to check for all package dependencies before changing the k8s version.
E.g. Kubernetes v1.26.0 requires contained version 1.6.X and above.

E.g. Kubernetes v1.26.0 requires contained version 1.6.X and above. Be sure to check for all package dependencies before changing the k8s version.
- Update 1.XX.X-00 based on Kubernetes release version in `deployments/setup.sh`
- Update 1.XX.X-00 based on Kubernetes release version in `deployments/deployment.yml` for the Kubectl Client

## Node Details

- All the provisioned instances run the same OS

```
```bash
ubuntu@ip-10-192-10-110:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
Expand All @@ -40,165 +44,163 @@ BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
```
# Usage Instructions

## A. Deploying the Infrastructure with CloudFormation from AWS Console
## Getting Started

## Step 1: Deploy the Infrastructure on AWS

Follow any of the options below:

**Option A: Deploy the Infrastructure with CloudFormation from AWS Console:**

- Clone the repo
- Goto AWS Console > Choose Region (e.g. eu-west-1) > CloudFormation > Create Stack
- Use the CF Yaml template in *infrastructure/k8s_aws_instances.yml*
- Use the CF Yaml template in `infrastructure/k8s_aws_instances.yml`
- See image below:

![Create Infrastructure](./images/CF-infrastructure.png)
![Create Infrastructure](./images/CF-infrastructure.png)

**Option B: Deploy the Infrastructure with CloudFormation from AWS CLI:**

## B. Deploying the Infrastructure with CloudFormation from AWS CLI
```
git clone repo
- Clone the repo

```bash
git clone repo
cd kubeadm-lab-on-aws
```

### Define your environment variables e.g
- Define your environment variables

```
```bash
export REGION="eu-west-1"
export key_pair="my-EC2-key-name"
```

**Note: key_pair is your key pair and should already be created in AWS EC2.**

## create the infrastructure for your stack
- Create the infrastructure for your stack

```
```bash
aws cloudformation create-stack --stack-name kubeadm-lab --template-body file://infrastructure/k8s_aws_instances.yml --parameters ParameterKey=EnvironmentName,ParameterValue=k8s ParameterKey=KeyName,ParameterValue=${key_pair} --capabilities CAPABILITY_NAMED_IAM --region ${REGION}

```

## Check stack status for CREATE_COMPLETE. Takes about 3mins
- Check stack status for CREATE_COMPLETE. Takes about 3mins

```
```bash
aws cloudformation describe-stacks --stack-name kubeadm-lab --query 'Stacks[].StackStatus' --region ${REGION} --output text
aws cloudformation wait stack-create-complete --stack-name kubeadm-lab
```

## Step 2: Configuring the environment



## Accessing the EC2 instances
- Define your global variables

```
```bash
export LOCAL_SSH_KEY_FILE="~/.ssh/key.pem"
export REGION="eu-west-1"
export AWS_PROFILE="work"
```

**Note: By default, the AWS CLI uses the settings found in the profile named default. To use alternate settings, you can [create and reference additional profiles](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html).**

## Setting up for deployments
- Confirm the instances created and the Public IP of the Ansible controller server

```
```bash
aws ec2 describe-instances --filters "Name=tag:project,Values=k8s-kubeadm" "Name=instance-state-name,Values=running" --query 'Reservations[*].Instances[*].[Placement.AvailabilityZone, State.Name, InstanceId, PrivateIpAddress, PublicIpAddress, [Tags[?Key==`Name`].Value] [0][0]]' --output text --region ${REGION}
```

- Define your Ansible server environment variable. if you are using AWS profile other than default, substitue it in the commands below:

```
```bash
export ANSIBLE_SERVER_PUBLIC_IP="$(aws ec2 describe-instances --filters "Name=tag-value,Values=ansible_controller_kubeadm_lab" "Name=instance-state-name,Values=running" --query 'Reservations[*].Instances[*].[PublicIpAddress]' --output text --region ${REGION} --profile ${AWS_PROFILE})"
```

- Transfer your SSH key to the Ansible Server. This will be need in the Ansible Inventory file.

```
```bash
echo "scp -i ${LOCAL_SSH_KEY_FILE} ${LOCAL_SSH_KEY_FILE} ubuntu@${ANSIBLE_SERVER_PUBLIC_IP}:~/.ssh/"
```

- Inspect and execute the output of the command generated above.

- To Create inventory file. Edit the inventory.sh and update the REGION if different from `eu-west-1`.

- View the inventory file and update it according to your AWS environment setup
```

```bash
vi deployments/inventory.sh
```

- Proceed with the commands below:
```

```bash
chmod +x deployments/inventory.sh
bash deployments/inventory.sh ${LOCAL_SSH_KEY_FILE} ${REGION}
chmod +x deployments/config.sh
bash deployments/config.sh ${LOCAL_SSH_KEY_FILE} ${REGION}
```

- Transfer all playbooks in deployments/playbooks to the ansible server

```
```bash
cd deployments
scp -i ${LOCAL_SSH_KEY_FILE} deployment.yml setup.sh ../inventory ../config *.cfg ubuntu@${ANSIBLE_SERVER_PUBLIC_IP}:~
scp -i ${LOCAL_SSH_KEY_FILE} ../config ubuntu@${ANSIBLE_SERVER_PUBLIC_IP}:~/.ssh/config
```

- Connect to the Ansible Server
```

```bash
ssh -i ${LOCAL_SSH_KEY_FILE} ubuntu@${ANSIBLE_SERVER_PUBLIC_IP}
```

- Your ssh key copied to the Ansible server

```
```bash
chmod 400 ~/.ssh/key_name.pem
```



- After building the inventory file, test if all hosts are reachable

1. list all hosts to confirm that the inventory file is properly configured
1. List all hosts to confirm that the inventory file is properly configured

```
```bash
ansible all --list-hosts -i inventory
```

Expected output:

```bash
hosts (5):
controller1
worker1
worker2
```

2. Test ping on all the hosts
2. Test ping on all the hosts

```
```bash
ansible -i inventory k8s -m ping
```

# Deploy with Ansible

```
## Deploy with Ansible

```bash
ansible-playbook -i inventory -v deployment.yml
```

## Final Result

```
```bash
TASK [Ansible Host Kubectl Commands] **************************************************************************************************************************************************
ok: [localhost -> 10.192.10.133] => {
ok: [localhost] => {
"msg": [
"NAME STATUS ROLES AGE VERSION",
"k8s-controller Ready control-plane 2m22s v1.27.5",
"k8s-worker1 Ready <none> 66s v1.27.5",
"k8s-worker2 Ready <none> 62s v1.27.5"
"k8s-controller Ready control-plane 2m28s v1.28.2",
"k8s-worker1 Ready <none> 75s v1.28.2",
"k8s-worker2 Ready <none> 70s v1.28.2"
]
}

Expand All @@ -207,35 +209,38 @@ localhost : ok=8 changed=4 unreachable=0 failed=0 s
```

## Test Kubectl Commands on the Ansible Controller Server
```

```bash
ubuntu@ip-10-192-10-160:~$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-controller Ready control-plane 7m41s v1.27.5
k8s-worker1 Ready <none> 6m25s v1.27.5
k8s-worker2 Ready <none> 6m21s v1.27.5
ubuntu@ip-10-192-10-160:~$
k8s-controller Ready control-plane 4m3s v1.28.2
k8s-worker1 Ready <none> 2m50s v1.28.2
k8s-worker2 Ready <none> 2m45s v1.28.2
```

## Test Kubectl Commands on the Kubernetes Controller
```

```bash
ubuntu@ip-10-192-10-194:~$ ssh k8s-controller

ubuntu@k8s-controller:~$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-controller Ready control-plane 7m41s v1.27.5
k8s-worker1 Ready <none> 6m25s v1.27.5
k8s-worker2 Ready <none> 6m21s v1.27.5
k8s-controller Ready control-plane 4m3s v1.28.2
k8s-worker1 Ready <none> 2m50s v1.28.2
k8s-worker2 Ready <none> 2m45s v1.28.2
```

# Clean Up
*Delete the AWS CloudFormation Stack*
## Clean Up

To Delete the AWS CloudFormation Stack

>`aws cloudformation delete-stack --stack-name kubeadm-lab`
```bash
aws cloudformation delete-stack --stack-name kubeadm-lab
```


*Check if the AWS CloudFormation Stack still exist to confirm deletion*
Check if the AWS CloudFormation Stack still exist to confirm deletion

```
```bash
aws cloudformation list-stacks --stack-status-filter DELETE_COMPLETE --region ${REGION} --query 'StackSummaries[*].{Name:StackName,Date:CreationTime,Status:StackStatus}' --output text | grep kubeadm
```

```
2 changes: 1 addition & 1 deletion deployments/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@

- name: Install kubectl
get_url:
url: https://storage.googleapis.com/kubernetes-release/release/v1.27.5/bin/linux/amd64/kubectl
url: https://storage.googleapis.com/kubernetes-release/release/v1.28.2/bin/linux/amd64/kubectl
dest: /usr/local/bin/
mode: a+x
become_user: root
Expand Down
9 changes: 6 additions & 3 deletions deployments/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

# Install dependency packages:
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
sudo apt-get update && sudo apt-get install -y apt-transport-https ca-certificates curl gpg

# Download and add GPG key:
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
Expand All @@ -58,9 +58,12 @@ EOF
# Update package listings:
sudo apt-get update

#Determine which version to upgrade to
sudo apt-cache madison kubeadm

# Install Kubernetes packages of choice (Note: If you get a dpkg lock message, just wait a minute or two before trying the command again):
# Updtae 1.27.5-00 based on Kubernetes release version
sudo apt-get install -y kubelet=1.27.5-00 kubeadm=1.27.5-00 kubectl=1.27.5-00
# Updtae 1.28.2-00 based on Kubernetes release version
sudo apt-get install -y kubelet=1.28.2-00 kubeadm=1.28.2-00 kubectl=1.28.2-00

# Turn off automatic updates:
sudo apt-mark hold kubelet kubeadm kubectl
Expand Down

0 comments on commit c26c2aa

Please sign in to comment.