Declarative programming with Kubernetes and Idempotent.
Introduction
Thank you for clicking through to my arcticle. I've been a DevOps engineer for 2 years in dev-team of 7 engineers.
My name is MINSEOK, LEE, but I use Unchaptered as an alias on the interenet. So, you can call me anythings "MINSEOK, LEE" or "Unchaptered" to ask something.
Topic
What is the declarative?
What is the idempotent?
Why is kubernetes called declarative progamming?
What is the declarative?
Most programming writes about what to do and how to do it.
It called imperative programming. It requires you to write large amount of codes to adapt to various conditions.
According to declarative programming, you write desire state, only.
It change the current state to be same with desire state. So you just write what to do.
This is sample code of declarative codes, (ansible-playbook).
- hosts: server
remote_user: root
tasks:
- name: Install epel-release
yum: name=epel-release state=latest
- name: Install nginx web server
yum: name=nginx state=present
- name: Start nginx web server
service: name=nginx state=started
What is the idempotent?
Most programming writes as a non-idempotent.
For example, if you want to create folder as "sample_directory", you can use this command as a non-idempotent codes.
mkdir sample_directory
When the first execution, system create sample_directory.
When the second exeuction, system occured error.
mkdir: cannot create directory ‘sample_directory’: File exists
In this case, you write new codes and conditions for the situation "target directory is already exists"
But idempotent codes, all the execution will be succeed.
mkdir -p sample_directory
The idempotent is the property that performing the sample action multiple times doesn't change the result.
Why is kubernetes called declarative progamming?
The special feature is purpose of api call and final output of api call is devided using etcd.
For example, if you call the kube-apiserver for creating deployment.
The kube-apiserver record the task into etcd.
kubectl apply -f deployment.yml
And then, kube-scheduler create deployment after detecting change of etcd.
Here's an example code from deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 4
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
In the above file, the deployment.yml is declared to create 4 pods.
So, under no circumstances will kubernetes try to keep 4 pods running.
Kubernetes Control Loop
The above behavior consists of 3 steps, which we call the Kubernetes Control Loop. Or check up this files.
Conclusion
In generally, the kubernetes is developed as a declarative and parallel.
This feature ensures the system is more secure and robust.