Mevcut Clusterımız hakkında bilgi almak için ve doğru cluster üzerinde oturum açtığımızdan emin olmak için kullanmamız gereken kubectl komutu aşağıdaki gibidir. Burada aynı zamanda farklı bir clustera da bağlı olsaydık onlarında bilgisi çıkacaktı. Çıktı yanında bulunan * işareti o an aktif olarak bağlı bulunulan cluster bilgisini simgelemektedir.
kubectl config get-contexts
root@k8s-c1-cp1:~# kubectl config get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE * kubernetes-admin@kubernetes kubernetes kubernetes-admin root@k8s-c1-cp1:~#
Gerekirse ismini belirterek clusterlarımız arasında geçiş yapabilmekteyiz, geçiş işlemi için cluster ismini bilmemiz gerekmektedir. Yukarıda belirtilen komutta cluster ismini teyit ettikten sonra,
kubectl config use-context kubernetes-admin@kubernetes
root@k8s-c1-cp1:~# kubectl config use-context kubernetes-admin@kubernetes Switched to context "kubernetes-admin@kubernetes". root@k8s-c1-cp1:~#
Mevcut bağlandığımız kubernetes clusterimizın API Serveri hakkında bilgi almak için,
kubectl cluster-info
root@k8s-c1-cp1:~# kubectl cluster-info Kubernetes control plane is running at https://172.22.17.200:6443 CoreDNS is running at https://172.22.17.200:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. root@k8s-c1-cp1:~#
Clusterda bulunan API kayanklarının listesini temin etmek için,
kubectl api-resources | more
Kaynağın yapısını ve açıklamasını görmek için ise ve API referansını kullanmanın yanı sıra, yaml bildirimlerini yazmak için gerekli alanların keşfetmek için kullanılabilir,
kubectl explain pods | more
pod.spec ve pod.spec.containers da neye ihtiyacımız olduğuna daha yakından bakmak için aşağıdaki komutu kullanabiliriz,
kubectl explain pod.spec | more kubectl explain pod.spec.containers | more
YAML ile pod oluşturalım,
kubectl apply -f pod.yaml
pod.yaml içeriği,
apiVersion: v1 kind: Pod metadata: name: hello-world spec: containers: - name: hello-world image: gcr.io/google-samples/hello-app:1.0
$kubectl apply -f pod.yaml $pod/hello-world created
Çalışan podları listelemek için,
kubectl get pods
Podumuzu kaldırıp silelim,
kubectl delete pod hello-world
Kubectl üzerinde dry-run ile çalışalım. Bir bildirimin sunucu tarafı doğrulaması için kubectl dry-run kullanalım. Objeler API sunucusuna gönderilecektir. dry-run=server bize nesnenin oluşturulduğunu söyleyecektir.
deployment.yaml içeriği
apiVersion: apps/v1 kind: Deployment metadata: name: hello-world labels: app: hello-world spec: replicas: 4 selector: matchLabels: app: hello-world template: metadata: labels: app: hello-world spec: containers: - name: hello-world image: gcr.io/google-samples/hello-app:1.0 ports: - containerPort: 8080
kubectl apply -f deployment.yaml --dry-run=server
deployment.apps/hello-world created (server dry run)
deploymentları sorguladığımızda herhangi oluşan bir deployment olmadığı görülür,
kubectl get deployments
Bir manifestonun client tarafı doğrulaması için kubectl dry-run çalıştırmayı deneyelim,
kubectl apply -f deployment.yaml --dry-run=client
deployment.apps/hello-world created (dry run)
Bir kez daha hatalı şekilde yapalım ve işin içinde replika da olsun.
deployment-error.yaml içeriği
apiVersion: apps/v1 kind: Deployment metadata: name: hello-world labels: app: hello-world spec: replica: 4 selector: matchLabels: app: hello-world template: metadata: labels: app: hello-world spec: containers: - name: hello-world image: gcr.io/google-samples/hello-app:1.0 ports: - containerPort: 8080
error: error validating "deployment-error.yaml": error validating data: ValidationError(Deployment.spec): unknown field "replica" in io.k8s.api.apps.v1.DeploymentSpec; if you choose to ignore these errors, turn validation off with --validate=false
Obje için yaml oluşturmak ve kubectl dry-run client kullanalım,
root@k8s-c1-cp1:~# kubectl create deployment nginx --image=nginx --dry-run=client deployment.apps/nginx created (dry run) root@k8s-c1-cp1:~#
Dry-run client -o yaml ile birleştirirsek eğer obje için yaml alabilriz.
root@k8s-c1-cp1:~# kubectl create deployment nginx --image=nginx --dry-run=client -o yaml | more apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nginx name: nginx spec: replicas: 1 selector: matchLabels: app: nginx strategy: {} template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx name: nginx resources: {} status: {} root@k8s-c1-cp1:~#
Herhangi bir nesne olabilir, pod üzerinde denenmeli,
root@k8s-c1-cp1:~# kubectl run pod nginx-pod --image=nginx --dry-run=client -o yaml | more apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: pod name: pod spec: containers: - args: - nginx-pod image: nginx name: pod resources: {} dnsPolicy: ClusterFirst restartPolicy: Always status: {} root@k8s-c1-cp1:~#
IO yönlendirmesi ile birleştirerek bir YAML da saklayalımi
root@k8s-c1-cp1:~# kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > deployment-generated.yaml more deployment-generated.yaml apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nginx name: nginx spec: replicas: 1 selector: matchLabels: app: nginx strategy: {} template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx name: nginx resources: {} status: {} root@k8s-c1-cp1:~#
Ve sonrasında manifastodan yayımlayabiliriz. Ya da daha karmaşık manifestler için yapı taşı olarak kullanabiliriz.
root@k8s-c1-cp1:~# kubectl apply -f deployment-generated.yaml deployment.apps/nginx created
demomuzu -f parametresi ile silelim,
root@k8s-c1-cp1:~# kubectl delete -f deployment-generated.yaml deployment.apps "nginx" deleted
Kubectl ile diff kullanımı, 4 adet replikalı bir deployment oluşturalım,
apiVersion: apps/v1 kind: Deployment metadata: name: hello-world labels: app: hello-world spec: replicas: 4 selector: matchLabels: app: hello-world template: metadata: labels: app: hello-world spec: containers: - name: hello-world image: gcr.io/google-samples/hello-app:1.0 ports: - containerPort: 8080
kubectl apply -f deployment.yaml
deployment-new.yaml içeriği
apiVersion: apps/v1 kind: Deployment metadata: name: hello-world labels: app: hello-world spec: replicas: 5 selector: matchLabels: app: hello-world template: metadata: labels: app: hello-world spec: containers: - name: hello-world image: gcr.io/google-samples/hello-app:2.0 ports: - containerPort: 8080
kubectl diff -f deployment-new.yaml | more
İki yaml arasındaki farkları çıktıda verecektir,
kubectl apply -f deployment.yaml deployment.apps/hello-world created root@k8s-c1-cp1:~/K8S/2-managing-kubernetes-api-server-pods/02/demos/demos# kubectl diff -f deployment-new.yaml | more diff -u -N /tmp/LIVE-484529344/apps.v1.Deployment.default.hello-world /tmp/MERGED-261382943/apps.v1.Deploy ment.default.hello-world --- /tmp/LIVE-484529344/apps.v1.Deployment.default.hello-world 2022-03-16 20:43:54.115918922 +0000 +++ /tmp/MERGED-261382943/apps.v1.Deployment.default.hello-world 2022-03-16 20:43:54.119918938 +000 0 @@ -6,7 +6,7 @@ kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"hello-wor ld"},"name":"hello-world","namespace":"default"},"spec":{"replicas":4,"selector":{"matchLabels":{"app":"he llo-world"}},"template":{"metadata":{"labels":{"app":"hello-world"}},"spec":{"containers":[{"image":"gcr.i o/google-samples/hello-app:1.0","name":"hello-world","ports":[{"containerPort":8080}]}]}}}} creationTimestamp: "2022-03-16T20:42:14Z" - generation: 1 + generation: 2 labels: app: hello-world managedFields: @@ -100,7 +100,7 @@ uid: 4eb98792-f958-44ef-bbb7-a2b317cc8299 spec: progressDeadlineSeconds: 600 - replicas: 4 + replicas: 5 revisionHistoryLimit: 10 selector: matchLabels: @@ -117,7 +117,7 @@ app: hello-world spec: containers: - - image: gcr.io/google-samples/hello-app:1.0 + - image: gcr.io/google-samples/hello-app:2.0 imagePullPolicy: IfNotPresent name: hello-world ports:
Yanında + olarak çıkan satırlarda değişiklikleri göstermektedir.
İşlemimiz bittikten sonra ilgili deploymenti silelim,
kubectl delete -f deployment.yaml
Sonraki yazımızda görüşmek üzere,