Git Product home page Git Product logo

k8s-aws-with-terraform's Introduction

Kubernetes cluster on AWS

Description

This is a simple project that can help you install and configure a Kubernetes cluster on AWS Cloud using Cloudformation and Terraform.

Creating resources on AWS

Cloudformation

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

Terraform

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

Connect to your instances

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>

Creating the Kubernetes cluster

Installing components

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

Starting the cluster

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')"

Adding new nodes

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

Deploying my first pod

kubectl run --image=nginx --port 80
kubectl expose deployment nginx

kubectl expose deployment nginx --type=NodePort

tips

$ source <(kubectl completion bash)

k8s-aws-with-terraform's People

Contributors

guilhermehardt avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

smolino

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.