Skip to content

Instantly share code, notes, and snippets.

@brandon-braner
Last active December 15, 2022 21:07
Show Gist options
  • Save brandon-braner/599da3dd6f5d68b6d28c234a8c0f9a66 to your computer and use it in GitHub Desktop.
Save brandon-braner/599da3dd6f5d68b6d28c234a8c0f9a66 to your computer and use it in GitHub Desktop.
Kubernetes Imperative Commands

Create a pod

kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...] [options]

create nginx pod with env vars exposed on port 80

kubectl run nginx-pod --image=nginx --env="DNS_DOMAIN=cluster" --env="SOME_SECRET=secret" --port=80

create python pod and print the version number.

kubectl run py-command --image=python:3.11-buster --restart=Never --command -- 'python' '-V'

Create a service

Nodeport

kubectl create service nodeport my-ns --tcp=8085:80 --node-port=30800

expose a od on a nodeport

kubectl expose pod nginx-pod --port=8080 --type=NodePort --target-port=80

Deployments

Create deployments

Create actual deployment

kubectl create deployment nginx --image=nginx --port=80

create deployment manifest file

kubectl create deployment nginx --image=nginx --port=80 --dry-run=client -o yaml > deployment.yaml

Update deployment image

kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1

update the deployment above from nginx:1.21.1 to 1.23.2

kubectl set image deployment/nginx nginx=nginx:1.23.2

Rollouts

Check rollout status

Get the deployemnt name

kubectl get deployments

NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           11m

Check the status

kubectl rollout status deployment/nginx

deployment "nginx" successfully rolled out

Rollback

Undo the latest rollout

kubectl rollout undo deployment/nginx

Rollback to a specific rollout

kubectl rollout history deployment/nginx

kubectl rollout history deployment/nginx
deployment.apps/nginx
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

Rollback to revision 1

kubectl rollout undo deployment/nginx --to-revision=1
deployment.apps/nginx rolled back

Metrics API

  1. Install metrics server to provide data
  2. Access metrics data using kubectl top command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment