Table des matières
Kubernetes
Autres
Affichage
Repérer le champ à afficher en yaml, puis isoler le pour l'afficher :
$ kubectl get pod wordpress-2713623249-d46ky -o yaml $ kubectl get -o template pod wordpress-2713623249-d46ky --template={{.status.podIP}}
Commandes
kubectl
kubectl annotate
Met une annotation sur une ressource :
# Update pod 'foo' with the annotation 'description' and the value 'my frontend'. # If the same annotation is set multiple times, only the last value will be applied $ kubectl annotate pods foo description='my frontend' # Update a pod identified by type and name in "pod.json" $ kubectl annotate -f pod.json description='my frontend' # Update pod 'foo' with the annotation 'description' and the value 'my frontend running nginx', overwriting any existing value. $ kubectl annotate --overwrite pods foo description='my frontend running nginx' # Update all pods in the namespace $ kubectl annotate pods --all description='my frontend running nginx' # Update pod 'foo' only if the resource is unchanged from version 1. $ kubectl annotate pods foo description='my frontend running nginx' --resource-version=1 # Update pod 'foo' by removing an annotation named 'description' if it exists. # Does not require the --overwrite flag. $ kubectl annotate pods foo description-
kubectl api-versions
$ kubectl api-versions autoscaling/v1 batch/v1 extensions/v1beta1 v1
kubectl apply
Applique les changements sur les pods :
$ kubectl apply -f ./myapp.yaml
On peut prendre en compte également tous les fichiers d'un répertoire de façon récursive :
$ kubectl apply -R -f ./directory
kubectl attach
S'attacher à un conteneur qui est démarré :
# Get output from running pod 123456-7890, using the first container by default $ kubectl attach 123456-7890 # Get output from ruby-container from pod 123456-7890 $ kubectl attach 123456-7890 -c ruby-container # Switch to raw terminal mode, sends stdin to 'bash' in ruby-container from pod 123456-7890 # and sends stdout/stderr from 'bash' back to the client $ kubectl attach 123456-7890 -c ruby-container -i -t
kubectl autoscale
Auto-scale un Deployment, ReplicaSet, ou ReplicationController :
# Auto scale a deployment "foo", with the number of pods between 2 and 10, target CPU utilization specified so a default autoscaling policy will be used: $ kubectl autoscale deployment foo --min=2 --max=10 # Auto scale a replication controller "foo", with the number of pods between 1 and 5, target CPU utilization at 80%: $ kubectl autoscale rc foo --max=5 --cpu-percent=80
kubectl cluster-info
Affiche les informations du cluster :
$ kubectl cluster-info
kubectl config
kubectl config current-context
kubectl config set-cluster
kubectl config set-context
kubectl config set-credentials
kubectl config set
kubectl config unset
kubectl config use-context
kubectl config view
kubectl convert
Convertit des fichiers entre différentes versions d'API :
# Convert 'pod.yaml' to latest version and print to stdout. $ kubectl convert -f pod.yaml # Convert the live state of the resource specified by 'pod.yaml' to the latest version # and print to stdout in json format. $ kubectl convert -f pod.yaml --local -o json # Convert all files under current directory to latest version and create them all. $ kubectl convert -f . | kubectl create -f -
kubectl cordon
Empêche de nouveaux POD d'être lancé sur le noeud en question mais ne détruit pas les pods courant :
$ kubectl cordon NODE
kubectl create
# Create a pod using the data in pod.json. $ kubectl create -f ./pod.json # Recursive mode $ kubectl create -R -f ./mydirectory # Create a pod based on the JSON passed into stdin. $ cat pod.json | kubectl create -f -
kubectl create configmap
# Create a new configmap named my-config with keys for each file in folder bar $ kubectl create configmap my-config --from-file=path/to/bar # Create a new configmap named my-config with specified keys instead of names on disk $ kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt # Create a new configmap named my-config with key1=config1 and key2=config2 $ kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
Modifier une config map :
$ kubectl create configmap nginx-lb-config --from-file=nginx-lb.configmap --dry-run -o yaml | kubectl replace configmap nginx-lb-config -f -
Exemple de configmap :
apiVersion: v1 kind: ConfigMap metadata: Name: example-configmap data: # property-like keys game-properties-file-name: game.properties ui-properties-file-name: ui.properties # file-like keys game.properties: | enemies=aliens lives=3 enemies.cheat=true enemies.cheat.level=noGoodRotten secret.code.passphrase=UUDDLRLRBABAS secret.code.allowed=true secret.code.lives=30 ui.properties: | color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice
Exemple de création qui utilisera le configmap définit au-dessus :
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: configmap-example-deployment labels: name: configmap-example-deployment spec: replicas: 1 selector: matchLabels: name: configmap-example template: metadata: labels: name: configmap-example spec: containers: - name: game-container image: imaginarygame command: [ "game-server", "--config-dir=/etc/game/cfg" ] env: # consume the property-like keys in environment variables - name: GAME_PROPERTIES_NAME valueFrom: configMapKeyRef: name: example-configmap key: game-properties-file-name - name: UI_PROPERTIES_NAME valueFrom: configMapKeyRef: name: example-configmap key: ui-properties-file-name volumeMounts: - name: config-volume mountPath: /etc/game volumes: # consume the file-like keys of the configmap via volume plugin - name: config-volume configMap: name: example-configmap items: - key: ui.properties path: cfg/ui.properties - key: game.properties path: cfg/game.properties restartPolicy: Never
kubectl create namespace
Crée un namespace avec un nom spécifique :
$ kubectl create namespace my-namespace
kubectl create serviceaccount
$ kubectl create serviceaccount my-service-account
kubectl create secret
kubectl create secret docker-registry
Crée un secret pour un registry docker :
$ kubectl create secret docker-registry my-secret --docker-server=DOCKER_REGISTRY_SERVER --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL
kubectl create secret generic
Crée un secret à partir d'un dossier, fichier ou valeur :
# Create a new secret named my-secret with keys for each file in folder bar $ kubectl create secret generic my-secret --from-file=path/to/bar # Create a new secret named my-secret with specified keys instead of names on disk $ kubectl create secret generic my-secret --from-file=ssh-privatekey=~/.ssh/id_rsa --from-file=ssh-publickey=~/.ssh/id_rsa.pub # Create a new secret named my-secret with key1=supersecret and key2=topsecret $ kubectl create secret generic my-secret --from-literal=key1=supersecret --from-literal=key2=topsecret
kubectl delete
# Delete a pod using the type and name specified in pod.json. $ kubectl delete -f ./pod.json # Delete a pod based on the type and name in the JSON passed into stdin. $ cat pod.json | kubectl delete -f - # Delete pods and services with same names "baz" and "foo" $ kubectl delete pod,service baz foo # Delete pods and services with label name=myLabel. $ kubectl delete pods,services -l name=myLabel # Delete a pod immediately (no graceful shutdown) $ kubectl delete pod foo --now # Delete a pod with UID 1234-56-7890-234234-456456. $ kubectl delete pod 1234-56-7890-234234-456456 # Delete all pods $ kubectl delete pods --all
kubectl describe
# Describe a node $ kubectl describe nodes kubernetes-minion-emt8.c.myproject.internal # Describe a pod $ kubectl describe pods/nginx # Describe a pod identified by type and name in "pod.json" $ kubectl describe -f pod.json # Describe all pods $ kubectl describe pods # Describe pods by label name=myLabel $ kubectl describe po -l name=myLabel # Describe all pods managed by the 'frontend' replication controller (rc-created pods # get the name of the rc as a prefix in the pod the name). $ kubectl describe pods frontend
kubectl drain
Bloque la création de nouveau pod puis supprime les pods du noeud :
# Drain node "foo", even if there are pods not managed by a ReplicationController, ReplicaSet, Job, or DaemonSet on it. $ kubectl drain foo --force # As above, but abort if there are pods not managed by a ReplicationController, ReplicaSet, Job, or DaemonSet, and use a grace period of 15 minutes. $ kubectl drain foo --grace-period=900
kubectl edit
Edite la configuration d'une ressource :
$ kubectl edit node k-node1 $ kubectl edit deployment nginx -o json $ kubectl edit pod nginx-deployment-2644738339-826ks $ kubectl edit svc/docker-registry --output-version=v1 -o json
kubectl exec
Execute une commande dans un conteneur :
$ kubectl exec nginx-2644738339-826ks -it -- bash $ kubectl exec 123456-7890 -c ruby-container -i -t -- bash -il
kubectl explain
# Get the documentation of the resource and its fields $ kubectl explain pods # Get the documentation of a specific field of a resource $ kubectl explain pods.spec.containers
kubectl expose
Déclare une ressource comme un nouveau service :
# Create a service for a replicated nginx, which serves on port 80 and connects to the containers on port 8000. $ kubectl expose rc nginx --port=80 --target-port=8000 # Create a service for a replication controller identified by type and name specified in "nginx-controller.yaml", which serves on port 80 and connects to the containers on port 8000. $ kubectl expose -f nginx-controller.yaml --port=80 --target-port=8000 # Create a service for a pod valid-pod, which serves on port 444 with the name "frontend" $ kubectl expose pod valid-pod --port=444 --name=frontend # Create a second service based on the above service, exposing the container port 8443 as port 443 with the name "nginx-https" $ kubectl expose service nginx --port=443 --target-port=8443 --name=nginx-https # Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video-stream'. $ kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream # Create a service for a replicated nginx using replica set, which serves on port 80 and connects to the containers on port 8000. $ kubectl expose rs nginx --port=80 --target-port=8000 # Create a service for an nginx deployment, which serves on port 80 and connects to the containers on port 8000. $ kubectl expose deployment nginx --port=80 --target-port=8000
$ kubectl expose deployment my-nginx --port=80 --type=LoadBalancer
kubectl get
# List all pods in ps output format. $ kubectl get pods # List all pods in ps output format with more information (such as node name). $ kubectl get pods -o yaml # List a single replication controller with specified NAME in ps output format. $ kubectl get replicationcontroller web # List a single pod in JSON output format. $ kubectl get -o json pod web-pod-13je7 # List a pod identified by type and name specified in "pod.yaml" in JSON output format. $ kubectl get -f pod.yaml -o json # Return only the phase value of the specified pod. $ kubectl get -o template pod/web-pod-13je7 --template= # List all replication controllers and services together in ps output format. $ kubectl get rc,services # List one or more resources by their type and names. $ kubectl get rc/web service/frontend pods/web-pod-13je7
kubectl label
Gestion des labels sur les ressources :
# Update pod 'foo' with the label 'unhealthy' and the value 'true'. $ kubectl label pods foo unhealthy=true # Update pod 'foo' with the label 'status' and the value 'unhealthy', overwriting any existing value. $ kubectl label --overwrite pods foo status=unhealthy # Update all pods in the namespace $ kubectl label pods --all status=unhealthy # Update a pod identified by the type and name in "pod.json" $ kubectl label -f pod.json status=unhealthy # Update pod 'foo' only if the resource is unchanged from version 1. $ kubectl label pods foo status=unhealthy --resource-version=1 # Update pod 'foo' by removing a label named 'bar' if it exists. # Does not require the --overwrite flag. $ kubectl label pods foo bar-
kubectl logs
Affiche les logs d'un conteneur dans un pod :
# Return snapshot logs from pod nginx with only one container $ kubectl logs nginx # Return snapshot of previous terminated ruby container logs from pod web-1 $ kubectl logs -p -c ruby web-1 # Begin streaming the logs of the ruby container in pod web-1 $ kubectl logs -f -c ruby web-1 # Display only the most recent 20 lines of output in pod nginx $ kubectl logs --tail=20 nginx # Show all logs from pod nginx written in the last hour $ kubectl logs --since=1h nginx
kubectl patch
Met un jour le champ d'une ressource :
# Partially update a node using strategic merge patch $ kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}' # Partially update a node identified by the type and name specified in "node.json" using strategic merge patch $ kubectl patch -f node.json -p '{"spec":{"unschedulable":true}}' # Update a container's image; spec.containers[*].name is required because it's a merge key $ kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}' # Update a container's image using a json patch with positional arrays $ kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]
kubectl port-forward
Forward un port local vers un POD :
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod $ kubectl port-forward mypod 5000 6000 # Listen on port 8888 locally, forwarding to 5000 in the pod $ kubectl port-forward mypod 8888:5000 # Listen on a random port locally, forwarding to 5000 in the pod $ kubectl port-forward mypod :5000 # Listen on a random port locally, forwarding to 5000 in the pod $ kubectl port-forward mypod 0:5000
kubectl proxy
Met un proxy pour l'API kubernetes :
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod $ kubectl port-forward mypod 5000 6000 # Listen on port 8888 locally, forwarding to 5000 in the pod $ kubectl port-forward mypod 8888:5000 # Listen on a random port locally, forwarding to 5000 in the pod $ kubectl port-forward mypod :5000 # Listen on a random port locally, forwarding to 5000 in the pod $ kubectl port-forward mypod 0:5000
kubectl replace
Remplace une ressource par une autre :
# Replace a pod using the data in pod.json. $ kubectl replace -f ./pod.json # Replace a pod based on the JSON passed into stdin. $ cat pod.json | kubectl replace -f - # Update a single-container pod's image version (tag) to v4 $ kubectl get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | kubectl replace -f - # Force replace, delete and then re-create the resource $ kubectl replace --force -f ./pod.json
kubectl rolling-update
Remplace un replication controller par un autre :
# Update pods of frontend-v1 using new replication controller data in frontend-v2.json. $ kubectl rolling-update frontend-v1 -f frontend-v2.json # Update pods of frontend-v1 using JSON data passed into stdin. $ cat frontend-v2.json | kubectl rolling-update frontend-v1 -f - # Update the pods of frontend-v1 to frontend-v2 by just changing the image, and switching the # name of the replication controller. $ kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2 # Update the pods of frontend by just changing the image, and keeping the old name. $ kubectl rolling-update frontend --image=image:v2 # Abort and reverse an existing rollout in progress (from frontend-v1 to frontend-v2). $ kubectl rolling-update frontend-v1 frontend-v2 --rollback
kubectl rollout
kubectl rollout history
Affiche l'historique des déploiements :
# View the rollout history of a deployment $ kubectl rollout history deployment/abc # View the details of deployment revision 3 $ kubectl rollout history deployment/abc --revision=3
kubectl rollout pause
Met le déploiement d'une ressource en pause :
# Mark the nginx deployment as paused. Any current state of # the deployment will continue its function, new updates to the deployment will not # have an effect as long as the deployment is paused. $ kubectl rollout pause deployment/nginx
kubectl rollout resume
Continue le déploiement d'une ressource qui était en pause :
# Resume an already paused deployment $ kubectl rollout resume deployment/nginx
kubectl rollout undo
Effectue un retour arrière sur un déploiement précédent :
# Rollback to the previous deployment $ kubectl rollout undo deployment/abc # Rollback to deployment revision 3 $ kubectl rollout undo deployment/abc --to-revision=3
kubectl run
Démarre un conteneur à partir d'une image :
# Start a single instance of nginx. kubectl run nginx --image=nginx # Start a single instance of hazelcast and let the container expose port 5701 . kubectl run hazelcast --image=hazelcast --port=5701 # Start a single instance of hazelcast and set environment variables "DNS_DOMAIN=cluster" and "POD_NAMESPACE=default" in the container. kubectl run hazelcast --image=hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default" # Start a replicated instance of nginx. kubectl run nginx --image=nginx --replicas=5 # Dry run. Print the corresponding API objects without creating them. kubectl run nginx --image=nginx --dry-run # Start a single instance of nginx, but overload the spec of the deployment with a partial set of values parsed from JSON. kubectl run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }' # Start a pod of busybox and keep it in the foreground, don't restart it if it exits. kubectl run -i -t busybox --image=busybox --restart=Never # Start the nginx container using the default command, but use custom arguments (arg1 .. argN) for that command. kubectl run nginx --image=nginx -- <arg1> <arg2> ... <argN> # Start the nginx container using a different command and custom arguments. kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN> # Start the perl container to compute π to 2000 places and print it out. kubectl run pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)' # Start the scheduled job to compute π to 2000 places and print it out every 5 minutes. kubectl run pi --schedule="0/5 * * * ?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'
kubectl scale
# Scale a replicaset named 'foo' to 3. kubectl scale --replicas=3 rs/foo # Scale a resource identified by type and name specified in "foo.yaml" to 3. kubectl scale --replicas=3 -f foo.yaml # If the deployment named mysql's current size is 2, scale mysql to 3. kubectl scale --current-replicas=2 --replicas=3 deployment/mysql # Scale multiple replication controllers. kubectl scale --replicas=5 rc/foo rc/bar rc/baz # Scale job named 'cron' to 3. kubectl scale --replicas=3 job/cron
kubectl uncordon
Rend le nœud à nouveau éligible pour la création de PODs (voir kubectl cordon):
# Mark node "foo" as schedulable. $ kubectl uncordon foo
kubectl version
Affiche la version client/serveur :
$ kubectl version