<p>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. </p>

<p><strong>My Requirement</strong></p>

<p>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.</p>

<p><strong>My Confusion</strong></p>

<ol>
<li>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.</li>
<li>Can I directly add kubectl command within Jenkins pipeline schedule job? How can I use kubectl command for Kubernetes deployment?</li>
</ol>

<p>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 <code>$HOME/.kube/config</code> file.</p>

<p>So in your job you can just use kubectl like you do from your host.</p>

<p>Regarding the images from Docker Hub:</p>

<p>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.</p>

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

<pre><code>    spec:
      containers:
      - name: nginx
      Image: nginx:1.7.9
</code></pre>

<p>So ensure Jenkins can do the following things from command line:</p>

<ol>
<li>build Docker images</li>
<li>Push Docker Images  (make sure you called docker login on Jenkins)</li>
<li>Access your cluster via <code>kubectl get pods</code> </li>
</ol>

<p>So an easy pipeline needs to simply do this steps:</p>

<ol>
<li>trigger on SVN change</li>
<li>checkout code</li>
<li>create a unique version which could be Build number, SVN Revision, Date)</li>
<li>Build / Test</li>
<li>Build Docker Image</li>
<li>tag Docker Image with unique version</li>
<li>push Docker Image </li>
<li>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)</li>
<li>call <code>kubectl apply -f deployment.yaml</code></li>
</ol>

<p>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 <a href="https://maven.apache.org/maven-ci-friendly.html" rel="nofollow noreferrer">Maven CI Friendly Versions</a> with any maven docker plugin or <a href="https://github.com/GoogleContainerTools/jib" rel="nofollow noreferrer">jib</a>.</p>
