How to connect Jenkins with Google Cloud Shell particularly for GKE

8/8/2019

i am working on integrating jenkins on Google Kubernetes. Want a pipeline for my project which will perform following steps:

  • Build JAR from maven
  • Build docker image and push to google registry
  • kubectl apply -f commands or Helm commands to run for redeploys,upgrade, downgrade etc

I am familiar with above commands individually but i am new to this type of pipeline where i can run them but everytime jenkins try to teach me that you are in a container, not on kubernetes lol

I have a google cloud shell and i cannot directly install on that machine. So i have to somehow find a way to integrate jenkins to pass those commands directly to GKE environment.

i just want a proper step by step guide for anyone not familiar with how to achieve following points:

  • Have GKE
  • Have jenkins pod on it
  • Know all commands to execute (docker, gcloud, kubectl, helm etc)
  • Just need an integration between jenkins and GKE to utilize all above

Kindly bear me if i am unable to explain that much. Ask anything else you need to resolve this. Thank

-- vicky
continuous-integration
google-kubernetes-engine
jenkins
kubernetes

1 Answer

8/8/2019

If your Jenkins pod is running inside GKE, just go for accessing the api from a pod and wrap your last step into small python/go script.

If you prefer to use kubectl you could: set up correct rbac, for example (this one allow to list pods and servies):

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: list
rules:
- apiGroups: [""]
  resources: ["services", "pods"]
  verbs: ["get", "list"]

---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: list
subjects:
- kind: ServiceAccount
  name: default
  namespace: default
roleRef:
  kind: ClusterRole
  name: svc-list
  apiGroup: rbac.authorization.k8s.io

and then

kubectl run --restart=Never --rm -it ubuntu --image ubuntu:18.04
{
   apt-get update
   apt-get install -y apt-transport-https curl gnupg
   curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg |  apt-key add -

  cat <<EOF | tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

   apt-get update
   apt-get install -y kubectl
}

kubectl get po,svc

NAME         READY   STATUS    RESTARTS   AGE
pod/ubuntu   1/1     Running   0          16m

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   122d
-- FL3SH
Source: StackOverflow