Kubernetes


Basics

Kubernetes is the operating system for cloud-native applications.

Configuration

cat ~/.kube/config
# or
kubectl config view

Namespace

kubectl get namespaces

Context

# list contexts
kubectl config get-contexts

# switch context
kubectl config use-context <context-name>

Objects

Basic objects

Basic objects

# view Kubernetes objects/resources
kubectl get all [-l app=nginx] # all resources [with a label app=nginx]
kubectl get <type>             # all resources of given type
kubectl get <type> <object>    # specific resource

# details about an object
kubectl describe <type> <object>

# create, update objects
kubectl apply -f obj.yaml

# delete objects
kubectl delete -f obj.yaml  # no additional prompting!
kubectl delete <type> <object>

Pod

# Pod manifest - just a text-file representation of the Kubernetes API object
$ cat kuard-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: kuard
spec:
  containers:
    - image: gcr.io/kuar-demo/kuard-amd64:1
      name: kuard
      ports:
        - containerPort: 8080
          name: http
          protocol: TCP
# Creating a Pod
kubectl apply -f kuard-pod.yaml

What should I put into a single pod?

Deployment

One way to create a deployment:

kubectl create deployment quotes-prod --image=reisinge/quotes
kubectl scale deployment quotes-prod --replicas=3
kubectl label deployment quotes-prod ver=1 env=prod

Service

One way to create a service:

kubectl expose deployment quotes-prod --port=80 --target-port=5000

Ingress

https://kubernetes.io/docs/concepts/services-networking/ingress/

Looking beyond the cluster

NodePort

Tips and tricks

Useful output flags:

-o wide       # more details
-o json       # complete object in JSON format
-o yaml       # complete object in YAML format
--v=6         # verbosity
--no-headers

Clean up objecs:

kubectl delete deployments --all [--selector="app=myapp,env=dev"]

Create a proxy server between localhost and K8s API server:

kubectl proxy &                  # create proxy
curl localhost:8001/api/v1/pods  # get list of pods

Explain resource types:

kubectl explain svc

Debugging

Seeing logs:

kubectl logs [-f] <pod>
kubectl exec -it <pod> -- bash  # or sh instead of bash

Copying files:

kubectl cp <pod>:/path/to/remote/file /path/to/local/file

Access a Pod via port forwarding:

kubectl port-forward kuard 8080:8080  # tunnel: localhost -> k8s master -> k8s worker node

Running containers for troubleshooting:

kubectl run demo --image=cloudnatived/demo:hello --expose --port 8888 # pod to troubleshoot
kubectl run nslookup --image=busybox:1.28 --rm -it --restart=Never --command -- nslookup demo
kubectl run wget --image=busybox:1.28 --rm -it -restart=Never --command -- wget -qO- http://demo:8888

Resources

comments powered by Disqus