Skip to content

Lecture #04 - 09/29/2018

Agenda

Slides

Info

To view slides, students must login to Office 365 with their Microsoft Student Advantage account. Office 365 (Microsoft Student Advantage) access details can be found here.

Reading

Lab

Install Tools

Setup Kubernetes Cluster Using kops

Documentation

  1. Setup IAM user
  2. Configure DNS
    • Create subdomain
  3. Cluster State storage. aws s3api create-bucket --bucket $NAME --region us-east-1
  4. Creating your first cluster
    1. Create cluster configuration. kops create cluster --zones us-east-1a --node-size t2.medium --master-size t2.medium $NAME
  5. Customize Cluster Configuration
  6. Build the Cluster

Setup Kubernetes Dashboard

Create Sample User

Create a file called create-service-account.yaml with following content

1
2
3
4
5
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kube-system

Create a file called cluster-role-binding.yaml with following content

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kube-system

Now execute following commands

1
2
kubectl apply -f create-service-account.yaml
kubectl apply -f cluster-role-binding.yaml 

To get the token, running following command:

1
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')

Deploy Dashboard

1
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml

Access Dashboard

1
kubectl proxy

Now access Dashboard at: http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/.

Application

Save following to app.js.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const http = require('http');
const os = require('os');

console.log("Server starting...");

var handler = function(request, response) {
  console.log("Received request from " + request.connection.remoteAddress);
  response.writeHead(200);
  response.end("CSYE 7374: You've hit " + os.hostname() + "\n");
};

var www = http.createServer(handler);
www.listen(8080);

Docker File

In the same dirctory as app.js create a file called Dockerfile with following content

1
2
3
FROM node:7
ADD app.js /app.js
ENTRYPOINT ["node", "app.js"]

AWS Container Registry

Docker Basics for Amazon ECR

1
aws iam attach-group-policy --policy-arn arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess  --group-name kops

Create ECR Registry Using CLI

aws ecr create-repository --repository-name csye7374

Build & Push Docker Container Image to Amazon ECR

Retrieve the login command to use to authenticate your Docker client to your registry

aws ecr get-login --no-include-email --region us-east-1

Build Docker Image

1
docker build -t csye7374 .

Tag Docker Image

1
docker tag csye7374:latest <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/csye7374:latest

Push Docker Image

1
docker push <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/csye7374:latest

Run Application

Create Deployment

Create a file called csye7374-pod.yaml with following content

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
apiVersion: v1               
kind: Pod                    
metadata:
  name: csye7374
  labels:                                                 
    app: csye7374         
spec:
  containers:
  - image: <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/csye7374       
    name: csye7374            
    ports:
    - containerPort: 8080    
      protocol: TCP

Run following command

1
kubectl create -f csye7374-pod.yaml

Make REST calls to your application by creating a proxy

1
2
kubectl port-forward csye7374 8888:8080
curl http://localhost:8888

Create Service

Create a file called csye7374-service.yaml with following content

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
apiVersion: v1
kind: Service
apiVersion: v1
metadata:
  name: csye7374-service
spec:
  selector:
    app: csye7374
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080

Run following command

1
kubectl create -f csye7374-service.yaml

Create Deployment

Create a file called csye7374-deployment.yaml with following content

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: csye7374-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: csye7374
    spec:
      containers:
      - image: <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/csye7374       
        name: csye7374            
        ports:
        - containerPort: 8080    
          protocol: TCP

Run following command

1
kubectl create -f csye7374-deployment.yaml

Create Deployment with Resource Constraints

Create a file called csye7374-deployment.yaml with following content

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: csye7374-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: csye7374
    spec:
      containers:
      - image: <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/csye7374       
        name: csye7374            
        ports:
        - containerPort: 8080    
          protocol: TCP
        resources:
          limits:
            memory: "8Mi"
            cpu: "1m"
          requests:
            memory: "8Mi"
            cpu: "1m"

Run following command

1
kubectl create -f csye7374-deployment.yaml

Delete Kubernetes Cluster

1
kops delete cluster --name $NAME --yes