Deploying the latest container image to Kubernetes

After each build, Jenkins keeps pushing your container image on your Docker hub repository at the end of the CI process. Next, update the Jenkins job configuration to use the latest image to deploy to Kubernetes, via the following steps:

  1. The first time, we pre-deploy microservice application manually via kubectl deploy --record. Note that you may change spec.template.spec.containers.image: hidetosaito/my-calc to your repository:
$ cat my-calc.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-calc-deploy
spec:
replicas: 2
selector:
matchLabels:
run: my-calc
template:
metadata:
labels:
run: my-calc
spec:
containers:
- name: my-calc
image: hidetosaito/my-calc


//use --record to trace the history
$ kubectl create -f my-calc-deploy.yaml --record
deployment.apps "my-calc-deploy" created
  1. Open Jenkins Job configuration; on the Build tab, right after the Docker build settings, click Add build step and choose Execute shell:
Adding a build step
  1. Add this shell script and click Save:
#!/bin/sh

set +x

# These 2 are defined in Deployment YAML
DEPLOYMENT_NAME=my-calc-deploy
CONTAINER_NAME=my-calc

# change to your Docker Hub repository
REPOSITORY=hidetosaito/my-calc


echo "*********************"
echo "*** before deploy ***"
echo "*********************"
kubectl rollout history deployment $DEPLOYMENT_NAME
kubectl set image deployment $DEPLOYMENT_NAME $CONTAINER_NAME=$REPOSITORY:$BUILD_NUMBER


echo "******************************************"
echo "*** waiting to complete rolling update ***"
echo "******************************************"
kubectl rollout status --watch=true deployment $DEPLOYMENT_NAME


echo "********************"
echo "*** after deploy ***"
echo "********************"
kubectl rollout history deployment $DEPLOYMENT_NAME

  1. Trigger a new build; you can see that after Docker push, it runs the preceding script:
Kubernetes rollout result

Now you can extend continuous integration to continuous delivery! You may extend to add a unit test or integration test and roll back mechanisms onto the above script to make your CI/CD work stronger.

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

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