# Kubernetes

# Structures

# Commands

  • export KUBECONFIG=~/kube/test-kubeconfig
  • kubectl -n namespace apply -f xxx.yaml
  • kubectl -n namespace delete -f xxx.yaml
  • kubectl -n namespace get
    • pod(s): list all pods under this namespace
    • pod pod_name
    • svc: list all services under this namespace
    • svc svc_name
    • deploy: list all deployments under this namespace
    • deploy deploy_name
    • deploy deploy_name -o yaml > xxx.k8s.yaml: output current deployment config to yaml
    • namespace(s): can be got without namespace
    • node(s): can be got without namespace
  • kubectl get pods -o wide --all-namespaces
  • kubectl -n namespace describe
    • pod pod_name
    • svc svc_name
  • kubectl -n namespace -f --since=1m logs
    • pod_name
    • -f: if to stream the logs in real time
    • --since: logs since time ago
  • kubectl -n namespace port-forward --address 0.0.0.0
    • svc/svc_name local_port:port_in_service_yaml
    • pod/pod_name local_port:port_in_deployment_yaml
  • sudo -E kubectl -n namespace port-forward
    • -E used to pass environment variables
    • svc/svc_name 80:port_in_service_yaml
    • pod/pod_name 80:port_in_deployment_yaml
  • kubectl -n namespace scale deploy/deploy_name --replicas 0

# Bindings

  • makes sure a service has a pod binding, defined in selector attribute in yaml
  • selector in service should match label in pods

# CI/CD

  • services can be deployed once
  • product pods needed deploying every build
  • delete a pod and deployment will auto create a new one
  • internal service address: service_name.namespace

# Spinnaker

When service, ingress, etc. are fixed, deployment updates can be done by spinnaker:

  1. kubectl -n namespace get deploy deploy_name -o yaml > xxx.k8s.yaml
  2. remove non-necessary attributes: metadata.selfLink, metadata.uid, metadata.resourceVersion, metadata.creationTimestamp, metadata.annotations, metadata.generation
  3. reserve namespace
  4. update image: 'image_url:${parameters.IMAGE_VERSION}'
  5. copy to config
  6. as long as there is character change in this yaml file, pod will be updated

If no spinnaker, manually deployment:

  1. same steps
  2. remove namespace
  3. update image: image_url:right.version.0
  4. kubectl -n namespace apply -f xxx.k8s.yaml

# DNS

DNS queries may be expanded using the pod's /etc/resolv.conf. Kubelet sets this file for each pod. For example, a query for just data may be expanded to data.test.svc.cluster.local. The values of the search option are used to expand queries. To learn more about DNS queries, see the resolv.conf manual page.

kubectl exec -it container-name -n its-namespace-name sh
cat /etc/resolv.conf

In some cases /etc/resolv.conf is not used:

kubectl exec -it container-name -n its-namespace-name sh
nslookup svc_name

# Remarks

currently(v1.18.2) kubectl cp non-existing file(source files) fails silently

Last Updated: 3/27/2023, 4:00:33 PM