Kubernetes


Components

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

# view Kubernetes objects
kubectl get all [-l app=nginx]    # all resources [with a label app=nginx]
kubectl get <resource>            # all resources in a namespace
kubectl get <resource> <object>   # specific resource

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

# 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

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

# delete objects
kubectl delete -f obj.yaml  # no additional prompting!

# delete objects
kubectl delete <resource> <object>

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

# debugging
kubectl logs <pod>
kubectl exec -it <pod> -- bash  # or sh instead of bash
kubectl cp <pod>:/path/to/remote/file /path/to/local/file

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

# Accessing a Pod via port forwarding
kubectl port-forward kuard 8080:8080 # tunnel: localhost -> k8s master -> k8s worker node

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

Looking beyond the cluster

NodePorts

Resources

comments powered by Disqus