visit
Congratulations to all who made it this far in the ALCwithGoogle
program.
We were recently assigned tasks in each track. I am in the
cloud track and I would be taking you through my workflow for the task assigned to the Cloud Track deploying a
react application to Google Kubernetes Engine(GKE).
VM Specifications are:
name -> development-env
type -> n1-standard
OS -> Ubuntu 16:04
allow http traffic
Next Open port 3000 on the VM instance so we can preview the application while in development environment. To achieve this we need to create a firewall rule with a network tag, then edit the development-env VM instance to have this network tag.
name -> allow-traffic-port-3000
specified-target-tags -> nodejs-dev
Source IP Ranges -> 0.0.0.0/0
tcp port 3000
After this Edit the Virtual Machine to include the network tag nodejs-dev
From the SSH terminal, we will then Install NodeJs and Docker on our development environment.
run the following commands to install NodeJs
sudo apt-get update
curl -sL //deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install nodejs
sudo apt-get install npm
run the following commands to install Docker
curl -fsSL //download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
sudo add-apt-repository "deb [arch=amd64] //download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce
run the following commands to create the directory
cd /opt
sudo mkdir dev
cd dev
sudo chown -R [your-username] .
run the following command to install create-react-app
sudo npm install -g create-react-app
next we use create-react-app to create a react application
create-react-app test-andela-challenge
This will create a new folder called test-andela-challenge, this folder contains our react application, next we go into the directory and run the react application.
cd test-andela-challenge
npm start
you can now view the application from the public IP address of our development environment on port 3000, //[public-ip]:3000
Optional
If you want to make changes to the application, you can. use
Ctrl + C
to stop the application from the terminal. go into the src directory and edit App.js and App.css files. from within nano you use Ctrl + O
to save and Ctrl + X
to exit.cd src
nano App.js // nano App.css
First we build the React Application
npm run build
Next we create a Dockerfile
touch Dockerfile
Next we edit the Dockerfile and add the following content
FROM nginx:alpine
COPY /build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
The content of the file, means we are building from an existing image "nginx:alpine" Docker will pull this image from the Container Registry. it will then copy the content of the "build" directory to the html folder on our container, then expose port 80, and start the nginx service
Now we have defined our container as a Dockerfile. we can now build the container image and supply a name for it. I used "test-andela-challenge-docker" as the name for my image.
sudo docker build . -t test-andela-challenge-docker
sudo docker images
First we install Google Cloud SDK on our development environment
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] //packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
curl //packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add –
sudo apt-get update && sudo apt-get install google-cloud-sdk
you may or may not need to run these commands
cd ~
sudo chown -R [username] .config
gcloud init
after you supply the verification code, you will be authenticated to your google cloud account. follow the prompts and specify your project ID and google cloud region. I have been using eu-west-1b.
Next we use gcloud to configure dockergcloud auth configure-docker
now we tag the application using the google cloud format
sudo docker tag [image-name] [gcloud-host]/[project-id]/[image-tag:version]
this is what my own command looks like:sudo docker tag test-andela-challenge-docker eu.gcr.io/qwiklabs-gcp-00-ee40f571c6fa/react-docker:v1
sudo docker push eu.gcr.io/qwiklabs-gcp-00-ee40f571c6fa/react-docker:v1