11 - Kubernetes Essential Commands
I am an aspiring DevOps Engineer proficient with containers and container orchestration tools like Docker, Kubernetes along with experienced in Infrastructure as code tools and Configuration as code tools, Terraform, Ansible. Well-versed in CICD tool - Jenkins. Have hands-on experience with various AWS and Azure services. I really enjoy learning new things and connecting with people across a range of industries, so don't hesitate to reach out if you'd like to get in touch.
Security in Kubernetes
View Certificates
The manifests files (kubernetes configuration files) are stored at location
/etc/kubernetes/manifests/The certificate files (.crt and .key) are stored at location
/etc/kubernetes/pki/The etcd and ca certificate files (.crt and .key) are stored at location
/etc/kubernetes/pki/etcdCommand to view the certificate information in detailed format
openssl x509 -in /etc/kubernetes/pki/apiserver.crt -text -noout
Certificates API
Command to encode certificate (.csr) into base64 format to use it in the CSR object
cat akshay.csr | base64 -w 0Command to approve the CSR request
kubectl certificate approve akshayCommand to deny the CSR request
kubectl certificate deny akshayCommand to get more details on a particular CSR (groups, signerName, etc.)
kubectl get csr akshay -o yamlCommand to delete the CSR request
kubectl delete csr akshay
KubeConfig
Default location of kubeconfig file is (note: file name is config)
/root/.kube/configStructure of kubeconfig file
apiVersion: v1
kind: Config
current-context: user-name@cluster-name
clusters:
- cluster:
- cluster:
contexts:
- context:
- context:
users:
- name:
- name:
Command to view the context place at any other location than default
kubectl config view --kubeconfig=my-kube-configCommand to change the current context
kubectl config --kubeconfig=/root/my-kube-config use-context researchCommand to know the current context
kubectl config --kubeconfig=/root/my-kube-config current-context
Roles and RoleBindings
Command to count the number of lines from the kubectl output
kubectl get roles --all-namespaces --no-headers | wc -lCommand to describe a particular role
kubectl describe role kube-proxy -n kube-systemCommand to check a particular operation as a another user
kubectl auth can-i get pods --as dev-userCommand to describe a particular rolebinding
kubectl describe rolebinding kube-proxy -n kube-system
ClusterRole and ClusterRoleBindings
Command to count the number of lines of clusterroles from the kubectl output
kubectl get clusterroles --no-headers | wc -lCommand to count the number of lines of clusterrolebindings from the kubectl output
kubectl get clusterrolebindings --no-headers | wc -l
Service Accounts
Command to create service account
kubectl create sa <sa-name>Command to describe service account (to see the tokens mounted)
kubectl describe sa <-sa-name>Command to inspect a application (pod) to identify service account
kubectl get pod -o yamlLocation where the service account credentials available within the pod
/var/run/secrets- (you can get this by firstly running kubectl describe pod <pod-name> command and then look for
Mountsfield)
- (you can get this by firstly running kubectl describe pod <pod-name> command and then look for
Command to create a token for service account
kubectl create token <sa-name>Command to to inspect a application (pod) to identify service account
kubectl get pod -o yaml
Install Kubeadm
Installing Kubeadm =>
Installing container runtime =>
Enable IPv4 packet forwarding =>
Getting started with containerd =>
GitHub =>
Docker Engine doc =>
systemctl status containerd =>
Configuring the systemd cgroup driver =>
systemctl restart containerd =>
Installing kubeadm, kubelet and kubectl
Linux commands
Command to check the OS distribution name and release
cat /etc*-releaseCommand to remove all the content of a file after opening it using vi
:1,$dCommand to check the OS distribution name and version
cat /etc*-release
Linux Networking commands
Command to list the network interfaces for the hosts
ip linkroot@linux:~# ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: enp0s1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 link/ether c2:f6:aa:e3:7d:f5 brd ff:ff:ff:ff:ff:fCommand to list the IP addresses assigned to the network interfaces
ip addrCommand to assign/set IP address on the system/host/interface
ip addr add192.168.1.10/24 dev eth0Command to view the routing table configuration on the system
route / ip routeCommand to add entries into the routing table
ip route add 192.168.2.0/24 via 192.168.1.1Command to check if IP forwarding is enabled on host
cat /proc/sys/net/ipv4/ip_forward- If the output is 0, change it to 1 to enable the IP forwarding
DNS commands
File to put the details of host with IP and hostname on the host (local file)
/etc/hostsYou can have as many names as you want for as many servers as you want in the
/etc/hostsfile.Can have multiple hostnames for the same IP address as well.
root@linux:~# cat /etc/hosts
=> 192.168.1.10 web
File to put the details of host with IP and hostname on the host (DNS resolution configuration file)
/etc/resolv.confroot@linux:~# cat /etc/resolv.conf => 192.168.1.10 web Foward All to 8.8.8.8File to change the sequence used by the system to resolve or map a hostname to IP addresses
/etc/nsswitch.confroot@linux:~# cat /etc/nsswitch.conf => ... hosts: file dns ...
change the default namespace
kubectl config set-context --current --namespace alpha
Networking commands
Command to view the
bridgetype interfaceip addr show type bridgeIf you were to ping google from the
controlplanenode, which route does it take?ip route- and look for default route ipView the port the
kube-scheduleris listening onnetstat -nplt | grep schedulernetstat -nplt | grep etcdETCD is listening on two ports. Which of these have more client connections established?
netstat -anp | grep etcd | grep 2379 | wc -lCommand to get the container-runtime endpoints defined in kubelet
ps aux | grep kubelet | grep container-runtimePath configured with all binaries of CNI supported plugins
/opt/cni/binPath to view the configured CNI plugin on cluster
/etc/cni/net.dRange of IP addresses configured for PODs on this cluster
kubectl logs <weave-pod-name> weave -n kube-systemand look foripalloc-rangeIP Range configured for the services within the cluster
cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep cluster-ip-rangeType of proxy is the
kube-proxyconfigured to usekubectl logs <kube-proxy-pod-name> -n kube-systemIdentify the type of DNS
kubectl get pods -Aand look for dns podPath of configuration file for configuring the CoreDNS service
/etc/coredns/CorefileCorefile passed to coredns service
as configmapRoot domain/zone configured for this kubernetes cluster
kubectl describe configmap coredns -n kube-system