Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
elearning:workbooks:kubernetes:k8s02 [2022/09/11 09:31] adminelearning:workbooks:kubernetes:k8s02 [2024/12/15 06:47] (Version actuelle) admin
Ligne 1: Ligne 1:
 ~~PDF:LANDSCAPE~~ ~~PDF:LANDSCAPE~~
  
-Version - **2020.03**+Version - **2024.01**
  
 Dernière mise-à-jour : ~~LASTMOD~~ Dernière mise-à-jour : ~~LASTMOD~~
  
-======DOF304 - Les Commandes kubectl, krew et kustomize======+======DOF303 - Les Commandes kubectl, krew et kustomize======
  
 =====Contenu du Module===== =====Contenu du Module=====
  
-  * **DOF304 - Les Commandes kubectl, krew et kustomize** +  * **DOF303 - Les Commandes kubectl, krew et kustomize**
-    * Contenu du Module+
     * Contenu du Module     * Contenu du Module
     * LAB #1 - Utilisation de la Commande kubectl     * LAB #1 - Utilisation de la Commande kubectl
Ligne 26: Ligne 25:
         * La Commande top         * La Commande top
       * 1.5 - Travailler avec la commande kubectl       * 1.5 - Travailler avec la commande kubectl
 +        * La Commande apply
 +        * La Commande create
 +        * La Commande get
 +        * Utilisation des Options
 +        * La Commande exec
 +        * Commandes Impératives
     * LAB #2 - Gestion les plugins de kubectl avec la Commande krew     * LAB #2 - Gestion les plugins de kubectl avec la Commande krew
       * 2.1 - Installation de krew       * 2.1 - Installation de krew
Ligne 32: Ligne 37:
       * 2.4 - Mise à jour et suppression de plugins       * 2.4 - Mise à jour et suppression de plugins
     * LAB #3 - Gestion des patchs avec la Commande kustomize     * LAB #3 - Gestion des patchs avec la Commande kustomize
 +
 +=====Ressources=====
 +
 +====Lab #1=====
 +
 +  * https://www.dropbox.com/scl/fi/a5ki48szu8q5159177opv/components.yaml?rlkey=354a4ybivgbmu97gyoduadm61&dl=0
 +  * https://www.dropbox.com/scl/fi/weivllvybgtxtf2psq1ko/pod.yaml?rlkey=x1spvh9v0hy8p8m86rv8oqd60&dl=0
 +
 +====Lab #2====
 +
 +  * https://www.dropbox.com/scl/fi/qhpr75m74mrho0c2k7ky9/krew-linux_amd64.tar.gz?rlkey=bigm37fy2c3rzqgziszq71tb8&dl=0
 +
 +====Lab #3====
 +
 +  * https://www.dropbox.com/scl/fi/0af2lqns1q26u63lotu7x/deployment.yaml?rlkey=ese25kacg2ibagpio9bky3cv9&dl=0
 +  * https://www.dropbox.com/scl/fi/qnr0m18sa3lwjbsk1qrj5/service.yaml?rlkey=sd5t8a4bh1trawum5vpy2bzww&dl=0
 +  * https://www.dropbox.com/scl/fi/5pz0wg63yd68w018248nc/kustomization.yaml?rlkey=liff7kt68fsicgjfyvxaozadi&dl=0
 +  * https://www.dropbox.com/scl/fi/2j60pti1wmd60kuv6kvc2/dev_kustomization.yaml?rlkey=iz6488nj8rrwg2b3y83suwch2&dl=0
 +  * https://www.dropbox.com/scl/fi/bbdxrx0vxdx4y67uxehlv/prod_kustomization.yaml?rlkey=c723auhoeuzlut1p6qx96ktse&dl=0
  
 =====LAB #1 - Utilisation de la Commande kubectl===== =====LAB #1 - Utilisation de la Commande kubectl=====
Ligne 693: Ligne 717:
 </code> </code>
  
-<WRAP center round important 40%>+<WRAP center round important 60%>
 **Important** : Notez l'utilisation de **^C** pour sortir de l'écran de la commande **watch**. **Important** : Notez l'utilisation de **^C** pour sortir de l'écran de la commande **watch**.
 </WRAP> </WRAP>
Ligne 825: Ligne 849:
     command: ['sh', '-c', 'while true; do sleep 3600; done']     command: ['sh', '-c', 'while true; do sleep 3600; done']
 </code> </code>
 +
 +====La Commande apply====
  
 Créez maintenant le pod en utilisant le fichier **pod.yaml** : Créez maintenant le pod en utilisant le fichier **pod.yaml** :
Ligne 832: Ligne 858:
 pod/my-pod created pod/my-pod created
 </code> </code>
 +
 +====La Commande create====
 +
 +La commande **create** ne peut être utilisée que dans le cas où un objet du même nom n'existe pas déjà dans le cluster :
  
 <code> <code>
-root@kubemaster:~# kubectl get +root@kubemaster:~# kubectl create -f pod.yaml 
-You must specify the type of resource to getUse "kubectl api-resourcesfor a complete list of supported resources.+Error from server (AlreadyExists): error when creating "pod.yaml": pods "my-podalready exists 
 +</code>
  
-errorRequired resource not specified+Pour consulter la liste des objets qui peuvent être créés, utilisez la commande **kubectl create** : 
-Use "kubectl explain <resource>" for a detailed description of that resource (e.g. kubectl explain pods). + 
-See 'kubectl get -hfor help and examples+<code> 
 +root@kubemaster:~# kubectl create 
 +Error: must specify one of -f and -k 
 + 
 +Create a resource from a file or from stdin
 + 
 + JSON and YAML formats are accepted. 
 + 
 +Examples: 
 +  # Create a pod using the data in pod.json 
 +  kubectl create -f ./pod.json 
 +   
 +  # Create a pod based on the JSON passed into stdin 
 +  cat pod.json | kubectl create -f - 
 +   
 +  # Edit the data in registry.yaml in JSON then create the resource using the edited data 
 +  kubectl create -f registry.yaml --edit -o json 
 + 
 +Available Commands: 
 +  clusterrole           Create a cluster role 
 +  clusterrolebinding    Create a cluster role binding for a particular cluster role 
 +  configmap             Create a config map from a local file, directory or literal value 
 +  cronjob               Create a cron job with the specified name 
 +  deployment            Create a deployment with the specified name 
 +  ingress               Create an ingress with the specified name 
 +  job                   Create a job with the specified name 
 +  namespace             Create a namespace with the specified name 
 +  poddisruptionbudget   Create a pod disruption budget with the specified name 
 +  priorityclass         Create a priority class with the specified name 
 +  quota                 Create a quota with the specified name 
 +  role                  Create a role with single rule 
 +  rolebinding           Create a role binding for a particular role or cluster role 
 +  secret                Create a secret using specified subcommand 
 +  service               Create a service using a specified subcommand 
 +  serviceaccount        Create a service account with the specified name 
 +  token                 Request a service account token 
 + 
 +Options: 
 +    --allow-missing-template-keys=true: 
 +        If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to 
 +        golang and jsonpath output formats. 
 + 
 +    --dry-run='none': 
 +        Must be "none", "server", or "client". If client strategy, only print the object that would be sent, without 
 +        sending it. If server strategy, submit server-side request without persisting the resource
 + 
 +    --edit=false: 
 +        Edit the API resource before creating 
 + 
 +    --field-manager='kubectl-create': 
 +        Name of the manager used to track field ownership. 
 + 
 +    -f, --filename=[]: 
 +        Filename, directory, or URL to files to use to create the resource 
 + 
 +    -k, --kustomize='': 
 +        Process the kustomization directory. This flag can't be used together with -f or -R. 
 + 
 +    -o, --output='': 
 +        Output format. One of: (json, yaml, name, go-template, go-template-file, template, templatefile, jsonpath, 
 +        jsonpath-as-json, jsonpath-file). 
 + 
 +    --raw='': 
 +        Raw URI to POST to the server.  Uses the transport specified by the kubeconfig file. 
 + 
 +    -R, --recursive=false: 
 +        Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests 
 +        organized within the same directory. 
 + 
 +    --save-config=false: 
 +        If true, the configuration of current object will be saved in its annotation. Otherwise, the annotation will 
 +        be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future. 
 + 
 +    -l, --selector='': 
 +        Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2). Matching 
 +        objects must satisfy all of the specified label constraints. 
 + 
 +    --show-managed-fields=false: 
 +        If true, keep the managedFields when printing objects in JSON or YAML format. 
 + 
 +    --template='': 
 +        Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format 
 +        is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. 
 + 
 +    --validate='strict': 
 +        Must be one of: strict (or true), warn, ignore (or false).              "true" or "strict" will use a schema to validate 
 +        the input and fail the request if invalid. It will perform server side validation if ServerSideFieldValidation 
 +        is enabled on the api-server, but will fall back to less reliable client-side validation if not.                "warn" will 
 +        warn about unknown or duplicate fields without blocking the request if server-side field validation is enabled 
 +        on the API server, and behave as "ignore" otherwise.            "false" or "ignore" will not perform any schema 
 +        validation, silently dropping any unknown or duplicate fields. 
 + 
 +    --windows-line-endings=false: 
 +        Only relevant if --edit=true. Defaults to the line ending native to your platform. 
 + 
 +Usage: 
 +  kubectl create -f FILENAME [options] 
 + 
 +Use "kubectl <command> --help" for more information about a given command. 
 +Use "kubectl options" for a list of global command-line options (applies to all commands).
 </code> </code>
 +
 +La commande **apply** est ensuite utilisée pour appliquer des modifications apportées au fichier yaml :
 +
 +<code>
 +root@kubemaster:~# kubectl apply -f pod.yaml
 +pod/my-pod unchanged
 +</code>
 +
 +====La Commande get====
 +
 +Constatez le statut du pod :
  
 <code> <code>
Ligne 850: Ligne 991:
 myapp-deployment-689f9d59-rnc4r   1/    Running            6d1h myapp-deployment-689f9d59-rnc4r   1/    Running            6d1h
 </code> </code>
 +
 +Rappelez-vous que vous pouvez utiliser une abréviation pour pods :
  
 <code> <code>
Ligne 859: Ligne 1002:
 myapp-deployment-689f9d59-rnc4r   1/    Running            6d1h myapp-deployment-689f9d59-rnc4r   1/    Running            6d1h
 </code> </code>
 +
 +Pour ne voir qu'un seul pod, il convient de préciser son nom en tant qu'argument :
  
 <code> <code>
Ligne 865: Ligne 1010:
 my-pod   1/    Running            109s my-pod   1/    Running            109s
 </code> </code>
 +
 +====Utilisation des Options====
 +
 +Rappelez-vous que l'option **wide** vous permet de voir les adresses IP des pods ainsi que les noeuds qui les hébergent :
  
 <code> <code>
Ligne 874: Ligne 1023:
 myapp-deployment-689f9d59-rnc4r   1/    Running            6d1h   192.168.239.12   kubenode1.ittraining.loc   <none>           <none> myapp-deployment-689f9d59-rnc4r   1/    Running            6d1h   192.168.239.12   kubenode1.ittraining.loc   <none>           <none>
 </code> </code>
 +
 +L'utilisation de l'option **json** permet de voir cette même information au format json :
  
 <code> <code>
Ligne 918: Ligne 1069:
 --More-- --More--
 </code> </code>
 +
 +L'utilisation de l'option **yaml** permet de voir cette même information au format yaml :
  
 <code> <code>
Ligne 962: Ligne 1115:
 --More-- --More--
 </code> </code>
 +
 +L'option **--sort-by** permet de trier la sortie en fonction d'une clef yaml :
  
 <code> <code>
Ligne 971: Ligne 1126:
 my-pod                            1/1     Running            3m22s   192.168.150.9    kubenode2.ittraining.loc   <none>           <none> my-pod                            1/1     Running            3m22s   192.168.150.9    kubenode2.ittraining.loc   <none>           <none>
 </code> </code>
 +
 +L'option **--selector** vous permet de ne voir que les pods qui correspondent à l'étiquette indiquée, par exemple, **k8s-app** :
  
 <code> <code>
Ligne 980: Ligne 1137:
 </code> </code>
  
-k8s-app is a label+====La Commande exec====
  
-<code> +La commande **exec** permet d'exécuter une commande dans le conteneurLa commande est précédée par les caractères **-** :
-root@kubemaster:~# kubectl create -f pod.yaml +
-Error from server (AlreadyExists): error when creating "pod.yaml": pods "my-pod" already exists +
-</code> +
- +
-<code> +
-root@kubemaster:~# kubectl apply -f pod.yaml +
-pod/my-pod unchanged +
-</code>+
  
 <code> <code>
Ligne 997: Ligne 1146:
 </code> </code>
  
-busybox is the container name+<WRAP center round important 60%> 
 +**Important** : Notez l'utilisation de **-c** pour pour indiquer le nom du conteneur. 
 +</WRAP>
  
-<code> 
-root@kubemaster:~# kubectl delete pod my-pod 
-pod "my-pod" deleted 
-</code> 
  
-<code> +====5.6 Commandes Impératives ====
-root@kubemaster:~# kubectl create +
-Error: must specify one of -f and -k+
  
-Create a resource from a file or from stdin. +Avant de poursuivre, supprimez le pod **my-pod** :
- +
- JSON and YAML formats are accepted. +
- +
-Examples: +
-  # Create a pod using the data in pod.json +
-  kubectl create -f ./pod.json +
-   +
-  # Create a pod based on the JSON passed into stdin +
-  cat pod.json | kubectl create -f - +
-   +
-  # Edit the data in registry.yaml in JSON then create the resource using the edited data +
-  kubectl create -f registry.yaml --edit -o json +
- +
-Available Commands: +
-  clusterrole           Create a cluster role +
-  clusterrolebinding    Create a cluster role binding for a particular cluster role +
-  configmap             Create a config map from a local file, directory or literal value +
-  cronjob               Create a cron job with the specified name +
-  deployment            Create a deployment with the specified name +
-  ingress               Create an ingress with the specified name +
-  job                   Create a job with the specified name +
-  namespace             Create a namespace with the specified name +
-  poddisruptionbudget   Create a pod disruption budget with the specified name +
-  priorityclass         Create a priority class with the specified name +
-  quota                 Create a quota with the specified name +
-  role                  Create a role with single rule +
-  rolebinding           Create a role binding for a particular role or cluster role +
-  secret                Create a secret using specified subcommand +
-  service               Create a service using a specified subcommand +
-  serviceaccount        Create a service account with the specified name +
-  token                 Request a service account token +
- +
-Options: +
-    --allow-missing-template-keys=true: +
-        If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to +
-        golang and jsonpath output formats. +
- +
-    --dry-run='none': +
-        Must be "none", "server", or "client". If client strategy, only print the object that would be sent, without +
-        sending it. If server strategy, submit server-side request without persisting the resource. +
- +
-    --edit=false: +
-        Edit the API resource before creating +
- +
-    --field-manager='kubectl-create': +
-        Name of the manager used to track field ownership. +
- +
-    -f, --filename=[]: +
-        Filename, directory, or URL to files to use to create the resource +
- +
-    -k, --kustomize='': +
-        Process the kustomization directory. This flag can't be used together with -f or -R. +
- +
-    -o, --output='': +
-        Output format. One of: (json, yaml, name, go-template, go-template-file, template, templatefile, jsonpath, +
-        jsonpath-as-json, jsonpath-file). +
- +
-    --raw='': +
-        Raw URI to POST to the server.  Uses the transport specified by the kubeconfig file. +
- +
-    -R, --recursive=false: +
-        Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests +
-        organized within the same directory. +
- +
-    --save-config=false: +
-        If true, the configuration of current object will be saved in its annotation. Otherwise, the annotation will +
-        be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future. +
- +
-    -l, --selector='': +
-        Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2). Matching +
-        objects must satisfy all of the specified label constraints. +
- +
-    --show-managed-fields=false: +
-        If true, keep the managedFields when printing objects in JSON or YAML format. +
- +
-    --template='': +
-        Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format +
-        is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. +
- +
-    --validate='strict': +
-        Must be one of: strict (or true), warn, ignore (or false).              "true" or "strict" will use a schema to validate +
-        the input and fail the request if invalid. It will perform server side validation if ServerSideFieldValidation +
-        is enabled on the api-server, but will fall back to less reliable client-side validation if not.                "warn" will +
-        warn about unknown or duplicate fields without blocking the request if server-side field validation is enabled +
-        on the API server, and behave as "ignore" otherwise.            "false" or "ignore" will not perform any schema +
-        validation, silently dropping any unknown or duplicate fields. +
- +
-    --windows-line-endings=false: +
-        Only relevant if --edit=true. Defaults to the line ending native to your platform. +
- +
-Usage: +
-  kubectl create -f FILENAME [options] +
- +
-Use "kubectl <command> --help" for more information about a given command. +
-Use "kubectl options" for a list of global command-line options (applies to all commands). +
-</code>+
  
 <code> <code>
-kubectl create deployment my-deployment --image=nginx+root@kubemaster:~# kubectl delete pod my-pod 
 +pod "my-pod" deleted
 </code> </code>
  
-Imperitive command+Créez ensuite un deployment avec une commande impérative :
  
 <code> <code>
Ligne 1117: Ligne 1167:
 </code> </code>
  
-<code> +En exécutant la même commande impérative, il est possible de créer des instructions au format yaml en utilisant l'option **--dry-run** :
-kubectl create deployment my-deployment --image=nginx --dry-run -o yaml > deployment.yml +
-</code>+
  
 <code> <code>
Ligne 1149: Ligne 1197:
 status: {} status: {}
 </code> </code>
 +
 +Ces instructions peuvent ensuite être injectées dans un fichier afin d'utiliser celui-ci pour créer un deployment indentique :
  
 <code> <code>
Ligne 1753: Ligne 1803:
 </code> </code>
  
-<WRAP center round important>+<WRAP center round important 60%>
 **Important** - le contenu de ce fichier crée un **deployment** de 1 **replica** du pod **nginx** à partir de l'image **nginx:1.18.0**. **Important** - le contenu de ce fichier crée un **deployment** de 1 **replica** du pod **nginx** à partir de l'image **nginx:1.18.0**.
 </WRAP> </WRAP>
Ligne 1778: Ligne 1828:
 </code> </code>
  
-<WRAP center round important>+<WRAP center round important 60%>
 **Important** - le contenu de ce fichier crée un **service** de type **ClusterIP** en utilisant le **deployment** précédent. Le Service ClusterIP permet de regrouper les PODs offrant le même service afin de faciliter la communication. **Important** - le contenu de ce fichier crée un **service** de type **ClusterIP** en utilisant le **deployment** précédent. Le Service ClusterIP permet de regrouper les PODs offrant le même service afin de faciliter la communication.
 </WRAP> </WRAP>
Ligne 1799: Ligne 1849:
 </code> </code>
  
-<WRAP center round important>+<WRAP center round important 60%>
 **Important** - le contenu de ce fichier contient un **patch** pour l'application **nginx** créée par les deux fichiers précédent. Notez le tag **newTag** dans la section **images**. Dans la section **resources** se trouve la liste des manifests concernés par le patch. Notez que seul le manifest **deployment.yaml** fait référence à une image. Cependant, le fichier **service.yaml** est inclus ici car il sera nécessaire par la suite. **Important** - le contenu de ce fichier contient un **patch** pour l'application **nginx** créée par les deux fichiers précédent. Notez le tag **newTag** dans la section **images**. Dans la section **resources** se trouve la liste des manifests concernés par le patch. Notez que seul le manifest **deployment.yaml** fait référence à une image. Cependant, le fichier **service.yaml** est inclus ici car il sera nécessaire par la suite.
 </WRAP> </WRAP>
Ligne 1868: Ligne 1918:
 </code> </code>
  
-<WRAP center round important 40%>+<WRAP center round important 60%>
 **Important** - notez que le fichier généré contient les contenus des **deux** fichiers **deployment.yaml** et **service.yaml** séparés par les caractères **---**. Le contenu du fichier **service.yaml** n'a pas été modifié tandis que l'image a été modifiée de **image: nginx:1.18.0** vers **image: nginx:1.19.1** dans le contenu du fichier **deployment.yaml**. Notez que les deux fichiers d'origine n'ont **pas** été modifiés. **Important** - notez que le fichier généré contient les contenus des **deux** fichiers **deployment.yaml** et **service.yaml** séparés par les caractères **---**. Le contenu du fichier **service.yaml** n'a pas été modifié tandis que l'image a été modifiée de **image: nginx:1.18.0** vers **image: nginx:1.19.1** dans le contenu du fichier **deployment.yaml**. Notez que les deux fichiers d'origine n'ont **pas** été modifiés.
 </WRAP> </WRAP>
Ligne 1897: Ligne 1947:
 </code> </code>
  
-Créez le fichier **overlays/development/kustomization.yaml** :+Créez le fichier **dev_kustomization.yaml** :
  
 <code> <code>
-root@debian10:~/kustomize# vi overlays/development/kustomization.yaml +root@kubemaster:~/kustomize# vi overlays/development/kustomization.yaml 
-root@debian10:~/kustomize# cat overlays/development/kustomization.yaml+root@kubemaster:~/kustomize# cat overlays/development/kustomization.yaml
 apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
 kind: Kustomization kind: Kustomization
Ligne 1919: Ligne 1969:
  
 <code> <code>
-root@debian10:~/kustomize# kubectl kustomize overlays/development/+root@kubemaster:~/kustomize# kubectl kustomize overlays/development/
 apiVersion: v1 apiVersion: v1
 kind: Service kind: Service
Ligne 1974: Ligne 2024:
 </code> </code>
  
-Maintenant créez le fichier **overlays/production/kustomization.yaml** :+Maintenant créez le fichier **prod_kustomization.yaml** :
  
 <code> <code>
-root@debian10:~/kustomize# vi overlays/production/kustomization.yaml +root@kubemaster:~/kustomize# vi overlays/production/kustomization.yaml 
-root@debian10:~/kustomize# cat overlays/production/kustomization.yaml+root@kubemaster:~/kustomize# cat overlays/production/kustomization.yaml
 apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
 kind: Kustomization kind: Kustomization
Ligne 2000: Ligne 2050:
  
 <code> <code>
-root@debian10:~/kustomize# kubectl kustomize overlays/production/+root@kubemaster:~/kustomize# kubectl kustomize overlays/production/
 apiVersion: v1 apiVersion: v1
 kind: Service kind: Service
Ligne 2136: Ligne 2186:
  
 ---- ----
-Copyright © 2022 Hugh Norris+Copyright © 2024 Hugh Norris
  
Menu