Gulp build example

Let's start by creating a project directory named node-gulp:

$ mkdir node-gulp
$ cd node-gulp

Next, we will install the gulp package and check whether it's ready by running the npm command with the version flag, as follows:

$ npm install -g gulp

You may need to open a new terminal window to make sure that gulp is on your path. Also, make sure to navigate back to your node-gulp directory:

 $ gulp -v

Next, we will install gulp locally in our project folder as well as the gulp-git and gulp-shell plugins, as follows:

$ npm install --save-dev gulp
$ npm install gulp-git -save
$ npm install --save-dev gulp-shell

Finally, we need to create a Kubernetes controller and service definition file, as well as a gulpfile.js file, to run all our tasks. Again, these files are available in the book file bundle, if you wish to copy them instead. Refer to the following code:

apiVersion: v1 
kind: ReplicationController
metadata:
name: node-gulp
labels:
name: node-gulp
spec:
replicas: 1
selector:
name: node-gulp
template:
metadata:
labels:
name: node-gulp
spec:
containers:
- name: node-gulp
image: <your username>/node-gulp:latest
imagePullPolicy: Always
ports:
- containerPort: 80

Listing 7-1: node-gulp-controller.yaml

As you can see, we have a basic controller. You will need to replace <your username>/node-gulp:latest with your Docker Hub username:

apiVersion: v1 
kind: Service
metadata:
name: node-gulp
labels:
name: node-gulp
spec:
type: LoadBalancer
ports:
- name: http
protocol: TCP
port: 80
selector:
name: node-gulp

Listing 7-2: node-gulp-service.yaml

Next, we have a simple service that selects the pods from our controller and creates an external load balancer for access, as earlier:

var gulp = require('gulp'); 
var git = require('gulp-git');
var shell = require('gulp-shell');

// Clone a remote repo
gulp.task('clone', function(){
return git.clone('https://github.com/jonbaierCTP/getting-started-with-kubernetes-se.git', function (err) {
if (err) throw err;
});

});

// Update codebase
gulp.task('pull', function(){
return git.pull('origin', 'master', {cwd: './getting-started-with-kubernetes-se'}, function (err) {
if (err) throw err;
});
});

//Build Docker Image
gulp.task('docker-build', shell.task([
'docker build -t <your username>/node-gulp ./getting-started-with-kubernetes-se/docker-image-source/container-info/',
'docker push <your username>/node-gulp'
]));

//Run New Pod
gulp.task('create-kube-pod', shell.task([
'kubectl create -f node-gulp-controller.yaml',
'kubectl create -f node-gulp-service.yaml'
]));

//Update Pod
gulp.task('update-kube-pod', shell.task([
'kubectl delete -f node-gulp-controller.yaml',
'kubectl create -f node-gulp-controller.yaml'
]));

Listing 7-3: gulpfile.js

Finally, we have the gulpfile.js file. This is where all our build tasks are defined. Again, fill in your Docker Hub username in both the <your username>/node-gulp sections.

Looking through the file, first, the clone task downloads our image source code from GitHub. The pull tasks execute a git pull on the cloned repository. Next, the docker-build command builds an image from the container-info subfolder and pushes it to DockerHub. Finally, we have the create-kube-pod and update-kube-pod commands. As you can guess, the create-kube-pod command creates our controller and service for the first time, whereas the update-kube-pod command simply replaces the controller.

Let's go ahead and run these commands and see our end-to-end workflow:

$ gulp clone
$ gulp docker-build

The first time through, you can run the create-kube-pod command, as follows:

$ gulp create-kube-pod

This is all there is to it. If we run a quick kubectl describe command for the node-gulp service, we can get the external IP for our new service. Browse to that IP and you'll see the familiar container-info application running. Note that the host starts with node-gulp, just as we named it in the previously mentioned pod definition:

Service launched by Gulp build

On subsequent updates, run the pull and update-kube-pod commands, as shown here:

$ gulp pull
$ gulp docker-build
$ gulp update-kube-pod

This is a very simple example, but you can begin to see how easy it is to coordinate your build and deployment end to end with a few simple lines of code. Next, we will look at how to use Kubernetes to actually run builds using Jenkins.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.41.27