HUB
Sign inSign up
kubectl logo

boyroywax/kubectl

By boyroywax

β€’Updated about 1 year ago

A lightweight, secure Alpine-based container image with kubectl client.

Image
Developer tools
0

1.5K

boyroywax/kubectl repository overview

⁠Alpine kubectl In-Cluster Container Image

Docker Hub License: MIT

A lightweight, secure Alpine-based container image with kubectl client, designed for running Kubernetes jobs and automation tasks within clusters. Perfect for PersistentVolume reconciliation, cluster management jobs, and GitOps workflows.

Now available in three optimized variants:

  • Standard (v1.0.0) - 76 MB - Full features with bash support
  • Slim (v1.0.0-slim) - 20 MB - UPX compressed, sh only (73% smaller!)
  • Minimal (v1.0.0-minimal) - 14 MB - Distroless, most secure (82% smaller!)

β πŸš€ Features

  • πŸ”οΈ Alpine Linux 3.22 - Minimal, secure base image
  • πŸ”§ Pinned kubectl Version - Kubernetes v1.33.5 client
  • πŸ“¦ UPX Compressed - Binary compressed from 57MB to 11MB (slim/minimal)
  • 🐚 Shell Support - bash + sh (standard) or sh only (slim) or no shell (minimal)
  • ⚑ Ultra Lightweight - From 76MB down to 14MB with distroless
  • πŸ”’ Security Hardened - Minimal attack surface, distroless option
  • ☸️ Kubernetes Ready - Purpose-built for cluster automation and jobs
  • πŸ“¦ Docker Hub Ready - Published with full attestation and SBOM
  • πŸ“‰ Size Optimized - Choose from standard (76MB), slim (20MB), or minimal (14MB)

β πŸ“‹ Table of Contents

⁠🎯 Variants

Choose the right variant for your needs:

VariantTagSizeBashShellUPXBest For
Standard1.0.076 MBβœ… Yessh + bash❌ NoProduction, complex scripts, debugging
Slim1.0.0-slim20 MB❌ Nosh onlyβœ… YesSize-critical, simple scripts, CI/CD
Minimal1.0.0-minimal14 MB❌ No❌ Noneβœ… YesMaximum security, no debugging needed
⁠Standard Version (76 MB)
  • Full bash + sh shell support
  • All essential tools (curl, ca-certificates)
  • Perfect for complex automation scripts
  • Best for development and Helm chart hooks
  • Alpine package manager available
⁠Slim Variant (20 MB) - 73% smaller!
  • POSIX sh only (no bash)
  • UPX compressed kubectl (57MB β†’ 11MB binary)
  • Minimal packages (curl removed after build)
  • Perfect for production jobs
  • Alpine package manager for troubleshooting
  • Good balance of size and utility
⁠Minimal Variant (14 MB) - 82% smaller! ⭐ Most Secure
  • Distroless base - no shell, no package manager
  • UPX compressed kubectl (57MB β†’ 11MB binary)
  • Smallest attack surface
  • Perfect for security-critical environments
  • No debugging tools (by design)
  • Ideal for simple kubectl jobs

⁠⚑ Quick Start

⁠Standard Version (Default)
⁠Docker Run
# Run kubectl command
docker run --rm boyroywax/kubectl:1.0.0 version --client

# Interactive shell
docker run --rm -it boyroywax/kubectl:1.0.0 /bin/bash
⁠Slim Variant (Lightweight)
# Ultra-lightweight kubectl with UPX compression
docker run --rm boyroywax/kubectl:1.0.0-slim version --client

# Interactive shell (sh only)
docker run --rm -it boyroywax/kubectl:1.0.0-slim /bin/sh
⁠Minimal Variant (Most Secure)
# Distroless - no shell, maximum security
docker run --rm boyroywax/kubectl:1.0.0-minimal version --client

# Note: No shell available in minimal variant (by design)
⁠With kubeconfig
# Mount kubeconfig for cluster access (works with both variants)
docker run --rm \
  -v ~/.kube/config:/root/.kube/config:ro \
  boyroywax/kubectl:1.0.0 get pods

⁠☸️ Kubernetes Jobs

⁠PersistentVolume Creation Job
apiVersion: batch/v1
kind: Job
metadata:
  name: create-nfs-pv
spec:
  template:
    spec:
      serviceAccountName: pv-creator
      containers:
      - name: kubectl
        image: boyroywax/kubectl:1.0.0
        command: ["/bin/bash", "-c"]
        args:
          - |
            kubectl apply -f - <<EOF
            apiVersion: v1
            kind: PersistentVolume
            metadata:
              name: nfs-pv
            spec:
              capacity:
                storage: 100Gi
              accessModes:
                - ReadWriteMany
              nfs:
                server: nfs-server.default.svc.cluster.local
                path: /nfsshare/data
            EOF
      restartPolicy: OnFailure
⁠Cluster Automation Job
apiVersion: batch/v1
kind: Job
metadata:
  name: cleanup-resources
spec:
  template:
    spec:
      serviceAccountName: cluster-admin
      containers:
      - name: kubectl
        image: boyroywax/kubectl:1.0.0
        command: ["/bin/bash", "-c"]
        args:
          - |
            # Cleanup old pods
            kubectl delete pods --field-selector status.phase=Failed -n default
            
            # Check cluster status
            kubectl get nodes
      restartPolicy: OnFailure

β πŸ”§ Configuration

⁠Build Arguments
ArgumentDefaultDescription
KUBECTL_VERSIONv1.33.5Kubernetes client version to install
⁠Environment Variables
VariablePurposeExample
KUBECONFIGPath to kubeconfig file/root/.kube/config
⁠Installed Tools
⁠Standard Variant
  • kubectl - Kubernetes command-line tool (v1.33.5, 57MB uncompressed)
  • curl - Data transfer utility
  • ca-certificates - SSL/TLS certificate bundle
  • bash - GNU Bourne-Again shell
  • Alpine Linux base utilities (30 packages total)
⁠Slim Variant
  • kubectl - Kubernetes command-line tool (v1.33.5, 11MB UPX compressed)
  • ca-certificates - SSL/TLS certificate bundle
  • sh - POSIX shell (busybox)
  • Minimal Alpine utilities (17 packages total)
⁠Minimal Variant
  • kubectl - Kubernetes command-line tool (v1.33.5, 11MB UPX compressed)
  • ca-certificates - SSL/TLS certificate bundle
  • Distroless base (0 packages, no package manager)

⁠🎯 Use Cases

⁠Kubernetes Automation
  • PersistentVolume Management: Create and manage PVs from within the cluster
  • Resource Cleanup: Scheduled jobs for cleaning up stale resources
  • Cluster Validation: Health checks and validation jobs
⁠Helm Chart Integration
  • Pre-install Jobs: Setup tasks before Helm chart deployment
  • Post-install Hooks: Configuration and validation after deployment
  • NFS PV Creation: Automated PV creation for NFS shares
⁠GitOps Workflows
  • CI/CD Integration: Deploy and manage resources from pipelines
  • Automated Rollouts: Progressive delivery workflows
  • Configuration Sync: Keep cluster state synchronized

β πŸ—οΈ Building

⁠Build Locally
# Clone the repository
git clone https://github.com/boyroywax/kubectl.git
cd kubectl

# Build all variants (default)
./build-with-attestation.sh

# Build standard variant only
./build-with-attestation.sh standard

# Build slim variant only
./build-with-attestation.sh slim

# Build minimal variant only
./build-with-attestation.sh minimal

# Build with specific kubectl version
./build-with-attestation.sh all 1.0.0 v1.31.0
⁠Build Individual Variants
# Standard variant (76 MB)
docker build -t kubectl:1.0.0 -f Dockerfile .

# Slim variant (20 MB) - with UPX compression
docker build -t kubectl:1.0.0-slim -f Dockerfile.slim .

# Minimal variant (14 MB) - distroless
docker build -t kubectl:1.0.0-minimal -f Dockerfile.minimal .

# With specific kubectl version
docker build --build-arg KUBECTL_VERSION=v1.31.0 -t kubectl:1.31.0 .
⁠Build with Custom Registry
export REGISTRY="registry.example.com/your-namespace"
export IMAGE_NAME="${REGISTRY}/kubectl"
docker build --build-arg KUBECTL_VERSION=v1.33.5 -t "${IMAGE_NAME}:v1.33.5" .
docker push "${IMAGE_NAME}:v1.33.5"
⁠Build with Attestation
# Build with SBOM and provenance
./build-with-attestation.sh

β πŸ“š Examples

⁠Helm Chart Integration

Update your Helm chart values to use the kubectl image:

nfs:
  pv:
    enabled: true
    jobImage:
      repository: boyroywax/kubectl
      tag: 1.0.0
      pullPolicy: IfNotPresent
⁠CronJob for Periodic Tasks
apiVersion: batch/v1
kind: CronJob
metadata:
  name: cluster-health-check
spec:
  schedule: "0 */6 * * *"  # Every 6 hours
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: cluster-reader
          containers:
          - name: kubectl
            image: boyroywax/kubectl:1.0.0
            command: ["/bin/bash", "-c"]
            args:
              - |
                echo "=== Cluster Health Check ==="
                kubectl get nodes
                kubectl get pods --all-namespaces | grep -v Running
          restartPolicy: OnFailure
⁠Multi-Cluster Management
# Build image with multiple kubeconfig contexts
docker run --rm \
  -v ~/.kube/config:/root/.kube/config:ro \
  boyroywax/kubectl:1.0.0 \
  config get-contexts

β πŸ› οΈ Troubleshooting

⁠Permission Issues

Problem: Job fails with RBAC errors

Error from server (Forbidden): persistentvolumes is forbidden

Solution: Create appropriate ServiceAccount and RBAC:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: pv-creator
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: pv-creator
rules:
- apiGroups: [""]
  resources: ["persistentvolumes"]
  verbs: ["create", "get", "list", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: pv-creator
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: pv-creator
subjects:
- kind: ServiceAccount
  name: pv-creator
  namespace: default
⁠kubectl Not Found

Problem: Command not found errors

/bin/sh: kubectl: not found

Solution: Ensure you're using the correct entrypoint:

command: ["/usr/local/bin/kubectl"]
# Or use full path in scripts
args: ["/usr/local/bin/kubectl get pods"]
⁠Version Compatibility

Problem: API version mismatch

error: SchemaError(io.k8s.api.core.v1.PersistentVolume)

Solution: Build with compatible kubectl version:

# Check cluster version
kubectl version --short

# Build with matching version
docker build --build-arg KUBECTL_VERSION=v1.30.0 -t kubectl:1.30.0 .
⁠Network Connectivity

Problem: Cannot connect to cluster

The connection to the server localhost:8080 was refused

Solution: Ensure in-cluster config or mounted kubeconfig:

# Use in-cluster service account (recommended)
serviceAccountName: kubectl-runner

# Or mount kubeconfig
volumes:
- name: kubeconfig
  secret:
    secretName: kubeconfig-secret
volumeMounts:
- name: kubeconfig
  mountPath: /root/.kube
  readOnly: true

β πŸ”„ Updating kubectl Version

To use a different kubectl version:

  1. Build with new version:

    docker build --build-arg KUBECTL_VERSION=v1.34.0 -t kubectl:1.34.0 .
    
  2. Update Helm values:

    jobImage:
      tag: 1.34.0
    
  3. Verify version:

    kubectl run test --rm -it --image=kubectl:1.34.0 -- version --client
    

β πŸ“¦ Container Specifications

⁠Standard Variant
  • Base Image: Alpine Linux 3.22
  • kubectl Version: v1.33.5 (57 MB uncompressed)
  • Shell: bash, sh
  • Packages: 30
  • Entrypoint: /usr/local/bin/kubectl
  • Default CMD: --help
  • Size: 76 MB
⁠Slim Variant
  • Base Image: Alpine Linux 3.22 (multi-stage)
  • kubectl Version: v1.33.5 (11 MB UPX compressed)
  • Shell: sh only
  • Packages: 17
  • Entrypoint: /usr/local/bin/kubectl
  • Default CMD: --help
  • Size: 20 MB (73% smaller)
⁠Minimal Variant
  • Base Image: Distroless Static (Debian 12)
  • kubectl Version: v1.33.5 (11 MB UPX compressed)
  • Shell: None (distroless)
  • Packages: 0 (no package manager)
  • Entrypoint: /usr/local/bin/kubectl
  • Default CMD: --help
  • Size: 14 MB (82% smaller)

⁠🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

⁠Development Setup
# Clone the repository
git clone https://github.com/boyroywax/kubectl.git
cd kubectl

# Build locally
docker build -t kubectl:dev .

# Test
docker run --rm kubectl:dev version --client

β πŸ“„ License

This project is licensed under the MIT License - see the LICENSE⁠ file for details.


Docker Hub: https://hub.docker.com/r/boyroywax/kubectl⁠

Tag summary

Content type

Image

Digest

sha256:70b2a4730…

Size

27.5 MB

Last updated

about 1 year ago

docker pull boyroywax/kubectl