This skill enables connection to Kubernetes clusters using YAML configuration files as input. It provides tools to apply, validate, and manage Kubernetes resources through kubectl commands.
Use this skill when:
kubectl must be installed and available in PATHIf kubectl is not installed, you can install it using:
macOS:
# Using Homebrew
brew install kubectl
# Or download directly
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
Linux:
# Using package manager (Ubuntu/Debian)
sudo apt-get update
sudo apt-get install -y kubectl
# Or download directly
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
Windows:
# Using Chocolatey
choco install kubernetes-cli
# Or download from official release
Verify installation:
kubectl version --client
Before applying any YAML, always validate the syntax:
kubectl apply --dry-run=client -f - <<'EOF'
[YAML_CONTENT]
EOF
Apply validated YAML to the current context:
kubectl apply -f - <<'EOF'
[YAML_CONTENT]
EOF
If you have kubeconfig YAML, save it and update context:
# Save kubeconfig
cat > /tmp/kubeconfig.yaml <<'EOF'
[KUBECONFIG_YAML]
EOF
# Set KUBECONFIG environment variable
export KUBECONFIG=/tmp/kubeconfig.yaml
# Verify connection
kubectl cluster-info
List and switch contexts:
# List available contexts
kubectl config get-contexts
# Switch to specific context
kubectl config use-context [CONTEXT_NAME]
# Get current context
kubectl config current-context
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_ENV: "production"
LOG_LEVEL: "info"
# Check if kubectl is installed
command -v kubectl
# Check cluster connectivity
kubectl version --short
# Check if context is set
kubectl config view --minify
Always use dry-run first to catch errors:
kubectl apply --dry-run=client -f [FILE_OR_STDIN]
# YAML content as variable
YAML_CONTENT=$(cat <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-deployment
spec:
replicas: 2
selector:
matchLabels:
app: test
template:
metadata:
labels:
app: test
spec:
containers:
- name: test
image: nginx:alpine
EOF
)
# Apply to cluster
kubectl apply -f - <<< "$YAML_CONTENT"
kubectl apply -f - <<'EOF'
---
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
key: value
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-deployment
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: app
image: myapp:latest
envFrom:
- configMapRef:
name: app-config
EOF
For more detailed information, see:
kubectl apply --dry-runkubectl apply for updates or kubectl replace for forced updates# Get detailed error information
kubectl describe [RESOURCE_TYPE] [RESOURCE_NAME]
# Check events
kubectl get events --sort-by='.lastTimestamp'
# Check pod logs
kubectl logs [POD_NAME]
Remember: Always test YAML in a non-production environment first when possible.
共 1 个版本