The objective here is to create a minimal react app and hook it up with kubernetes.
- 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/
-
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/Dockerfile
FROM 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.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: {}
-
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: {}
-
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.yaml
To delete image
➜ docker rmi kswebimage
To switch context
➜ eval $(docker-machine env -u)
To stop minikube
➜ minikube stop