Below steps are used in Jenkins to connect with Kubernetes 1. Goto Jenkins portal and Select Manage Jenkins---> Manage Plugins--> Kube...
Below steps are used in Jenkins to connect with Kubernetes
1. Goto Jenkins portal and Select Manage Jenkins---> Manage Plugins--> Kubernetes CLI Plugin and install it with restart jenkins.
2. Create a config file on your system which has the content same as KubeConfig file ( Kubernetes cluster /root/.kube/config)
3. Manage Jenkins --->Credentials --> jenkins-->GlobalCredentials-->Add Credential
Kind:- secret file
file:- Select the file which is created on Step2
id: config
Click the Ok button to create the credential.
4. Create New Item let's name it a Kubernetes job and the Project type is Pipeline
open config file of Kubernetes cluster side by side for reference
node {
stage('List pods') {
withKubeConfig([credentialsId: 'config',
caCertificate: 'LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM1ekNDQWMrZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJeU1EVXdOakExTVRJMU5Wb1hEVE15TURVd016QTFNVEkxTlZvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBS1YwCmdabjd1OUc5d0wrc2s5endsc0NEVVViMWsvSUZ5OTNFS1ZHV0R6eVFsYlN3VlpueHhGY09nQjJvQkxtUXFZM2EKckxqdTgva3o3S0crMTh6Q2NOTWxrWUl2RExvVFRxN3pubjd5RXRQakFpQ1N0YnhrempCYUV1ZVlXcFFGL0piOAplNjdoNjkzbXNOaThpMDZPQmVDMHh4TVJleEZkZ0ttL2pOa3Z6M0dUT1o5Yk1LdFV3SmQ1RUV3QVpDQm1UTGtmCkI2VWJTMisvMkdIV2FDOTlTaGVRZngvNWpVT2VKMEQ5WDdkZTRPWm1UOUs1U2VkOTlmd0N0SzlUN1J5L0NKdEgKMllURkV5VitmRm1VOUJFeVBJdTNobVdHeGk0WnZmZ1RVS0hzTmN4dHRvZEgxWEFWNlhRRXpQZjFrVGQ2dkFhTwpOUUFoTGtNMjlqUkdJQ0NNaVBNQ0F3RUFBYU5DTUVBd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0hRWURWUjBPQkJZRUZHTVVWa3BZR0svRzdwYTd4b2Z6RjAwWUQyUVpNQTBHQ1NxR1NJYjMKRFFFQkN3VUFBNElCQVFCU1VuTmtwemxZVUxCZ2V2MHRjYnFQOTdhNG5raG5ST2pLR2sxalpXakY2cnF1RHk1RgptNTZpZkd2bkMyQ0poWWVMc2dkUGJuQkVkV0l6cGprc09tYmhuUUdaR2xPUGxic2NDb0V5Z3pYaytmM0xpeHN1CnZUWlQwMGVtNHFYdzJveU5vV200bEZ1ekdmTExubzEzSU11VzlFMk5yNWZaNTQwYWJBcmxWdXFxZ1R4eERKazkKMU1Nb0x0cWM1QjJYSmVpWmpBSjZsNzdkS3dwbTJnaGdnbm42a3hTcFJNZG0vZkh4c2pGNXR2L0tOYW1RQkthUwpVWlRnOUFFT3lhUjhuQjJxZ05qdEV6SGpWdktsQWVHSlZFL2k4Umo1azdkd21yTXAyK1Y2ckVCbTVUWnFRTUF6Cm5HQ0R1ZWFIS1ladFlJQnorYytzakU1Yi94alBoMFFLcW5GWgotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==',
serverUrl: 'https://172.31.51.153:6443',
contextName: 'kubernetes-admin@kubernetes',
clusterName: 'kubernetes',
namespace: ''
]) {
sh 'kubectl get pods'
sh 'kubectl run pod13 --image nginx'
}
}
}
5. Build the job and job should run successfully,
COMMENTS