It may seem daunting to run kubernetes commands, but once you learned its main components, the rest is just about learning the syntax to tell kubernetes what you want to do.
1. Main Components of K8s
We all know K8s is an orchestration tool, but what does that mean exactly? The most basic understanding can be: it allows you to define what resources you need, and how they are allocated to different workloads. K8s not only lets you set those things up, it helps you to monitor and maintain all the services you desires. To own this power, K8S has its own pre-defined structure so you can write scripts based on it.
(I drew this image myself for the purpose of not overwhelming you, but please check out their official website for the whole picture. For example, there are 5 components in the master, but it can be managed by AWS EKS so you don’t actually have to touch them)
I know, it looks very complex, but once you understand what each component does and how they rely on each other, the whole system will look natural to you. So, just bear with me and let me walk you through it!
In the beginning, you can think the whole system has two types of objects: the components(container, pod, deployment, replicaset) who service a function in its own and the components(service and master) who help the former components to work together harmoniously.
For a web server, you need (1) the frontend and the backend (2) they talk to each other (3) fontend can be accessed by the public (4) backend can access external database. Again, the (1) is the first type of components and (2)(3)(4) are the second type of components.
Now you are clear about all the components we need for a web server, how do we tell K8s to set this up for us?
2. Two Approaches To Give K8s Commands
There are two ways to communicate with K8S:
(1) Declarative VS Imperative
Declarative means you tell k8s what you want, and you don’t care how it gets it for you, it will always maintain the desired state itself. If you have a clear defined system already, you can use this.
Imperative means you tell k8s exactly what to do. This means it only does it when you ask it so. This action will only happen once and k8s won’t be smart as the declarative way to maintain anything for you.
The confusing part is some of k8s command lines is designed for declarative way, some are for imperative way. Without reading the doc or other experience, you cannot really tell which command you are using just based on the syntax of the command.
Good news is we don’t need to worry about this, I just want you guys know there is such thing and it can be very handy when you want to go deeper!
(2) Yaml VS Command line
If you have used yaml before, yes, it works the same way! It is a preferred approach if you want to give any command with some complexity simply because it is written in a file, it then becomes easier for you to check everything. Also, there are some functions of k8s simply are not supported by command line (e.x. customizing some port number). So it’s your time to pick up yaml! It’s similar to JSON, just has a bit more syntaxt and data structure, but all are very basic and readable once you are willing to spend some time to learn!
3. Top 10 Frequently Used Commands
- eksctl create cluster/nodegroup => create the cluster or the nodegroup
- eksctl utils associate-iam-oidc-provider => associate OIDC provider so you can use AWS IAM roles later down the road
- kubectl get pods/service/deployment/nodes/quota/limits
- kubectl get nodes -o wide => when you need to see your external IP
- kubectl create -f filename.yml => declarative
- kubectl apply -f filename.yml => imperative
- kubectl apply -f folder-name/ => apply all the yaml files in this folder
- kubectl describe pods/svc/nodes/deployment/limit/quota => current details of your object
- kubectl get logs your-pod-id => details of what you did to this object
- kubectl delete pod/svc/deployment name-of-your-object => if you give commands in declarative way, k8s will delete then recreate to maintain the desired state.