Kubernetes with KOPS in AWS
Introduction:
There are multiple ways to install kubernetes some of the ways are:
- kubernetes the hard way https://github.com/kelseyhightower/kubernetes-the-hard-way
- KOPS
- EKS/GKE/AKS cloud hosted service
- EKSCTL https://eksctl.io
Installing K8 with KOPS:
We are going to explore how to create K8 cluster using KOPS way in AWS for this blog. Please note this configuration is not for Production, and should only be used for dev K8 clusters. In future blog we will look at Production grade K8 setup with KOPS with 3 Masters in 3 AZ and minimum of 3 nodes.
- Create a bastion host in AWS. Choose any small image like amazon linux t2.micro
- Install AWS cli and setup the profile
pip install awscli --upgrade --usercreate two files in ~/.aws1) config [default]
region=us-east-22) credentials[default]
aws_access_key_id = xxxxxxxxxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3. Install pip
centos/amazon linux: yum install python-pip
4. Install kubectl
centos/amazon linux:mkdir ~/tmpcurl -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
Debian:sudo apt-get update && sudo apt-get install -y apt-transport-httpscurl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.listsudo apt-get updatesudo apt-get install -y kubectl
5. download KOPS
wget https://github.com/kubernetes/kops/releases/download/1.10.0/kops-linux-amd64chmod +x kops-linux-amd64mv kops-linux-amd64 /usr/local/bin/kops
5. Create a Route53 sub domain
In Route53
Create Hosted Zone
Domain Name: ue2.yourcompany.com
Type: Public Hosted ZoneTake a note of name serversIn Parent Domain
yourcompany.com Create Record Set
Name: ue2.yourcompany.com
Type: NSThis will create new sub domain ue2.yourcompany.com
6. Create S3 bucket to store cluster state for KOPS
aws s3 mb s3://kubeclusters-dev.ue2.yourcompany.comexport KOPS_STATE_STORE=s3://clusters.ue2.yourcompany.com and set it in .profile
7. create ssh key pair for the bastion to connect to master and nodes
ssh-keygen -t rsa -C "your_email@yourcompany.com"
8. Create the cluster files and create cluster (for Prod setup skip just this step and follow this blog Production setup for Kubernetes with KOPS in AWS for production HA cluster and then continue with step 9)
kops create cluster --zones=us-east-2a kube-dev.ue2.yourcompany.comkops update cluster kube-dev.ue2.yourcompany.com --yes
9. Create kubernetes dashboard
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
10. Get admin password to login to kubernetes dashboard
kops get secrets kube --type secret -oplaintext#xxxxxxxxxxxxxxxxxxxx
11. Get dashboard URL
kubectl cluster-info#https://api.kube-dev.ue2.yourcompany.com/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#!/loginAccess the URL and enter
username: admin
password: from step 10 above
12. On the dashboard select token and enter the following token
kops get secrets admin --type secret -oplaintext
Validate cluster:
kops validate clusterkubectl get nodes
Test with a sample deployment:
- Create Sample deployment
kubectl create deployment helloworld --image=gcr.io/google-samples/node-hello:1.0kubectl expose deployment helloworld --type=LoadBalancer --name=helloworld --port 8080
2. Get the External IP/Loadbalancer DNS name:
kubectl get services helloworld
3. Create CNAME for ELB DNS name/ External IP:
In Sub Domain
ue2.yourcompany.com Create Record Set
Name: helloworld.ue2.yourcompany.com
Type: CNAME
Value: xxxxx-xxxx.us-east-2.elb.amazonaws.com (ELB name)
4. Access the application
http://helloworld.ue2.yourcompany.com:8080