A Bash script that generates a kubeconfig file for a specified Kubernetes service account.
This script creates a kubeconfig file for a Kubernetes service account in a specified namespace. It automatically handles different Kubernetes versions (pre and post 1.24) and provides options for long-lived tokens, making it useful for CI/CD pipelines and automation processes.
- Works with both newer and older Kubernetes versions
- Creates long-lived tokens for service accounts
- Generates complete kubeconfig files with proper authentication
- Includes error handling and validation
- Multiple token generation methods based on cluster capabilities
kubectl
installed and configured with access to your cluster- Bash shell environment
- Proper permissions to create and view service accounts and secrets
./generate-kubeconfig.sh <service_account> <namespace> <kubeconfig_file>
service_account
: Name of the service account to create a kubeconfig fornamespace
: Kubernetes namespace where the service account existskubeconfig_file
: Path where the resulting kubeconfig file should be saved
./generate-kubeconfig.sh ci-cd-pipeline ci-cd ./ci-cd-kubeconfig.yaml
This generates a kubeconfig file for the ci-cd-pipeline
service account in the ci-cd
namespace and saves it to ./ci-cd-kubeconfig.yaml
.
The script uses the following process:
- Verifies the service account exists in the specified namespace
- Checks if the service account has an existing token secret
- If no secret exists:
- Attempts to create a long-lived token using the TokenRequest API
- Falls back to creating a manual token secret if needed
- Extracts the cluster connection details from your current kubectl context
- Generates a properly formatted kubeconfig file with all required authentication information
- Long-lived tokens present a security risk if compromised
- Consider implementing token rotation for production environments
- Some Kubernetes distributions may enforce token expiration regardless of settings
- Store the generated kubeconfig file securely
If you encounter issues:
- Ensure your kubectl has proper permissions to access the service account
- Check if your Kubernetes cluster has any security policies that restrict token creation
- Verify the service account exists in the specified namespace
- For newer Kubernetes versions, make sure the service account has proper RBAC permissions