Kubernetes Tipps, Tricks and Reads

Kubernetes Tidbits

Let’s face it. Most of us are not using the Kubernetes CLI every day. This posting is more a reminder for myself; I’d like to list some little helpers that help to improve your Kubernetes command-line skills:

Kubernetes Contexts

Show all available contexts (e.g. Minikube, GKE, Oracle Wercker):

$ kubectl config view --minify

YAML Output

Get the output of a deployment as more readable as YAML.

$ kubectl get deployment my-nginx -o yaml

Set custom namespace as default

Set default namespace, e.g. when working in a shared cluster.

$ kubectl config set-context $(kubectl config current-context) --namespace=XYZ
# Validate it
$ kubectl config view | grep namespace:

This one is again more a note to myself. Altogether I have spent way too much time to discover how to talk to containers running in Kubernetes from outside the Kubernetes cluster (Kubernetes for Docker, Oracle, GKE).

Run Pod via kubectl run then use kubectl expose deployment to expose via NodePort

  • deploy the container with kubectl run microg --image=fmunz/microg --port 5555
  • you will not see it as a service, e.g kubectl get services
  • expose the pod with kubectl expose deployment microg --type=NodePort. Note that other types are possible, see section below for deployment with a YAML file.
  • get the NodePort with kubectl describe service microg | grep NodePort
  • you will see the new service exposed as kubectl describe service microg

Deploy with Service YAML

For “kind” in the service YAML specify either (partly taken from K8s doc):

  • ClusterIP: Exposes the service on a cluster-internal IP. Choosing this value makes the service only reachable from within the cluster. This is the default ServiceType. To be able to talk to your service, start a Proxy server: kubectl proxy --port=8080

  • NodePort: Exposes the service on each Node’s IP at a static port (the NodePort). A ClusterIP service, to which the NodePort service will route, is automatically created. You’ll be able to contact the NodePort service, from outside the cluster, by requesting <nodeip>:<nodeport>. To find the NodePort, use the following command: kubectl describe service microg | grep NodePort

  • LoadBalancer: Exposes the service externally using a cloud provider’s load balancer. NodePort and ClusterIP services, to which the external load balancer will route, are automatically created.


Other resources

I will add more here for sure. These days I am using K8s a lot 🙂 Also check out the following resources:

To be continued