Skip to content

Latest commit

 

History

History
 
 

minimal

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Minimal Example for rok8s-scripts

This is an example rok8s-scripts configuration that deploys a NodeJS application to proudction. It is built for CircleCI, but should help give a basic overview of the pieces of a rok8s-scripts deployment regardless of your CI platform.

Configuration

The file ./deploy/production.config is the main entry point for rok8s-scripts. It tells rok8s-scripts how to build the image and what registry to push it to.

CI/CD

The file ./.circleci/config.yml is the main entry point for CircleCI. In it, we run some of the scripts provided by rok8s-scripts. In particular, we use:

  • docker-pull to get the last image we pushed, which warms the local docker cache for faster builds
  • docker-build to build the image for the current comment
  • docker-push to push the image for the current commit to our image repository
  • prepare-kubectl to set up for pushing the latest image to our Kubernetes cluster
  • k8s-deploy-and-verify to push our image to Kubernetes and make sure the deployment succeeded

We also use the rok8s-scripts CI image, quay.io/reactiveops/ci-images:v9-stretch, to ensure rok8s-scripts and its dependencies are all available during the build and deploy jobs.

Try it out

  • Run the following to copy this directory to a new git repository:
git clone https://github.com/FairwindsOps/rok8s-scripts
cp -r rok8s-scripts/examples/minimal rok8s-scripts-test
cd rok8s-scripts-test
git init
git add .
git commit -m "testing rok8s scripts"
  • Create a new repository on GitHub
  • Follow the instructions on GitHub to push your code to your new repo
  • Go to circleci.com and add your repo as a new project
  • Click "Start Building" to kick off your first build

You'll see CircleCI start two jobs - one to build the image, and one to deploy. The build job should succeed, but the deploy job will fail, because we haven't set up credentials for our image repository or Kubernetes cluster.

Setting up the image repository

These instructions are for quay.io, and will have to be adapted if you're using another image registry like AWS ECR or DockerHub.

  • Set up a new repo on quay.io
  • Create a new robot account
  • Click the settings icon next to the robot account, and click "View credentials"
  • In your project settings on CircleCI, go to "Environment Variables"
  • Add an environment variable named quay_robot, and paste in the value from Quay
  • Add an environment variable named quay_token, and paste in the value from Quay on Quay. We'll pass credentials for this account to CircleCI so that it can
  • Edit ./deploy/production.config with your quay username and repository
  • Edit ./deploy/minimal-production.deployment.yml with your quay username and repository

Setting up Kubernetes

Note: Using your personal kubeconfig is NOT recommended. It is much more secure to create a service account. See the docs on Kubernetes auth for more details In order to deploy to Kubernetes, you'll need to base64 encode a valid kubeconfig and set it as an environment variable in CircleCI. We recommend creating a service account for this, but for simplicity the example below uses the kubeconfig stored in your home directory.

  • Run:
cat ~/.kube/config | base64 -w 0 > kube-config-encoded.txt
  • Create a new environment variable in CircleCI called KUBECONFIG_DATA
  • Paste the contents of kube-config-encoded.txt as the environment variable value

Rerun the build

Commit and push your changes to the master branch to trigger another build. Everything should run successfully this time, and you should see your app running inside your Kubernetes cluster.

You can check your app by running

kubectl port-forward --namespace rok8s-example svc/rok8s-example 3000:80

and visiting localhost:3000 in your browser.