Yaml üzerinden kaynaklarımızı oluşturmak için Apply komutunu kullanacağız, YAML’i elle oluşturabiliriz ancak onu bizim için oluşturmak için dry-run=client kullanabiliriz. Bu işlem karmaşık deploymentları taşımak için şablon olarak kullanılabilir.
kubectl create deployment hello-world \ --image=gcr.io/google-samples/hello-app:1.0 \ --dry-run=client -o yaml | more
apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: hello-world name: hello-world spec: replicas: 1 selector: matchLabels: app: hello-world strategy: {} template: metadata: creationTimestamp: null labels: app: hello-world spec: containers: - image: gcr.io/google-samples/hello-app:1.0 name: hello-app resources: {} status: {}
Deployment işlemini yaml dosyasına yazalım,
root@k8s-c1-cp1:~# kubectl create deployment hello-world \ --image=gcr.io/google-samples/hello-app:1.0 \ --dry-run=client -o yaml > deployment.yaml root@k8s-c1-cp1:~#
Yaml dosyasının içeriğini görüntüleyelim, dağıtımın tamanını gösterir.
root@k8s-c1-cp1:~# more deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: hello-world name: hello-world spec: replicas: 1 selector: matchLabels: app: hello-world strategy: {} template: metadata: creationTimestamp: null labels: app: hello-world spec: containers: - image: gcr.io/google-samples/hello-app:1.0 name: hello-app resources: {} status: {} root@k8s-c1-cp1:~#
Deploymenti yaml dosyamızı kullanarak oluşturalım,
kubectl apply -f deployment.yaml
root@k8s-c1-cp1:~# kubectl apply -f deployment.yaml deployment.apps/hello-world created root@k8s-c1-cp1:~#
Servis için yaml oluşturalım,
kubectl expose deployment hello-world \ --port=80 --target-port=8080 \ --dry-run=client -o yaml | more
apiVersion: v1 kind: Service metadata: creationTimestamp: null labels: app: hello-world name: hello-world spec: ports: - port: 80 protocol: TCP targetPort: 8080 selector: app: hello-world status: loadBalancer: {}
Servis yaml manifestosunu bir dosyaya yazalım,
kubectl expose deployment hello-world \ --port=80 --target-port=8080 \ --dry-run=client -o yaml > service.yaml
Yaml dosyasının içeriği Servis tanımını gösterir,
root@k8s-c1-cp1:~# more service.yaml apiVersion: v1 kind: Service metadata: creationTimestamp: null labels: app: hello-world name: hello-world spec: ports: - port: 80 protocol: TCP targetPort: 8080 selector: app: hello-world status: loadBalancer: {} root@k8s-c1-cp1:~#
Servisi ilgili yaml dosyasını kullanarak oluşturalım,
kubectl apply -f service.yaml
Mevcut durumumuzu, Deployment, ReplicaSet, Poda ve servislere göz gezdirelim.
kubectl get all
root@k8s-c1-cp1:~# kubectl get all NAME READY STATUS RESTARTS AGE pod/hello-world-5457b44555-96dxx 1/1 Running 0 2m36s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/hello-world ClusterIP 10.105.238.150 <none> 80/TCP 31s service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11h NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/hello-world 1/1 1 1 2m36s NAME DESIRED CURRENT READY AGE replicaset.apps/hello-world-5457b44555 1 1 1 2m36s root@k8s-c1-cp1:~#
Scale up ile deploymentımızı code içerisinde genişletelim,
vi deployment.yaml Change spec.replicas from 1 to 20 replicas: 20
İlgili datayı kaydedip çıkalım. Apply ile Deploymentımızı güncelleyelim.
kubectl apply -f deployment.yaml
Ve deploymentımız mevcut yapılandırılmasını kontrol edelim 20/20 olarak replicasetimizi görmeliyiz.
kubectl get deployment hello-world kubectl get pods | more
root@k8s-c1-cp1:~# kubectl get deployment hello-world NAME READY UP-TO-DATE AVAILABLE AGE hello-world 20/20 20 20 5m25s root@k8s-c1-cp1:~# kubectl get pods | more NAME READY STATUS RESTARTS AGE hello-world-5457b44555-2m2p5 1/1 Running 0 33s hello-world-5457b44555-5crqx 1/1 Running 0 33s hello-world-5457b44555-62ns7 1/1 Running 0 33s hello-world-5457b44555-6ztvd 1/1 Running 0 33s hello-world-5457b44555-7c6qq 1/1 Running 0 33s hello-world-5457b44555-7w4fn 1/1 Running 0 33s hello-world-5457b44555-8v7q4 1/1 Running 0 33s hello-world-5457b44555-96dxx 1/1 Running 0 5m29s hello-world-5457b44555-9lhkh 1/1 Running 0 33s hello-world-5457b44555-cm2nq 1/1 Running 0 33s hello-world-5457b44555-flp2z 1/1 Running 0 33s hello-world-5457b44555-fz25s 1/1 Running 0 33s hello-world-5457b44555-fz5vn 1/1 Running 0 33s hello-world-5457b44555-g2hk6 1/1 Running 0 33s hello-world-5457b44555-gh8bt 1/1 Running 0 33s hello-world-5457b44555-jrz86 1/1 Running 0 33s hello-world-5457b44555-ps488 1/1 Running 0 33s hello-world-5457b44555-sskgr 1/1 Running 0 33s hello-world-5457b44555-t5tzb 1/1 Running 0 33s hello-world-5457b44555-zjzfz 1/1 Running 0 33s root@k8s-c1-cp1:~#
Http request atarak Load Balancing hizmetinin nasıl çalıştığını gözlemleyelim,
kubectl get service hello-world curl http://$SERVICEIP:PORT
root@k8s-c1-cp1:~# kubectl get service hello-world NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello-world ClusterIP 10.105.238.150 <none> 80/TCP 5m41s root@k8s-c1-cp1:~# curl http://10.105.238.150 Hello, world! Version: 1.0.0 Hostname: hello-world-5457b44555-5crqx root@k8s-c1-cp1:~# curl http://10.105.238.150 Hello, world! Version: 1.0.0 Hostname: hello-world-5457b44555-sskgr root@k8s-c1-cp1:~# curl http://10.105.238.150 Hello, world! Version: 1.0.0 Hostname: hello-world-5457b44555-5crqx root@k8s-c1-cp1:~# curl http://10.105.238.150 Hello, world! Version: 1.0.0 Hostname: hello-world-5457b44555-gh8bt root@k8s-c1-cp1:~# curl http://10.105.238.150 Hello, world! Version: 1.0.0 Hostname: hello-world-5457b44555-g2hk6 root@k8s-c1-cp1:~# curl http://10.105.238.150 Hello, world! Version: 1.0.0 Hostname: hello-world-5457b44555-zjzfz root@k8s-c1-cp1:~#
Kubectl ile kaynakları anında düzenleyebilirz ancak bu yaml ımıza hemen yansımaz ancak bu değişiklik etcd de kalıcıdır. Cluster deposunda test için 20 yi 30 olarak güncelleyelim,
kubectl edit deployment hello-world
Deployment 20 den 30 a yükseltilmiş ve 30 adet pod oluşmuştur,
root@k8s-c1-cp1:~# kubectl get deployment hello-world NAME READY UP-TO-DATE AVAILABLE AGE hello-world 30/30 30 30 13m root@k8s-c1-cp1:~#
Ayrıca scale kullanarak depyloymenti ölçeklendirebiliriz,
kubectl scale deployment hello-world --replicas=40 kubectl get deployment hello-world
root@k8s-c1-cp1:~# kubectl scale deployment hello-world --replicas=40 deployment.apps/hello-world scaled root@k8s-c1-cp1:~# kubectl get deployment hello-world NAME READY UP-TO-DATE AVAILABLE AGE hello-world 40/40 40 40 14m root@k8s-c1-cp1:~#
Deploymenti silip, her şeyi kaldıralım,
kubectl delete deployment hello-world kubectl delete service hello-world kubectl get all
oot@k8s-c1-cp1:~# kubectl delete deployment hello-world deployment.apps "hello-world" deleted root@k8s-c1-cp1:~# kubectl delete service hello-world service "hello-world" deleted root@k8s-c1-cp1:~# kubectl get all NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11h root@k8s-c1-cp1:~#
Başka yazımızda görüşmek üzere,