# 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 namespacepod pod_name
svc
: list all services under this namespacesvc svc_name
deploy
: list all deployments under this namespacedeploy deploy_name
deploy deploy_name -o yaml > xxx.k8s.yaml
: output current deployment config to yamlnamespace(s)
: can be got without namespacenode(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 variablessvc/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 matchlabel
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:
kubectl -n namespace get deploy deploy_name -o yaml > xxx.k8s.yaml
- remove non-necessary attributes:
metadata.selfLink
,metadata.uid
,metadata.resourceVersion
,metadata.creationTimestamp
,metadata.annotations
,metadata.generation
- reserve
namespace
- update
image: 'image_url:${parameters.IMAGE_VERSION}'
- copy to config
- as long as there is character change in this yaml file, pod will be updated
If no spinnaker, manually deployment:
- same steps
- remove
namespace
- update
image: image_url:right.version.0
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