How to update k8s pods in another namespace using Jenkins - Minikube - jenkins

I have created a cluster using minikube which has 2 namespaces, dev and infra. dev contains my UI and backend apps while infra contains my Jenkins StatefulSet. I set Jenkins and added the Kubernetes plugin (v 1.1.3). Now I want to create a Jenkins job so that I can redeploy services in my dev namespace.
However, when my Jenkins job runs, I can see that it spins a new pod in the infra namespace as expected for the build, but this pod does not have access to the kubeconfig or the kubectl command. How do I promote builds in this case?
Here is my Kubernetes Cloud Configuration
And here is the console output of a sample job
The sample job above does nothing, I was just testing to make sure that it spins off a pod of its own every time it is run.
How can I use these Jenkins jobs now to redeploy my services/pods in the dev namespace?

this pod does not have access to the kubeconfig or the kubectl command
You need to use a jenkins agent docker image that has those commands
You also need that agent pod to use a service account that has permission to access the dev namespace if you want to change things there

Related

How can I throw /run/containerd/containerd.sock into k8s pod?

I am using Digital Ocean Kubernetes cluster as well as Jenkins Helm Chart running inside this cluster. Digital Ocean using containerd://1.4.13 as container runtime and to run containerized tests inside Jenkins Pipeline, I need to download sources from the repository, build a Dockerfile with unit tests and docker-compose.yml with integration tests (in addition to the application there is also a database) and run it all. The problem here is that I can't throw /run/containerd/containerd.sock from the node that runs this pod into the pod itself, because I didn't find any functionality in kubernetes similar to throwing a specific file inside a container in docker. How can I solve this problem by leaving containerized tests? Thank you in advance!

Rolling updation with "kubectl apply" command

Currently I am practicing with Kubernetes resources like replicaSet , deployment and services. When I created my sample deployment and service for testing , I used kube apply command for creating the deployment and service into the Kubernetes cluster.That is properly working.
Now I am trying to make a end-to-end deployment using CI/CD pipeline. So when I am adding the deployment step in the Jenkinsfile , Can I use the kube apply command inside Jenkinsfile. Means If I am continuously committing in my SVN repo , changes will get update by using kube apply command? For each change I am making to repo need to reflect in deployment.
Can I use kube apply command for update also in Kubernetes ? Or Do I need to use rollout for updation of Kubernetes resource?
If I am continuously committing in my SVN repo , changes will get update by using kube apply command?
Ans:Yes
Below is the definition from kube document
Apply is a command that will update a Kubernetes cluster to match
state defined locally in files.
https://kubectl.docs.kubernetes.io/pages/app_management/apply.html

Implementation of CD with Jenkins for on premise kubernetes cluster?

I have my on premise kubernetes cluster.
I want to setup CD with jenkins.
I have tried two plugins which are available for kubernetes deployment.
Kubernetes Continuous Deploy Plugin
Kubernetes plugin
The question here is, jenkins master will require the .kube/config
file to connect to the kubernetes cluster for doing deployments.
Is this the best practice to copy the .kube/config file of your kubernetes master to jenkins master to provide full acesss of your kubernetes cluster for deployment purpose.
Please do let me know if you have any other suggestions for my ON-PREM kubernetes cluster CD.
you have two options
1. put all kubeconfig files in a separate git repo, say, kubeVault .git, and name the files using cluster name. Accept cluster as build paramter in jenkins. Lookup the respective kubeconfig from kubeVault repo and use it as target platform and deploy the container. We have used this approach in one of the project. you need to build some logic in the pipeline groovy code
define build parameters and set them using the fields from kubeconfig file. post build generate actual kubeconfig file from build params and keep it in /tmp directory. pass the kubeconfig location to kubectl to deploy the k8s obejects/pod

How can I allow "go get" or "go install" to pull from github inside a Jenkins Kubernetes pod executor?

I am running Jenkins inside a Kubernetes cluster and setup to spawn an executor pod with 3 containers: jnlp, golang and kubectl.
Using "golang" container, I would like to be able to pull dependencies from github using "go install"
However, I am getting these errors (my code is in /go/src/test-app):
cannot find package "github.com/andreid/test-app/controllers"
cannot find package "github.com/sirupsen/logrus"
and it seems unable to pull from github (though that's where Jenkins is pulling my code from and it's configured to do that using a developer oauth key that I generated).
It seems that the "jnlp" container handles the pulling of code successfully.
What can I do to allow "go install" to work as expected?
Thanks!
For posterity: when running Jenkins in a Kubernetes cluster with all the proper paraphernalia (Kubernetes pod agents, etc), there's no straightforward way to do this. You might expect agents will pick up github credentials setup in Jenkins, but they don't.
A quick way is to create a secret containing a github access token and mounting it as the env var GITHUB_TOKEN in the golang container used in the execution pod. YOu will need to maintain that secret or use multiple secrets if you have pipelines with multiple needs.
Then, you need to run:
git config --global url."https://${GITHUB_TOKEN}:x-oauth-basic#github.com/<myproject>".insteadOf "https://github.com/<myproject>" before doing build/get.

Copying file from host laptop(mac) to dynamically created kubernetes pod via Jenkins pipeline

I installed minikube and started Jenkins inside a pod. I am able to create a new job and execute it inside a dynamically created maven container. However. I have a folder in my host machine which is a Mac based and I need that folder inside this dynamically created pod when the job is started. How can I achieve that?
Option 1.
https://kubernetes.io/docs/setup/minikube/#interacting-with-your-cluster
configure kubectl on your MAC, then use kubectl cp <hostdir> <podname>:<dir>
Option 2.
use the Host path option for POD, like this post
Mount local directory into pod in minikube
Maybe the easiest solution is to push that folder as git repo ( if it is not large ) to either github or your private git server , and then clone it as a first step in your jenkins job.

Resources