Changelog
https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG.md
Dump Kubernetes Objects
kubectl get componentstatuses
kubectl get configmaps
kubectl get daemonsets
kubectl get deployments
kubectl get events
kubectl get endpoints
kubectl get horizontalpodautoscalers
kubectl get ingress
kubectl get jobs
kubectl get limitranges
kubectl get nodes
kubectl get namespaces
kubectl get pods
kubectl get pods --all-namespaces -o wide
kubectl get persistentvolumes
kubectl get persistentvolumeclaims
kubectl get quota
kubectl get resourcequotas
kubectl get replicasets
kubectl get replicationcontrollers
kubectl get secrets
kubectl get serviceaccounts
kubectl get services
kubectl get ep nginx
kubectl -n ingress-nginx get pods
kubectl run redis --image=redis -dry-run=client -o yaml > pod.yaml
kubectl edit pod redis
kubectl delete service my-service
kubectl run nginx --image=nginx --dry-run=client -o yaml
kubectl create deployment --image=nginx nginx
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml > nginx-deployment.yaml
kubectl create -f nginx-deployment.yaml
kubectl create deployment --image=nginx nginx --replicas=4 --dry-run=client -o yaml > nginx-deployment.yaml
kubectl run nginx --image=nginx
kubectl run nginx --image=nginx --dry-run=client -o yaml
kubectl create deployment --image=nginx nginx --dy-run=client -o yaml
kubectl scale deployment nginx --replicas=4
kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml
kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml
kubectl expose pod nginx --type=NodePort --port=80 --name=nginx-service --dry-run=client -o yaml
kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml
kubectl run custom-nginx --image=nginx --port=8080
kubectl create namespace dev-ns
kubectl get all --all-namespaces
kubectl run httpd --image=httpd:alpine --port=80 --expose
kubectl run nginx --image=nginx
kubectl run nginx --image=nginx --dry-run=client -o yaml
kubectl create deployment --image=nginx nginxkubectl create deployment --image=nginx nginx --dry-run=client -o yaml
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml > nginx-deployment.yaml
kubectl create -f nginx-deployment.yaml
kubectl create deployment --image=nginx nginx --replicas=4 --dry-run=client -o yaml > nginx-deployment.yaml
kubectl create deployment --image=nginx nginx --dry-run=client
kubectl top pod --containers=true
kubectl -n project-snake get pod -L app
kubectl get events -A --sort-by=.metadata.creationTimestamp
kubectl -n secret create secret generic secret5 --from-literal=user=foo --from-literal=pass=bar
kubeadm certs check-expiration
kubeadm certs renew all
kubeadm token list
# run temporary container
kubectl run tmp-shell --restart=Never --rm -i --tty --image centos -- /bin/bashAccess Kubernetes cluster without floating IP
CLUSTER_JUMPHOST=1.2.3.4
CLUSTER_MASTER=10.0.0.5
sshuttle --python /usr/bin/python3 --remote ubuntu@${CLUSTER_JUMPHOST} ${CLUSTER_MASTER}/32
export no_proxy=$no_proxy,10.0.0.5Deploy nginx
kubectl create deployment nginx --image=nginx
kubectl scale deployment --replicas 2 nginx
kubectl get pods -o wide
# kubectl create service nodeport nginx --tcp=80:80
kubectl expose deployment nginx --type=LoadBalancer --name=nginx --port=80
kubectl get deployment nginx -o wide
kubectl get service nginx -o wide
kubectl get pods -o wide -l app=nginx
kubectl exec -it ${POD_NAME} -- /bin/shkubectl exec -it ${POD_NAME} -- bashDeploy apache
kubectl create deployment apache --image=httpd
kubectl scale deployment --replicas 3 apache
kubectl expose deployment apache --type=LoadBalancer --name=apache --port=80Debug
systemctl list-units --failed
systemctl status kubelet.service
systemctl status kube-apiserver.service
kubectl get pods --all-namespaces -o wide
kubectl -v=7 get nodes
kubectl logs -f -n default nginx-5c7588df-8k5lh
Documentation
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ - kubectl Cheat Sheet
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands
Configuration
# ~/.kube/config
apiVersion: v1
clusters:
- cluster:
server: https://10.11.22.33:6443
name: api-server
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []
foo.bar
Limits
https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource
https://kubernetes.io/docs/tasks/administer-cluster/manage-resources/memory-default-namespace/
https://kubernetes.io/docs/tasks/administer-cluster/manage-resources/cpu-default-namespace/
apiVersion: v1
kind: LimitRange
metadata:
name: mem-limit-range
spec:
limits:
- default:
memory: 512Mi
defaultRequest:
memory: 256Mi
type: Container
apiVersion: v1
kind: LimitRange
metadata:
name: cpu-limit-range
spec:
limits:
- default:
cpu: 1
defaultRequest:
cpu: 0.5
type: ContainerMetrics
git clone https://github.com/kodekloudhub/kubernetes-metrics-server.git
kubectl create -f .Edit / patch configuration of the fly
kubectl -n kube-system get deployment coredns -o yaml | \
sed 's/allowPrivilegeEscalation: false/allowPrivilegeEscalation: true/g' | \
kubectl apply -f -Output / sort data with jsonpath
kubectl config view --kubeconfig=my-kube-config -o jsonpath='{.users[*].name }'
kubectl get pv -o custom-columns='NAME:.metadata..name,CAPACITY:.spec.capacity.storage' --sort-by='{.spec.capacity.storage}' > /opt/outputs/pv-and-capacity-sorted.txt
kubectl config view --kubeconfig my-kube-config -o jsonpath='{.contexts[?(@.context.user=="aws-user")].name}'curl Kubernetes API
curl -k https://kubernetes.default/api/v1/secrets
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
curl -k https://kubernetes.default/api/v1/secrets -H "Authorization: Bearer ${TOKEN}"
Install kubernetes with Ansible
https://github.com/derailed/k9s/releases
---
- name: Get latest stable kubectl version
ansible.builtin.uri:
url: https://dl.k8s.io/release/stable.txt
return_content: yes
register: kubectl_version
- name: Set kubectl version
ansible.builtin.set_fact:
kubectl_release: "{{ (kubectl_version.content | trim).split('.')[0:2] | join('.') }}"
- name: Download Kubernetes APT key
get_url:
url: https://pkgs.k8s.io/core:/stable:/v1.33/deb/Release.key
dest: /tmp/kubernetes-release.key
- name: Convert and install GPG key
command: gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg /tmp/kubernetes-release.key
args:
creates: /etc/apt/keyrings/kubernetes-apt-keyring.gpg
- name: Add Kubernetes APT repository
ansible.builtin.apt_repository:
update_cache: yes
repo: "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/{{ kubectl_release }}/deb/ /"
- name: Install Kubernetes binaries
ansible.builtin.apt:
name:
- kubectl
# - kubeadm
- name: Create kubectl bash completion
command: kubectl completion bash
register: kubectl_completion_output
args:
creates: /etc/bash_completion.d/kubectl
- name: Create /etc/bash_completion.d/kubectl
ansible.builtin.copy:
content: "{{ kubectl_completion_output.stdout }}"
dest: /etc/bash_completion.d/kubectl
when: kubectl_completion_output.changed
- name: Add k alias to .bashrc
blockinfile:
path: ~/.bashrc
block: |
alias k=kubectl
complete -o default -F __start_kubectl k
Install helm
https://helm.sh/docs/intro/install/
curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | sudo tee /usr/share/keyrings/helm.gpg > /dev/null
sudo apt-get install apt-transport-https --yes
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
sudo apt-get update
sudo apt-get install helm
Kubeconfig
https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
https://able8.medium.com/how-to-merge-multiple-kubeconfig-files-into-one-36fc987c2e2f
https://nikgrozev.com/2019/10/03/switch-between-multiple-kubernetes-clusters-with-ease/
export KUBECONFIG="$(find ~/.kube/ -type f | tr '\n' ':')"
kubectl config get-clusters
kubectl config current-context
kubectl config use-context my-cluster-01
Links
https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG.md
https://github.com/coreos/matchbox/tree/master/examples/terraform/bootkube-install
https://kubernetes.io/docs/reference/kubectl/cheatsheet/