Skip to content

Latest commit

 

History

History
224 lines (166 loc) · 4.54 KB

ks1.md

File metadata and controls

224 lines (166 loc) · 4.54 KB

Kubernetes series part 1

The objective here is to create a minimal react app and hook it up with kubernetes.

Build a React app

  • we start with a react application similar to the result of create react app as our baseline.
  1. navigate to ks1

    pwd
        ~/dev/github/redgate/ks/ks1
  2. build app

    cd ./app
    ➜ yarn
        yarn install v1.1.0
        [1/4] 🔍  Resolving packages...
        [2/4] 🚚  Fetching packages...
        [3/4] 🔗  Linking dependencies...
        [4/4] 📃  Building fresh packages...
        success Saved lockfile.
        ✨  Done in 2.92s.
    ➜ yarn start
        Compiled successfully!
    
        You can now view app in the browser.
    
        Local:            http://localhost:3000/
        On Your Network:  http://192.168.0.6:3000/
    
        Note that the development build is not optimized.
        To create a production build, use yarn build.

    This should serve your website at http://localhost:3000/

Move app into kubernetes

  1. start minikube

    ➜ minikube start
  2. switch to minikube context

    eval $(minikube docker-env)

    If you ever need to switch back to your machine's context do:

    eval $(docker-machine env -u)
  3. create apps Dockerfile

    The docker image is based on the following docker file File: ./ks1/web/Dockerfile

    FROM node:8.6.0
    
    ADD ./app /app
    
    WORKDIR /app
    
    EXPOSE 3000
    
    RUN yarn install
    RUN yarn
    CMD ["yarn", "start"]
  4. create the docker image

    ➜ docker build -f ./web/Dockerfile -t kswebimage .
  5. create kubernetes deployment file

    File: ./ks1/config/dev.ks.deployment.yaml

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
    creationTimestamp: null
    labels:
        run: ksweb
    name: ksweb
    spec:
    replicas: 1
    selector:
        matchLabels:
        run: ksweb
    strategy: {}
    template:
        metadata:
        creationTimestamp: null
        labels:
            run: ksweb
        spec:
        containers:
        - image: kswebimage:latest
            name: ksweb
            imagePullPolicy: IfNotPresent
            ports:
            - containerPort: 3000
            resources: {}
    status: {}
  6. create kubernetes service file

    File: ./ks1/config/dev.ks.service.yaml

    apiVersion: v1
    kind: Service
    metadata:
    creationTimestamp: null
    labels:
        run: ksweb
    name: ksweb
    spec:
    ports:
    - port: 80
        protocol: TCP
        targetPort: 3000
    selector:
        run: ksweb
    type: LoadBalancer
    status:
    loadBalancer: {}
  7. create deployment and service

    ➜ kubectl create -f ./config/dev.ks.deployment.yaml
    deployment "ksweb" created
    
    ➜ kubectl create -f ./config/dev.ks.service.yaml
    service "ksweb" created
  8. check cluster status

    ➜ kubectl get all
    NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    deploy/ksweb   1         1         1            1           2m
    
    NAME                  DESIRED   CURRENT   READY     AGE
    rs/ksweb-1832552125   1         1         1         2m
    
    NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    deploy/ksweb   1         1         1            1           2m
    
    NAME                        READY     STATUS    RESTARTS   AGE
    po/ksweb-1832552125-6p0z3   1/1       Running   0          2m
  9. check web logs

    ➜ kubectl logs ksweb-1832552125-6p0z3
    yarn run v1.1.0
    $ react-scripts start
    Starting the development server...
    
    Compiled successfully!
    
    You can now view app in the browser.
    
    Local:            http://localhost:3000/
    On Your Network:  http://172.17.0.2:3000/
    
    Note that the development build is not optimized.
    To create a production build, use yarn build.
  10. service app

    Get URL and navigate to it.

    ➜ minikube service ksweb --url
  11. delete app

    To delete deployment and service:

    ➜ kubectl delete -f ./config/dev.ks.deployment.yaml
    ➜ kubectl delete -f ./config/dev.ks.service.yaml

    To delete image

    ➜ docker rmi kswebimage

    To switch context

    eval $(docker-machine env -u)

    To stop minikube

    ➜ minikube stop