#Arsitektur Kubernetes
1. Kubernetes Master => Yang Manage
~ Kube-apiserver interaksi dengan kubernetes cluster
~ etcd sebagai database utk menyimpan data kubernetes cluster
~ Kube-scheduler memperhatikan aplikasi yg berjalan dan meminta node utk menjalakan aplikasi
~ Kube-controller-manager bertugas melakukan kontrol terhadap kubernetes cluster
~ Cloud-controller-manager bertugas melakukan kontrol terhadap interaksi dgn cloud provider mirip seperti Kube-controller-manager
2. Kubernetes Nodes/worker (Kubelet, Kube-proxy, Container manager) => Semua aplikasi jalan di worker.
~ Kubelet berjalan di setiap node, memastikan aplikasi berjalan di node
~ Kube-proxy berjalan di setiap node, sebagai proxy terhadap arus network yg masuk ke aplikasi dan sebagai load balancer juga
~ Container-manager berjalan di setiap node, bertugas sebagai container manager (docker, containerd, cri-o, rklet)
#Alur Kerja Menggunakan Kubernetes
Developer -> Configuration File -> Kubernetes Master -> Kubernetes Nodes/Workers
#Node
## kubectl get nodes (Melihat semua node)
## kubectl describe node namanode (Melihat detail node)
#Pod (Berisi satu atau lebih container, pod adalah aplikasi kita yang running di kubernetes cluster)
====pod.yaml====
apiVersion: v1
kind: Pod
metadata:
name: pod-name
spec:
contianers:
- name: container-name
image: image-name
ports:
- containerPort: 80
===End===
## kubectl get pod (Melihat semua pod)
## kubectl describe pod namapod (Melihat detail pod)
## kubectl create -f filepod.yaml (Membuat pod, mengsubmit yaml file ke kubernetes cluster)
Example: kubectl create -f nginx.yaml
## kubectl get pod -o wide (Melihat lebih detail pod)
## kubectl port-forward namapod portAkese:portPod (Hanya utk testing saja)
## kubectl delete pod namapod (Menghapus pod)
## kubectl delete pod -l key=vlaue (Menghapus pod dengan selektor label)
## kubectl delete pod --all --namespace namanamespace (Menghapus semua pod di namespace)
## kubectl delete pod --all (Menghapus semua pod)
----------------
#Label (Untuk informasi tambahan seperti tanda, file dll)
===pod-with-label.yaml====
apiVersion: v1
kind: Pod
metadata:
name: nginx-with-label
labels:
team: finance
version: 1.4.5
environment: production
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
==End===
##kubectl get pods --show-labels (Melihat label)
##kubectl labelspod namapod
----------------
#Annotation (menambahkan informasi tambahan dalam ukuran besar)
===nginx-with-annotation.yaml===
apiVersion: v1
kind: Pod
metadata:
name: nginx-with-annotation
labels:
team: product
version: 1.0.1
environment: development
annotations:
description: Ini adalah aplikasi yang di buat oleh tim product
apapun: apaun itu ....
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
===End===
## kubectl describe pod namapod
## kubectl annotate pod namapod
----------------
#Namespace (memisahkan atau membuat grup jika resource di kubernetes sudah terlalu banyak. Nama resource bisa sama jika beberapa
#di namespace yg berbeda)
===finance-namespace.yaml===
apiVersion: v1
kind: Namespace
metadata:
name: finance
==End===
## kubectl get namespace (Melihat namespace)
## kubectl get pod --namespace namanamespace (Melihat pod di namespace)
## kubectl create -f namafilenamespace.yaml (Membuat namespace)
## kubectl create -f namafile.yaml --namespace namanamespace (Membuat pod di Namespace)
## kubectl delete namespace namanamespace (Menghapus namespace)
---------------
#Probe (Pengecekan)
1. Liveness probe => health check jika pod bermasalah pod akan di restart
2. Readiness probe => health check jika pod bermasalah traffic pod akan di stop. Jika pod sehat, pod diperbolehkan
mengirim traffic kembali
3. Startup probe => health check saat awal saja, memastikan aplikasi pod health
4. Mekanisme pengecekan menggunakan HTTP get, TCP socket, Command exec
===nginx-with-probe.yaml===
apiVersion: v1
kind: Pod
metadata:
name: nginx-with-probe
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /404
port: 80
initialDelaySeconds: 5
periodSeconds: 5
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
==End===
## kubectl describe pod namapod (Melihat detail liveness dll)
----------------
#Replication Controller (Bertugas memastikan pod selalu berjalan)
===nginx-rc.yaml===
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-rc
spec:
replicas: 3
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
===End===
## kubectl get replicationcontroller (Melihat replication controller)
## kubectl get rc (Melihat replication controller)
## kubectl delete rc namarc (Menghapus rc beserta pod nya)
## kubectl delete rm namarc --cascade=false atau --cascade=orphan (Hanya menghapus rc saja)
----------------
#Replica Set (Sama seperti relication controller, sekarang yg di gunakan adalah replica set, replication controller tidak
#direkomedasikan. Replica set label selectornya lebih expressive)
===nginx-rs.yaml===
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
===End===
## kubectl get rs (Melihat replica set)
## kubectl delete rs namarc (Menghapus rc beserta pod nya)
## kubectl delete rm namarc --cascade=false atau --cascade=orphan (Hanya menghapus rs saja)
----------------
#Match Expression
1. In => value label harus ada di value in
2. NotIn => value label tidak boleh ada di value in
3. Exists => label harus ada
4. NotExist => label tidak boleh ada
===nginx-rs-match-expression.yaml===
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx
spec:
replicas: 3
selector:
matchExpressions:
- key: app
operator: In
values:
- nginx
- key: env
operator: In
values:
- prod
- qa
- dev
template:
metadata:
name: nginx
labels:
app: nginx
env: prod
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
===End===
----------------
#Daemon Set (Digunakan untuk menentukan di node mana pod berjalan, by default menjalankan pod di setiap node yang
#ada di kubernetes cluster)
===daemon-nginx.yaml===
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemon-nginx
labels:
name: daemon-nginx
spec:
selector:
matchLabels:
name: daemon-nginx
template:
metadata:
name: daemon-nginx
labels:
name: daemon-nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 0
periodSeconds: 10
failureThreshold: 3
successThreshold: 1
timeoutSeconds: 1
===End===
## kubectl get daemonsets (Melihat semua daemon set)
## kubectl delete daemonset namadaemonset (Menghapus daemon set)
----------------
#Job (Digunakan untuk menjalankan Pod yang hanya butuh berjalan sekali, lalu berhenti, contoh penggunaan seperti backup atau
#restore database, export atau import data, dll)
===job-nodejs.yaml===
apiVersion: batch/v1
kind: Job
metadata:
name: nodejs-job
spec:
completions: 4
parallelism: 2
template:
spec:
restartPolicy: Never
containers:
- name: nodejs-job
image: khannedy/nodejs-job
===End===
## kubectl get jobs (Melihat semua job)
## kubectl delete job namajob (Menghapus job)
## kubectl describe job namajob (Melihat detail job)
----------------
#Cron Job (Digunakan untuk menjalankan Pod yang berjalan sesuai dengan waktu yang di tentukan secara berulang-ulang, contoh
#penggunaan membackup data secara berkala)
===cronjob-nodejs.yaml===
apiVersion: batch/v1
kind: CronJob
metadata:
name: nodejs-cronjob
labels:
name: nodejs-cronjob
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
metadata:
name: nodejs-cronjob
labels:
name: nodejs-cronjob
spec:
restartPolicy: Never
containers:
- name: nodejs-cronjob
image: khannedy/nodejs-job
===End===
## kubectl get cronjobs (Melihat semua cron job)
## kubectl delete cronjobs namacronjob (Menghapus cron job)
## kubectl describe cronjob namacronjob (Melihat detail cron jon)
----------------
#Node Selector (Memilih pod mau berjalan di node mana. contoh penggunaan kebutuhan khusus sepert GPU)
===nginx-node-selector.yaml===
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
nodeSelector:
ssd: "true"
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
===End===
## kubectl label node namanode key=value (Menambahkan label di node)
----------------
#ALL
## kubectl get all (Melihat semua resource di namespace default)
## kubectl get all --namespace namanamespace (Melihat semua resource di namespace tertentu)
## kubectl delete all --all (Menghapus semua resource di default namespace)
## kubectl delete all --all --namespace namanamespace (Menghapus semua resource di namespace tertentu)
#Service (Digunakan untuk membuat satu gerbang untuk mengakses satu pod atau lebih, service memiliki ip address dan
#port yg tidak pernah berubah selama service itu ada)
===service-nginx.yaml===
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
name: nginx
template:
metadata:
name: nginx
labels:
name: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
name: nginx
ports:
- port: 8080
targetPort: 80
---
apiVersion: v1
kind: Pod
metadata:
name: curl
labels:
name: curl
spec:
containers:
- name: curl
image: khannedy/nginx-curl
===End===
## kubectl get services (Melihat service)
## kubectl delete service namaservice (Menghapus service)
## kubectl exec nama-pod -it --/bin/bash (Mengakses service dari dalam cluster)
curl http://cluster-ip:port/ (Contoh untuk web)
## kubectl exec nama-pod -- env (Melihat Environment Variable)
## nama-service.nama-namespace.svc.cluster.local (Mengakses service menggunakan DNS)
## kubectl get endpoints (Melihat semua endpoint)
## kubectl get endpoints nama-service (Melihat satu service endpoint)
## kubectl describe service nama-service (Melihat detail service)
-----------------
#External Service (Digunakan sebagai gateway untuk internal pod, bisa juga digunakan sebagai gateway untuk aplikasi eksternal yg
#berada di luar kubernetes cluster)
===service-example.yaml===
apiVersion: v1
kind: Service
metadata:
name: example-service
labels:
name: example-service
spec:
type: ExternalName
externalName: example.com
ports:
- port: 80
---
apiVersion: v1
kind: Pod
metadata:
name: curl
labels:
name: curl
spec:
containers:
- name: curl
image: khannedy/nginx-curl
===End===
----------------
#Mengekspos Service (Digunakan untuk mengakses aplikasi dari luar cluster/public)
## Tipe service
1. ClusterIP => Mengekspos service di dalam internal kubernetes cluster
2. ExternalName => Memetakan service ke externalName (misal: example.com)
3. NodePort => Mengekspos service pada setiap IP node dan port yang sama. Kita dapat mengakses service dengan tipe ini dari
luar cluster melalui NodeIP:NodePort
4. LoadBalancer => Mengekspos service secara eksternal dengan menggunakan LoadBalancer yg disediakan oleh penyedia layanan cloud
5. Ingress => Mengekspos service menggunakan domain, ingress hanya mendukung protocol HTTP
===service-nginx-nodeport.yaml===
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
name: nginx
template:
metadata:
name: nginx
labels:
name: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
selector:
name: nginx
ports:
- port: 80
targetPort: 80
nodePort: 30001
==End===
===service-nginx-loadbalancer.yaml===
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
name: nginx
template:
metadata:
name: nginx
labels:
name: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: LoadBalancer
selector:
name: nginx
ports:
- port: 80
targetPort: 80
===End===
===cat service-nginx-ingress.yaml===
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
name: nginx
template:
metadata:
name: nginx
labels:
name: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
name: nginx
ports:
- port: 80
targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
labels:
name: nginx-ingress
spec:
rules:
- host: nginx.khannedy.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
===End===
## minikube service nama-service (Melihat NodePort di minikube)
## kubectl get ingresses (Melihat ingress)
## kubectl delete ingress namaingress (Mengapus ingress)
## minikube ip (Untuk melihat IP minikube)
----------------
#Multi Container Pod
===multi-container-pod.yaml===
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
name: nginx
template:
metadata:
name: nginx
labels:
name: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
- name: nodejs-web
image: khannedy/nodejs-web
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
name: nginx
ports:
- port: 8080
targetPort: 80
name: nginx
- port: 3000
targetPort: 3000
name: nodejs-web
---
apiVersion: v1
kind: Pod
metadata:
name: curl
labels:
name: curl
spec:
containers:
- name: curl
image: khannedy/nginx-curl
===End===
----------------
#Volume
## Jenis Volume
1. emptyDir => Direktori sederhana yang kosong (note: pod mati folder kosong kembali, utk test)
2. hostPath => Digunakan untuk men-sharing direktori di node ke pod
3. gitRepo => Direktori yang dibuat pertama kali dengan meng-clone git repository
4. nfs => Sharing network file system
5. dll
===volume.yaml===
apiVersion: v1
kind: Pod
metadata:
name: nodejs-writer
labels:
name: nodejs-writer
spec:
volumes:
- name: html
emptyDir: {}
containers:
- name: nodejs-writer
image: khannedy/nodejs-writer
volumeMounts:
- mountPath: /app/html
name: html
===End===
----------------
#Sharing Volume
===sharing-volume.yaml===
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
name: nginx
template:
metadata:
name: nginx
labels:
name: nginx
spec:
volumes:
- name: html
emptyDir: {}
containers:
- name: nodejs-writer
image: khannedy/nodejs-writer
volumeMounts:
- mountPath: /app/html
name: html
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- mountPath: /usr/share/nginx/html
name: html
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
selector:
name: nginx
ports:
- port: 8080
targetPort: 80
nodePort: 30001
===End===
----------------
#Environment Variable (Digunakan untuk konfigurasi aplikasi seperti konfig database dll)
===environment-variable.yaml===
apiVersion: v1
kind: Pod
metadata:
name: nodejs-writer
labels:
name: nodejs-writer
spec:
volumes:
- name: html
emptyDir: {}
containers:
- name: nodejs-writer
image: khannedy/nodejs-writer
volumeMounts:
- mountPath: /app/folder-from-env
name: html
env:
- name: HTML_LOCATION
value: /app/folder-from-env
===End===
----------------
#Config Map (Digunakan untuk memisahkan konfigurasi contohnya environment di dev, prod, dll)
===configmap.yaml===
apiVersion: v1
kind: ConfigMap
metadata:
name: nodejs-env-config
data:
APPLICATION: My Cool Application
VERSION: 1.0.0
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nodejs-env
spec:
replicas: 3
selector:
matchLabels:
name: nodejs-env
template:
metadata:
name: nodejs-env
labels:
name: nodejs-env
spec:
containers:
- name: nodejs-env
image: khannedy/nodejs-env
ports:
- containerPort: 3000
envFrom:
- configMapRef:
name: nodejs-env-config
---
apiVersion: v1
kind: Service
metadata:
name: nodejs-env-service
spec:
type: NodePort
selector:
name: nodejs-env
ports:
- port: 3000
targetPort: 3000
nodePort: 30001
==End===
## kubectl get configmaps (Melihat configmap)
## kubectl describe configmap namaconfigmap (Melihat detail configmap)
## kubectl delete configmap namaconfigmap (Mengapus configmap)
----------------
#Secret (Digunakan untuk konfigurasi yang bersifat sensitif seperti username, password dll)
===secret.yaml===
apiVersion: v1
kind: ConfigMap
metadata:
name: nodejs-env-config
data:
APPLICATION: My Cool Application
---
apiVersion: v1
kind: Secret
metadata:
name: nodejs-env-secret
stringData:
VERSION: 1.0.0
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nodejs-env
spec:
replicas: 3
selector:
matchLabels:
name: nodejs-env
template:
metadata:
name: nodejs-env
labels:
name: nodejs-env
spec:
containers:
- name: nodejs-env
image: khannedy/nodejs-env
ports:
- containerPort: 3000
envFrom:
- configMapRef:
name: nodejs-env-config
- secretRef:
name: nodejs-env-secret
---
apiVersion: v1
kind: Service
metadata:
name: nodejs-env-service
spec:
type: NodePort
selector:
name: nodejs-env
ports:
- port: 3000
targetPort: 3000
nodePort: 30001
===End===
## kubectl get secrets (Melihat secret)
## kubectl describe secrets namasecret (Melhat detail secret)
## kubectl delete secret namasecret (Menghapus secret)
#Downward API (Digunakan untuk mengambil informasi seputar pod dan node melalui environment variable)
## metadata
1. requests.cpu => Jumlah CPU yang di request
2. requests.memory => Jumlah memory yang di request
3. limits.cpu => Jumlah limit maksimal CPU
4. limits.memory => Jumlah limit maksimal memory
5. metadata.name => Nama pod
6. metadata.namespace => Namespace pod
7. metadata.uid => Id pod
8. metadata.labels['<KEY>'] => Label pod
9. metadata.annotation['<KEY>'] => Annotationpod
10. status.podIP => IP address pod
11. spec.serviceAccountName => Nama service account pod
12. spec.nodeName => Nama node
13. status.hostIP => IP address node
===downward-api.yaml===
apiVersion: v1
kind: ConfigMap
metadata:
name: nodejs-env-config
data:
APPLICATION: My Cool Application
VERSION: 1.0.0
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nodejs-env
spec:
replicas: 3
selector:
matchLabels:
name: nodejs-env
template:
metadata:
name: nodejs-env
labels:
name: nodejs-env
spec:
containers:
- name: nodejs-env
image: khannedy/nodejs-env
ports:
- containerPort: 3000
envFrom:
- configMapRef:
name: nodejs-env-config
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: POD_NODE
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: POD_NODE_IP
valueFrom:
fieldRef:
fieldPath: status.hostIP
---
apiVersion: v1
kind: Service
metadata:
name: nodejs-env-service
spec:
type: NodePort
selector:
name: nodejs-env
ports:
- port: 3000
targetPort: 3000
nodePort: 30001
===End===
----------------
#Manage Kubernetes Object
## Imperative Management
1. kubectl create -f namafile.yaml => Membuat kubernetes object
2. kubectl replace -f namafile.yaml => Mengupdate kubernetes obejct
3. kubectl get -f namafile.yaml -o yaml/json => Melihat kubernetes obejct
4. kubectl delete -f namafile.yaml => Menghapus kubernetes obejct
## Declarative Management
1. kubectl apply -f namafile.yaml => Membuat atau mengupdate kubernetes object
#Deployment (yaitu resource utk melakukan deployment aplikasi dan update aplikasi secara declarative menggunakan file konfigurasi)
===deployment.yaml===
apiVersion: apps/v1
kind: Deployment
metadata:
name: nodejs-web
labels:
name: nodejs-web
spec:
replicas: 3
selector:
matchLabels:
name: nodejs-web
template:
metadata:
name: nodejs-web
labels:
name: nodejs-web
spec:
containers:
- name: nodejs-web
image: khannedy/nodejs-web:1
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: nodejs-web-service
spec:
type: NodePort
selector:
name: nodejs-web
ports:
- port: 3000
targetPort: 3000
nodePort: 30001
===End===
## kubectl apply -f deployent.yaml (Membuat/update deployment)
## kubectl get deployments (Melihat semua deployment)
## kubectl describe deployment namadeployment (Melihat detail deployment)
----------------
#Deployment update
===deployment-update.yaml===
apiVersion: apps/v1
kind: Deployment
metadata:
name: nodejs-web
labels:
name: nodejs-web
spec:
replicas: 3
selector:
matchLabels:
name: nodejs-web
template:
metadata:
name: nodejs-web
labels:
name: nodejs-web
spec:
containers:
- name: nodejs-web
image: khannedy/nodejs-web:2
ports:
- containerPort: 3000
===End===
----------------
#RollBack Deployment
## Kubernetes RollOut
1. kubectl rollout history object name(deployment, dll) => Melihat history rollout
2. kubectl rollout pause object name(deployment, dll) => Menandai sebagai pause
3. kubectl rollout resume object name(deployment, dll) => Resume pause
4. kubectl rollout restart object name(deployment, dll) => Merestart rollout
5. kubectl rollout status object name(deployment, dll) => Melihat status rollout
6. kubectl rollout undo object name(deployment, dll) => Undo ke rollout sebelumnya
## kubectl rollout undo deployment deployment.yaml (rollback aplikasi ke versi sebelumnya)
===deployment-update-again.yaml===
apiVersion: apps/v1
kind: Deployment
metadata:
name: nodejs-web
labels:
name: nodejs-web
spec:
replicas: 3
selector:
matchLabels:
name: nodejs-web
template:
metadata:
name: nodejs-web
labels:
name: nodejs-web
spec:
containers:
- name: nodejs-web
image: khannedy/nodejs-web:3
ports:
- containerPort: 3000
===End===
----------------
#Persistent Volume (flow: Create persistent volume -> claim -> use to pod)
## kubectl get pv (Melihat persistent volume)
## kubectl describe pv namapv (Melihat detail persistent volume)
## kubectl delete pv namapv (Menghapus persistent volume)
## kubectl get pvc (Melihat persistent volume claim)
## kubectl describe pvc namapvc (Melhat detail peristent volume claim)
## kubectl delete pvc namapvc (Menghapus persistent volume claim)
===persistent-volume.yaml===
apiVersion: v1
kind: PersistentVolume
metadata:
name: nodejs-writer-volume
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
hostPath:
path: /data/location
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nodejs-writer-volume-claim
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
name: nodejs-writer
labels:
name: nodejs-writer
spec:
volumes:
- name: html
persistentVolumeClaim:
claimName: nodejs-writer-volume-claim
containers:
- name: nodejs-writer
image: khannedy/nodejs-writer
volumeMounts:
- mountPath: /app/html
name: html
===End===
----------------
#Statefulset (adalah object di kubernetes untuk memanage aplikasi jenis stateful)
## kubectl get statefulsets (Melihat statefulset)
## kubectl describe statfulset namastatefulset (Melihat detail statefulset)
## kubectl delete statefulset namastatefulset (Menghapus statefulset)
===statefulset.yaml===
apiVersion: v1
kind: PersistentVolume
metadata:
name: nodejs-stateful-volume
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
hostPath:
path: /data/location
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nodejs-stateful
labels:
name: nodejs-stateful
spec:
# https://github.com/kubernetes/kubernetes/issues/69608
serviceName: nodejs-stateful-service
replicas: 3
selector:
matchLabels:
name: nodejs-stateful
volumeClaimTemplates:
- metadata:
name: nodejs-stateful-volume-claim
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: 1Gi
template:
metadata:
name: nodejs-stateful
labels:
name: nodejs-stateful
spec:
containers:
- name: nodejs-stateful
image: khannedy/nodejs-stateful
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
volumeMounts:
- mountPath: /app/data
name: nodejs-stateful-volume-claim
==End===
----------------
#Kubernetes Dashboard
## minikube addons enable dashboard (Enable dashboard)
## minikube dashboard (Membuka dashboard)
----------------
#Computational Resources (Limit cpu dan memory)
===resources.yaml===
apiVersion: apps/v1
kind: Deployment
metadata:
name: nodejs-web
labels:
name: nodejs-web
spec:
replicas: 3
selector:
matchLabels:
name: nodejs-web
template:
metadata:
name: nodejs-web
labels:
name: nodejs-web
spec:
containers:
- name: nodejs-web
image: khannedy/nodejs-web
ports:
- containerPort: 3000
resources:
requests:
cpu: 1000m
memory: 1000Mi
limits:
cpu: 1000m
memory: 1000Mi
---
apiVersion: v1
kind: Service
metadata:
name: nodejs-web-service
spec:
type: NodePort
selector:
name: nodejs-web
ports:
- port: 3000
targetPort: 3000
nodePort: 30001
==End===
----------------
#Horizontal Pod Autoscaler (Scaling otomatis secara horizontal dengan cara menambah pod baru dan menurunkan secara otomatis jika
#di perlukan)
## minikube addons enable metrics-server (Enable metrics server)
## kubectl get hpa (Melihat hpa)
## kubectl describe hpa namahpa (Melihat detail hpa)
## kubectl delete hpa namahpa (Menghapus hpa)
===horizontal-pod-autoscaler.yaml===
apiVersion: apps/v1
kind: Deployment
metadata:
name: nodejs-web
labels:
name: nodejs-web
spec:
selector:
matchLabels:
name: nodejs-web
template:
metadata:
name: nodejs-web
labels:
name: nodejs-web
spec:
containers:
- name: nodejs-web
image: khannedy/nodejs-web:1
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: nodejs-web-service
spec:
type: NodePort
selector:
name: nodejs-web
ports:
- port: 3000
targetPort: 3000
nodePort: 30001
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: hpa-name
spec:
minReplicas: 3
maxReplicas: 5
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nodejs-web
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 70
===End===
Source: PZN
Note: Hanya sebagai catatan saya
0 komentar:
Posting Komentar