Monday, 19 March 2018

Jenkins on Kubernetes (Deployment Configuration)


Deploy Jenkins Master on kubernetes 
using deployment configuration


Jenkins Master Installation
As soon as you have the Kubernetes cluster in place you can deploy jenkins 
on top of the cluster. In order to deploy any application inside Kubernetes, 
you need to create a deployment configuration, which will be used as the 
main recipe to 'cook' your application inside the cluster.
Kubernetes deployment is presented in a YAML format text file with all the
configuration params that might be needed to run your application. Let's have a
look at the basic deployment configuration that is required to run
Jenkins inside Kubernetes:

Define simple Jenkins deployment
##############################################
[ec2-user@ip-172-31-26-230 ~]$ vim jenkins-deployment.yml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: jenkins
spec:
 replicas: 1
 template:
   metadata:
     labels:
       app: jenkins
   spec:
     containers:
     - name: jenkins
       image: jenkins:2.32.2
       ports:
       - containerPort: 8080



To create the deployment execute:
#################################
[ec2-user@ip-172-31-26-230 ~]$ kubectl create -f jenkins-deployment.yml
deployment "jenkins" created

To validate that creating the deployment was successful
############################################
[ec2-user@ip-172-31-26-230 ~]$ kubectl get deployments
NAME      DESIRED CURRENT   UP-TO-DATE AVAILABLE   AGE
jenkins   1 1         1 0     18s

[ec2-user@ip-172-31-26-230 ~]$ kubectl get pods
NAME                       READY STATUS   RESTARTS AGE
jenkins-6b9f5d55d9-nd5jl   0/1 ContainerCreating   0 28s

To make Jenkins accessible outside the Kubernetes cluster the Pod needs to 
be exposed as a Service. With a local deployment this means creating a 
NodePortservice type. A NodePort service type exposes a service on a port on 
each node in the cluster. It’s then possible to access the service given the Node 
IP address and the service nodePort.

To define simple node port
##########################################
[ec2-user@ip-172-31-26-230 ~]$ vim jenkins-service.yml

apiVersion: v1
kind: Service
metadata:
name: jenkins
spec:
type: NodePort
ports:
- name: jenkins
port: 8080
targetPort: 8080
- name: agent
port: 50000
targetPort: 50000

selector:
app: jenkins

To create the service execute:
##########################################
[ec2-user@ip-172-31-26-230 ~]$ kubectl create -f jenkins-service.yml
service "jenkins" created

To validate that creating the service was successful
###############################################
[ec2-user@ip-172-31-26-230 ~]$ kubectl get services
NAME         TYPE CLUSTER-IP       EXTERNAL-IP PORT(S)  AGE
jenkins      NodePort 10.110.109.200   <none> 8080:32705/TCP  15s
kubernetes   ClusterIP 10.96.0.1        <none> 443/TCP  4h

See the running pods
###################################
[ec2-user@ip-172-31-26-230 ~]$ kubectl get pods
NAME                       READY STATUS RESTARTS   AGE
jenkins-6b9f5d55d9-nd5jl   1/1 Running 0    1m

To access jenkins dashboard
###################################
Open your browser and type the node hostname with port no as:


Jenkins will ask for initial Admin password. You can get that from the pod 
logs either from kubernetes dashboard or  CLI.

Get the pod details using the following CLI command
###############################################
[ec2-user@ip-172-31-26-230 ~]$ kubectl logs jenkins-6b9f5d55d9-nd5jl 
--namespace=default

Copy and paste the password as seen below:
#########################################

INFO:
*************************************************************
*************************************************************
*************************************************************
Jenkins initial setup is required. An admin user has been created and a 
password generated.
Please use the following password to proceed to installation:
12f555e968b3472fa354b89b3ed87632
This may also be found at: /var/jenkins_home/secrets/initialAdminPassword
*************************************************************
*************************************************************

No comments:

Post a Comment

Amazon EKS - Kubernetes on AWS

By Komal Devgaonkar Amazon Elastic Container Service for Kubernetes (Amazon EKS), which is highly available and scalable AWS service....