Setup Kubernetes On AWS Using KOPS
“Kubernetes is an open source system for automating deployments, scaling and management of containerised applications.”
I recommend you to go through my blog-post on “How Kubernetes works” to understand the internal architecture of k8s.
https://www.youtube.com/watch?v=Ut7qSWUZJ1M
In this post, I will try to cover up the detail steps to make the kubernetes cluster up and running using the tool KOPS.
I have also created a youtube video to demonstrate the steps followed in this post. Here is the video:
NOTE: Before proceeding, I assume that you have a basic understanding of Kubernetes and AWS.
Prerequisites:
– Ubuntu instance (You may use other linux instance as well)
– AWS-cli setup
– S3 bucket
Install kubectl
On ubuntu instance, make sure you have AWS cli and KOPS setup. We shall also need kubectl (Kubernetes cli)
– Install Kubectl on Linux:
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectlchmod +x ./kubectlsudo mv ./kubectl /usr/local/bin/kubectl
Now, let’s install kops on ubuntu box:
wget https://github.com/kubernetes/kops/releases/download/1.6.1/kops-linux-amd64chmod +x kops-linux-amd64sudo mv kops-linux-amd64 /usr/local/bin/kops
Create Route53 domain for the cluster
kubernetes make use of DNS for discovery within the cluster so that you can reach out kubernetes-API-server from clients.
Create a hosted zone on Route53, say, k8s.appychip.vpc. The API server endpoint will then be api.k8s.appychip.vpc
Create a S3 bucket
Now, create a S3 bucket to store the configuration for the cluster. Make sure the instance have right role to access S3 and Route53:
$ aws s3 mb s3://clusters.k8s.appychip.vpc
Expose environment variable:
$ export KOPS_STATE_STORE=s3://clusters.k8s.appychip.vpc
Create Kubernetes Cluster
Now comes the interesting part to create the cluster. You can reuse existing VPC (kops will create a new subnet in this VPC) by providing the vpc-id option. The following command will give you details what all things are going to happen:
$ kops create cluster --cloud=aws --zones=us-east-1d --name=useast1.k8s.appychip.vpc --dns-zone=appychip.vpc --dns private
NOTE: Make sure you have ssh keys already generated otherwise it will throw an error.
Now to actually create cluster run the following command:
kops update cluster useast1.k8s.appychip.vpc --yes
This will create the VPC, subnets, autoscaling-groups, nodes etc. which you can observe in the output. If you want to review what all things going to happen when this command would be run then run the above command without –yes option. Without –yes option, it will print the action it is going to perform without actually doing it.
You can then edit the cluster settings with one of these commands:
- List clusters with:
kops get cluster
- Edit this cluster with:
kops edit cluster useast1.k8s.appychip.vpc
- Edit your node instance group:
kops edit ig --name=useast1.k8s.appychip.vpc nodes
- Edit your master instance group:
kops edit ig --name=useast1.k8s.appychip.vpc master-us-east-1d
Wait for some time as it takes some time for the instances to boot and the DNS entries to be added in the hosted zone. Once everything is up you should be able to get the kubernetes nodes. Remember we are running all these command from the ubuntu box on which we have setup aws-cli and kubectl:
$ kubectl get nodesNAME STATUS AGE VERSION
ip-172-20-33-144.ec2.internal Ready 4m v1.6.2
ip-172-20-39-78.ec2.internal Ready 1m v1.6.2
ip-172-20-45-174.ec2.internal Ready 2m v1.6.2
Enable the Kubernetes UI by installing the UI service:
$ kubectl create -f https://rawgit.com/kubernetes/dashboard/master/src/deploy/kubernetes-dashboard.yaml
You can use the kubctl proxy to access the UI from your machine:
$ kubectl proxy --port=8080 &
The UI should now be available at http://localhost:8080
Deploying Nginx Container
Let’s deploy a simple service made up of some nginx containers:
Create an nginx deployment:
$ kubectl run sample-nginx --image=nginx --replicas=2 --port=80
$ kubectl get podsNAME READY STATUS RESTARTS AGE
sample-nginx-379829228-xb9y3 1/1 Running 0 10s
sample-nginx-379829228-yhd25 1/1 Running 0 10s$ kubectl get deploymentsNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
sample-nginx 2 2 2 2 29s
Expose the deployment as service. This will create an ELB in front of those 2 containers and allow us to publicly access them:
$ kubectl expose deployment sample-nginx --port=80 --type=LoadBalancer$ kubectl get services -o wideNAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes 100.64.0.1 <none> 443/TCP 25m <none>
sample-nginx 100.70.129.69 adca6650a60e611e7a66612ae64874d4-175711331.us-east-1.elb.amazonaws.com/ 80/TCP 19m run=sample-nginx
There is an ELB running on http://adca6650a60e611e7a66612ae64874d4-175711331.us-east-1.elb.amazonaws.com with our nginx containers behind it:
You can also view the UI by accessing master node. Hit master node’s IP/Domain in browser, it will ask for credentials. Run command kubectl config view to see the credentials.
To delete the cluster and remove all AWS resources with, run the following command:
$ kops delete cluster --name=useast1.k8s.appychip.vpc --yes
This is all about how to setup kubernetes on AWS using KOPS. If you get stuck at any point, please watch the video tutorial.