Kubernetes series part 1
November 1, 2017 ยท View on GitHub
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.
-
navigate to ks1
โ pwd ~/dev/github/redgate/ks/ks1 -
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
-
start minikube
โ minikube start -
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) -
create apps Dockerfile
The docker image is based on the following docker file File:
./ks1/web/DockerfileFROM node:8.6.0 ADD ./app /app WORKDIR /app EXPOSE 3000 RUN yarn install RUN yarn CMD ["yarn", "start"] -
create the docker image
โ docker build -f ./web/Dockerfile -t kswebimage . -
create kubernetes deployment file
File:
./ks1/config/dev.ks.deployment.yamlapiVersion: 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: {} -
create kubernetes service file
File:
./ks1/config/dev.ks.service.yamlapiVersion: 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: {} -
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 -
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 -
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. -
service app
Get URL and navigate to it.
โ minikube service ksweb --url -
delete app
To delete deployment and service:
โ kubectl delete -f ./config/dev.ks.deployment.yaml โ kubectl delete -f ./config/dev.ks.service.yamlTo delete image
โ docker rmi kswebimageTo switch context
โ eval $(docker-machine env -u)To stop minikube
โ minikube stop