kubectl

Kubernetes is for clusters, cluster love kube.

Install kubectl

$ gcloud components install kubectl

On the google container engine, the google cloud SDK is installed in one of these locations:

/usr/local/share/google/google-cloud-sdk
/usr/lib/google-cloud-sdk

If gcloud doesn't allow upgrades, the config file must be configured to allow for it:

$ sed -i -e 's/true/false/' \
  /usr/local/share/google/google-cloud-sdk/lib/googlecloudsdk/core/config.json

If kubectl is installed but the bin cannot be found, you'll probably need to source it from the path:

PATH="/usr/lib/google-cloud-sdk/bin:$PATH"

Enable from gcloud

$ gcloud container clusters list
$ gcloud container clusters get-credentials <cluster_name>

Run a pod

Requires a working cluster, preferably created on the goog cloud.

# runs the "hello-node" example from google
$ kubectl run <deployment_name> \
  --image=gcr.io/google-samples/node-hello:1.0 \
  --port=8080

Expose a deployment to the outside world

$ kubectl expose deployment <deployment_name> --type="LoadBalancer"

List deployments

$ kubectl get deployments

List service info

Useful to retrieve open ports and public IP for a service. If you're not sure which services are available, run kubectl get deployments first.

$ kubectl get services <service_name>

List pods

$ kubectl get pods

Delete pod

Usually you'd want to delete a deployment though, but if you want to delete a pod do:

$ kubectl get pods
# kubectl delete pod <pod_name>

Echo cluster info

Echo the DNS, dashboard, Heapster and other stuff. Useful if you wanna login to the dashboard:

$ kubectl cluster-info

View logs

Get the pod name using kubectl get pods and then:

$ kubectl logs <pod_name>

View config

$ kubectl config view

View configuration

If you're trying to login to the admin view / dashboard:

# get dashboard url
$ kubectl cluster-info | grep kubernetes-dashboard | awk '{ print $5 }'

# get configuration; grab the values under "users" to log into the admin UI
$ kubectl config view

Scaling deployments

$ kubectl scale deployment <deployment_name> --replicas=4
$ kubectl get deployment
$ kubectl get pods

Deleting deployments

$ kubectl get deployments
$ kubectl delete deployments <deployment_name>

Create a secret from a file

$ kubectl create secret generic <secret_name> \
  --from-file=ssh-privatekey=/path/to/.ssh/id_rsa \
  --from-file=ssh-publickey=/path/to/.ssh/id_rsa.pub

Or from a .yaml file:

$ kubectl create secret -f ./my-secret.yml

Create a deployment from a file

$ kubectl apply -f ./my-deployment.yaml

Roll back a deployment

$ apply -f docs/user-guide/bad-nginx-deployment.yaml
$ kubectl get rs
$ kubectl get pods
$ kubectl describe deployment
$ kubectl rollout history deployment/nginx-deployment
$ kubectl rollout history deployment/nginx-deployment --revision=2
$ kubectl rollout undo deployment/nginx-deployment
$ kubectl rollout undo deployment/nginx-deployment --to-revision=2

Debugging pods and containers

Sometimes things break and you need to debug stuff. For example SSH in:

$ kubectl get po
$ kubectl describe pods ${POD_NAME}

Attach to existing pod

Almost like ssh:

$ kubectl get po
$ kubectl exec -it <podname> -- bash

See Also

results matching ""

    No results matching ""