I need to restart another Kubernetes like
kubectl rollout restart -n nams deployment xxx-deployment
deployment from a Jenkins server running inside the same Kubernetes cluster.
How to do that?
I was already tried without success with Jenkins Build Agents. I have a kubernetes pod that runs a Payara application server and I need to restart the deployment to take effect a new version off the deployed application.
Install Kubernetes CLI plugin on your Jenkins. With this plugin, it does not matter what platform your Jenkins is running on as long as your target Kubernetes cluster API endpoint is accessible by the Jenkins.
All your jobs will have the option to specify the Kubernetes cluster connection details in the Build Environment
section.
Then, add an Execute Shell
build step where you can happily use the kubectl
commands however you want.
--- ADDITIONAL INFO ---
Here is the script for creating the kubectl-token
needed by the plugin. Just modify KUBE_NS
variable to the name of the particular namespace in which the job will be run:
#!/bin/bash
KUBE_NS="default"
if [[ ! -n $(kubectl get ns --field-selector=metadata.name=${KUBE_NS} --no-headers 2> /dev/null) ]]; then
echo Namespace $KUBE_NS not found!
exit 1
fi
if [[ ! -n "${SA_NAME}" ]]; then
SA_NAME=sa-${KUBE_CLUSTER}-${KUBE_NS}
fi
kubectl apply -f - <<EOF
apiVersion: v1
kind: ServiceAccount
metadata:
namespace: ${KUBE_NS}
name: ${SA_NAME}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
namespace: ${KUBE_NS}
name: ${SA_NAME}-edit-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: edit
subjects:
- kind: ServiceAccount
name: ${SA_NAME}
namespace: ${KUBE_NS}
EOF
SA_SECRET=$(kubectl get -n ${KUBE_NS} serviceaccount/${SA_NAME} -o jsonpath='{.secrets[0].name}')
if [[ ! -n "${SA_SECRET}" ]]; then
SA_SECRET="${SA_NAME}-token"
kubectl apply -f - <<EOF
apiVersion: v1
kind: Secret
type: kubernetes.io/service-account-token
metadata:
name: ${SA_SECRET}
annotations:
kubernetes.io/service-account.name: "${SA_NAME}"
EOF
fi
TOKEN=$(kubectl get -n ${KUBE_NS} secret/${SA_SECRET} -o jsonpath='{.data.token}' | base64 -d)