Kubeconfig & Kubernetes PKI - Practical Challenges
Hands-on exercises for mastering Kubernetes certificate management and kubeconfig operations. All scenarios use kind clusters.
Challenge 1: Certificate Analysis in Running Cluster
Create a kind cluster and investigate its PKI infrastructure.
What you need to do:
- Create a kind cluster named
cka-cluster - Find all certificate files in the cluster's PKI directory
- Determine the CN (Common Name) of the cluster CA
- Verify whether the CA certificate is self-signed
- Check what the API server certificate's issuer is
- Extract the CA certificate from your kubeconfig and verify its validity dates
- Calculate how many days until the CA certificate expires
- List all DNS names that the API server certificate is valid for
- List all IP addresses that the API server certificate is valid for
- Explain why multiple DNS names and IPs are needed in the API server certificate
Deliverables:
- Write a script that extracts and prints:
- Certificate validity dates
- Certificate CN
- Days until expiration
- All SANs (DNS names and IPs)
- Whether cert is self-signed
Success criteria:
- Your script runs without errors
- Output is human-readable and formatted
- You understand why each certificate component matters
Challenge 2: Multi-Cluster Kubeconfig Management
Manage three separate kind clusters with merged kubeconfigs.
What you need to do:
- Create three kind clusters:
prod-cluster,staging-cluster,dev-cluster - Get individual kubeconfigs for each cluster
- Merge all three kubeconfigs into a single file
- Create separate contexts for each cluster, each pointing to a different default namespace:
- prod-cluster → production namespace
- staging-cluster → staging namespace
- dev-cluster → development namespace
- Switch between contexts and verify that the active namespace changes
- Write a function that lets you interactively select and switch to any context
- Display current cluster, user, and namespace information after each switch
- Make the function persistent so it loads every time you open a shell
Deliverables:
- Merged kubeconfig file
- Function in your shell rc file
- Demonstrate switching between all three clusters
Success criteria:
- All contexts work without errors
- Switching contexts changes both the active cluster and namespace
- Function shows context info after switching
Challenge 3: Create Users Using Kubernetes CSR API
Provision three new users with certificate-based authentication using the Kubernetes CSR API.
User specifications:
- User 1:
alicewith groupdevelopers- needs read-only pod access - User 2:
bobwith groupdevelopers- needs read-only pod access - User 3:
admin-carolwith groupkubeadm:cluster-admins- needs full cluster admin
What you need to do:
- Generate private key for each user
- Create a certificate signing request for each user with proper CN and Organization fields
- Submit each CSR to the Kubernetes API as a CertificateSigningRequest object
- Approve the CSRs
- Extract the signed certificates from Kubernetes
- Add each user to your kubeconfig
- Create contexts for each user
- Apply RBAC permissions so developers can only view pods and admin has full access
- Test that alice can list pods but cannot delete them
- Test that admin-carol can delete pods
- Test that alice cannot access nodes or other resources
Deliverables:
- Three working user contexts in kubeconfig
- RBAC permissions applied correctly
- Test results showing permission enforcement
Success criteria:
- All users can authenticate
- Permissions match the intended roles
- Developer users are restricted, admin user has full access
Challenge 4: Build a User Provisioning Automation Script
Create a fully automated script that provisions a complete user with certificate and RBAC.
Script must:
- Accept username, group, and cluster as arguments
- Generate a private key
- Create a CSR with the specified CN and Organization
- Submit CSR to Kubernetes
- Wait for CSR approval (manual or automatic)
- Extract the signed certificate
- Configure the user's kubeconfig
- Create appropriate RBAC rolebindings based on the group
- Test authentication by attempting to list pods
- Output clear messages showing each step
- Save the private key with restrictive permissions
- Generate a warning about key security
Test your script:
- Provision 3 different users with different groups
- Verify each user context works
- Verify permissions are enforced
Deliverables:
- Single script file that handles complete provisioning
- Usage documentation (how to run the script)
- Script output showing all 10+ steps
Success criteria:
- Script runs without manual intervention (except CSR approval)
- All users are properly configured and have correct permissions
- Script output is clear and useful for understanding what happened
Challenge 5: Troubleshoot Certificate Mismatches
You have a certificate problem to solve.
Scenario 1: Connection fails with certificate signed by unknown authority
Your developer cannot connect to the cluster. They're getting an error about the certificate not being signed by a known authority.
- Diagnose what's wrong
- Verify the user's certificate was signed by the correct CA
- Verify the CA certificate in their kubeconfig matches the cluster CA
- Fix the issue and test that they can connect
Scenario 2: API server hostname mismatch
Someone tries connecting to the API server using a hostname that's not in the certificate SAN.
- Determine why the connection fails
- Show what hostname they're using
- Show what hostnames are valid in the certificate
- Explain why this is a security feature
- Find a valid way to connect using information from the certificate
Scenario 3: Kubeconfig context is missing
A user's kubeconfig is missing the context they need.
- Determine what context is missing
- Determine what cluster and user it should reference
- Create the missing context
- Test that it works
Deliverables:
- Document each problem
- Show diagnostic steps you took
- Show how you fixed each issue
- Prove the issue is resolved with a successful test
Success criteria:
- You fix each problem correctly
- You understand why each problem occurred
- You demonstrate the solution works
Challenge 6: Monitor Certificate Expiration
Create a monitoring script that tracks certificate expiration across multiple users.
Your script must:
- Read all users from your kubeconfig
- For each user, extract their certificate
- Determine when each certificate expires
- Calculate days remaining until expiration
- Highlight certificates expiring within 30 days
- Show expired certificates
- Output a formatted report
- Color-code the output (OK/Warning/Critical)
Deliverables:
- Script that produces an expiration report
- Sample output showing multiple users with different expiration times
- Proof that it correctly identifies soon-to-expire certificates
Success criteria:
- Script runs without errors
- Output is clear and actionable
- Dates are accurate and correct
Challenge 7: Certificate Renewal Workflow
Renew an expiring user certificate.
What you need to do:
- Identify a user certificate that will expire soon (or create one with short validity)
- Generate a new private key for renewal
- Create a new CSR with the same CN and Organization as the original
- Submit the renewal CSR to Kubernetes
- Approve the new CSR
- Extract the new certificate
- Update the user's kubeconfig with the new certificate
- Verify the user can still authenticate with the new certificate
- Compare the old and new certificates - show what changed and what stayed the same
Deliverables:
- Completed renewal process
- Comparison of old and new certificates
- Proof that the renewed certificate works
Success criteria:
- Renewal process completes without errors
- User can authenticate with the new certificate
- You understand the renewal workflow
Challenge 8: Custom Certificate with Specific SANs
Create a custom server certificate with specific Subject Alternative Names.
What you need to do:
- Create an OpenSSL config file that specifies custom SANs including:
- Multiple DNS names
- Multiple IP addresses
- Specific key usage and extended key usage extensions
- Generate a private key
- Create a CSR using your config
- Sign the certificate with the cluster CA
- Verify that all your specified SANs are in the final certificate
- Verify the key usage extensions are correct
Deliverables:
- OpenSSL config file
- Generated certificate
- Verification that SANs match what you specified
Success criteria:
- Certificate contains all specified SANs
- Certificate is properly signed by cluster CA
- Extensions are exactly as you configured them
Challenge 9: Embedded vs Referenced Certificates in Kubeconfig
Compare and convert between embedded and referenced certificates.
What you need to do:
- Create a user context with embedded certificates (full base64 data in kubeconfig file)
- Create another user context with referenced certificates (file paths in kubeconfig)
- Compare the kubeconfig files - show size differences
- Show which approach is portable (can move kubeconfig to another machine)
- Show which approach requires external files
- Convert an embedded certificate to a referenced one
- Verify both approaches work for authentication
Deliverables:
- Two kubeconfigs (one embedded, one referenced)
- Size comparison
- Explanation of when to use each approach
- Proof both work for authentication
Success criteria:
- You understand the trade-offs between the two approaches
- Both approaches authenticate successfully
- You can explain when to use each one
Challenge 10: Complete User Lifecycle
Create, manage, monitor, and eventually retire a user account.
What you need to do:
- Create a new user with full provisioning automation
- Add the user to your kubeconfig
- Create RBAC permissions for the user
- Verify the user can authenticate and has correct permissions
- Monitor the user's certificate expiration date
- Track the certificate through its lifecycle
- Renew the certificate before it expires
- Verify the renewed certificate works
- Create a report showing certificate history
- Demonstrate removing the user from kubeconfig
Deliverables:
- Complete user lifecycle documentation
- Provisioning script that handles all steps
- Expiration monitoring report
- Certificate renewal proof
- User removal process
Success criteria:
- User provisioning is fully automated
- Certificate is successfully renewed before expiration
- All steps are documented and reproducible
Challenge 11: CSR Management in Kubernetes
Work directly with Kubernetes CSR objects.
What you need to do:
- Create multiple CSRs in the cluster
- List all pending CSRs
- View details of specific CSRs (including the original request)
- Approve some CSRs
- Deny other CSRs
- Delete CSRs
- Extract certificates from approved CSRs
- Create a script that auto-approves CSRs from specific users or groups
Deliverables:
- Demonstration of CSR operations
- Script for automated CSR approval
- Proof that extracted certificates work for authentication
Success criteria:
- You understand the CSR API
- You can perform all CSR operations
- Auto-approval script works correctly
Challenge 12: Multi-Cluster User Management
Manage the same user across multiple clusters.
What you need to do:
- Create the same user (same CN, same group) in both prod and dev clusters
- Generate certificates for this user in each cluster
- Create a single kubeconfig that has contexts for both clusters with the same user
- Test that the user can authenticate to both clusters
- Verify that RBAC permissions are enforced independently in each cluster
- Apply different permissions to the same user in each cluster
- Verify that the user's permissions match what you set in each cluster
Deliverables:
- Kubeconfig with user contexts for both clusters
- RBAC configuration for both clusters
- Test results showing user works in both clusters with different permissions
Success criteria:
- Same user authenticates to both clusters
- Permissions are independent per cluster
Challenge 21: Kubectl Config Deep Dive — Use kubectl config to the fullest
This lab forces you to master the kubectl config subcommands for creating, editing, merging, and inspecting kubeconfig files.
What you need to do:
- Create two minimal cluster entries (fake clusters are fine using
server: https://127.0.0.1:6443with different names) and two user entries usingkubectl config set-clusterandkubectl config set-credentials(no manual YAML editing allowed). - Create two contexts that combine the clusters and users, and set different default namespaces for each context using
kubectl config set-context. - Rename one context using
kubectl config rename-contextand verify the change withkubectl config get-contexts. - Switch between contexts using
kubectl config use-contextand confirmkubectl config view --minify --output 'jsonpath={.contexts[0].context.namespace}'reflects the active namespace. - Use
kubectl config setandkubectl config unsetto add a custom field under a user (e.g.,user.token) and then remove it; verify withkubectl config view. - Merge two kubeconfig files by setting
KUBECONFIG(create two separate files, then merge withkubectl config view --flatten > merged-config); confirm the merged file contains all clusters, contexts, and users. - Extract a single context into its own kubeconfig using
kubectl config view --flatten --minify --context=<context>and verify it works by settingKUBECONFIGto the extracted file and runningkubectl config current-context. - Demonstrate how to make a context the default for the current shell session without modifying files (hint: use environment variables or
kubectl config use-context). - Show how to safely rotate credentials in kubeconfig: update a user's client-certificate-data or token using
kubectl config set-credentialsand verify that the new value is present and effective. - Create a small helper script (or set of commands) that lists contexts, prompts to choose one, and switches to it while printing the current cluster, user, and namespace.
Deliverables:
- A short script (bash) demonstrating tasks 6 and 10 (merge and context switch helper)
- A document showing the commands used for tasks 1–9 with short explanations and verification commands
- An exported merged kubeconfig file (
merged-config) and an extracted single-context kubeconfig (single-<context>.kubeconfig)
Success criteria:
All
kubectl configsubcommands are used at least once as described aboveMerged kubeconfig contains expected entries and the extracted kubeconfig works when used directly
The helper script successfully switches contexts and prints the expected info
No direct manual edits to kubeconfig YAML are made (all changes were via
kubectl configsubcommands)You understand how RBAC works across clusters
Challenge 13: Kubeconfig Recovery and Backup
Backup your kubeconfig and recover from corruption.
What you need to do:
- Create a backup strategy for your kubeconfig
- Create automated backups
- Intentionally corrupt your kubeconfig
- Attempt to use the corrupted kubeconfig (should fail)
- Recover from your backup
- Verify all contexts work after recovery
- Create a script that verifies kubeconfig integrity
- Create a validation function that tests all contexts can connect
Deliverables:
- Backup script
- Recovery script
- Validation script
- Proof that recovery works
Success criteria:
- Backup and recovery process works
- Validation correctly identifies working and broken contexts
- You can recover from corruption
Challenge 14: Understanding Certificate Authorities
Work with multiple CAs.
What you need to do:
- Extract the cluster CA
- Extract other CAs from the cluster (ETCD CA, front-proxy CA, etc.)
- Understand which certificates are signed by which CA
- Verify that API server cert is signed by cluster CA
- Verify that kubelet cert is signed by cluster CA
- Understand what would happen if a certificate was signed by a different CA
- Create a script that validates all system certificates are signed by expected CAs
Deliverables:
- List of all CAs in cluster
- Which certificates are signed by which CA
- Validation script
Success criteria:
- You understand the CA hierarchy
- Your validation script correctly checks certificate chains
Challenge 15: Real-World Incident Response
Simulate and respond to certificate-related incidents.
Incident 1: Kubelet certificate expires
- A kubelet certificate expires
- Node goes NotReady
- Diagnose why (certificate expiration)
- Fix the issue
- Restore node to Ready state
Incident 2: User certificate theft
- Assume a user's certificate was compromised
- Revoke the user's access (remove RBAC and kubeconfig)
- Provision the user with a new certificate
- Verify old certificate no longer works
Incident 3: Certificate configuration mistake
- Someone created a certificate without the correct SANs
- Connections fail with hostname mismatch
- Diagnose the problem
- Create a new certificate with correct SANs
- Update configuration to use new certificate
Deliverables:
- Diagnosis and resolution for each incident
- Steps you took to fix each problem
- Proof that each issue is resolved
Success criteria:
- You successfully diagnose and fix all incidents
- You understand how to prevent each incident in the future
Challenge 16: Kubelet Client Certificate Rotation
Investigate and manage kubelet client certificates which authenticate the kubelet to the API server.
What you need to do:
- Create a kind cluster with kubelet client certificate rotation enabled
- Find the kubelet client certificate in the node
- Extract and examine the kubelet client certificate (CN should be system:node:NODENAME)
- Verify the certificate is signed by the cluster CA
- Simulate certificate rotation by manually creating a new CSR from the kubelet's key
- Understand what happens when kubelet certificate expires without rotation
- Create a monitoring script that tracks kubelet certificate expiration dates
- Compare kubelet certs across multiple nodes (if multi-node cluster)
Deliverables:
- Script that monitors kubelet certificate expiration
- Report showing certificate details for all kubelet certs
- Explanation of how kubelet certificate rotation prevents service disruption
Success criteria:
- You can locate and analyze kubelet certificates
- You understand the CN format for kubelet certificates
- Your monitoring script correctly identifies all kubelet certs
Challenge 17: Service Account Token and RBAC Integration
Connect service account tokens to RBAC permissions.
What you need to do:
- Create a service account in a namespace
- Extract its token from the secret
- Create a kubeconfig that uses the service account token for authentication
- Use this kubeconfig to attempt API calls (will be denied without RBAC)
- Create a Role with specific pod permissions
- Bind the Role to the service account
- Verify the service account can now perform only the permitted actions
- Create another service account with different permissions in the same namespace
- Verify each service account has different access levels
- Mount a service account in a pod and test it can access the API
Deliverables:
- Kubeconfig using service account token
- RBAC Role and RoleBinding
- Test results showing permission enforcement
- Pod that successfully uses the mounted service account
Success criteria:
- Service account token works for authentication
- RBAC permissions are enforced correctly
- Tokens are properly scoped to their permissions
Challenge 18: Kube-Controller-Manager and Kube-Scheduler Authentication
Understand how system components authenticate.
What you need to do:
- Create a kind cluster
- Find the certificates used by kube-controller-manager to authenticate to the API server
- Find the certificates used by kube-scheduler to authenticate to the API server
- Examine CN and Organization fields - they should be
system:kube-controller-managerandsystem:kube-scheduler - Verify these certificates are signed by the cluster CA
- Understand what permissions these components need (check RBAC clusterroles)
- Extract these certificates and create kubeconfigs using them
- Test that you can use these kubeconfigs to authenticate as the components
- Verify that each component can only do what their RBAC roles allow
- Explain why system components use certificates instead of service accounts
Deliverables:
- Analysis of system component certificates
- Kubeconfigs for kube-controller-manager and kube-scheduler
- RBAC permissions for each component
- Explanation of component authentication design
Success criteria:
- You can locate all system component certificates
- You understand the CN format for system components (system:COMPONENTNAME)
- You understand why each component needs its own identity
Challenge 19: Network Policy and Certificate-Based Service Communication
Restrict communication between pods based on network policies while using certificate authentication.
What you need to do:
- Create a kind cluster with CNI that supports network policies (e.g., Calico)
- Create three namespaces: frontend, backend, database
- Deploy applications in each namespace
- Create network policies that:
- Allow frontend → backend communication
- Allow backend → database communication
- Deny frontend → database communication
- Deny database → anything
- Create certificates for each service
- Configure services to use mutual TLS (mTLS) for communication
- Test that allowed traffic flows but restricted traffic is blocked by network policy
- Verify that even though some pods could communicate at layer 3, network policy prevents it
- Create a network policy audit script that shows allowed and denied traffic
Deliverables:
- Network policy definitions
- Certificates for each service
- Test results showing allowed/denied connections
- Audit script output
Success criteria:
- Network policies enforce correct traffic restrictions
- You understand the relationship between network policies and application-level authentication
- All tests show expected behavior
Challenge 20: Audit Logging of Authentication Events
Monitor and analyze authentication events in your cluster.
What you need to do:
- Create a kind cluster with audit logging enabled
- Configure audit logging to capture:
- All authentication events
- All certificate-based authentication
- All service account token usage
- Perform various authentication actions:
- Connect as a user with certificate
- Use service account token from a pod
- Attempt unauthenticated connection (should fail)
- Use wrong certificate (should fail)
- Review audit logs and identify authentication events
- Parse audit logs to extract:
- User identity
- Timestamp
- Whether it succeeded or failed
- API action attempted
- Create a script that generates an authentication report
- Identify suspicious authentication patterns
- Create alerts for failed authentication attempts
Deliverables:
- Audit logging configuration
- Audit log parser script
- Authentication report showing all login events
- Security analysis of authentication patterns
Success criteria:
- Audit logging captures all authentication events
- You can parse and analyze audit logs
- You understand what to look for in authentication logs
- Your report clearly shows who accessed the cluster and when
Challenge 21: Complete kubectl config Command Mastery
Master every kubectl config subcommand through hands-on practice.
What you need to do:
- Use
kubectl config set-clusterto create two cluster entries with different server URLs - Use
kubectl config set-credentialsto create two user entries (one with cert, one with token) - Use
kubectl config set-contextto create contexts linking clusters and users with different namespaces - Use
kubectl config rename-contextto rename a context, verify withkubectl config get-contexts - Use
kubectl config use-contextto switch contexts and verify current context - Use
kubectl config current-contextandkubectl config view --minifyto inspect active config - Use
kubectl config delete-cluster,kubectl config delete-user,kubectl config delete-contextto remove entries - Use
kubectl config setandkubectl config unsetto modify custom fields - Merge multiple kubeconfigs using
KUBECONFIGenvironment variable andkubectl config view --flatten - Extract specific contexts using
kubectl config view --minify --context=<name>
Commands to demonstrate:
kubectl config set-cluster
kubectl config set-credentials
kubectl config set-context
kubectl config use-context
kubectl config current-context
kubectl config get-contexts
kubectl config get-clusters
kubectl config get-users
kubectl config view
kubectl config rename-context
kubectl config delete-cluster
kubectl config delete-user
kubectl config delete-context
kubectl config set
kubectl config unsetDeliverables:
- Demo script showing all 15 subcommands in action
- Sample kubeconfig with multiple clusters/users/contexts
- Merged kubeconfig from multiple sources
Success criteria:
- All kubectl config subcommands demonstrated successfully
- Kubeconfig manipulation done entirely via CLI (no manual YAML editing)
- Contexts work for cluster switching
Challenge 21: Complete kubectl config Command Mastery
Master every kubectl config subcommand through hands-on practice.
What you need to do:
- Use
kubectl config set-clusterto create two cluster entries with different server URLs - Use
kubectl config set-credentialsto create two user entries (one with cert, one with token) - Use
kubectl config set-contextto create contexts linking clusters and users with different namespaces - Use
kubectl config rename-contextto rename a context, verify withkubectl config get-contexts - Use
kubectl config use-contextto switch contexts and verify current context - Use
kubectl config current-contextandkubectl config view --minifyto inspect active config - Use
kubectl config delete-cluster,kubectl config delete-user,kubectl config delete-contextto remove entries - Use
kubectl config setandkubectl config unsetto modify custom fields - Merge multiple kubeconfigs using
KUBECONFIGenvironment variable andkubectl config view --flatten - Extract specific contexts using
kubectl config view --minify --context=<name>
Commands to demonstrate:
kubectl config set-cluster
kubectl config set-credentials
kubectl config set-context
kubectl config use-context
kubectl config current-context
kubectl config get-contexts
kubectl config get-clusters
kubectl config get-users
kubectl config view
kubectl config rename-context
kubectl config delete-cluster
kubectl config delete-user
kubectl config delete-context
kubectl config set
kubectl config unsetDeliverables:
- Demo script showing all 15 subcommands in action
- Sample kubeconfig with multiple clusters/users/contexts
- Merged kubeconfig from multiple sources
Success criteria:
- All kubectl config subcommands demonstrated successfully
- Kubeconfig manipulation done entirely via CLI (no manual YAML editing)
- Contexts work for cluster switching
Notes for all challenges:
- Use kind clusters - no special production setup required
- Refer to
notes.mdfor technical background and reference commands - Use
solutions.mdfor hints if you get stuck - All challenges should be completed using a kind cluster you create
- Document your work as you go
- Test each step to ensure it works before moving to the next