Task: Deploy a react app in kubernetes
make sure cli tools such as node, docker, kubectl are installed
Steps:
- Use create-react-app tool to generate a new project
create-react-app new-app
cd new-app
- Configure a Dockerfile for the react app
-
use alpine-node as base docker image(Minimal Node.js Docker Images)
1
2
3
4
5
6
7
8
9
10
11
12
13# example
# create a production build
FROM mhart/alpine-node AS build
WORKDIR /app
COPY . .
RUN npm run build
#serve the built app
FROM mhart/alpine-node
RUN npm install -g serve
WORKDIR /app
COPY --from=build /app/build .
CMD [ "serve", "-p 80", "-s", "."] -
build the image:
docker build -t $repo-name .
-
run it with
docker run -p 8080:80 $repo-name
, go tolocalhost:8080
-
- Deploy your app to kubenetes cluster
- push your local image to a remote repo:
$ docker push $DOCKER_ID_USER/repo:tag
to docker hub - create a new deployment:
kubectl run $deployment-name --image=$image-location --port=80
- expose your service through NodePort:
kubectl expose deployment/$deployment-name --type="NodePort" --port 80
- get the node $name:
kubectl get node
- get the service external $port:
kubectl describe services/$service-name
- check the app out in the brower:
$nodename:$port
- push your local image to a remote repo:
References
- Running React app inside a Docker container
- Building and Deploying a Containerised React App and Microservice Using the Same OpenAPI Contract