Skip to content

Instantly share code, notes, and snippets.

@joshuaquek
Last active August 27, 2021 03:24
Show Gist options
  • Save joshuaquek/bbb2a8e31663bff561aaf5de7d7d1cd3 to your computer and use it in GitHub Desktop.
Save joshuaquek/bbb2a8e31663bff561aaf5de7d7d1cd3 to your computer and use it in GitHub Desktop.
Summary: Useful Kubectl commands for deploying & managing apps in a Kubernetes cluster (Minikube as example)
#!/bin/bash
## Deploy a simple dummy echo service
kubectl create deployment hello-minikube1 --image=k8s.gcr.io/echoserver:1.3
## Expose the service via a NodePort and map the internal port 8080 to an external port, which is generated when the command is executed
kubectl expose deployment hello-minikube1 --type=NodePort --port 8080
## Minikube does not support LoadBalancers, so this example ends here
#!/bin/bash
## Deploy a simple dummy echo service
kubectl create deployment hello-minikube1 --image=k8s.gcr.io/echoserver:1.3
## Expose the service via a NodePort and map the internal port 8080 to an external port, which is generated when the command is executed
kubectl expose deployment hello-minikube1 --type=NodePort --port 8080
## Get the generated external port and echo it
export NODE_PORT=$(kubectl get services hello-minikube1 -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
## Curl the exposed port
curl $(minikube ip):$NODE_PORT
#!/bin/bash
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
kubectl label pod $POD_NAME app=v1
kubectl describe pods $POD_NAME
#!/bin/bash
## Delete the pod
kubectl delete service -l run=hello-minikube1
## Confirm that the pod is deleted
curl $(minikube ip):$NODE_PORT
#!/bin/bash
## Scale up the number of instances to 4 replicasets within the Pod
kubectl scale deployments hello-minikube1 --replicas=4
## Check that the ReplicaSets were scaled up to 4
kubectl get deployments
kubectl get pods -o wide
## Note: A NodePort is able to load balance between the replicasets automatically
## Scale down the number of instances to 2 replicasets within the Pod
kubectl scale deployments hello-minikube1 --replicas=2
## Check that the ReplicaSets were scaled down to 2
kubectl get deployments
kubectl get pods -o wide
#!/bin/bash
## Update image from version echoserver:1.3 to echoserver:1.4
kubectl set image deployments hello-minikube1 hello-minikube1=k8s.gcr.io/echoserver:1.4
## Check Rolling Update Status
kubectl rollout status deployments hello-minikube1
## Performing a Rollback/Undo for a Rolling Update
kubectl rollout undo deployments/kubernetes-bootcamp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment