Setting up a Kubernetes cluster with kops


In order to start you will need the following:

  • AWS Accout with IAM user that has Admin privleges
  • Route53 Zone (e.g.
  • S3 Bucket (e.g. s3://my-terraform-state)
  • SSH Keypair (e.g.
  • Avialable Subnet CIDR (e.g.
  • Linux client with the following installed on it:
    • awscli
    • terraform
    • kops
    • kubectl

Setting up the Cluster with kops

Run kops to genrate the terraform code needed to setup the cluster

kops create cluster --state=s3://my-terraform-state --out=. --target=terraform --zones="us-east-1a,us-east-1b,us-east-1c" --ssh-public-key="../" --topology private --networking calico

Expected output:

kops has set your kubectl context to

Terraform output has been placed into .
Run these commands to apply the configuration:
   cd .
   terraform plan
   terraform apply

Run the following command:

terraform apply

To add a bastion servers do:

kops create instancegroup bastions --role Bastion --subnet utility-us-east-1c --name --state=s3://my-terraform-state
kops update cluster --state=s3://my-terraform-state --yes

To figure out the name of the AWS ELB in front on the bustion docker:

aws elb --region=us-east-2 --output=table describe-load-balancers|grep DNSName.\*bastion|awk '{print $4}'

To access the bastion server:

ssh -A admin@`aws elb --region=us-east-2 --output=table describe-load-balancers|grep DNSName.\*bastion|awk '{print $4}'`

Setting up a Kubernetes Service and Deployment

Lets say you want to setup a Web site service running nginx

First create a namespace so in future you can run multiple environments on the same cluster

Create a file namespace.yaml:

apiVersion: v1
kind: Namespace
  name: mysite-dev

Run the following command:

kubectl apply -f ./namespace.yaml`

Now lets create a Kubernetes Service - i.e. an external Load Balancer in front of the dockers running nginx that will be setup momentarily

Create a file: service.yaml

apiVersion: v1
kind: Service
  name: mywebsite
  namespace: mysite-dev
    app: mywebsite
  - protocol: TCP
    port: 80
  type: LoadBalancer


kubectl -n mysite-dev apply -f ./service.yaml

Check your AWS account for a new Loadbalancer that two instances attached - currently un-healthy

Create a deployment.yaml file:

apiVersion: apps/v1beta1
kind: Deployment
  name: mywebsite
  namespace: mysite-dev
  replicas: 2
        app: mywebsite
        - name: nginx
          image: nginx:1.7.9
        - containerPort: 80


kubectl -n mysite-dev apply -f deployment.yaml


  1. Check the nginx pods have come up by running this command:
kubectl -n mysite-dev get pods

At first the pods will appear in status ContainerCreating and eventually Running


NAME                       READY     STATUS    RESTARTS   AGE
mywebsite-5fbc6664-b26nf   1/1       Running   0          4m
mywebsite-5fbc6664-n25kl   1/1       Running   0          4m
  1. Check the AWS Service Load Blanacer has to Instances InService

  2. Open the External URL of the ELB in port 80 - you should get the default nginx page