{"id":82902,"date":"2020-05-26T13:42:27","date_gmt":"2020-05-26T11:42:27","guid":{"rendered":"https:\/\/prohoster.info\/blog\/administrirovanie\/luchshie-praktiki-kubernetes-mapping-vneshnih-servisov"},"modified":"2020-05-26T13:42:27","modified_gmt":"2020-05-26T11:42:27","slug":"luchshie-praktiki-kubernetes-mapping-vneshnih-servisov","status":"publish","type":"post","link":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/luchshie-praktiki-kubernetes-mapping-vneshnih-servisov","title":{"rendered":"Kubernetes Best Practices: Mapping External Services","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p><noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/ua-hosting\/blog\/502052\/\">Best Practices for Kubernetes. Creating Small Containers<\/a><\/noindex><br \/>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/ua-hosting\/blog\/502320\/\">Best Practices for Kubernetes. Organizing Kubernetes with Namespaces<\/a><\/noindex><br \/>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/ua-hosting\/blog\/502430\/\">Best Practices for Kubernetes. Checking Kubernetes Health with Readiness and Liveness Tests<\/a><\/noindex><br \/>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/ua-hosting\/blog\/502614\/\">Best Practices for Kubernetes: Configuring Resource Requests and Limits<\/a><\/noindex><br \/>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/ua-hosting\/blog\/503488\/\">Kubernetes Best Practices. Proper Shutdown Terminate.<\/a><\/noindex><\/p>\n<p>If you are like most people, you are likely using resources that function outside of your cluster. You may be utilizing the Taleo API to send text messages or analyzing images with the Google Cloud Vision API.<noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><\/p>\n<p>If you are using the same endpoint \u2014 the request receiving point on the server side across all your environments and do not plan to move your servers to Kubernetes, then it is perfectly fine to have the service endpoint directly in your code. However, there are many other scenarios. In this 'Kubernetes Best Practices' series, you will learn how to use Kubernetes' built-in mechanisms for service discovery both inside and outside the cluster. <\/p>\n<p>An example of a widely used external service is a database operating outside a Kubernetes cluster. Unlike cloud databases such as Google Cloud Datastore or Google Cloud Spanner, which use a single endpoint for all access types, most databases have separate endpoints for different circumstances.<br \/>\nThe best practice for using traditional databases like MySQL and MongoDB typically involves connecting to different components for different environments. You might have a large machine for production data and a smaller machine for the testing environment. Each will have its own IP address or domain name, but you certainly do not want to change your code when switching from one environment to another. Instead, you can use Kubernetes' built-in service discovery for external services based on DNS just like for native Kubernetes services.<\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices: Mapping External Services\" src=\"\/wp-content\/uploads\/2020\/05\/b64f2f5857f32d2d927c8dad1e4b1e36.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nSuppose you are running a MongoDB database on Google Compute Engine. You will remain in this hybrid world until you can migrate it to the cluster.<\/p>\n<p>Fortunately, you can use static Kubernetes services to ease your life a bit. In this example, I created a MongoDB server using Google Cloud Launcher. Since it is created in the same network (or VPC of the Kubernetes cluster), it is accessed via a high-performance internal IP address.<\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices: Mapping External Services\" src=\"\/wp-content\/uploads\/2020\/05\/cafe19eabc8ac144d27bd06f6c44ac5f.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nIn Google Cloud, this is the default setting, so you don't have to configure anything. Now that there is an IP address, the first step is to create a service. You may notice that there are no pod selectors for this service. This means we have created a service that won't know where to send traffic. This will allow you to manually create an endpoint object that will receive traffic from this service. <\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices: Mapping External Services\" src=\"\/wp-content\/uploads\/2020\/05\/4f3643f3ce2f2d3f5b207429bb269058.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nThe next code example shows that endpoints define the IP address for the database, using the same mongo name as the service.<\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices: Mapping External Services\" src=\"\/wp-content\/uploads\/2020\/05\/dbba2655a9e2295cb8a7de4c97eddc8f.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nKubernetes will use all IP addresses to find endpoints as if they were regular Kubernetes pods, so now you can access the database with a simple connection string to the above-mentioned name mongodb:\/\/mongo. There is no need to use IP addresses in your code at all.<\/p>\n<p>If the IP addresses change in the future, you can simply update the endpoints with the new IP address, and your applications won't need to be modified in any additional way.<\/p>\n<p>If you are using a database hosted by a third party, the host owners likely provided you with a unified resource identifier URI for the connection. So if you were given an IP address, you can simply use the previous method. This example shows that I have two MongoDB databases hosted on mLab.<\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices: Mapping External Services\" src=\"\/wp-content\/uploads\/2020\/05\/088a578041c3d7c924c77f0a2e67e323.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nOne is the developer database, and the other is the production database. The connection strings for these databases look as follows \u2014 mLab provides you with a dynamic URI and a dynamic port. As you can see, they are different.<\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices: Mapping External Services\" src=\"\/wp-content\/uploads\/2020\/05\/41d2e22371e85b7e794a9df7b2db515d.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nTo abstract from this, we use Kubernetes and connect to the developer database. You can create an external Kubernetes service name that will provide you with a static service that will redirect traffic to the external service. <\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices: Mapping External Services\" src=\"\/wp-content\/uploads\/2020\/05\/ed07c1e9818a6641b29d5c941bdf68cb.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nThis service will perform a simple CNAME redirection at the kernel level, which will have minimal impact on performance. Thanks to this, you can use a simpler connection string.<\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices: Mapping External Services\" src=\"\/wp-content\/uploads\/2020\/05\/ee5442aec9c1a9d19f78bd92d44a97ec.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nHowever, since the external name uses CNAME redirection, it cannot perform port remapping. Therefore, this solution is applicable only for static ports and cannot be used with dynamic ports. However, the free mLab Free Tier by default provides a dynamic port number to the user, and you cannot change this. This means that you need different connection string commands for dev and prod. The downside is that this requires hardcoding the port number. So how do you get port remapping to work?<\/p>\n<p>The first step is to obtain the IP address from the URI. By executing the nslookup command, hostname, or pinging the URI, you can get the database's IP address. If the service returns multiple IP addresses, all of these can be used in the object endpoints.<\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices: Mapping External Services\" src=\"\/wp-content\/uploads\/2020\/05\/6cd63eb3c8bd733afbd7fe791ef7b8f4.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nIt is important to remember that URI IP addresses can change without prior notice, making them quite risky to use in prod. With such an IP address, you can connect to a remote database without specifying a port. Thus, the Kubernetes service transparently performs port remapping.<\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices: Mapping External Services\" src=\"\/wp-content\/uploads\/2020\/05\/3f718da15739fd42dc640f44b34c7cab.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nMapping, or associating external resources with internal ones, allows you to flexibly use these services within the cluster in the future while minimizing refactoring efforts. It also facilitates management and provides insight into the external services your company utilizes. <\/p>\n<p>Continuation will be available very soon\u2026<\/p>\n<p><center><div class=\"youtube-placeholder\" data-id=\"fvpq4jqtuZ8\" onclick=\"loadVideo(this)\">\r\n        <img decoding=\"async\" src=\"https:\/\/img.youtube.com\/vi\/fvpq4jqtuZ8\/hqdefault.jpg\" alt=\"Play video\" loading=\"lazy\" width=\"480\" height=\"360\" style=\"width:100%;height:auto;\">\r\n        <div class=\"play-button\"><\/div>\r\n    <\/div><\/center><\/p>\n<h3>A little advertisement \ud83d\ude42<\/h3>\n<p>\nThank you for staying with us. Do you enjoy our articles? Want to see more interesting content? Support us by placing an order or recommending us to your friends, <noindex><a rel=\"nofollow\" href=\"https:\/\/ua-hosting.company\/cloudvps\/nl\">cloud VPS for developers starting at $4.99<\/a><\/noindex>, <b>a unique entry-level server alternative that we have created for you:<\/b> <noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/company\/ua-hosting\/blog\/347386\/\">The whole truth about VPS (KVM) E5-2697 v3 (6 Cores) 10GB DDR4 480GB SSD 1Gbps from $19 or how to properly share a server?<\/a><\/noindex> (options available with RAID1 and RAID10, up to 24 cores and up to 40GB DDR4).<\/p>\n<p><b>Dell R730xd at half the price in the Equinix Tier IV data center in Amsterdam?<\/b> Only with us <b><noindex><a rel=\"nofollow\" href=\"https:\/\/ua-hosting.company\/serversnl\">2 x Intel TetraDeca-Core Xeon 2x E5-2697v3 2.6GHz 14C 64GB DDR4 4x960GB SSD 1Gbps 100TB starting at $199<\/a><\/noindex> in the Netherlands! <b>Dell R420 \u2014 2x E5-2430 2.2GHz 6C 128GB DDR3 2x960GB SSD 1Gbps 100TB \u2014 from $99!<\/b><\/b> Read about how <noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/company\/ua-hosting\/blog\/329618\/\">To build a corporate-class infrastructure using Dell R730xd E5-2650 v4 servers costing 9000 euros for peanuts?<\/a><\/noindex><br \/>\n<br \/>Source: <a content=\"nofollow\" rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/ua-hosting\/blog\/503612\/\">habr.com<\/a> <\/p>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>\u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 \u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes. \u041e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f Kubernetes \u0441 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e\u043c \u0438\u043c\u0435\u043d \u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes. \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0436\u0438\u0437\u043d\u0435\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 Kubernetes \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u0435\u0441\u0442\u043e\u0432 Readiness \u0438 Liveness \u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes. \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043b\u0438\u043c\u0438\u0442\u043e\u0432 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes. \u041a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 Terminate \u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u0445\u043e\u0436\u0438 \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u043b\u044e\u0434\u0435\u0439, \u0442\u043e, \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b, \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u0432\u0430\u0448\u0435\u0433\u043e [&hellip;]<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":1,"featured_media":82903,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[688],"tags":[],"class_list":["post-82902","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-administrirovanie"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.2 - aioseo.com -->\n\t<meta name=\"description\" content=\"\u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Yuri Gagarin\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/luchshie-praktiki-kubernetes-mapping-vneshnih-servisov\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.2\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"\ud83e\udd47\u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes. \u041c\u0430\u043f\u043f\u0438\u043d\u0433 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 | ProHoster\" \/>\n\t\t<meta property=\"og:description\" content=\"\u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/luchshie-praktiki-kubernetes-mapping-vneshnih-servisov\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t\t<meta property=\"og:image:height\" content=\"350\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2020-05-26T11:42:27+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2020-05-26T11:42:27+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"\ud83e\udd47Best Practices for Kubernetes. Mapping External Services | ProHoster","description":"Best practices for Kubernetes.","canonical_url":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/luchshie-praktiki-kubernetes-mapping-vneshnih-servisov","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b","og:type":"article","og:title":"\ud83e\udd47\u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes. \u041c\u0430\u043f\u043f\u0438\u043d\u0433 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 | ProHoster","og:description":"\u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes.","og:url":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/luchshie-praktiki-kubernetes-mapping-vneshnih-servisov","og:image":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:secure_url":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:width":350,"og:image:height":350,"article:published_time":"2020-05-26T11:42:27+00:00","article:modified_time":"2020-05-26T11:42:27+00:00","article:publisher":"https:\/\/www.facebook.com\/prohoster","article:author":"https:\/\/www.facebook.com\/prohoster"},"aioseo_meta_data":{"post_id":"82902","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"seo_analyzer_scan_date":null,"breadcrumb_settings":null,"limit_modified_date":false,"reviewed_by":null,"ai":null,"created":"2021-02-28 15:29:29","updated":"2022-10-02 08:31:16","focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/82902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/comments?post=82902"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/82902\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media\/82903"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media?parent=82902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/categories?post=82902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/tags?post=82902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}