{"id":396,"date":"2023-11-05T09:38:38","date_gmt":"2023-11-05T09:38:38","guid":{"rendered":"https:\/\/devopsopen.com\/?p=396"},"modified":"2023-11-27T00:08:17","modified_gmt":"2023-11-27T00:08:17","slug":"boite-a-outil-k8s","status":"publish","type":"post","link":"https:\/\/devopsopen.com\/index.php\/2023\/11\/05\/boite-a-outil-k8s\/","title":{"rendered":"Bo\u00eete \u00e0 outil K8S"},"content":{"rendered":"<h2>get all resources in a cluster<\/h2>\n<pre><code>kubectl get all -A\n<\/code><\/pre>\n<h2>Sort cpu or memory of pods or nodes(Find the pod who consumm more than others)<\/h2>\n<p>install server metrics<\/p>\n<pre><code>wget  https:\/\/github.com\/kubernetes-sigs\/metrics-server\/releases\/latest\/download\/components.yaml\n<\/code><\/pre>\n<p>add those line into file yaml<\/p>\n<pre><code>        command:\n          - \/metrics-server\n          - --kubelet-insecure-tls\n          - --kubelet-preferred-address-types=InternalIP<\/code><\/pre>\n<p>like <\/p>\n<pre><code>spec:\n  selector:\n    matchLabels:\n      k8s-app: metrics-server\n  strategy:\n    rollingUpdate:\n      maxUnavailable: 0\n  template:\n    metadata:\n      labels:\n        k8s-app: metrics-server\n    spec:\n      containers:\n      - args:\n        - --cert-dir=\/tmp\n        - --secure-port=4443\n        - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname\n        - --kubelet-use-node-status-port\n        - --metric-resolution=15s\n        image: registry.k8s.io\/metrics-server\/metrics-server:v0.6.4\n        command:\n          - \/metrics-server\n          - --kubelet-insecure-tls\n          - --kubelet-preferred-address-types=InternalIP\n        imagePullPolicy: IfNotPresent\n        livenessProbe:\n          failureThreshold: 3\n          httpGet:<\/code><\/pre>\n<p>then apply the file<\/p>\n<pre><code>kubectl apply -f components.yaml<\/code><\/pre>\n<p>then get sorted informations<\/p>\n<pre><code>kubectl top pod | awk &#039;{ print $1 &quot;;&quot; $2 &quot;;&quot; $3}&#039; &gt; sort.in ; sort -t&quot;;&quot; -V -k3 sort.in\n<\/code><\/pre>\n<p>-t is seprator<br \/>\n-V alphanumeric<br \/>\n-k3 column 3 to be sorted<br \/>\nsort.in the file input<\/p>\n<h2>Info cluster service CIDR...<\/h2>\n<pre><code>kubectl cluster-info dump | grep -i service-cluster\nkubectl cluster-info dump | grep -i cluster-cidr<\/code><\/pre>\n<h2>Change image of a deployment<\/h2>\n<p>create a deployment with image<\/p>\n<pre><code>kubectl create deploy nginx-deploy --image=nginx:1.16 --replicas=1<\/code><\/pre>\n<p>change the image of the deployment<\/p>\n<pre><code>kubectl set image deployment nginx-deploy nginx=nginx:1.17<\/code><\/pre>\n<h2>get all pod in a specific node<\/h2>\n<pre><code>kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=&lt;node&gt;<\/code><\/pre>\n<h2>Migrate pods where are in a specific node to another node<\/h2>\n<p>unschedule pods with cordon command to not let other pods to come to this node.<br \/>\nkubectl cordon node-1<\/p>\n<p>drain node(vider) to exclude pods to other pods that are scheduled. drain containt the cordon command naturally<br \/>\nkubectl drain node-2 --force<\/p>\n<p>get node scheduled with command uncordon<br \/>\nkubectl uncordon node-1<\/p>\n<h2>Make a node unavailable<\/h2>\n<pre><code>kubectl drain node-01 --force  \nor\nkubectl drain node-01 --grace-period 0<\/code><\/pre>\n<h2>Creation  pod:<\/h2>\n<pre><code>kubectl run static-busybox --image=busybox --command &quot;sleep 1000&quot; --dry-run=client -o yaml<\/code><\/pre>\n<h2>Cr\u00e9ation deploy<\/h2>\n<pre><code>kubectl create deployment hr-web-app --image=redis --replicas=3 --dry-run=client -o yaml<\/code><\/pre>\n<h2>Scale a Deploy or RS<\/h2>\n<pre><code>kubectl scale deploy\/test2 --replicas=3<\/code><\/pre>\n<h2>Expose a pod (deploy or rs)<\/h2>\n<pre><code>kubectl expose pod messaging --name=messaging-service --type=ClusterIP --port=6379<\/code><\/pre>\n<h2>Create a static pod (important because static pod does not scheduled by api, you should put it in manifests)<\/h2>\n<pre><code>kubectl run --restart=Never --image=busybox static-busybox --dry-run=client -o yaml --command -- sleep 1000 &gt; \/etc\/kubernetes\/manifests\/static-busybox.yaml<\/code><\/pre>\n<h2>Create deploy with replicas<\/h2>\n<pre><code>kubectl create deployment hr-web-app --image=kodekloud\/webapp-color --replicas=2<\/code><\/pre>\n<h2>Correct a pod description with edit command. when you edit a pod, a temp file is created and you should force<\/h2>\n<pre><code>kubectl edit pod namepod\nkubectl replace -f \/tmp\/kubectl-edit-xxxx.yaml --force<\/code><\/pre>\n<h2>Expose a deploy with a specific NodePort. after this command you should change the port wanted in the file<\/h2>\n<pre><code>kubectl expose deployment hr-web-app --type=NodePort --port=8080 --name=hr-web-app-service --dry-run=client -o yaml &gt; hr-web-app-service.yaml<\/code><\/pre>\n<h2>Get OSImage of nodes and put in the file<\/h2>\n<pre><code>kubectl get nodes -o jsonpath=&#039;{.items[*].status.nodeInfo.osImage}&#039; &gt; \/opt\/outputs\/nodes_os_x43kj56.txt<\/code><\/pre>\n<h2>Create a persistent volume<\/h2>\n<pre><code>apiVersion: v1\nkind: PersistentVolume\nmetadata:\n  name: pv-analytics\nspec:\n  capacity:\n    storage: 100Mi\n  volumeMode: Filesystem\n  accessModes:\n    - ReadWriteMany\n  hostPath:\n      path: \/pv\/data-analytics<\/code><\/pre>\n<h2>Lens pour g\u00e9rer son Cluster<\/h2>\n<ul>\n<li>\n<p>T\u00e9l\u00e9charger et installer Lens Open : <a href=\"https:\/\/k8slens.dev\/\" title=\"Lien\">Lien<\/a><\/p>\n<\/li>\n<li>\n<p>R\u00e9cup\u00e9rer la conf de votre cluster depuis Kub avec la commande :<\/p>\n<\/li>\n<\/ul>\n<pre><code>kubectl config view --minify --raw\nOr\nkubectl config view --flatten &gt; out.txt<\/code><\/pre>\n<ul>\n<li>Ajouter la conf dans la fen\u00eatre ci-dessous<br \/>\n<img decoding=\"async\" src=\"https:\/\/devopsopen.com\/wp-content\/uploads\/2023\/11\/lens-conf.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/devopsopen.com\/wp-content\/uploads\/2023\/11\/lens-conf-cluster.png\" alt=\"\" \/><\/li>\n<\/ul>\n<h2>Use JsonPath to extract informations with kubectl<\/h2>\n<p><a href=\"https:\/\/kubernetes.io\/docs\/reference\/kubectl\/jsonpath\/\">https:\/\/kubernetes.io\/docs\/reference\/kubectl\/jsonpath\/<\/a><\/p>\n<p>The list bellow can help you, but for more details please see the link above<\/p>\n<pre><code>kubectl config view \n\nResult :\n\nclusters:\n- cluster:\n    certificate-authority-data: DATA+OMITTED\n    server: https:\/\/111.97.79.11:6443\n  name: kubernetes\ncontexts:\n- context:\n    cluster: kubernetes\n    namespace: ns-0001-example-prod\n    user: kubernetes-admin\n  name: kubernetes-admin@kubernetes\ncurrent-context: kubernetes-admin@kubernetes\nkind: Config\npreferences: {}\nusers:\n- name: kubernetes-admin\n  user:\n    client-certificate-data: DATA+OMITTED\n    client-key-data: DATA+OMITTED<\/code><\/pre>\n<pre><code>kubectl config view -o jsonpath=&#039;{..namespace}&#039;\nresult : ns-0001-example-prod<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>get all resources in a cluster kubectl get all -A Sort cpu or memory of pods or nodes(Find the pod who consumm more than others) install server metrics wget https:\/\/github.com\/kubernetes-sigs\/metrics-server\/releases\/latest\/download\/components.yaml add those line into file yaml command: &#8211; \/metrics-server &#8211; &#8211;kubelet-insecure-tls &#8211; &#8211;kubelet-preferred-address-types=InternalIP like spec: selector: matchLabels: k8s-app: metrics-server strategy: rollingUpdate: maxUnavailable: 0 template: metadata: labels: k8s-app: metrics-server spec: containers: &#8211; args: &#8211; &#8211;cert-dir=\/tmp &#8211; &#8211;secure-port=4443 &#8211; &#8211;kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname &#8211; &#8211;kubelet-use-node-status-port &#8211; &#8211;metric-resolution=15s image: registry.k8s.io\/metrics-server\/metrics-server:v0.6.4 command: &#8211; \/metrics-server &#8211; &#8211;kubelet-insecure-tls &#8211; &#8211;kubelet-preferred-address-types=InternalIP imagePullPolicy: IfNotPresent livenessProbe: failureThreshold: 3 httpGet: then apply the file kubectl apply -f components.yaml then get sorted informations kubectl\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":""},"categories":[12],"tags":[19],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":5}},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"admin","author_link":"https:\/\/devopsopen.com\/index.php\/author\/admin_bak\/"},"uagb_comment_info":5,"uagb_excerpt":"get all resources in a cluster kubectl get all -A Sort cpu or memory of pods or nodes(Find the pod who consumm more than others) install server metrics wget https:\/\/github.com\/kubernetes-sigs\/metrics-server\/releases\/latest\/download\/components.yaml add those line into file yaml command: - \/metrics-server - --kubelet-insecure-tls - --kubelet-preferred-address-types=InternalIP like spec: selector: matchLabels: k8s-app: metrics-server strategy: rollingUpdate: maxUnavailable: 0 template: metadata:&hellip;","_links":{"self":[{"href":"https:\/\/devopsopen.com\/index.php\/wp-json\/wp\/v2\/posts\/396"}],"collection":[{"href":"https:\/\/devopsopen.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devopsopen.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devopsopen.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devopsopen.com\/index.php\/wp-json\/wp\/v2\/comments?post=396"}],"version-history":[{"count":9,"href":"https:\/\/devopsopen.com\/index.php\/wp-json\/wp\/v2\/posts\/396\/revisions"}],"predecessor-version":[{"id":425,"href":"https:\/\/devopsopen.com\/index.php\/wp-json\/wp\/v2\/posts\/396\/revisions\/425"}],"wp:attachment":[{"href":"https:\/\/devopsopen.com\/index.php\/wp-json\/wp\/v2\/media?parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devopsopen.com\/index.php\/wp-json\/wp\/v2\/categories?post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devopsopen.com\/index.php\/wp-json\/wp\/v2\/tags?post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}