This is a simple project that can help you install and configure a Kubernetes cluster on AWS Cloud using Cloudformation and Terraform.
Log in to the AWS Management Console, select Cloudformation in the Services Menu and create a new stack. On Specify template page, choose the file iam-terraform-user. After create the stack, go to Outputs tab and copy the values: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
export AWS_ACCESS_KEY_ID="XXXXXXXXXXXXXXXXX"
export AWS_SECRET_ACCESS_KEY="XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
export AWS_DEFAULT_REGION="us-west-2"
Generating a new SSH key to connect on EC2 instance
ssh-keygen -t rsa -f "k8s-aws-tf-key" -C ""
chmod 400 k8s-aws-tf-key k8s-aws-tf-key.pub
Exporting the Terraform's variables with SSH public key
export TF_VAR_ssh_public_key=$(cat k8s-aws-tf-key.pub)
export TF_VAR_ssh_key_name=kp-k8s-aws-tf
Check the Terraform variables file and change the values. Now we need initialize the Terraform on your local machine
terraform init
Creating the resources needed on AWS with Terraform
# Check all resources that will be created
terraform plan
# If it's ok, create the resources
terraform apply -auto-approve
Add the private key into the SSH authentication
$ ssh-add k8s-aws-tf-key
Connect on your instances
$ ssh -A ubuntu@<Copy the instances public ips from Terraform output>
Run the bellow commands in ALL master and worker nodes
# Install Docker
$ curl -fsSL https://get.docker.com | bash
# Install Kubernetes
$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
$ echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list
$ apt update
$ apt install kubelet kubeadm kubectl -y
# Change the cgroup drive to systemd
$ cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
$ mkdir -p /etc/systemd/system/docker.service.d
# Restart docker.
$ systemctl daemon-reload
$ systemctl restart docker
Run the bellow commands in master nodes ONLY. First we need make download of kubernetes components (for example: kube-apiserver, kube-scheduler, etcd, coredns, etc)
$ kubeadm config images pull
If you are using instances with 1 CPU, like the free tier instance (t2.micro), you should init the cluster with the flag --ignore-preflight-errors
# instances with 1 CPU
$ kubeadm init --ignore-preflight-errors=NumCPU
# instances with more than 1 CPU
$ kubeadm init
Configure the kubeconfig file that kubectl looks to find the information about the cluster
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
Installing pod network that is required to pod communication cross-nodes. In this case we will install the Weave.
$ kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
On Master node run the bellow command to print the full 'kubeadm join' flag needed to join the cluster
$ kubeadm token create --print-join-command
Copy the output ('kubeadm join ...' command) e run it into yours Worker nodes. Now you can check if the new node already join the cluster:
$ kubectl get nodes
kubectl run --image=nginx --port 80
kubectl expose deployment nginx
kubectl expose deployment nginx --type=NodePort
- To know more see the Kubernetes official docs
- Check the Configuration best practices
- Enable the kubectl completion
$ source <(kubectl completion bash)