Kubedo Academy’e hoş geldiniz! Bu laboratuvarda Kubernetes mimarisi, temel kavramları ve kubectl kullanımını öğreneceksiniz.
Zorluk seviyesi: İleri
Süre: Yaklaşık 60 dakika
Önkoşullar:
- Linux 101 - Temel Linux komutları
- Docker 101 - Container temelleri
1. Kubernetes Nedir?
Kubernetes (K8s), Google tarafından geliştirilen ve şu an Cloud Native Computing Foundation (CNCF) tarafından yönetilen açık kaynaklı container orkestrasyon platformudur.
Neden Kubernetes?
| Özellik | Açıklama |
|---|---|
| Otomatik Scaling | İş yüküne göre pod sayısını ayarlar |
| Self-healing | Crash eden container’ları yeniden başlatır |
| Load Balancing | Trafiği pod’lar arasında dağıtır |
| Rolling Updates | Kesintisiz güncelleme |
| Secret Management | Hassas verileri güvenli yönetim |
| Service Discovery | Container’lar arası iletişim |
Kubernetes vs Docker Swarm
| Özellik | Kubernetes | Docker Swarm |
|---|---|---|
| Öğrenme Eğrisi | Dik | Düşük |
| Ölçeklenebilirlik | Çok iyi | İyi |
| Networking | Esnek (CNI) | Basit |
| Storage | Esnek (CSI) | Sınırlı |
| Topluluk | Çok büyük | Küçük |
2. Kubernetes Mimarisi
Cluster Yapısı
[Kubernetes Cluster]
|
┌─────────────────┴─────────────────┐
| |
[Control Plane] [Worker Nodes]
| |
┌─────┼─────┐ ┌────────┼────────┐
| | | | | |
[API] [etcd] [Scheduler] [Node 1] [Node 2] [Node 3]
| | | | |
[Controller] [CCM] [Pods] [Pods] [Pods]
Control Plane Bileşenleri
| Bileşen | Görev |
|---|---|
| kube-apiserver | Kubernetes API’sini sunar, tüm iletişim buradan |
| etcd | Cluster state’ini saklayan key-value store |
| kube-scheduler | Pod’ları hangi node’a atayacağına karar verir |
| kube-controller-manager | Controller’ları çalıştırır (ReplicaSet, Deployment vb.) |
| cloud-controller-manager | Cloud provider entegrasyonu |
Worker Node Bileşenleri
| Bileşen | Görev |
|---|---|
| kubelet | Node üzerinde pod’ları yönetir |
| kube-proxy | Network kurallarını yönetir |
| Container Runtime | Container’ları çalıştırır (containerd, CRI-O) |
3. kubectl Kurulumu
Linux
$ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
mv kubectl /usr/local/bin/macOS
brew install kubectl
Windows
choco install kubernetes-cli
Kurulum Doğrulama
$ kubectl version --client4. Minikube ile Local Cluster
Minikube, local geliştirme için tek node’lu Kubernetes cluster’ı oluşturur.
Minikube Kurulumu
# Linux
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
# macOS
brew install minikube
Cluster Başlatma
$ minikube start
minikube status
kubectl get nodesDiğer Seçenekler
| Araç | Kullanım Alanı |
|---|---|
| Minikube | Local geliştirme |
| kind | Docker içinde K8s |
| k3s | Hafif K8s (IoT, Edge) |
| kubeadm | Production cluster |
| EKS/GKE/AKS | Cloud managed |
5. kubectl Temel Komutları
Cluster Bilgisi
$ kubectl cluster-info
kubectl get nodes
kubectl get allNamespace İşlemleri
kubectl get namespaces
kubectl create namespace dev
kubectl get pods -n dev
kubectl get pods --all-namespaces
Context Yönetimi
kubectl config get-contexts
kubectl config use-context minikube
kubectl config set-context --current --namespace=dev
6. Pod
Pod, Kubernetes’in en küçük deploy edilebilir birimidir. Bir veya daha fazla container içerir.
Pod Özellikleri
- Aynı network namespace’i paylaşır (localhost ile haberleşir)
- Aynı storage volume’leri paylaşabilir
- Birlikte schedule edilir
Pod YAML Örneği
# pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.24
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Pod İşlemleri
$ kubectl apply -f pod.yaml
kubectl get pods
kubectl describe pod nginx-pod
kubectl logs nginx-pod
kubectl exec -it nginx-pod -- /bin/bash
kubectl delete pod nginx-podPod Lifecycle
Pending → Running → Succeeded/Failed
↓
CrashLoopBackOff
7. Deployment
Deployment, pod’ların declarative yönetimini sağlar. ReplicaSet aracılığıyla istenen sayıda pod çalıştırır.
Deployment YAML
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.24
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "200m"
Deployment İşlemleri
$ kubectl apply -f deployment.yaml
kubectl get deployments
kubectl get replicasets
kubectl get podsScaling
# Manuel scaling
kubectl scale deployment nginx-deployment --replicas=5
# Horizontal Pod Autoscaler
kubectl autoscale deployment nginx-deployment --min=2 --max=10 --cpu-percent=80
Rolling Update
# Image güncelleme
kubectl set image deployment/nginx-deployment nginx=nginx:1.25
# Güncelleme durumu
kubectl rollout status deployment/nginx-deployment
# Güncelleme geçmişi
kubectl rollout history deployment/nginx-deployment
# Geri alma
kubectl rollout undo deployment/nginx-deployment
8. Service
Service, pod’lara stabil bir network endpoint sağlar.
Service Türleri
| Tür | Açıklama |
|---|---|
| ClusterIP | Cluster içi erişim (varsayılan) |
| NodePort | Node IP:Port ile dış erişim |
| LoadBalancer | Cloud load balancer entegrasyonu |
| ExternalName | Harici DNS adı |
ClusterIP Service
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: ClusterIP
selector:
app: nginx
ports:
- port: 80
targetPort: 80
NodePort Service
apiVersion: v1
kind: Service
metadata:
name: nginx-nodeport
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 80
targetPort: 80
nodePort: 30080 # 30000-32767
Service İşlemleri
$ kubectl apply -f service.yaml
kubectl get services
kubectl describe service nginx-service9. ConfigMap ve Secret
ConfigMap
Yapılandırma verilerini saklar.
# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
DB_HOST: mysql
DB_PORT: "3306"
app.properties: |
server.port=8080
spring.profiles.active=prod
Secret
Hassas verileri saklar (base64 encoded).
# secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: app-secrets
type: Opaque
data:
DB_PASSWORD: c2VjcmV0MTIz # echo -n 'secret123' | base64
API_KEY: YWJjMTIzNDU2
Pod’da Kullanım
spec:
containers:
- name: app
image: myapp:1.0
env:
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: app-config
key: DB_HOST
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: app-secrets
key: DB_PASSWORD
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: app-config
10. Namespace
Namespace, cluster kaynaklarını izole etmek için kullanılır.
Varsayılan Namespace’ler
| Namespace | Açıklama |
|---|---|
default | Varsayılan namespace |
kube-system | Kubernetes sistem bileşenleri |
kube-public | Herkese açık kaynaklar |
kube-node-lease | Node heartbeat |
Namespace İşlemleri
$ kubectl create namespace production
kubectl get namespaces
kubectl get pods -n production
kubectl delete namespace production11. Labels ve Selectors
Label Ekleme
metadata:
labels:
app: nginx
environment: production
tier: frontend
Selector ile Filtreleme
kubectl get pods -l app=nginx
kubectl get pods -l 'environment in (production,staging)'
kubectl get pods -l environment!=test
12. kubectl Cheat Sheet
| Komut | Açıklama |
|---|---|
kubectl get all | Tüm kaynakları listele |
kubectl apply -f file.yaml | YAML’dan oluştur/güncelle |
kubectl delete -f file.yaml | YAML’dan sil |
kubectl logs pod-name | Pod logları |
kubectl exec -it pod-name -- sh | Pod’a bağlan |
kubectl port-forward pod-name 8080:80 | Port yönlendirme |
kubectl describe pod pod-name | Detaylı bilgi |
kubectl top pods | Kaynak kullanımı |
Konu Tekrarı - Kısa Sınav
Sonraki Adımlar
Bu temellerle Kubernetes kullanmaya başlayabilirsiniz. İleri konular için:
- Ingress: HTTP routing ve SSL termination
- Persistent Volumes: Kalıcı depolama
- StatefulSets: Stateful uygulamalar
- DaemonSets: Her node’da çalışan pod’lar
- Jobs/CronJobs: Batch işler
- RBAC: Yetki yönetimi
- Helm: Paket yönetimi
- Operators: Custom controllers