Official cheatsheet: https://kubernetes.io/docs/reference/kubectl/cheatsheet/#interacting-with-nodes-and-cluster
kubectl
utility: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands
Copy ~/.kube/config
from your cluster (a control node) to your local/develop machine.
Installation using Ansible script:
mkdir ~/.kube
scp k3smaster01:~/.kube/config ~/kube/config
Or manual installation:
Create local config file:
nano ~/.kube/config
Copy and paste content from /etc/rancher/k3s/k3s.yaml
into ~/.kube/config:
sudo cat /etc/rancher/k3s/k3s.yaml
Copy private key (jpl-k3s
) to ~/.ssh
directory.
Create a hosts.ini
file containing all your machines.
[master]
k3smaster01
k3smaster02
k3smaster03
[master:vars]
ansible_user=joeplaa
ansible_ssh_private_key_file=~/.ssh/jpl-k3s
[worker]
k3sworker01
k3sworker02
k3sworker03
[worker:vars]
ansible_user=joeplaa
ansible_ssh_private_key_file=~/.ssh/jpl-k3s
[k3s_cluster:children]
master
worker
Create a ansible.cfg
file to point to the hosts.ini
file.
Create a secrets.txt
containing the sudo password of the machines.
Run ansible
commands from directory containing ansible.cfg
file.
Ping all machines (in your hosts file):
ansible all -m ping -v
Ping master machines (in your hosts file):
ansible master -m ping -v
Update apt cache:
ansible all -m apt -a "update_cache=yes cache_valid_time=86400" --become --become-password-file=secrets.txt
Update and upgrade apt cache:
ansible all -m apt -a "upgrade=yes update_cache=yes cache_valid_time=86400" --become --become-password-file=secrets.txt
Install apt application/package:
ansible all -m apt -a "name=<package> state=present" --become --become-password-file=secrets.txt
ansible all -m apt -a "name=python3 state=present" --become --become-password-file=secrets.txt
Run playbook:
ansible-playbook <playbook> --become-password-file=secrets.txt
ansible-playbook <playbook> -i <path to */**/hosts.ini> --become-password-file=secrets.txt
Update apt cache
ansible-playbook playbooks/update.yml --become-password-file=secrets.txt
ansible-playbook playbooks/update.yml -i <path to */**/hosts.ini> --become-password-file=secrets.txt
# update.yml
---
- name: Update apt repo and cache
hosts: k3s_cluster
become: true
become_user: root
tasks:
- name: Update apt repo and cache
apt:
update_cache: true
force_apt_get: true
cache_valid_time: 86400
Upgrade apt packages
ansible-playbook playbooks/upgrade.yml --become-password-file=secrets.txt
ansible-playbook playbooks/upgrade.yml -i <path to */**/hosts.ini> --become-password-file=secrets.txt
# upgrade.yml
---
- name: Update apt repo and cache and upgrade packages
hosts: k3s_cluster
become: true
become_user: root
tasks:
- name: Update apt repo and cache
apt:
update_cache: true
force_apt_get: true
cache_valid_time: 86400
- name: Upgrade all packages
apt:
update_cache: true
force_apt_get: true
cache_valid_time: 86400
Setting up k3s on the nodes:
ansible-playbook playbooks/site.yml --become-password-file=secrets.txt
ansible-playbook playbooks/site.yml -i inventory/jpl-cluster/hosts.ini --become-password-file=secrets.txt
Rebooting all the nodes:
ansible-playbook playbooks/reboot.yml --become-password-file=secrets.txt
ansible-playbook playbooks/reboot.yml -i inventory/jpl-cluster/hosts.ini --become-password-file=secrets.txt
# reboot.yml
---
- name: Reboot k3s_cluster
hosts: k3s_cluster
gather_facts: yes
tasks:
- name: Reboot the nodes (and Wait upto 5 mins max)
become: true
reboot:
reboot_timeout: 300
Add repository:
helm add repo <NAME> <URL>
helm repo add longhorn https://charts.longhorn.io
Update repositories:
helm update repositories
Install "app" (<NAME>
can be anything you like):
helm install <NAME> <APP> -n <NAMESPACE>
helm install <NAME> <APP> --namespace <NAMESPACE>
helm install longhorn longhorn/longhorn -n longhorn-system
Install "app" as LoadBalancer
(<NAME>
can be anything you like):
helm install <NAME> <APP> -n <NAMESPACE> --set service.ui.type="LoadBalancer"
helm install longhorn longhorn/longhorn -n longhorn-system --set service.ui.type="LoadBalancer"
Upgrade "app" (set normal app to LoadBalancer
):
helm upgrade <NAME> <APP> -n <NAMESPACE> --set service.ui.type="LoadBalancer"
helm upgrade longhorn longhorn/longhorn -n longhorn-system --set service.ui.type="LoadBalancer"
Install "app" using values.yaml
file:
helm install <NAME> <APP> -n <NAMESPACE> -f override_values.yaml
Uninstall "app":
helm uninstall <NAME>
helm uninstall longhorn
Create a namespace
kubectl create namespace <NAMESPACE>
Get all namespaces:
kubectl get namespaces
Get all nodes:
kubectl get nodes
Get pods in namespace:
kubectl get pods -n <NAMESPACE>
kubectl get pods --namespace <NAMESPACE>
Get pods in all namespaces:
kubectl get pods --all-namespaces
Get all used container images (https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/):
kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec['initContainers', 'containers'][*].image}" |\
tr -s '[[:space:]]' '\n' |\
sort |\
uniq -c
Get all images on a worker node https://github.com/k3s-io/k3s/issues/1900:
sudo k3s crictl images
Prune unused images on a worker node https://github.com/k3s-io/k3s/issues/1900:
sudo k3s crictl rmi --prune
Get all services:
kubectl get services --all-namespaces
kubectl get svc --all-namespaces -o wide
Describe service:
kubectl describe service <SERVICE> -n <NAMESPACE>
Edit service:
kubectl edit service <SERVICE> -n <NAMESPACE>
Get service:
kubectl get service <SERVICE> -n <NAMESPACE>
Expose service:
kubectl expose service <SERVICE>