Add Service Accounts
Prerequisites
-
A cluster admin role on your Kubernetes cluster
To add a cluster in Portworx Backup, you must first create a service account. This account enables you to retrieve the kubeconfig and token necessary to add a cluster in Portworx Backup.
Perform the following steps to add a service account:
-
Create a yaml spec called
pxbackup-sa.yamlwith the following content:---apiVersion: v1kind: ServiceAccountmetadata:name: pxbackup-sanamespace: kube-system---apiVersion: v1kind: Secrettype: kubernetes.io/service-account-tokenmetadata:name: pxbackup-sanamespace: kube-systemannotations:kubernetes.io/service-account.name: pxbackup-sa---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: pxbackup-sa-clusterrolebindingsubjects:- kind: ServiceAccountname: pxbackup-sanamespace: kube-systemroleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: cluster-admin -
Apply the spec file:
kubectl apply -f pxbackup-sa.yaml -
You need to update the server URL before running the script. Execute the following command to update the URL:
kubectl cluster-info -
Create a shell script
kubeconfig-sa.shwith the following content. If you have a valid certificate, follow step a, otherwise go to step b.a. If you have a valid certificate, use the below content. This creates a certificate-based service account:
#!/bin/bashSERVICE_ACCOUNT=pxbackup-saNAMESPACE=kube-systemSERVER=https://<SERVER-ADDRESS:PORT>SERVICE_ACCOUNT_TOKEN_COUNT=$(kubectl -n ${NAMESPACE} get secret -o=jsonpath='{.items[?(@.metadata.annotations.kubernetes\.io/service-account\.name=="'${SERVICE_ACCOUNT}'")].metadata.name}' | wc -w)if [ ${SERVICE_ACCOUNT_TOKEN_COUNT} -gt 1 ]thenSERVICE_ACCOUNT_TOKEN_NAME=$(kubectl -n ${NAMESPACE} get secret -o=jsonpath='{.items[?(@.metadata.annotations.kubernetes\.io/service-account\.name=="'${SERVICE_ACCOUNT}'")].metadata.name}' | awk '{for(i=1;i<=NF;i++){ if($i ~ /-token/){print $i} } }' | head -n 1)elseSERVICE_ACCOUNT_TOKEN_NAME=$(kubectl -n ${NAMESPACE} get secret -o=jsonpath='{.items[?(@.metadata.annotations.kubernetes\.io/service-account\.name=="'${SERVICE_ACCOUNT}'")].metadata.name}')fiSERVICE_ACCOUNT_TOKEN=$(kubectl -n ${NAMESPACE} get secret ${SERVICE_ACCOUNT_TOKEN_NAME} -o "jsonpath={.data.token}" | base64 --decode)SERVICE_ACCOUNT_CERTIFICATE=$(kubectl -n ${NAMESPACE} get secret ${SERVICE_ACCOUNT_TOKEN_NAME} -o "jsonpath={.data['ca\.crt']}")cat <<ENDapiVersion: v1kind: Configclusters:- name: default-clustercluster:certificate-authority-data: ${SERVICE_ACCOUNT_CERTIFICATE}server: ${SERVER}contexts:- name: default-contextcontext:cluster: default-clusternamespace: ${NAMESPACE}user: ${SERVICE_ACCOUNT}current-context: default-contextusers:- name: ${SERVICE_ACCOUNT}user:token: ${SERVICE_ACCOUNT_TOKEN}ENDb. If you do not have valid certificate, use the following content:
#!/bin/bashSERVICE_ACCOUNT=pxbackup-saNAMESPACE=kube-systemSERVER=https://<SERVER-ADDRESS:PORT>SERVICE_ACCOUNT_TOKEN_COUNT=$(kubectl -n ${NAMESPACE} get secret -o=jsonpath='{.items[?(@.metadata.annotations.kubernetes\.io/service-account\.name=="'${SERVICE_ACCOUNT}'")].metadata.name}' | wc -w)if [ ${SERVICE_ACCOUNT_TOKEN_COUNT} -gt 1 ]thenSERVICE_ACCOUNT_TOKEN_NAME=$(kubectl -n ${NAMESPACE} get secret -o=jsonpath='{.items[?(@.metadata.annotations.kubernetes\.io/service-account\.name=="'${SERVICE_ACCOUNT}'")].metadata.name}' | awk '{for(i=1;i<=NF;i++){ if($i ~ /-token/){print $i} } }' | head -n 1)elseSERVICE_ACCOUNT_TOKEN_NAME=$(kubectl -n ${NAMESPACE} get secret -o=jsonpath='{.items[?(@.metadata.annotations.kubernetes\.io/service-account\.name=="'${SERVICE_ACCOUNT}'")].metadata.name}')fiSERVICE_ACCOUNT_TOKEN=$(kubectl -n ${NAMESPACE} get secret ${SERVICE_ACCOUNT_TOKEN_NAME} -o "jsonpath={.data.token}" | base64 --decode)SERVICE_ACCOUNT_CERTIFICATE=$(kubectl -n ${NAMESPACE} get secret ${SERVICE_ACCOUNT_TOKEN_NAME} -o "jsonpath={.data['ca\.crt']}")cat <<ENDapiVersion: v1kind: Configclusters:- name: default-clustercluster:insecure-skip-tls-verify: trueserver: ${SERVER}contexts:- name: default-contextcontext:cluster: default-clusternamespace: ${NAMESPACE}user: ${SERVICE_ACCOUNT}current-context: default-contextusers:- name: ${SERVICE_ACCOUNT}user:token: ${SERVICE_ACCOUNT_TOKEN}END -
Run the shell script and save the kubeconfig file you obtain. You need this file later to add a cluster in the Portworx Backup web console.
chmod 755 kubeconfig-sa.sh./kubeconfig-sa.shPortworx Backup utilizes the service account token as a long-lived connection to the target cluster, therefore the script does not set the expiry field on the service account token. If expiry is set on this token, cluster owners will need to ensure that the token is refreshed before expiry timeline to avoid cluster authentication issues.