Skip to content

CloudNativeWales/CardiffTechTalks042019

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cardiff Tech Talks: Use Kubernetes to deploy and scale applications

Join the chat at https://gitter.im/cloudnativewales/CardiffTechTalks042019

Description

More information for the meetup can be found here.

Introduction

Setup

We're going to offer 3 different ways to run a cluster:

You're more than welcome to use another service if you already know how to.

Steps

Create a Pod and a Service

  • Save the following to apple.yaml:
---
kind: Pod
apiVersion: v1
metadata:
  name: apple-app
  labels:
    app: apple
spec:
  containers:
    - name: apple-app
      image: denhamparry/apple:1.0.0
---
kind: Service
apiVersion: v1
metadata:
  name: apple-service
spec:
  selector:
    app: apple
  ports:
    - port: 3000
  • Send the file to Kubernetes:
$ kubectl apply -f apple.yaml
pod "apple-app" created
service "apple-service" created

Create another Pod and Service

  • Save the following to banana.yaml:
---
kind: Pod
apiVersion: v1
metadata:
  name: banana-app
  labels:
    app: banana
spec:
  containers:
    - name: banana-app
      image: denhamparry/banana:1.0.0
---
kind: Service
apiVersion: v1
metadata:
  name: banana-service
spec:
  selector:
    app: banana
  ports:
    - port: 3000
  • Send the file to Kubernetes:
$ kubectl apply -f banana.yaml
pod "banana-app" created
service "banana-service" created

Create an Ingress

  • Save the following to ingress.yaml:
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: app
  annotations:
    kubernetes.io/ingress.class: nginx
    ingress.kubernetes.io/rewrite-target: '/'
spec:
  rules:
  - host: demo.local
    http:
      paths:
      - path: /apple
        backend:
          serviceName: apple-service
          servicePort: 3000
      - path: /banana
        backend:
          serviceName: banana-service
          servicePort: 3000
      - path: /
        backend:
          serviceName: apple-service
          servicePort: 3000
  • Send the file to Kubernetes:
$ kubectl apply -f ingress.yaml
ingress.extensions "app" created

Test your cluster

  • Using curl:
$ curl demo.local
<html style="background-color:#8db600 ">
  <head>
    <title>v1.0.0</title>
  </head>
  <body style="display:flex;align-items:center;justify-content:center;color:#FFFFFF;font-family:sans-serif;font-size:6rem;margin:0;letter-spacing:-0.1em">
    <h1>Apple</h1>
  </body>
</html>
$ curl demo.local/apple
<html style="background-color:#8db600 ">
  <head>
    <title>v1.0.0</title>
  </head>
  <body style="display:flex;align-items:center;justify-content:center;color:#FFFFFF;font-family:sans-serif;font-size:6rem;margin:0;letter-spacing:-0.1em">
    <h1>Apple</h1>
  </body>
</html>
$ curl demo.local/banana
<html style="background-color:#ffe135 ">
  <head>
    <title>v1.0.0</title>
  </head>
  <body style="display:flex;align-items:center;justify-content:center;color:#000000;font-family:sans-serif;font-size:6rem;margin:0;letter-spacing:-0.1em">
    <h1>Banana</h1>
  </body>
</html>

Delete your pods

$ kubectl delete pod apple-app banana-app
pod "apple-app" deleted
pod "banana-app" deleted

Create your deployments

  • Save the following to apple-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: apple-deployment
spec:
  replicas: 5
  selector:
    matchLabels:
      app: apple
  template:
    metadata:
      labels:
        app: apple
    spec:
      containers:
      - name: apple
        image: denhamparry/apple:1.0.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3000
  • Send the file to Kubernetes:
$ kubectl apply -f apple-deployment.yaml
deployment.apps "apple-deployment" configured
  • Save the following to banana-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: banana-deployment
spec:
  replicas: 5
  selector:
    matchLabels:
      app: banana
  template:
    metadata:
      labels:
        app: banana
    spec:
      containers:
      - name: banana
        image: denhamparry/banana:1.0.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3000
  • Send the file to Kubernetes:
$ kubectl apply -f banana-deployment.yaml
deployment.apps "banana-deployment" configured

How do we scale deployments

  • Change the number of replicas in your deployment files:
...
  replicas: 5
...
$ kubectl apply -f apple-deployment.yaml -f banana-deployment.yaml
deployment.apps "apple-deployment" configured
deployment.apps "banana-deployment" configured

Exercises

When you're ready, try out these exercises here

Clean up