{"id":639,"date":"2018-09-16T21:31:37","date_gmt":"2018-09-16T19:31:37","guid":{"rendered":"http:\/\/dekarlab.de\/wp\/?p=639"},"modified":"2020-05-23T15:34:43","modified_gmt":"2020-05-23T13:34:43","slug":"book-notes-kubernetes-up-and-running-dive-into-the-future-of-infrastructure","status":"publish","type":"post","link":"https:\/\/dekarlab.de\/wp\/?p=639","title":{"rendered":"Book notes \u2013 Kubernetes: Up and Running: Dive into the Future of Infrastructure"},"content":{"rendered":"<p>You can buy this book from <a href=\"https:\/\/www.amazon.de\/Kubernetes-Running-Dive-Future-Infrastructure\/dp\/1491935677\/ref=sr_1_1?ie=UTF8&#038;qid=1537117718&#038;sr=8-1&#038;keywords=kubernetes+up+and+running\">amazon.de<\/a>.<br \/>\n<!--more--><\/p>\n<p>This book descirbes not only, how Kubernetes works, but also design principles and ideas behind it. Notes below are ideas from this book with my own understanding. Therefore it is recommended also to read this nice book.<\/p>\n<p>Containers should be <strong>immutable<\/strong>. That means, you should change them only during build process, not during runtime. Runtime changes only acceptable, if you would like quick recover running system, but these changes should be then integrated in build scripts.<\/p>\n<p>State handling is very important in Kubernetes. You define state of your systems, send this state to Kubernetes, and Kubernetes will apply this state to running system. For example, you need 2 parallel running containers instead of 1. You inform about this Kubernetes and it starts additional container for you. State is better to save in git, so anytime you are able to see in what state the system right now, and what state was before.<\/p>\n<p>Self-healing is one very attractive property. Kubernetes has possibility to perform automatic checks for health of the components. But it is clear, that development of components should be done with this idea in mind. In world of data applications, that means, components should be restartable, without the need of cleaning up the data storage.<\/p>\n<p>Define microservices independent from each other. They should share data only through API. This helps to reduce teams dependency and coordination. In world of data applications your microservice can have own domain model and should not share it with other applications, like it is done in data warehousing.<\/p>\n<p>Teams in Kubernetes world: Application, Cluster, Kernel, Hardware.<\/p>\n<p>Namespaces is very powerfull in Kubernetes. For example, if you have DEV\/TEST\/PROD environments, they can share same hardware, but can be separated on logical view using dedicated namespaces. For data intensive analytical applications this is a very nice feature, since for copying data from one to another environment and keeping it in sync is very time consuming task. And simply reusing data but in logically separated way will help to reduce development time.<\/p>\n<p>Layers in docker should be created from less likely changed layer to more often changed layers. Be careful, files, which are deleted on upper layers in any case occupy space.<\/p>\n<p>Secrets and images should never be mixed.<\/p>\n<p>Use registry for storing containers.<\/p>\n<p>Commands:<br \/>\n<strong>kubectl version<br \/>\nkubectl get componentstatuses<br \/>\nkubectl get nodes<br \/>\nkubectl describe nodes <node name><\/strong><\/p>\n<p>Request vs limit for resources. Request is minimum needed, but can be more, limit is maximum allowed. CPU &#8211; easy to reduce, Memory, container will be killed.<\/p>\n<p>Proxy<br \/>\nfrom network to services<br \/>\n<strong>kubectl get daemonSets &#8211;namespace=kube-system kube-proxy<\/strong><\/p>\n<p>DNS<br \/>\nNames for services<br \/>\n<strong>kubectl get deployments &#8211;namespace=kube-system kube-dns<\/strong><br \/>\n<strong>kubectl get services &#8211;namespace=kube-system kube-dns<\/strong><br \/>\n\/etc\/resolv.conf<\/p>\n<p>UI<br \/>\n<strong>kubectl get deployments &#8211;namespace=kube-system kubernetes-dashboard<\/strong><br \/>\n<strong>kubectl get services &#8211;namespace=kube-system kubernetes-dashboard<\/strong><\/p>\n<p>Cluster-IP vs Network IP<\/p>\n<p>Context<br \/>\n<strong>kubectl config set-context my-context &#8211;namespace=abc<br \/>\nkubectl config use-context use-context my-context<\/strong><\/p>\n<p>Objects<br \/>\nAs it was said before kubernetes check state of the system. System consists of objects, therefor to see current state or change state the following command can be used:<br \/>\n<strong>kubectl get [resource-name] [object-name] -o [json or yaml]<\/strong><br \/>\n<strong>kubectl describe [resource-name] [object-name] -o [json or yaml]<\/strong><br \/>\n<strong>kubectl apply -f obj.yaml<\/strong><br \/>\n<strong>kubectl edit [resource-name] [object-name]<\/strong><br \/>\n<strong>kubectl delete -f obj.yaml<\/strong><\/p>\n<p>Labels\/Annotations (add and remove)<br \/>\n<strong>kubectl label pods bar color=red<br \/>\nkubectl label pods bar -color<\/strong><\/p>\n<p>Logs<br \/>\n<strong>kubectl logs [pod-anme] -c [container]<\/strong><\/p>\n<p>Execution of commands in container:<br \/>\n<strong>kubectl exec -it [pod-name] &#8212; bash<\/strong><\/p>\n<p>Copy<br \/>\n<strong>kubectl cp [pod-name]:\/path\/to\/remote \/path\/to\/local<\/strong><\/p>\n<p>Pods<br \/>\nAll containers in pod always run on the same machine.<br \/>\n&#8220;Will these containers work correctly if they land on different machines?&#8221; no => use same pod. yes => use multiple pods.<br \/>\nPossible create pod as:<br \/>\n<strong>kubectl run [pod-name] &#8211;immage=[image path]<\/strong><br \/>\n<strong>kubectl delete deployments\/[pod-name]<\/strong><br \/>\nBetter is to change state and kubernetes will do this for you:<br \/>\n<strong>kubectl apply -f [pod-name].yaml<\/strong><br \/>\n<strong>kubectl get pods<\/strong><br \/>\n<strong>kubectl describe pods [pod-name]<\/strong><br \/>\n<strong>kubectl delete pods\/[pod-name]<\/strong><br \/>\n<strong>kubectl delete -f [pod-name].yaml<\/strong><\/p>\n<p>Access pod from outside:<br \/>\n<strong>kubectl port-forward [pod-name] [port-pod]:[ext:pod]<\/strong><\/p>\n<p>Health Checks<br \/>\nliveness &#8211; checks app specific logic<br \/>\nreadiness &#8211; ready to serve user requests<br \/>\ntcpSocket &#8211; checks socket<br \/>\nexec &#8211; any custom check<\/p>\n<p>Resource Management<br \/>\nRequests vs Limits<\/p>\n<p>Persisting Data with Volumes<br \/>\nContainer deleted => data is also deleted. Volumes are needed for persisted data.<br \/>\nPods => volumes => volumes may be accessed<br \/>\nContainers => volumeMounts => volume accessed<br \/>\nSpecial volume emptyDir => for caching, shared folder between containers. Will be deleted after pod is deleted.<\/p>\n<p>Labels\/Annotations<br \/>\nLabels => grouping objects in Kubernetes<br \/>\nAnnotations => description of objects for using by tools.<br \/>\nLabels can be used to separate between different environments: dev\/test\/prod. As labels we can mark different versions of application.<br \/>\n<strong>kubectl get pods &#8211;show-labels<\/strong><br \/>\nPods can be selected by labels<br \/>\n<strong>kubectl get pods &#8211;selector=&#8221;[label=value]&#8221;<\/strong><br \/>\nDifferent selectors are possible.<br \/>\nSelectors can be used also in yaml.<\/p>\n<p>Annotations can be used for<br \/>\n&#8211; track of changes<br \/>\n&#8211; what tool updates object<br \/>\n&#8211; build\/release\/image info<br \/>\n&#8211; track rollbacl for Deployment object<\/p>\n<p>Services and Service Discovery<br \/>\nService object in Kubernetes is for service discovery.<br \/>\n<strong>kubectl run [deploymnet-name]<br \/>\nkubectl expose deployment [deploymnet-name]<br \/>\nkubectl port-forward [pod] [pod port]:[external pod]<br \/>\n<\/strong><br \/>\nCluster IP is a virtual IP.<br \/>\nDNS<br \/>\n[service-name].[namespace].svc.cluster.local<br \/>\nsvc means service.<\/p>\n<p><strong>kubectl get endpoints [service name]<\/strong> &#8211;watch see changes of object overtime.<br \/>\nNodePort &#8211; access cluster from outside.<br \/>\nLoadBalancer &#8211; cloud integration<br \/>\nEndpoints &#8211; for accessing services without cluster IP<\/p>\n<p>Old way of service discovery using labels, but they should be in sync. New way using service object.<\/p>\n<p>Comunication<br \/>\nRequest => kube-proxy => Cluster IP (load balance + iptables rules to redirect) => Endpoints => Service<\/p>\n<p>ReplicaSets &#8211; pod manager replicates needed pods<br \/>\n&#8211; Redundancy<br \/>\n&#8211; Scale<br \/>\n&#8211; Sharding<br \/>\nReplicaSets are for self healing.<\/p>\n<p>Reconciliation Loops => check of desired state and current state. ReplicSet uses labels for this.<\/p>\n<p>Action for misbehaved pods => put them in quarantine using ReplicaSet.<br \/>\n<strong>kubectl describe rs [replica set name]<\/strong><\/p>\n<p>HPA horizontal (creates more pods) pod autoscaling. Special pod heapster communicates current load.<\/p>\n<p>Vertical (increases CPU) scaling is planned for implementation in Kubernetes.<\/p>\n<p>Always use ReplicaSet, also if only one pod is needed. This will help with scaling in the future.<\/p>\n<p>DaemonSets<br \/>\nDaemonSet is used to run one instance of specific pod on every node on cluster. This is like agents, for example logs collecting agents.<\/p>\n<p><strong>kubectl describe daemonset [ds name]<\/strong><br \/>\nIt is possible to label also nodes, not pods. And then use them in DaemonSet.<br \/>\nDaemonSets support rolling update.<\/p>\n<p>Jobs are useful for batch tasks.<br \/>\nPatterns: one shot, parallel fixed completions (parallelism and completions), work queue: parallel Jobs (producers and consumers).<\/p>\n<p>ConfigMaps and Secrets<br \/>\nConfigMap are supposed to be pod configuration for different environments. ConfigMap and Pod are combined together during start up.<br \/>\nConfigMap can be created from file, environment varible, command line arguments.<br \/>\nSecrets can be created in a cluster. The can be accessible throug secrets volume.<\/p>\n<p>Deployment<br \/>\nDeployment manages ReplicaSets.<br \/>\nStrategies: Recreate and RollingUpdate<\/p>\n<p><strong>kubectl rollout status deployment [name]<\/strong><br \/>\n<strong>kubectl rollout pause deployment [name]<\/strong><br \/>\n<strong>kubectl rollout resume deployment [name]<\/strong><br \/>\n<strong>kubectl rollout history deployment [name]<\/strong><br \/>\n<strong>kubectl rollout undo deployment [name]<\/strong><br \/>\nInstead of rollout undo it is better to revert YAML files from version control system.<\/p>\n<p>Dynamic Volume Provisioning<br \/>\nStorageClass<br \/>\nPersistentVolumeClaim<\/p>\n<p>StatefulSets<br \/>\n&#8211; each replica gets persistent hostname name-i<br \/>\n&#8211; each replica created in order of i<br \/>\n&#8211; each replica delted in reverse order of i<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can buy this book from amazon.de.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[27,25],"tags":[57,35,49,50,31],"_links":{"self":[{"href":"https:\/\/dekarlab.de\/wp\/index.php?rest_route=\/wp\/v2\/posts\/639"}],"collection":[{"href":"https:\/\/dekarlab.de\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dekarlab.de\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dekarlab.de\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dekarlab.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=639"}],"version-history":[{"count":41,"href":"https:\/\/dekarlab.de\/wp\/index.php?rest_route=\/wp\/v2\/posts\/639\/revisions"}],"predecessor-version":[{"id":700,"href":"https:\/\/dekarlab.de\/wp\/index.php?rest_route=\/wp\/v2\/posts\/639\/revisions\/700"}],"wp:attachment":[{"href":"https:\/\/dekarlab.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dekarlab.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dekarlab.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}