Usage of Kubectl command and deployment of pods using Kubernetes and Jenkins

8/1/2018

I am trying to implement CI/CD pipeline for my spring boot microservice deployment. Here I have some sample microservices. When I am exploring about Kubernetes , I found that pods, services, replica sets/ controller, statefulsets etc. I understood those Kubernetes terminologies properly. And I am planning to use Docker hub for my image registry.

My Requirement

When there is a commit made to my SVN code repository, then the Jenkins need to pull code from Subversion repository and need to build the project , create docker image, push into Docker hub - as mentioned earlier. And after that need to deploy into my test environment from Dockerhub by pulling by Jenkins.

My Confusion

  1. When am I creating services and pods, how I can define the docker image path within pod/services / statefulsets? Since it pulling from Docker hub for deployment.
  2. Can I directly add kubectl command within Jenkins pipeline schedule job? How can I use kubectl command for Kubernetes deployment?
-- Jacob
jenkins
kubernetes

2 Answers

8/1/2018

Jenkins can do anything you can do given that the tools are installed and accessible. So an easy solution is to install docker and kubectl on Jenkins and provide him with the correct kube config so he can access the cluster. So if your host can use kubectl you can have a look at the $HOME/.kube/config file.

So in your job you can just use kubectl like you do from your host.

Regarding the images from Docker Hub:

Docker Hub is the default Docker Registry for Docker anyway so normally there is no need to change anything in your cluster only if you want to use your own Private Hosted Registry. If you are running your cluster at any cloud provider I would use there Docker registries because they are better integrated.

So this part of a deployment will pull nginx from Docker Hub no need to specify anything special for it:

    spec:
      containers:
      - name: nginx
      Image: nginx:1.7.9

So ensure Jenkins can do the following things from command line:

  1. build Docker images
  2. Push Docker Images (make sure you called docker login on Jenkins)
  3. Access your cluster via kubectl get pods

So an easy pipeline needs to simply do this steps:

  1. trigger on SVN change
  2. checkout code
  3. create a unique version which could be Build number, SVN Revision, Date)
  4. Build / Test
  5. Build Docker Image
  6. tag Docker Image with unique version
  7. push Docker Image
  8. change image line in Kubernetes deployment.yaml to newly build version (if your are using Jenkins Pipeline you can use readYaml, writeYaml to achive this)
  9. call kubectl apply -f deployment.yaml

Depending on your build system and languages used there are some useful tools which can help building and pushing the Docker Image and ensuring a unique tag. For example for Java and Maven you can use Maven CI Friendly Versions with any maven docker plugin or jib.

-- mszalbach
Source: StackOverflow

8/1/2018

To create deployment you need to create yaml file. In the yaml file you the row: image: oronboni/serviceb

Leads you to the container that in this case in DockerHub.

apiVersion: extensions/v1beta1 kind: Deployment metadata: name: serviceb namespace: default spec: replicas: 1 selector: matchLabels: app: serviceb template: metadata: labels: app: serviceb spec: containers: - name: serviceb image: oronboni/serviceb ports: - containerPort: 5002

I strongly suggest that you will see the kubernetes deployment webinar in the link below: https://m.youtube.com/watch?v=_vHTaIJm9uY

Good luck.

-- Oron Golan
Source: StackOverflow