<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hitesh Pattanayak on Medium]]></title>
        <description><![CDATA[Stories by Hitesh Pattanayak on Medium]]></description>
        <link>https://medium.com/@hitesh-pattanayak?source=rss-183cc8bcd17d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*7WBxTsM-1NrJ99HO</url>
            <title>Stories by Hitesh Pattanayak on Medium</title>
            <link>https://medium.com/@hitesh-pattanayak?source=rss-183cc8bcd17d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 23:29:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hitesh-pattanayak/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Steps to choose fundamentally strong stocks]]></title>
            <link>https://hitesh-pattanayak.medium.com/steps-to-choose-fundamentally-strong-stocks-633a602bb2c5?source=rss-183cc8bcd17d------2</link>
            <guid isPermaLink="false">https://medium.com/p/633a602bb2c5</guid>
            <category><![CDATA[money-management]]></category>
            <category><![CDATA[long-term-investment]]></category>
            <category><![CDATA[stocks]]></category>
            <category><![CDATA[nifty]]></category>
            <category><![CDATA[fundamental-analysis]]></category>
            <dc:creator><![CDATA[Hitesh Pattanayak]]></dc:creator>
            <pubDate>Tue, 14 Nov 2023 08:37:38 GMT</pubDate>
            <atom:updated>2023-11-14T08:37:38.777Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Disclaimer</strong>: This is not a stock recommendation or the process to choose is not full-proof and does not gurantee all-weather stocks. The idea is to understand the underlying concepts. Also this is based on experiences with Indian stock exchanges.</p><p>This document focuses on identifying stocks with the potential for long-term returns, making them suitable for long-hold investments.</p><p>Given the vast number of stocks in the market, pinpointing lucrative investment opportunities can be challenging.</p><p>However, by posing pertinent questions and employing specific filters and statistics, we can pinpoint such stocks.</p><p>To facilitate this process, access to data and a mechanism for applying these filters is essential, and <a href="https://www.tickertape.in/">Tickertape</a> serves as a reliable platform for this purpose.</p><p>At the moment of writing this blog, we have all total of 4723 stocks listed in Indian Exchanges.</p><p>So lets start applying our filters:</p><ol><li><strong>Benchmark</strong>: Nifty 500</li><li><strong>Market cap</strong>: Greater than <strong>10000 crores<br></strong>The rationale for implementing the aforementioned 2 filters is our focus on stocks that promise satisfactory returns with lower to medium risks.<br>We now start filtering fundamentally good stocks.</li><li><strong>5 year net revenue</strong>: do not fall for this.</li><li><strong>5 year net profit margin</strong>: greater than 10 %.<br>Opting for net profit margin over revenue stems from the recognition that merely generating revenue without corresponding profits signifies unsustainable growth in the short term. Conversely, if a company demonstrates increased profits despite lower revenue, it reflects effective management and quality products. This is indicative of the company’s ability to sell products successfully even with a higher profit margin.</li><li><strong>5 years historical EPS (earnings per share)</strong>: greater than 10 %.</li><li><strong>5 years average return on equity</strong>: greater than 15 %. Otherwise one should go and invest in passive index funds that require almost no monitroing and relatively safe. Although there is no gurantee on this but we are relying on fundamentally good stocks.</li><li><strong>Dividend yield vs sub-sector</strong>: lesser than 2 %. We should prefer stocks that have lower dividend yields w.r.t sectoral dividen yield. If a comapany gives out excessive dividends, it means they are choosing to keep their investor happy over re-investing for future growths.</li><li><strong>Beta</strong>: lesser than 1.5 %. Here we are elliminating riskier stocks. Beta indicates how much sensitive is a stock w.r.t the market conditions, which means if market goes down/up how much is that going to affect the stock. These stocks are less cyclic and more stable.</li><li><strong>Pledged promoters holdings</strong>: less than 5 %. How much promoters have pledged stocks to get loans. The higher the number the greater the risk. For a reputed company, the money can be arranged without having to pledge stocks.</li><li><strong>Long term debt / Total Equity</strong>: This is a very sector specific paramenter. Sectors like Financial, Real Estate or Power sectors companies tend to take huge loans considering the nature of business. So we cannot pin point a relative number. So do a comparsion and choose accordingly.</li><li><strong>Percentage Buy Reco %</strong>: More than 50 %. This checks what analysts are trying to say about the companies. Since common individuals are not necessarily experts, its good to look what experts say.</li><li><strong>Difference of P/E and sector P/E ratio</strong>: less than 0. This indicates that the stock is undervalued. Undervalued stocks have always opportunity to rise.</li><li><strong>Price to intrinsic value</strong>: More than 50 %. This indicates that which companies are significantly undervalued to what they deserve.</li></ol><p>At the end we will arrive at 10–12 fundamentally strong stocks to invest in to get more than 15% of returns. Which beats the returns of traditional investment asset classes by a good margin. Also these kind of stocks helps us sleep well considering they operate on good factors, have ability to transmit inflatory price hikes, have good management personnel, etc.</p><p>As always, the list will keep changing on a quarter-to-quarter and year-to-year basis. So being vigilant and operating once a year/quarter basis will serve us good in a longer run.</p><p>Folks, if you like my content, would you consider following me on <a href="https://www.linkedin.com/in/hitesh-pattanayak/">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=633a602bb2c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Steps to choose safer stocks to invest]]></title>
            <link>https://hitesh-pattanayak.medium.com/steps-to-choose-safer-stocks-to-invest-459621d6d561?source=rss-183cc8bcd17d------2</link>
            <guid isPermaLink="false">https://medium.com/p/459621d6d561</guid>
            <category><![CDATA[long-term-investment]]></category>
            <category><![CDATA[nifty]]></category>
            <category><![CDATA[stocks]]></category>
            <category><![CDATA[money]]></category>
            <category><![CDATA[investment]]></category>
            <dc:creator><![CDATA[Hitesh Pattanayak]]></dc:creator>
            <pubDate>Tue, 14 Nov 2023 08:10:47 GMT</pubDate>
            <atom:updated>2023-11-14T08:22:30.032Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Disclaimer</strong>: This is not a stock recommendation or the process to choose is not full-proof and does not gurantee all-weather stocks. The idea is to understand the underlying concepts. Also this is based on experiences with Indian stock exchanges.</p><p>This document focusses on finding out stocks that are safe to invest. Safe-ness is about mitigating risks.</p><p>There are various categories of risks:</p><ol><li>Market risk</li><li>Valuation risk</li><li>Profitability risk</li><li>Liquidity risk</li><li>Solvency risk</li><li>Governance risk</li></ol><p>There are certain risks that cannot be measured but we can be aware of by lending ears to news:</p><ol><li>Regulatory risk → Governement regulations that might affect the sector or target company.</li><li>Management risk → Change in the management level personnel, might be for good or bad.</li><li>Headline risk → Some news, false or correct, just drive the stock price up or down.</li></ol><p>Our focus here should be to mitigate the first set of risks because that is in our control.</p><p>And in a longer run, shorter risks do not matter if companies are fundamentally sound.</p><p><a href="https://www.tickertape.in/">Tickertape</a> serves as a reliable platform to provide data, stats and filters which we can use to quantify the risks.</p><p>To begin with, its safer to invest in <strong>NIFTY 100</strong> stocks as they are stable companies.</p><ol><li><strong>Market risk</strong>: There is an indicator called <strong>Beta</strong> which says how much is a stock sensitive to market. It means if market moves upward, by what factor will it move upward. Same applies for downward direction. It is good to have <strong>Beta less than 1</strong> to avoid ultra-sensitive stocks.</li><li><strong>Valuation risk</strong>: There is always a question when to buy a stock and when to sell a stock. There are concepts like higher-highs, lower-lows, candle stick patterns, chart patterns to decide the timings. But the simpler parameter is to check <strong>difference between stock PE ratio and sector PE ratio</strong>. This difference should be <strong>less than 5</strong>. There are some short comings to this, hence to mitigate the shortcoming complement this with <strong>PEG ratio</strong>. <strong>PEG ratio</strong> should be <strong>less than 3</strong>(when overall maket is overvalued) or <strong>1</strong>(when overall market is undervalued). <strong>PEG ratio</strong> filter is not readily available, to calculate this create a custom formula of <strong>(Price / EPS) / EPS-Growth</strong>.</li><li><strong>Profitability risk</strong>: Stocks shouldFolks, if you like my content, would you consider following me on LinkedIn. always be profitable. It does not matter how much revenue they generate if they are not profitable. Question might be that why look for 1 Year parameters, if we are looking at respective 5 Years. The reason is to be sure that no abrupt changes happened in last financial year. Always look at these parameters with no compromise:<br>a. <strong>5 Year EPS</strong> → should be <strong>greater than 10 %.</strong><br>b. <strong>5 Year Average Net Profit Margin</strong> → should be <strong>greater than 10 %.</strong><br>c. <strong>1 Year EPS</strong> → should be <strong>greater than 10 %.</strong><br>d. <strong>1 Year Average Net Profit Margin</strong> → should be <strong>greater than 10 %.</strong></li><li><strong>Liquidity risk</strong>: Companies should always have cash in hand to address few months of operating costs as well as for some fallback safety purposes. Just like we need to have emergency funds in place to breeze through tough times.<br>a. <strong>Free cash flow</strong> needs to be positive.<br>b. Also look at <strong>Debt to equity ratio</strong> → this is sector specific. Compare with the sector ratio and ensure they are considerbaly lower than industry standards. Look sectors like Finance, Power, Manufacturing are very capital intensive and hence they should not be generallized.</li><li><strong>Solvency risk</strong>: For this visit <a href="https://www.valueresearchonline.com/stocks/">Value-Research</a> website and look for <strong>Altman Z-Score</strong> for that stock.</li><li><strong>Governance risk</strong>: For this again visit <a href="https://www.valueresearchonline.com/stocks/">Value-Research</a> website and look for <strong>Modified C-Score</strong>. This ensures financial books are not manipulated or altered.</li></ol><p>At the end we will arrive at countable safe stocks to invest in to get 10–15% of returns.</p><p>Having a smaller bucket ensures we can regularly monitor them.</p><p>Also the list will change year-to-year or quarter-to-quarter. Thus invest, withdraw, book-profits, handle-losses according to the time you perform analysis.</p><p>Folks, if you like my content, would you consider following me on <a href="https://www.linkedin.com/in/hitesh-pattanayak/">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=459621d6d561" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CKAD preparation practice #3]]></title>
            <link>https://hitesh-pattanayak.medium.com/ckad-preparation-practice-2-bae8ff5a26c2?source=rss-183cc8bcd17d------2</link>
            <guid isPermaLink="false">https://medium.com/p/bae8ff5a26c2</guid>
            <category><![CDATA[ckad]]></category>
            <category><![CDATA[yaml]]></category>
            <category><![CDATA[k8s]]></category>
            <category><![CDATA[kodekloud]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Hitesh Pattanayak]]></dc:creator>
            <pubDate>Sun, 27 Aug 2023 04:25:03 GMT</pubDate>
            <atom:updated>2023-08-27T04:25:24.072Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post we shall solve the challenges section of CKAD course by <a href="https://kodekloud.com/">KodeKloud</a></p><h3>Challenge — 1</h3><p>Below is the architecture asked to deploy</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/633/0*S-dYTUXFywj11m50.png" /></figure><p>Since the question says that the PersistentVolume jekyll-site has already created, so lets verify:</p><pre>kubectl get pv<br><br>NAME          CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS    REASON   AGE<br>jekyll-site   1Gi        RWX            Delete           Available           local-storage            5m39s</pre><p>Next since the whole architecture has been create under namespace development, let verify is the namespace exists or not:</p><pre>kubectl get ns | grep development<br>development       Active   7m22s</pre><p>Lets start by deploying/creating other components of the architecture. We will try to use imperative commands wherever necessary to quickly finish the challenge.</p><p>Lets deploy PersistentVolumeClaim jekyll-site first. Below are the requirements:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/537/0*2aRJ8IqM6XrJwXyF.png" /></figure><p>We will require the storageClassName of the PersistentVolume to which we want to attach the PersistentVolumeClaim. So that would be:</p><pre>kubectl describe pv jekyll-site | grep StorageClass<br>StorageClass:      local-storage</pre><p>The manifest pvc.yaml would look like this:</p><pre>apiVersion: v1<br>kind: PersistentVolumeClaim<br>metadata:<br>  name: jekyll-site<br>  namespace: development<br>spec:<br>  accessModes:<br>    - ReadWriteMany<br>  storageClassName: local-storage<br>  resources:<br>    requests:<br>      storage: 1Gi</pre><p>Lets create and validate:</p><pre># create<br>kubectl apply -f pvc.yaml<br><br># validate<br>kubectl get pvc -n development<br><br>NAME          STATUS   VOLUME        CAPACITY   ACCESS MODES   STORAGECLASS    AGE<br>jekyll-site   Bound    jekyll-site   1Gi        RWX            local-storage   6m32s<br><br>kubectl get pv<br>NAME          CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                     STORAGECLASS    REASON   AGE<br>jekyll-site   1Gi        RWX            Delete           Bound    development/jekyll-site   local-storage            21m</pre><p>Both the PersistentVolumeClaim and PersistentVolume have their STATUS as Bound. And the CLAIM of PersistentVolume says it is bound to development/jekyll-site which means it is claimed by PV named jekyll-site in development namespace.</p><p>Lets create and deploy Role developer-role Below are the requirements:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/477/0*ISeuZ_RL4swTLGbw.png" /></figure><p>The manifest role.yaml would look like this:</p><pre>apiVersion: rbac.authorization.k8s.io/v1<br>kind: Role<br>metadata:<br>  namespace: development<br>  name: developer-role<br>rules:<br>- apiGroups: [&quot;&quot;]<br>  resources: [&quot;pods&quot;, &quot;services&quot;, &quot;persistentvolumeclaims&quot;]<br>  verbs: [&quot;*&quot;]</pre><p>Lets create and validate the role:</p><pre># create<br>kubectl apply -f role.yaml<br>role.rbac.authorization.k8s.io/development-role created<br><br># validate<br>kubectl get role -n development<br>NAME               CREATED AT<br>development-role   2023-08-12T06:48:01Z</pre><p>Lets create and deploy RoleBinding developer-rolebinding Below are the requirements:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/0*eVFzSkvxEFQfZxVl.png" /></figure><p>The manifest binding.yaml would look like this:</p><pre>apiVersion: rbac.authorization.k8s.io/v1<br>kind: RoleBinding<br>metadata:<br>  name: developer-rolebinding<br>  namespace: development<br>subjects:<br>- kind: User<br>  name: martin<br>  apiGroup: rbac.authorization.k8s.io<br>roleRef:<br>  kind: Role<br>  name: developer-role<br>  apiGroup: rbac.authorization.k8s.io</pre><p>Lets create and validate the role:</p><pre># create<br>kubectl apply -f binding.yaml<br>rolebinding.rbac.authorization.k8s.io/developer-rolebinding created<br><br># validate<br>kubectl get rolebinding -o wide -n development<br>NAME                    ROLE                  AGE   USERS    GROUPS   SERVICEACCOUNTS<br>developer-rolebinding   Role/developer-role   30s   martin</pre><p>Now lets create the Pod jekyll Below are the requirements:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/489/0*7Zv3RGRPLK12mAfk.png" /></figure><p>Lets use imperative commands to generate the Pod manifest:</p><pre>k run jekyll -n development --image=kodekloud/jekyll-serve --dry-run=client -oyaml &gt; pod.yaml</pre><p>Then now lets edit and populate other requirements. The final manifest pod.yaml would look like this:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  creationTimestamp: null<br>  labels:<br>    run: jekyll<br>  name: jekyll<br>  namespace: development<br>spec:<br>  initContainers:<br>    - image: kodekloud/jekyll<br>      name: copy-jekyll-site<br>      command: [&quot;jekyll&quot;, &quot;new&quot;, &quot;/site&quot;]<br>      volumeMounts:<br>        - name: site<br>          mountPath: &quot;/site&quot;<br>  containers:<br>  - image: kodekloud/jekyll-serve<br>    name: jekyll<br>    resources: {}<br>    volumeMounts:<br>      - name: site<br>        mountPath: &quot;/site&quot;<br>  volumes:<br>    - name: site<br>      persistentVolumeClaim:<br>        claimName: jekyll-site<br>  dnsPolicy: ClusterFirst<br>  restartPolicy: Always<br>status: {}</pre><p>Lets create and validate the pod:</p><pre># create<br>kubectl apply -f pod.yaml <br>pod/jekyll created<br><br># validate<br>kubectl get po -n development<br><br>NAME     READY   STATUS    RESTARTS   AGE<br>jekyll   1/1     Running   0          33s</pre><p>Now lets create the Service jekyll Below are the requirements:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/0*JDiJiz60MzdskNJP.png" /></figure><p>Lets use imperative commands to generate the Service manifest:</p><pre>kubectl expose -n development pod jekyll --name=jekyll --type=NodePort --port=8080 --target-port=4000 --dry-run=client -oyaml &gt; svc.yaml</pre><p>Then now lets edit and populate the node port. The final manifest svc.yaml would look like this:</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  creationTimestamp: null<br>  labels:<br>    run: jekyll<br>  name: jekyll<br>  namespace: development<br>spec:<br>  ports:<br>  - port: 8080<br>    protocol: TCP<br>    targetPort: 4000<br>    nodePort: 30097<br>  selector:<br>    run: jekyll<br>  type: NodePort<br>status:<br>  loadBalancer: {}</pre><p>Lets create and validate the pod:</p><pre># create<br>kubectl apply -f svc.yaml <br>service/jekyll created<br><br># validate<br>kubectl get svc -n development<br><br>NAME     TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE<br>jekyll   NodePort   10.109.111.12   &lt;none&gt;        8080:30097/TCP   31s<br><br>curl -v http://10.109.111.12:8080 # should give 200 OK</pre><p>Now lets update the kube-config file to add user martin and a new context developer. Below are the requirements:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/0*c-ldiLQJcrA5KHjo.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/0*cqbHo5oI9dCPw_ru.png" /></figure><p>The diff of kube-config file would be as below:</p><pre>...<br>contexts:<br>- context:<br>    cluster: kubernetes<br>    user: martin<br>  name: developer<br>  namespace: development<br>...<br>users:<br>- name: martin<br>  user:<br>    client-certificate: /root/martin.crt<br>    client-key: /root/martin.key<br>...</pre><p>Now lets set the current context to developer context</p><pre>k config use-context developer<br>Switched to context &quot;developer&quot;.</pre><p>Now verify if we are able to access pods, services and persistent volumes or not.</p><p>Finally check your answer.</p><h3>Challenge — 3</h3><p>Below is the architecture asked to deploy</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/0*yWXyndHQacUR0dHe.png" /></figure><p>Lets start by deploying/creating other components of the architecture. We will try to use imperative commands wherever necessary to quickly finish the challenge.</p><p>since the whole architecture has been create under namespace vote, let verify is the namespace exists or not:</p><pre>kubectl get ns | grep vote<br># no output</pre><p>Since the vote namespace does not exist, lets create it</p><pre>kubectl create ns vote</pre><p>Lets create vote-deployment Below is the requirement:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/0*b2IIJKosky4qL0-U.png" /></figure><p>Lets use imperative commands to create the deployment:</p><pre>kubectl create deploy -n vote vote-deployment --image=kodekloud/examplevotingapp_vote:before</pre><p>Verify the pods are in Running state or not:</p><pre>kubectl get po -n vote -l app=vote-deployment<br><br>NAME                              READY   STATUS    RESTARTS   AGE<br>vote-deployment-8d495c5b7-vj82w   1/1     Running   0          103s</pre><p>Lets create vote-service Below is the requirement:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/0*7TjdoTjG88aRwnjO.png" /></figure><p>Lets use imperative commands to generate the manifest of the service:</p><pre>kubectl expose -n vote deployment vote-deployment --name=vote-service --type=NodePort --port=5000 --target-port=80 --dry-run=client -oyaml &gt; vote-svc.yaml</pre><p>Now lets populate the node port in the generated manifest. The final manifest vote-svc.yaml would look like:</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  creationTimestamp: null<br>  labels:<br>    app: vote-deployment<br>  name: vote-service<br>  namespace: vote<br>spec:<br>  ports:<br>  - port: 5000<br>    protocol: TCP<br>    targetPort: 80<br>    nodePort: 31000<br>  selector:<br>    app: vote-deployment<br>  type: NodePort<br>status:<br>  loadBalancer: {}</pre><p>Lets create and verify the service:</p><pre># create<br>kubectl apply -f vote-svc.yaml <br>service/vote-service created<br><br>kubectl get svc -n vote<br>NAME           TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE<br>vote-service   NodePort   10.99.180.188   &lt;none&gt;        5000:31000/TCP   9s<br><br>curl -v http://10.99.180.188:5000 ## should give 200 OK</pre><p>Now lets create the redis deployment. Below is the requirement:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/0*McCsDJVQzgmRq3y_.png" /></figure><p>Lets use imperative commands to generate the manifest of the deployment:</p><pre>kubectl create deploy redis-deployment -n vote --image=redis:alpine --dry-run=client -oyaml &gt; redis-deploy.yaml</pre><p>Now lets edit the manifest and populate the volumes and volumeMounts. The final manifest redis-deploy.yaml would look like this:</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  creationTimestamp: null<br>  labels:<br>    app: redis-deployment<br>  name: redis-deployment<br>  namespace: vote<br>spec:<br>  replicas: 1<br>  selector:<br>    matchLabels:<br>      app: redis-deployment<br>  strategy: {}<br>  template:<br>    metadata:<br>      creationTimestamp: null<br>      labels:<br>        app: redis-deployment<br>    spec:<br>      containers:<br>      - image: redis:alpine<br>        name: redis<br>        resources: {}<br>        volumeMounts:<br>          - name: redis-data<br>            mountPath: /data<br>      volumes:<br>        - name: redis-data<br>          emptyDir: {}<br>status: {}</pre><p>Lets create and verify the deployment:</p><pre># create<br>kubectl apply -f redis-deploy.yaml<br>deployment.apps/redis-deployment created<br><br># verify<br>kubectl get deploy -n vote<br><br>NAME               READY   UP-TO-DATE   AVAILABLE   AGE<br>redis-deployment   1/1     1            1           80s<br>vote-deployment    1/1     1            1           13m</pre><p>Lets create redis-service Below is the requirement:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/438/0*6qdk_e2LnjUIF_tg.png" /></figure><p>Lets use imperative commands to create the service:</p><pre>kubectl expose -n vote deployment redis-deployment --name=redis --type=ClusterIP --port=6379 --target-port=6379</pre><p>verify the service:</p><pre>kubectl get svc -n vote<br><br>NAME           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE<br>redis          ClusterIP   10.106.60.131   &lt;none&gt;        6379/TCP         26s<br>vote-service   NodePort    10.99.180.188   &lt;none&gt;        5000:31000/TCP   10m</pre><p>Now lets create the worker deployment. Below is the requirement:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/0*cYsGCNGGgfl-wN3s.png" /></figure><p>Lets use imperative commands to create the deployment:</p><pre>kubectl create deploy -n vote worker --image=kodekloud/examplevotingapp_worker</pre><p>verify the deployment:</p><p>Now lets create the db deployment: Below is the requirement:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/0*96UNF6M4ZeabDsiW.png" /></figure><p>Lets use imperative commands to generate the manifest:</p><pre>kubectl create deploy -n vote db-deployment --image=postgres:9.4 --dry-run=client -oyaml &gt; db-deploy.yaml</pre><p>Now lets edit the deployment and populate environment and volumes. The final manifest db-deploy.yaml would look like this:</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  creationTimestamp: null<br>  labels:<br>    app: db-deployment<br>  name: db-deployment<br>  namespace: vote<br>spec:<br>  replicas: 1<br>  selector:<br>    matchLabels:<br>      app: db-deployment<br>  strategy: {}<br>  template:<br>    metadata:<br>      creationTimestamp: null<br>      labels:<br>        app: db-deployment<br>    spec:<br>      containers:<br>      - image: postgres:9.4<br>        name: postgres<br>        resources: {}<br>        env:<br>          - name: POSTGRES_HOST_AUTH_METHOD<br>            value: trust<br>        volumeMounts:<br>          - name: db-data<br>            mountPath: /var/lib/postgresql/data<br>      volumes:<br>        - name: db-data<br>          emptyDir: {}</pre><p>lets create and validate the deployment</p><pre># create<br>kubectl apply -f db-deploy.yaml<br>deployment.apps/db-deployment created<br><br># validate<br>kubectl get deploy -n vote<br><br>NAME               READY   UP-TO-DATE   AVAILABLE   AGE<br>db-deployment      1/1     1            1           73s<br>redis-deployment   1/1     1            1           15m<br>vote-deployment    1/1     1            1           27m<br>worker             1/1     1            1           7m42s</pre><p>Now lets create db service. Below is the requirement:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/211/0*LZTUC7j0OrA7X-bw.png" /></figure><p>Lets use imperative commands to create the service:</p><pre>kubectl expose deploy -n vote db-deployment --name=db --type=ClusterIP --port=5432 --target-port=5432</pre><p>verify the service:</p><pre>kubectl get svc -n vote<br><br>NAME           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE<br>db             ClusterIP   10.100.153.37   &lt;none&gt;        5432/TCP         30s<br>redis          ClusterIP   10.106.60.131   &lt;none&gt;        6379/TCP         16m<br>vote-service   NodePort    10.99.180.188   &lt;none&gt;        5000:31000/TCP   26m</pre><p>Now lets create the result deployment Below is the requirement:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/0*YZBm5VAc8GPgaFXf.png" /></figure><p>Lets use imperative commands to create the deployment:</p><pre>kubectl create deploy -n vote result-deployment --image=kodekloud/examplevotingapp_result:before</pre><p>Verify the deployment</p><pre># validate<br>kubectl get deploy -n vote<br><br>NAME                READY   UP-TO-DATE   AVAILABLE   AGE<br>db-deployment       1/1     1            1           7m42s<br>redis-deployment    1/1     1            1           22m<br>result-deployment   1/1     1            1           45s<br>vote-deployment     1/1     1            1           34m<br>worker              1/1     1            1           14m</pre><p>Now lets create result service. Below is the requirement:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/171/0*985_jHVoi4F6qQ3m.png" /></figure><p>Lets use imperative commands to generate the service manifest:</p><pre>kubectl expose deploy -n vote result-deployment --name=result-service --type=NodePort --port=5001 --target-port=80 --dry-run=client -oyaml &gt; result-svc.yaml</pre><p>Now lets edit and populate the node port. The final manifest result-svc.yaml would look like this:</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  creationTimestamp: null<br>  labels:<br>    app: result-deployment<br>  name: result-service<br>  namespace: vote<br>spec:<br>  ports:<br>  - port: 5001<br>    protocol: TCP<br>    targetPort: 80<br>    nodePort: 31001<br>  selector:<br>    app: result-deployment<br>  type: NodePort<br>status:<br>  loadBalancer: {}</pre><p>create and verify the service:</p><pre># create<br>kubectl apply -f result-svc.yaml<br>service/result-service created<br><br>kubectl get svc -n vote<br><br>NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE<br>db               ClusterIP   10.100.153.37   &lt;none&gt;        5432/TCP         7m19s<br>redis            ClusterIP   10.106.60.131   &lt;none&gt;        6379/TCP         22m<br>result-service   NodePort    10.108.38.244   &lt;none&gt;        5001:31001/TCP   16s<br>vote-service     NodePort    10.99.180.188   &lt;none&gt;        5000:31000/TCP   32m</pre><p>We have deployed all the components.</p><p>Finally check your answer.</p><h3>Challenge — 4</h3><p>Below is the architecture asked to deploy</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/0*-BzCFCrnlnRuCc_v.png" /></figure><p>Lets create the persistent volumes.</p><p>There are 6 PersistentVolume resources that needs to deployed and all of them similar specifications except for name and path.</p><p>Lets create a single manifest to create all the PVs together. Below are the requirements:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/0*iZNeJilUeufRufxK.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/0*MDBt1Z9HTTQ62EAS.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/0*F6dKVW2KP4YSp8xK.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/483/0*T2zoAH0NX-SV50rd.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/0*54_14I6A8gbwHzkq.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/0*R_BtTNtM9zxp_NWO.png" /></figure><p>The manifest pvs.yaml would like this:</p><pre>apiVersion: v1<br>kind: PersistentVolume<br>metadata:<br>  name: redis01<br>spec:<br>  accessModes:<br>  - ReadWriteOnce<br>  capacity:<br>    storage: 1Gi<br>  hostPath:<br>    path: /redis01<br><br>---<br><br>apiVersion: v1<br>kind: PersistentVolume<br>metadata:<br>  name: redis02<br>spec:<br>  accessModes:<br>  - ReadWriteOnce<br>  capacity:<br>    storage: 1Gi<br>  hostPath:<br>    path: /redis02<br><br>---<br><br>apiVersion: v1<br>kind: PersistentVolume<br>metadata:<br>  name: redis03<br>spec:<br>  accessModes:<br>  - ReadWriteOnce<br>  capacity:<br>    storage: 1Gi<br>  hostPath:<br>    path: /redis03<br><br>---<br><br>apiVersion: v1<br>kind: PersistentVolume<br>metadata:<br>  name: redis04<br>spec:<br>  accessModes:<br>  - ReadWriteOnce<br>  capacity:<br>    storage: 1Gi<br>  hostPath:<br>    path: /redis04<br><br>---<br><br>apiVersion: v1<br>kind: PersistentVolume<br>metadata:<br>  name: redis05<br>spec:<br>  accessModes:<br>  - ReadWriteOnce<br>  capacity:<br>    storage: 1Gi<br>  hostPath:<br>    path: /redis05<br><br>---<br><br>apiVersion: v1<br>kind: PersistentVolume<br>metadata:<br>  name: redis06<br>spec:<br>  accessModes:<br>  - ReadWriteOnce<br>  capacity:<br>    storage: 1Gi<br>  hostPath:<br>    path: /redis06</pre><p>lets create and verify the persistent volumes:</p><pre># create<br>kubectl apply -f pvs.yaml <br>persistentvolume/redis01 created<br>persistentvolume/redis02 created<br>persistentvolume/redis03 created<br>persistentvolume/redis04 created<br>persistentvolume/redis05 created<br>persistentvolume/redis06 created<br><br># verify<br>kubectl get pv<br><br>NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE<br>redis01   1Gi        RWO            Retain           Available                                   52s<br>redis02   1Gi        RWO            Retain           Available                                   52s<br>redis03   1Gi        RWO            Retain           Available                                   52s<br>redis04   1Gi        RWO            Retain           Available                                   52s<br>redis05   1Gi        RWO            Retain           Available                                   52s<br>redis06   1Gi        RWO            Retain           Available                                   52s</pre><p>Now lets create the redis-cluster-service service before stateful set because stateful set requires a service name. Below are the requirements:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/519/0*pvnZcLOZLollBz6U.png" /></figure><p>The manifest redis-svc.yaml would look like this:</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: redis-cluster-service<br>spec:<br>  clusterIP: None<br>  ports:<br>  - name: client<br>    targetPort: 6379<br>    port: 6379<br>  - name: gossip<br>    targetPort: 16379<br>    port: 16379</pre><p>create and validate the service</p><pre># create<br>kubectl apply -f redis-svc.yaml <br>service/redis-cluster-service created<br><br># validate<br>kubectl get svc<br><br>NAME                    TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)              AGE<br>kubernetes              ClusterIP   10.96.0.1    &lt;none&gt;        443/TCP              3h22m<br>redis-cluster-service   ClusterIP   None         &lt;none&gt;        6379/TCP,16379/TCP   6s</pre><p>Now lets create the redis-cluster stateful-set</p><p>Below are the requirements:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/0*PDPc_KKqKWtELdTl.png" /></figure><p>The manifest statefulset.yaml would look like this:</p><pre>apiVersion: apps/v1<br>kind: StatefulSet<br>metadata:<br>  name: redis-cluster<br>spec:<br>  selector:<br>    matchLabels:<br>      app: redis-cluster<br>  serviceName: redis-cluster-service<br>  replicas: 6<br>  template:<br>    metadata:<br>      name: redis-cluster<br>      labels:<br>        app: redis-cluster<br>    spec:<br>      containers:<br>      - image: redis:5.0.1-alpine<br>        name: redis<br>        command: [&quot;/conf/update-node.sh&quot;, &quot;redis-server&quot;, &quot;/conf/redis.conf&quot;]<br>        env:<br>        - name: POD_IP<br>          valueFrom:<br>            fieldRef:<br>              fieldPath: status.podIP<br>        ports:<br>        - containerPort: 6379<br>          name: client<br>        - containerPort: 16379<br>          name: gossip<br>        volumeMounts:<br>        - name: conf<br>          mountPath: /conf<br>          readOnly: false<br>        - name: data<br>          mountPath: /data<br>          readOnly: false<br>      volumes:<br>      - name: conf<br>        configMap:<br>          name: redis-cluster-configmap<br>          defaultMode: 0755<br>  volumeClaimTemplates:<br>    - metadata:<br>       name: data<br>      spec:<br>        accessModes:<br>        - ReadWriteOnce<br>        resources:<br>          requests:<br>            storage: 1Gi</pre><p>create and validate the stateful set</p><pre># create<br>kubectl apply -f statefulset.yaml<br><br>statefulset.apps/redis-cluster created<br><br># validate<br><br>kubectl get po<br>NAME              READY   STATUS    RESTARTS   AGE<br>redis-cluster-0   1/1     Running   0          23s<br>redis-cluster-1   1/1     Running   0          18s<br>redis-cluster-2   1/1     Running   0          15s<br>redis-cluster-3   1/1     Running   0          12s<br>redis-cluster-4   1/1     Running   0          9s<br>redis-cluster-5   1/1     Running   0          6s</pre><p>execute command as mentioned in redis-cluster-config</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/527/0*_WzIp7_mxD491V0F.png" /></figure><pre>kubectl exec -it redis-cluster-0 -- redis-cli --cluster create --cluster-replicas 1 $(kubectl get pods -l app=redis-cluster -o jsonpath=&#39;{range.items[*]}{.status.podIP}:6379 {end}&#39;)<br><br>&gt;&gt;&gt; Performing hash slots allocation on 6 nodes...<br>Master[0] -&gt; Slots 0 - 5460<br>Master[1] -&gt; Slots 5461 - 10922<br>Master[2] -&gt; Slots 10923 - 16383<br>Adding replica 10.244.192.4:6379 to 10.244.192.1:6379<br>Adding replica 10.244.192.5:6379 to 10.244.192.2:6379<br>Adding replica 10.244.192.6:6379 to 10.244.192.3:6379<br>M: 17c25a0bd004f2372c74cf8d9e56b1c70946fd54 10.244.192.1:6379<br>   slots:[0-5460] (5461 slots) master<br>M: 98c0922e0a13d3efa9377f3f76c878da843f8a1e 10.244.192.2:6379<br>   slots:[5461-10922] (5462 slots) master<br>M: 37ff6f3eb0aca87682296e4bd09d96cd4b88cacb 10.244.192.3:6379<br>   slots:[10923-16383] (5461 slots) master<br>S: 86ae291916b863e0cd5b132579eaf9f33343fa59 10.244.192.4:6379<br>   replicates 17c25a0bd004f2372c74cf8d9e56b1c70946fd54<br>S: 83e503793a6845e776211df61d274b98bc1a5837 10.244.192.5:6379<br>   replicates 98c0922e0a13d3efa9377f3f76c878da843f8a1e<br>S: d889b408104efba53a9800d8eb8944e193008a04 10.244.192.6:6379<br>   replicates 37ff6f3eb0aca87682296e4bd09d96cd4b88cacb<br>Can I set the above configuration? (type &#39;yes&#39; to accept): yes<br>&gt;&gt;&gt; Nodes configuration updated<br>&gt;&gt;&gt; Assign a different config epoch to each node<br>&gt;&gt;&gt; Sending CLUSTER MEET messages to join the cluster<br>Waiting for the cluster to join<br>........<br>&gt;&gt;&gt; Performing Cluster Check (using node 10.244.192.1:6379)<br>M: 17c25a0bd004f2372c74cf8d9e56b1c70946fd54 10.244.192.1:6379<br>   slots:[0-5460] (5461 slots) master<br>   1 additional replica(s)<br>S: d889b408104efba53a9800d8eb8944e193008a04 10.244.192.6:6379<br>   slots: (0 slots) slave<br>   replicates 37ff6f3eb0aca87682296e4bd09d96cd4b88cacb<br>M: 98c0922e0a13d3efa9377f3f76c878da843f8a1e 10.244.192.2:6379<br>   slots:[5461-10922] (5462 slots) master<br>   1 additional replica(s)<br>S: 83e503793a6845e776211df61d274b98bc1a5837 10.244.192.5:6379<br>   slots: (0 slots) slave<br>   replicates 98c0922e0a13d3efa9377f3f76c878da843f8a1e<br>S: 86ae291916b863e0cd5b132579eaf9f33343fa59 10.244.192.4:6379<br>   slots: (0 slots) slave<br>   replicates 17c25a0bd004f2372c74cf8d9e56b1c70946fd54<br>M: 37ff6f3eb0aca87682296e4bd09d96cd4b88cacb 10.244.192.3:6379<br>   slots:[10923-16383] (5461 slots) master<br>   1 additional replica(s)<br>[OK] All nodes agree about slots configuration.<br>&gt;&gt;&gt; Check for open slots...<br>&gt;&gt;&gt; Check slots coverage...<br>[OK] All 16384 slots covered.</pre><p>We have deployed all the components.</p><p>Finally check your answer.</p><p>Folks, if you like my content, would you consider following me on <a href="https://www.linkedin.com/in/hitesh-pattanayak/">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bae8ff5a26c2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CKAD preparation practice #2]]></title>
            <link>https://hitesh-pattanayak.medium.com/ckad-preparation-practice-2-67d2e11202ba?source=rss-183cc8bcd17d------2</link>
            <guid isPermaLink="false">https://medium.com/p/67d2e11202ba</guid>
            <category><![CDATA[ckad]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[k8s]]></category>
            <category><![CDATA[kodekloud]]></category>
            <category><![CDATA[yaml]]></category>
            <dc:creator><![CDATA[Hitesh Pattanayak]]></dc:creator>
            <pubDate>Sun, 27 Aug 2023 04:09:20 GMT</pubDate>
            <atom:updated>2023-08-27T04:10:02.671Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post we shall solve the mock exam section of CKAD course by <a href="https://kodekloud.com/">KodeKloud</a></p><h3>Mock — 1</h3><h3>Q1</h3><p>Deploy a pod named nginx-448839 using the nginx:alpine image.</p><blockquote>Solution</blockquote><pre>kubectl run nginx-448839 --image=nginx:alpine<br><br>kubectl get po<br>NAME           READY   STATUS    RESTARTS   AGE<br>nginx-448839   1/1     Running   0          4s</pre><h3>Q2</h3><p>Create a namespace named apx-z993845</p><blockquote>Solution</blockquote><pre>kubectl create ns apx-z993845<br><br>kubectl get ns<br><br>NAME              STATUS   AGE<br>apx-z993845       Active   75s</pre><h3>Q3</h3><p>Create a new Deployment named httpd-frontend with 3 replicas using image httpd:2.4-alpine</p><blockquote>Solution</blockquote><pre>kubectl create deploy httpd-frontend --image=httpd:2.4-alpine --replicas=3<br><br>kubectl get deploy<br>NAME             READY   UP-TO-DATE   AVAILABLE   AGE<br>httpd-frontend   3/3     3            3           40s</pre><h3>Q4</h3><p>Deploy a messaging pod using the redis:alpine image with the labels set to tier=msg.</p><blockquote>Solution</blockquote><pre>kubectl run messaging --image=redis:alpine<br><br>kubectl label po messaging tier=msg<br><br>kubectl get po -l tier=msg<br><br>NAME        READY   STATUS    RESTARTS   AGE<br>messaging   1/1     Running   0          94s</pre><h3>Q5</h3><p>A replicaset rs-d33393 is created. However the pods are not coming up. Identify and fix the issue. Once fixed, ensure the ReplicaSet has 4 Ready replicas.</p><blockquote>Solution</blockquote><p>spot the issue: invalid image</p><pre>kubectl describe rs rs-d33393 | grep Image<br># Image:      busyboxXXXXXXX</pre><p>get the replica set manifest</p><pre>kubectl get rs rs-d33393 -o yaml &gt; rs.yaml</pre><p>delete the old replica set</p><pre>kubectl delete rs rs-d33393</pre><p>change the image and apply</p><pre>kubectl apply -f rs.yaml</pre><h3>Q6</h3><p>Create a service messaging-service to expose the redis deployment in the marketing namespace within the cluster on port 6379.</p><blockquote>Solution</blockquote><pre>kubectl expose -n marketing deploy redis --port 6379 --name=messaging-service<br><br>kubectl get svc<br><br>NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE<br>kubernetes   ClusterIP   10.96.0.1    &lt;none&gt;        443/TCP   90m</pre><h3>Q7</h3><p>Update the environment variable on the pod webapp-color to use a green background.</p><blockquote>Solution</blockquote><pre>kubectl get po webapp-color -oyaml &gt; webapp-color.yaml<br># update the environment variable APP_COLOR to green in the manifest<br><br># delete the po<br>kubectl delete po webapp-color<br><br># re-create the po<br>kubectl apply -f webapp-color.yaml</pre><h3>Q8</h3><p>Create a new ConfigMap named cm-3392845. Use the spec given on the below.</p><p>ConfigName Name: cm-3392845</p><ul><li>Data: DB_NAME=SQL3322</li><li>Data: DB_HOST=sql322.mycompany.com</li><li>Data: DB_PORT=3306</li></ul><blockquote>Solution</blockquote><p>the manifest cm.yaml would look like this</p><pre>apiVersion: v1<br>kind: ConfigMap<br>metadata: <br>  name: cm-3392845<br>data:<br>  DB_NAME: &quot;SQL3322&quot;<br>  DB_HOST: &quot;sql322.mycompany.com&quot;<br>  DB_PORT: &quot;3306&quot;</pre><p>directly using imperative command:</p><pre>kubectl create cm cm-3392845 --from-literal=DB_NAME=SQL3322 --from-literal=DB_HOST=sql322.mycompany.com --from-literal=DB_PORT=3306</pre><p>create and validate</p><pre># create<br>kubectl apply -f cm.yaml<br><br># validate<br>kubectl describe cm cm-3392845<br>Name:         cm-3392845<br>Namespace:    default<br>Labels:       &lt;none&gt;<br>Annotations:  &lt;none&gt;<br>Data<br>====<br>DB_HOST:<br>----<br>sql322.mycompany.com<br>DB_NAME:<br>----<br>SQL3322<br>DB_PORT:<br>----<br>3306</pre><h3>Q9</h3><p>Create a new Secret named db-secret-xxdf with the data given (on the below). Secret Name: db-secret-xxdf</p><ul><li>Secret 1: DB_Host=sql01</li><li>Secret 2: DB_User=root</li><li>Secret 3: DB_Password=password123</li></ul><blockquote>Solution</blockquote><pre>kubectl create secret generic db-secret-xxdf --from-literal=DB_User=root --from-literal=DB_Host=sql01 --from-literal=DB_Password=password123</pre><p>validate</p><pre>kubectl describe secret db-secret-xxdf<br><br>controlplane ~ ➜  kubectl describe secret db-secret-xxdf<br>Name:         db-secret-xxdf<br>Namespace:    default<br>Labels:       &lt;none&gt;<br>Annotations:  &lt;none&gt;<br>Type:  Opaque<br>Data<br>====<br>DB_Password:  11 bytes<br>DB_User:      4 bytes<br>DB_Host:      5 bytes</pre><h3>Q10</h3><p>Update pod app-sec-kff3345 to run as Root user and with the SYS_TIME capability. Pod Name: app-sec-kff3345 Image Name: ubuntu SecurityContext: Capability SYS_TIME</p><blockquote>Solution</blockquote><p>get the pod spec:</p><pre>kubectl get po app-sec-kff3345 -oyaml &gt; app-sec-kff3345.yaml</pre><p>update the container:</p><pre>...<br>securityContext:<br>  runAsUser: 0<br>  capabilities:<br>    add: [&quot;SYS_TIME&quot;]<br>...</pre><p>delete the pod</p><pre>kubectl delete po app-sec-kff3345</pre><p>re-create the pod</p><pre>kubectl apply -f app-sec-kff3345.yaml</pre><p>validate</p><pre>kubectl get po app-sec-kff3345<br><br>NAME              READY   STATUS    RESTARTS   AGE<br>app-sec-kff3345   1/1     Running   0          44s</pre><h3>Q11</h3><p>Export the logs of the e-com-1123 pod to the file /opt/outputs/e-com-1123.logs</p><blockquote>Solution</blockquote><p>check the pod namespace</p><pre>kubectl get po -A | grep e-com-1123<br>e-commerce    e-com-1123                             1/1     Running   0              49m</pre><p>export the logs</p><pre>kubectl logs e-com-1123 -n e-commerce &gt; /opt/outputs/e-com-1123.logs</pre><h3>Q12</h3><p>Create a Persistent Volume with the given specification.</p><ul><li>Volume Name: pv-analytics</li><li>Storage: 100Mi</li><li>Access modes: ReadWriteMany</li><li>Host Path: /pv/data-analytics</li></ul><blockquote>Solution</blockquote><p>the manifest pv.yaml would look like this</p><pre>apiVersion: v1<br>kind: PersistentVolume<br>metadata:<br>  name: pv-analytics<br>spec:<br>  accessModes:<br>  - ReadWriteMany<br>  capacity:<br>    storage: 100Mi<br>  hostPath:<br>    path: /pv/data-analytics</pre><h3>Q13</h3><p>Create a redis deployment using the image redis:alpine with 1 replica and label app=redis. Expose it via a ClusterIP service called redis on port 6379. Create a new Ingress Type NetworkPolicy called redis-access which allows only the pods with label access=redis to access the deployment.</p><blockquote>Solution</blockquote><p>create the pod</p><pre>kubectl create deploy redis --image=redis:alpine --replicas=1</pre><p>create the service</p><pre>kubectl expose deploy redis --port=6379</pre><p>create a network policy manifest and deploy it</p><pre>apiVersion: networking.k8s.io/v1<br>kind: NetworkPolicy<br>metadata:<br>  name: redis-access<br>spec:<br>  podSelector:<br>    matchLabels:<br>      app: redis<br>  policyTypes:<br>    - Ingress<br>  ingress:<br>    - from:<br>        - podSelector:<br>            matchLabels:<br>              access: redis<br>      ports:<br>        - protocol: TCP<br>          port: 6379</pre><h3>Q14</h3><p>Create a Pod called sega with two containers:</p><ul><li>Container 1: Name tails with image busybox and command: sleep 3600.</li><li>Container 2: Name sonic with image nginx and Environment variable: NGINX_PORT with the value 8080.</li></ul><blockquote>Solution</blockquote><p>create the pod manifest and deploy it</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: sega<br>spec:<br>  containers:<br>  - name: tails<br>    image: busybox<br>    command: [&quot;sleep&quot;, &quot;3600&quot;]<br>  - name: sonic<br>    image: nginx<br>    env:<br>    - name: NGINX_PORT<br>      value: &quot;8080&quot;</pre><h3>Mock — 2</h3><h3>Q1</h3><p>Create a deployment called my-webapp with image: nginx, label tier:frontend and 2 replicas. Expose the deployment as a NodePort service with name front-end-service , port: 80 and NodePort: 30083</p><blockquote>Solution</blockquote><p>create the deployment</p><pre>kubectl create deploy my-webapp --image=nginx --replicas=2</pre><p>label the deployment</p><pre>kubectl label deploy my-webapp tier=frontend</pre><p>create the service</p><pre>kubectl expose deploy my-webapp --name=front-end-service --type=NodePort --port=80 --dry-run=client -oyaml &gt; my-webapp.svc.yaml<br># edit the service manifest to add node port<br>nodePort: 30083<br># create the svc<br>kubectl apply -f my-webapp.svc.yaml</pre><h3>Q2</h3><p>Add a taint to the node node01 of the cluster. Use the specification below:</p><ul><li>key: app_type</li><li>value: alpha</li><li>effect: NoSchedule</li></ul><p>Create a pod called alpha, image: redis with toleration to node01.</p><blockquote>Solution</blockquote><p>taint the node</p><pre>kubectl taint node node01 app_type=alpha:NoSchedule # node/node01 tainted</pre><p>verify the taint</p><pre>kubectl describe node node01 | grep Taints # Taints: app_type=alpha:NoSchedule</pre><p>pod manifest pod.yaml with toleration:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: alpha<br>spec:<br>  containers:<br>  - image: redis<br>    name: alpha<br>  tolerations:<br>    - key: app_type<br>      value: alpha<br>      effect: NoSchedule</pre><p>verify</p><pre>kubectl get po -o wide<br><br>NAME                         READY   STATUS    RESTARTS   AGE   IP             NODE           NOMINATED NODE   READINESS GATES<br>alpha                        1/1     Running   0          27s   10.244.192.3   node01         &lt;none&gt;           &lt;none&gt;</pre><h3>Q3</h3><p>Apply a label app_type=beta to node controlplane. Create a new deployment called beta-apps with image: nginx and replicas: 3. Set Node Affinity to the deployment to place the PODs on controlplane only.</p><blockquote>Solution</blockquote><p>label the node</p><pre>kubectl label node controlplane  app_type=beta</pre><p>create the deployment</p><pre>kubectl create deploy beta-apps --image=nginx --replicas=3 --dry-run=client -oyaml &gt; beta-apps.yaml</pre><p>update the manifest to add node affinity</p><pre>affinity:<br>  nodeAffinity:<br>    requiredDuringSchedulingIgnoredDuringExecution:<br>      nodeSelectorTerms:<br>      - matchExpressions:<br>        - key: app_type<br>          operator: In<br>          values: [&quot;beta&quot;]</pre><p>apply the manifets beta-apps.yaml</p><pre>kubectl apply -f beta-apps.yaml</pre><p>validate</p><pre>kubectl get po -owide<br><br>NAME                         READY   STATUS    RESTARTS   AGE    IP             NODE           NOMINATED NODE   READINESS GATES<br>beta-apps-574fd8858c-gpk2s   1/1     Running   0          18s    10.244.0.6     controlplane   &lt;none&gt;           &lt;none&gt;<br>beta-apps-574fd8858c-nqpxd   1/1     Running   0          18s    10.244.0.7     controlplane   &lt;none&gt;           &lt;none&gt;<br>beta-apps-574fd8858c-xsq8r   1/1     Running   0          18s    10.244.0.5     controlplane   &lt;none&gt;           &lt;none&gt;</pre><h3>Q4</h3><p>Create a new Ingress Resource for the service my-video-service to be made available at the URL: <a href="http://ckad-mock-exam-solution.com:30093/video">http://ckad-mock-exam-solution.com:30093/video</a>.</p><p>To create an ingress resource, the following details are: -</p><ul><li>annotation: nginx.ingress.kubernetes.io/rewrite-target: /</li><li>host: ckad-mock-exam-solution.com</li><li>path: /video</li></ul><blockquote>Solution</blockquote><p>get the service details</p><pre># kubectl get svc my-video-service -o yaml<br><br>apiVersion: v1<br>kind: Service<br>metadata:<br>  creationTimestamp: &quot;2023-08-15T07:39:48Z&quot;<br>  name: my-video-service<br>  namespace: default<br>  resourceVersion: &quot;5038&quot;<br>  uid: 67a6728b-f6c5-43c8-8c11-171050c1807d<br>spec:<br>  clusterIP: 10.103.124.68<br>  clusterIPs:<br>  - 10.103.124.68<br>  internalTrafficPolicy: Cluster<br>  ipFamilies:<br>  - IPv4<br>  ipFamilyPolicy: SingleStack<br>  ports:<br>  - port: 8080<br>    protocol: TCP<br>    targetPort: 8080<br>  selector:<br>    app: webapp-video<br>  sessionAffinity: None<br>  type: ClusterIP<br>status:<br>  loadBalancer: {}</pre><p>the ingress manifest ingress.yaml would look like:</p><pre>apiVersion: networking.k8s.io/v1<br>kind: Ingress<br>metadata:<br>  name: video-ingress<br>  annotations:<br>    nginx.ingress.kubernetes.io/rewrite-target: /<br>spec:<br>  rules:<br>  - host: ckad-mock-exam-solution.com<br>    http:<br>      paths:<br>      - path: /video<br>        pathType: Prefix<br>        backend:<br>          service:<br>            name: my-video-service<br>            port:<br>              number: 8080</pre><p>deploy the ingress</p><pre>kubectl apply -f ingrss.yaml</pre><p>validate</p><pre>curl http://ckad-mock-exam-solution.com:30093/video<br>&lt;!doctype html&gt;<br>&lt;title&gt;Hello from Flask&lt;/title&gt;<br>&lt;body style=&quot;background: #30336b;&quot;&gt;<br>&lt;div style=&quot;color: #e4e4e4;<br>    text-align:  center;<br>    height: 90px;<br>    vertical-align:  middle;&quot;&gt;<br>    &lt;img src=&quot;https://res.cloudinary.com/cloudusthad/image/upload/v1547052431/video.jpg&quot;&gt;<br>&lt;/div&gt;<br>&lt;/body&gt;</pre><h3>Q5</h3><p>We have deployed a new pod called pod-with-rprobe. This Pod has an initial delay before it is Ready. Update the newly created pod pod-with-rprobe with a readinessProbe using the given spec:</p><ul><li>httpGet path: /ready</li><li>httpGet port: 8080</li></ul><blockquote>Solution</blockquote><p>fetch the pod manifest</p><pre>kubectl get po pod-with-rprobe -oyaml &gt; pod-with-rprobe.yaml</pre><p>check the pod spec to get the delay</p><pre>...<br>- env:<br>    - name: APP_START_DELAY<br>      value: &quot;180&quot;<br>...</pre><p>update the manifest with readiness probe</p><pre>readinessProbe:<br>  httpGet:<br>    path: /ready<br>    port: 8080<br>  initialDelaySeconds: 180</pre><p>delete the pod</p><pre>kubectl delete po pod-with-rprobe</pre><p>apply the updated manifest</p><pre>kubectl apply -f pod-with-rprobe.yaml</pre><p>validate after 180 seconds</p><pre>NAME              READY   STATUS    RESTARTS   AGE<br>pod-with-rprobe   1/1     Running   0          185s</pre><h3>Q6</h3><p>Create a new pod called nginx1401 in the default namespace with the image nginx. Add a livenessProbe to the container to restart it if the command ls /var/www/html/probe fails. This check should start after a delay of 10 seconds and run every 60 seconds.</p><blockquote>Solution</blockquote><p>generate the pod manifest</p><pre>kubectl run nginx1401 --image=nginx --dry-run=client -oyaml &gt; nginx1401.yaml</pre><p>update the manifest to add liveness probe</p><pre>livenessProbe:<br>  initialDelaySeconds: 10<br>  periodSeconds: 60<br>  exec:<br>    command: [&quot;ls&quot;, &quot;/var/www/html/probe&quot;]</pre><p>apply the manifest</p><pre>kubectl apply -f nginx1401.yaml</pre><p>validate</p><pre>kubectl get po nginx1401 <br><br>NAME        READY   STATUS    RESTARTS   AGE<br>nginx1401   1/1     Running   0          24s</pre><h3>Q7</h3><p>Create a job called whalesay with image docker/whalesay and command cowsay I am going to ace CKAD!.</p><ul><li>completions: 10</li><li>backoffLimit: 6</li><li>restartPolicy: Never</li></ul><blockquote>Solution</blockquote><p>the manifest whalesay.yaml would look like this</p><pre>apiVersion: batch/v1<br>kind: Job<br>metadata:<br>  name: whalesay<br>spec:<br>  completions: 10<br>  backoffLimit: 6<br>  template:<br>    metadata:<br>      name: whalesay<br>    spec:<br>        containers:<br>        - image: docker/whalesay<br>          name: whalesay<br>          command: [&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;cowsay I am going to ace CKAD!&quot;]<br>        restartPolicy: Never</pre><p>apply and validate</p><pre>kubectl apply -f whalesay.yaml<br><br>kubectl get job<br>NAME       COMPLETIONS   DURATION   AGE<br>whalesay   5/10          44s        44s<br><br>kubectl get po <br>whalesay-6dcvg                  0/1     Completed           0               22s<br>whalesay-cvhpf                  0/1     Completed           0               27s<br>whalesay-dfhcq                  0/1     Completed           0               37s<br>whalesay-f2xzk                  0/1     Completed           0               17s<br>whalesay-h722v                  0/1     Completed           0               32s<br>whalesay-jm8zv                  0/1     Completed           0               42s<br>whalesay-rfzlf                  0/1     Completed           0               62s<br>whalesay-wd92n                  0/1     ContainerCreating   0               1s<br>whalesay-wjw9z                  0/1     Completed           0               7s<br>whalesay-xprkx                  0/1     Completed           0               12s</pre><h3>Q8</h3><p>Create a pod called multi-pod with two containers.</p><ul><li>Container 1: name: jupiter, image: nginx</li><li>Container 2: name: europa, image: busybox command: sleep 4800</li></ul><p>Environment Variables:</p><ul><li>Container 1: type: planet</li><li>Container 2: type: moon</li></ul><blockquote>Solution</blockquote><p>the pod manifest would look like this</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: multi-pod<br>spec:<br>  containers:<br>  - name: jupiter<br>    image: nginx<br>    env:<br>    - name: type<br>      value: &quot;planet&quot;<br>  - name: europa<br>    image: busybox<br>    command: [&quot;sleep&quot;, &quot;4800&quot;]<br>    env:<br>    - name: type<br>      value: &quot;moon&quot;</pre><h3>Q9</h3><p>Create a PersistentVolume called custom-volume with:</p><ul><li>size: 50MiB</li><li>reclaim policy:retain</li><li>Access Modes: ReadWriteMany</li><li>hostPath: /opt/data</li></ul><blockquote>Solution</blockquote><p>the pv manifest would look like this</p><pre>apiVersion: v1<br>kind: PersistentVolume<br>metadata:<br>  name: custom-volume<br>spec:<br>  capacity:<br>    storage: 50Mi<br>  persistentVolumeReclaimPolicy: Retain<br>  accessModes:<br>  - ReadWriteMany<br>  hostPath:<br>    path: /opt/data</pre><p>Folks, if you like my content, would you consider following me on <a href="https://www.linkedin.com/in/hitesh-pattanayak/">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67d2e11202ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CKAD preparation practice #1]]></title>
            <link>https://hitesh-pattanayak.medium.com/ckad-preparation-practice-1-c226600f5a34?source=rss-183cc8bcd17d------2</link>
            <guid isPermaLink="false">https://medium.com/p/c226600f5a34</guid>
            <category><![CDATA[yaml]]></category>
            <category><![CDATA[k8s]]></category>
            <category><![CDATA[ckad-exam]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[ckad]]></category>
            <dc:creator><![CDATA[Hitesh Pattanayak]]></dc:creator>
            <pubDate>Sun, 27 Aug 2023 03:50:45 GMT</pubDate>
            <atom:updated>2023-08-27T03:52:21.287Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post we shall solve the lightning labs section of CKAD course by <a href="https://kodekloud.com/">KodeKloud</a></p><h3>Lab — 1</h3><h3>Q1</h3><p>Create a Persistent Volume called log-volume. It should make use of a storage class name manual. It should use RWX as the access mode and have a size of 1Gi. The volume should use the hostPath /opt/volume/nginx.</p><p>Next, create a PVC called log-claim requesting a minimum of 200Mi of storage. This PVC should bind to log-volume.</p><p>Mount this in a pod called logger at the location /var/www/nginx. This pod should use the image nginx:alpine.</p><blockquote>Solution</blockquote><p>The solution will have 3 steps in this sequence:</p><ul><li>create a PersistentVolume based on given specifications</li><li>create a PersistentVolumeClaim, that will bind to above PersistentVolume, based on given specifications</li><li>finally create a Pod with given specification that will bind to the above PersistentVolume via PersistentVolumeClaim</li></ul><p>Lets create the PersistentVolume. The manifest pv.yaml will look like this:</p><pre>apiVersion: v1<br>kind: PersistentVolume<br>metadata:<br>  name: log-volume<br>spec:<br>  accessModes:<br>  - ReadWriteMany<br>  storageClassName: manual<br>  capacity:<br>    storage: 1Gi<br>  hostPath:<br>    path: /opt/volume/nginx</pre><p>create and validate the PersistentVolume</p><pre># create<br>kubectl apply -f pv.yaml #persistentvolume/log-volume created<br><br># validate<br>kubectl get pv<br><br>NAME         CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE<br>log-volume   1Gi        RWX            Retain           Available           manual                  18s<br># notice the `STATUS` column --&gt; `Available`</pre><p>Now lets create the PersistentVolumeClaim The manifest pvc.yaml will look like this:</p><pre>apiVersion: v1<br>kind: PersistentVolumeClaim<br>metadata:<br>  name: log-claim<br>spec:<br>  accessModes:<br>  - ReadWriteMany<br>  storageClassName: manual<br>  resources:<br>    requests:<br>      storage: 200Mi</pre><p>create and validate the PersistentVolumeClaim</p><pre># create<br>kubectl apply -f pvc.yaml # persistentvolumeclaim/log-claim created<br><br># validate<br>kubectl get pvc<br>NAME        STATUS   VOLUME       CAPACITY   ACCESS MODES   STORAGECLASS   AGE<br>log-claim   Bound    log-volume   1Gi        RWX            manual         13s<br># notice the `STATUS` column<br><br># re-check the pv&#39;s STATUS and CLAIM columns<br>kubectl get pv<br><br>NAME         CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM               STORAGECLASS   REASON   AGE<br>log-volume   1Gi        RWX            Retain           Bound    default/log-claim   manual                  99s</pre><p>Finally lets create the Pod. Generate the manifest imperatively</p><pre>kubectl run logger --image=nginx:alpine --dry-run=client -oyaml &gt; pod.yaml</pre><p>Edit the generated manifest and populate volume specs. The final manifest pod.yaml would look like this:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: logger<br>  labels:<br>    run: logger<br>spec:<br>  containers:<br>  - image: nginx:alpine<br>    name: logger<br>    volumeMounts:<br>    - name: log-claim<br>      mountPath: /var/www/nginx<br>  volumes:<br>  - name: log-claim<br>    persistentVolumeClaim:<br>      claimName: log-claim</pre><p>create and validate the Pod</p><pre># create<br>kubectl apply -f pod.yaml # pod/logger created<br><br># validate<br>kubectl get po<br><br>NAME           READY   STATUS    RESTARTS   AGE<br>logger         1/1     Running   0          20s</pre><h3>Q2</h3><p>We have deployed a new pod called secure-pod and a service called secure-service.</p><p>Incoming or Outgoing connections to this pod are not working.</p><p>Troubleshoot why this is happening.</p><p>Make sure that incoming connection from the pod webapp-color are successful.</p><blockquote>Solution</blockquote><p>First lets check the resources already created.</p><p>Check the pod specifications: kubectl describe po secure-pod</p><p>Check the service specifications: kubectl describe svc secure-service</p><p>Is the service correctly attached the pod?</p><pre>kubectl describe po secure-pod | grep Labels<br># Labels:           run=secure-pod<br><br>kubectl describe svc secure-service | grep Selector<br># Selector:          run=secure-pod</pre><p>since Labels and Selctor match. The answer is yes.</p><p>Is the incoming request to the pod secure-pod from webapp-color pod not working?</p><pre>kubectl get po secure-pod -o wide<br># note the IP --&gt; 10.244.0.7<br><br>kubectl exec -it webapp-color -- /bin/sh -c &#39;ping 10.244.0.7&#39;</pre><p>since there are no replies for the pings, this confirms no incoming connection for webapp-color.</p><p>But what is stopping? Maybe there is a network policy. Lets check.</p><pre>kubectl get netpol<br><br>NAME           POD-SELECTOR   AGE<br>default-deny   &lt;none&gt;         11m<br><br>kubectl describe netpol default-deny<br>Name:         default-deny<br>Namespace:    default<br>Created on:   2023-08-13 03:55:32 -0400 EDT<br>Labels:       &lt;none&gt;<br>Annotations:  &lt;none&gt;<br>Spec:<br>  PodSelector:     &lt;none&gt; (Allowing the specific traffic to all pods in this namespace)<br>  Allowing ingress traffic:<br>    &lt;none&gt; (Selected pods are isolated for ingress connectivity)<br>  Not affecting egress traffic<br>  Policy Types: Ingress</pre><p>The above network policy:</p><ul><li>applies to all pods in default namespace.</li><li>blocks all incoming incoming connections</li></ul><p>Let create a new network policy secure-netpol to:</p><ul><li>only apply to secure-po</li><li>allow incoming traffic ingress from webapp-color pod</li></ul><p>The manifest netpol.yaml would look like this:</p><pre>apiVersion: networking.k8s.io/v1<br>kind: NetworkPolicy<br>metadata:<br>  name: secure-netpol<br>  namespace: default<br>spec:<br>  podSelector:<br>    matchLabels:<br>      run: secure-pod<br>  policyTypes:<br>  - Ingress<br>  ingress:<br>    - from:<br>      - podSelector:<br>          matchLabels:<br>            name: webapp-color<br>status: {}</pre><p>create the netpol and verify</p><pre>kubectl apply -f netpol.yaml<br><br>kubectl exec -it webapp-color -- /bin/sh -c &#39;ping 10.244.0.7&#39; # there are replies now</pre><h3>Q3</h3><p>Create a pod called time-check in the dvl1987 namespace. This pod should run a container called time-check that uses the busybox image. The time-check container should run the command: while true; do date; sleep $TIME_FREQ;done and write the result to the location /opt/time/time-check.log. The path /opt/time on the pod should mount a volume that lasts the lifetime of this pod.</p><p>Create a config map called time-config with the data TIME_FREQ=10 in the same namespace.</p><blockquote>Solution</blockquote><p>Lets verify the existence of namespace:</p><pre>kubectl get ns | grep dvl1987 # does not exist<br><br># so lets create it<br>kubectl create ns dvl1987 # namespace/dvl1987 created</pre><p>Now lets create the config map imperatively: kubectl create cm time-config -n dvl1987 --from-literal=TIME_FREQ=10</p><p>verify the config map</p><pre>kubectl describe cm time-config -n dvl1987<br><br>Name:         time-config<br>Namespace:    dvl1987<br>Labels:       &lt;none&gt;<br>Annotations:  &lt;none&gt;<br>Data<br>====<br>TIME_FREQ:<br>----<br>10</pre><p>Now lets generate the pod manifest imperatively:</p><pre>kubectl run time-check -n dvl1987 --image=busybox --dry-run=client -oyaml &gt; time-check.yaml</pre><p>Edit the manifest to populate commands, add volumes and map the config map time-config for TIME_FREQ data. The final manifest time-check.yaml would look like this:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  creationTimestamp: null<br>  labels:<br>    run: time-check<br>  name: time-check<br>  namespace: dvl1987<br>spec:<br>  containers:<br>  - image: busybox<br>    name: time-check<br>    resources: {}<br>    env: <br>      - name: TIME_FREQ<br>        valueFrom:<br>          configMapKeyRef:<br>            name: time-config<br>            key: TIME_FREQ<br>    command: [&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;while true; do date; sleep $TIME_FREQ; done &gt; /opt/time/time-check.log&quot;]<br>    volumeMounts:<br>      - name: log<br>        mountPath: /opt/time<br>  volumes:<br>    - name: log<br>      emptyDir: {}<br>  dnsPolicy: ClusterFirst<br>  restartPolicy: Always<br>status: {}</pre><p>Now lets create and verify the pod</p><pre>kubectl apply -f time-check.yaml # pod/time-check created<br><br>kubectl get po time-check -n dvl1987<br><br>NAME         READY   STATUS    RESTARTS   AGE<br>time-check   1/1     Running   0          2m3s<br><br># check if the TIME_FREQ environment variable is set<br>kubectl exec time-check -n dvl1987 -- /bin/sh -c &#39;echo $TIME_FREQ&#39; # 10<br><br># check if the time-check.log file is being populated<br>kubectl exec time-check -n dvl1987 -- /bin/sh -c &#39;cat /opt/time/time-check.log&#39;</pre><h3>Q4</h3><p>Create a new deployment called nginx-deploy, with one single container called nginx, image nginx:1.16 and 4 replicas. The deployment should use RollingUpdate strategy with maxSurge=1, and maxUnavailable=2.</p><p>Next upgrade the deployment to version 1.17.</p><p>Finally, once all pods are updated, undo the update and go back to the previous version.</p><blockquote>Solution</blockquote><p>Lets generate the deployment manifest imperatively:</p><pre>kubectl create deploy nginx-deploy --image=nginx:1.16 --replicas=4 --dry-run=client -oyaml &gt; nginx-deploy.yaml</pre><p>Edit the manifest and update the RollingUpdate strategy. The final manifest nginx-deploy.yaml would look like this:</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  creationTimestamp: null<br>  labels:<br>    app: nginx-deploy<br>  name: nginx-deploy<br>spec:<br>  strategy:<br>    type: RollingUpdate<br>    rollingUpdate:<br>      maxSurge: 1<br>      maxUnavailable: 2<br>  replicas: 4<br>  selector:<br>    matchLabels:<br>      app: nginx-deploy<br>  strategy: {}<br>  template:<br>    metadata:<br>      creationTimestamp: null<br>      labels:<br>        app: nginx-deploy<br>    spec:<br>      containers:<br>      - image: nginx:1.16<br>        name: nginx<br>        resources: {}<br>status: {}</pre><p>Now lets update the image to nginx:1.17: kubectl set image deploy nginx-deploy nginx=nginx:1.17 --&gt; deployment.apps/nginx-deploy image updated</p><p>Check the rollout status: kubectl rollout status deploy nginx-deploy --&gt; deployment &quot;nginx-deploy&quot; successfully rolled out</p><p>Undo the rollout: kubectl rollout undo deploy nginx-deploy --&gt; deployment.apps/nginx-deploy rolled back</p><p>Check the rollout history: kubectl rollout history deploy nginx-deploy</p><p>Check the current image: kubectl describe deploy nginx-deploy | grep Image --&gt; Image: nginx:1.16</p><h3>Q5</h3><p>Create a redis deployment with the following parameters:</p><ul><li>Name of the deployment should be redis using the redis:alpine image. It should have exactly 1 replica.</li><li>The container should request for .2 CPU. It should use the label app=redis.</li><li>It should mount exactly 2 volumes. a. An Empty directory volume called data at path /redis-master-data. b. A configmap volume called redis-config at path /redis-master.</li><li>The container should expose the port 6379.</li></ul><p>The configmap has already been created.</p><blockquote>Solution</blockquote><p>Let generate the deployment imperatively:</p><pre>kubectl create deploy redis --image=redis:alpine --replicas=1 --port=6379 --dry-run=client -oyaml &gt; redis.yaml</pre><p>Edit the manifest to populate resource requests and volumes. The final manifest redis.yaml would look like this:</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  creationTimestamp: null<br>  labels:<br>    app: redis<br>  name: redis<br>spec:<br>  replicas: 1<br>  selector:<br>    matchLabels:<br>      app: redis<br>  strategy: {}<br>  template:<br>    metadata:<br>      creationTimestamp: null<br>      labels:<br>        app: redis<br>    spec:<br>      containers:<br>      - image: redis:alpine<br>        name: redis<br>        ports:<br>        - containerPort: 6379<br>        resources:<br>          requests:<br>            cpu: .2<br>        volumeMounts:<br>          - name: data<br>            mountPath: /redis-master-data<br>          - name: redis-config<br>            mountPath: /redis-master<br>      volumes:<br>        - name: data<br>          emptyDir: {}<br>        - name: redis-config<br>          configMap:<br>            name: redis-config<br>status: {}</pre><p>create and validate the deployment</p><pre># create<br>kubectl get deploy<br><br>NAME           READY   UP-TO-DATE   AVAILABLE   AGE<br>nginx-deploy   4/4     4            4           14m<br>redis          1/1     1            1           33s<br><br># validate<br>kubectl get po<br>NAME                           READY   STATUS    RESTARTS   AGE<br>...<br>redis-6b5b7895c7-nbxj5         1/1     Running   0          47s<br>...</pre><h3>Lab — 2</h3><h3>Q1</h3><p>We have deployed a few pods in this cluster in various namespaces. Inspect them and identify the pod which is not in a Ready state. Troubleshoot and fix the issue.</p><p>Next, add a check to restart the container on the same pod if the command ls /var/www/html/file_check fails. This check should start after a delay of 10 seconds and run every 60 seconds.</p><blockquote>Solution</blockquote><p>First lets get all pods across namespaces and check which is not in Ready state.</p><pre>kubectl get po -A | awk &#39;$3 == &quot;0/1&quot;&#39;<br><br>#dev1401       nginx1401                              0/1     Running   0             5m29s</pre><p>Note The above command to extract is not the ideal way of doing. Here the assumption is READY column to be the 3rd column and has 0 out of 1 container running. However, READY column might be some other column and the container counts might vary.</p><p>Now lets describe the pod to check what is causing the pod to not enter READY state.</p><pre>kubectl describe po nginx1401 -n dev1401<br><br>...<br> Events:<br>  Type     Reason     Age                     From               Message<br>  ----     ------     ----                    ----               -------<br>  Normal   Scheduled  9m2s                    default-scheduler  Successfully assigned dev1401/nginx1401 to node01<br>  Normal   Pulling    8m57s                   kubelet            Pulling image &quot;kodekloud/nginx&quot;<br>  Normal   Pulled     8m46s                   kubelet            Successfully pulled image &quot;kodekloud/nginx&quot; in 4.503087569s (10.575886108s including waiting)<br>  Normal   Created    8m46s                   kubelet            Created container nginx<br>  Normal   Started    8m46s                   kubelet            Started container nginx<br>  Warning  Unhealthy  3m52s (x36 over 8m45s)  kubelet            Readiness probe failed: Get &quot;http://10.244.192.1:8080/&quot;: dial tcp 10.244.192.1:8080: connect: connection refused</pre><p>The events say that readiness probe is failing. Lets look at the readiness probe.</p><pre>Readiness:      http-get http://:8080/ delay=0s timeout=1s period=10s #success=1 #failure=3</pre><p>The readiness probe is configured to look at 8080 port. However the container port is exposed at 9080 port.</p><pre>Port:           9080/TCP</pre><p>so lets edit the pod to change the readiness probe. also lets add a liveness probe that will restart the container if the file /var/www/html/file_check does not exist.</p><pre>kubectl get pod -n dev1401 nginx1401 -o yaml &gt; pod.yaml</pre><p>The updated manifest pod.yaml would look like this:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  labels:<br>    run: nginx<br>  name: nginx1401<br>  namespace: dev1401<br>spec:<br>  containers:<br>  - image: kodekloud/nginx<br>    imagePullPolicy: IfNotPresent<br>    name: nginx<br>    ports:<br>    - containerPort: 9080<br>      protocol: TCP<br>    readinessProbe:<br>      failureThreshold: 3<br>      httpGet:<br>        path: /<br>        port: 9080<br>        scheme: HTTP<br>      periodSeconds: 10<br>      successThreshold: 1<br>      timeoutSeconds: 1<br>    livenessProbe:<br>      exec:<br>        command: [&quot;ls&quot;, &quot;/var/www/html/file_check&quot;]<br>      periodSeconds: 60<br>      initialDelaySeconds: 10<br>    resources: {}<br>status: {}</pre><p>now lets delete the pod and recreate it.</p><pre># delete<br>kubectl delete po -n dev1401 nginx1401<br><br># re-create<br>kubectl apply -f pod.yaml</pre><h3>Q2</h3><p>Create a cronjob called dice that runs every one minute. Use the Pod template located at /root/throw-a-dice. The image throw-dice randomly returns a value between 1 and 6. The result of 6 is considered success and all others are failure.</p><p>The job should be non-parallel and complete the task once. Use a backoffLimit of 25.</p><p>If the task is not completed within 20 seconds the job should fail and pods should be terminated.</p><blockquote>Solution</blockquote><p>The solution is pretty straight forward.</p><p>It asks us to use pod spec at /root/throw-a-dice to create a cron job.</p><pre># cat /root/throw-a-dice/throw-a-dice.yaml<br><br>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: throw-dice-pod<br>spec:<br>  containers:<br>  -  image: kodekloud/throw-dice<br>     name: throw-dice<br>  restartPolicy: Never</pre><p>further requirements are:</p><ul><li>backoffLimit of 25 → backoffLimit: 25</li><li>completed within 20 seconds → activeDeadlineSeconds: 20</li><li>complete the task only once → completions: 1</li></ul><p>the above requirement applies to job template and not cron job spec</p><p>only thing that applies to cron job spec is the schedule: runs every one minute --&gt; schedule: &quot;*/1 * * * *&quot;</p><p>so the final manifest cj.yaml would look like:</p><pre>apiVersion: batch/v1<br>kind: CronJob<br>metadata:<br>  name: dice<br>spec:<br>  schedule: &quot;*/1 * * * *&quot;<br>  jobTemplate:<br>    spec:<br>      backoffLimit: 25<br>      activeDeadlineSeconds: 20<br>      completions: 1<br>      template:<br>        metadata:<br>          name: throw-dice-pod<br>        spec:<br>          containers:<br>          -  image: kodekloud/throw-dice<br>             name: throw-dice<br>          restartPolicy: Never</pre><p>lets create and validate the cron job</p><pre># create<br>kubectl apply -f cj.yaml  # cronjob.batch/dice created<br><br>#validate<br>kubectl get cj<br><br>NAME   SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE<br>dice   */1 * * * *   False     0        47s             75s<br><br># after few minutes<br>kubectl get job<br><br>NAME            COMPLETIONS   DURATION   AGE<br>dice-28201299   0/1           58s        58s<br><br>kubectl get po<br><br>NAME                  READY   STATUS    RESTARTS   AGE<br>dice-28201300-dtg8q   0/1     Error     0          41s<br>dice-28201300-f5qzb   0/1     Error     0          30s</pre><h3>Q3</h3><p>Create a pod called my-busybox in the dev2406 namespace using the busybox image. The container should be called secret and should sleep for 3600 seconds.</p><p>The container should mount a read-only secret volume called secret-volume at the path /etc/secret-volume. The secret being mounted has already been created for you and is called dotfile-secret.</p><p>Make sure that the pod is scheduled on controlplane and no other node in the cluster.</p><blockquote>Solution</blockquote><p>first lets see the labels control plane node</p><pre>kubectl describe node controlplane<br><br>...<br>Labels:             beta.kubernetes.io/arch=amd64<br>                    beta.kubernetes.io/os=linux<br>                    kubernetes.io/arch=amd64<br>                    kubernetes.io/hostname=controlplane<br>                    kubernetes.io/os=linux<br>                    node-role.kubernetes.io/control-plane=<br>                    node.kubernetes.io/exclude-from-external-load-balancers=<br>...</pre><p>lets use the label unique to this node: kubernetes.io/hostname=controlplane</p><p>pods should be scheduled on control plane node means we have to use a node selector in pod spec:</p><pre>nodeSelector:<br>  kubernetes.io/hostname: controlplane</pre><p>the final manifest my-busybox.yaml would look like:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: my-busybox<br>  namespace: dev2406<br>spec:<br>  containers:<br>  - name: secret<br>    image: busybox<br>    command: [&quot;sleep&quot;, &quot;3600&quot;]<br>    volumeMounts:<br>    - name: secret-volume<br>      mountPath: /etc/secret-volume<br>      readOnly: true<br>  volumes:<br>  - name: secret-volume<br>    secret:<br>      secretName: dotfile-secret<br>  nodeSelector:<br>    kubernetes.io/hostname: controlplane</pre><p>create and validate the pod:</p><pre># create<br>kubectl apply -f pod.yaml<br><br># validate<br>kubectl get po -n dev2406<br><br>NAME          READY   STATUS    RESTARTS   AGE<br>my-busybox    1/1     Running   0          5s</pre><h3>Q4</h3><p>Create a single ingress resource called ingress-vh-routing. The resource should route HTTP traffic to multiple hostnames as specified below:</p><ul><li>The service video-service should be accessible on <a href="http://watch.ecom-store.com:30093/video">http://watch.ecom-store.com:30093/video</a></li><li>The service apparels-service should be accessible on <a href="http://apparels.ecom-store.com:30093/wear">http://apparels.ecom-store.com:30093/wear</a></li></ul><blockquote>Solution</blockquote><p>Get the ports of the services to be used in maifest:</p><pre>kubectl get svc<br><br>NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE<br>apparels-service   ClusterIP   10.111.146.50   &lt;none&gt;        8080/TCP   2m50s<br>video-service      ClusterIP   10.99.60.40     &lt;none&gt;        8080/TCP   2m51s</pre><p>The pod manifest ingress.yaml should look like:</p><pre>apiVersion: networking.k8s.io/v1<br>kind: Ingress<br>metadata:<br>  name: ingress-vh-routing<br>  annotations:<br>    nginx.ingress.kubernetes.io/rewrite-target: /<br>spec:<br>  rules:<br>  - host: &quot;watch.ecom-store.com&quot;<br>    http:<br>      paths:<br>      - pathType: Prefix<br>        path: &quot;/video&quot;<br>        backend:<br>          service:<br>            name: video-service<br>            port:<br>              number: 8080<br>  - host: &quot;apparels.ecom-store.com&quot;<br>    http:<br>      paths:<br>      - pathType: Prefix<br>        path: &quot;/wear&quot;<br>        backend:<br>          service:<br>            name: apparels-service<br>            port:<br>              number: 8080</pre><p>create and validate the ingress</p><pre># create<br>kubectl apply -f ingress.yaml<br><br># validations<br>kubectl get ingress<br><br>NAME                 CLASS    HOSTS                                          ADDRESS       PORTS   AGE<br>ingress-vh-routing   &lt;none&gt;   watch.ecom-store.com,apparels.ecom-store.com   10.96.100.3   80      49s<br><br>curl http://watch.ecom-store.com:30093/video<br><br>&lt;!doctype html&gt;<br>&lt;title&gt;Hello from Flask&lt;/title&gt;<br>&lt;body style=&quot;background: #30336b;&quot;&gt;<br>&lt;div style=&quot;color: #e4e4e4;<br>    text-align:  center;<br>    height: 90px;<br>    vertical-align:  middle;&quot;&gt;<br>    &lt;img src=&quot;https://res.cloudinary.com/cloudusthad/image/upload/v1547052431/video.jpg&quot;&gt;<br>&lt;/div&gt;<br>&lt;/body&gt;<br><br>curl http://apparels.ecom-store.com:30093/wear<br><br>&lt;!doctype html&gt;<br>&lt;title&gt;Hello from Flask&lt;/title&gt;<br>&lt;body style=&quot;background: #2980b9;&quot;&gt;<br>&lt;div style=&quot;color: #e4e4e4;<br>    text-align:  center;<br>    height: 90px;<br>    vertical-align:  middle;&quot;&gt;<br>    &lt;img src=&quot;https://res.cloudinary.com/cloudusthad/image/upload/v1547052428/apparels.jpg&quot;&gt;<br>&lt;/div&gt;<br>&lt;/body&gt;</pre><h3>Q5</h3><p>A pod called dev-pod-dind-878516 has been deployed in the default namespace. Inspect the logs for the container called log-x and redirect the warnings to /opt/dind-878516_logs.txt on the controlplane node</p><blockquote>Solution</blockquote><p>To fetch logs and populate mentioned file:</p><pre>kubectl logs dev-pod-dind-878516 -c log-x &gt; /opt/dind-878516_logs.txt</pre><p>validate</p><pre>cat /opt/dind-878516_logs.txt</pre><p>Folks, if you like my content, would you consider following me on <a href="https://www.linkedin.com/in/hitesh-pattanayak/">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c226600f5a34" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[gRPC dynamic loadbalancing]]></title>
            <link>https://blog.devgenius.io/grpc-dynamic-loadbalancing-24d738b079e5?source=rss-183cc8bcd17d------2</link>
            <guid isPermaLink="false">https://medium.com/p/24d738b079e5</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[load-balancing]]></category>
            <category><![CDATA[grpc]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Hitesh Pattanayak]]></dc:creator>
            <pubDate>Wed, 24 May 2023 16:21:33 GMT</pubDate>
            <atom:updated>2023-05-24T18:32:52.587Z</atom:updated>
            <content:encoded><![CDATA[<p>gRPC has many benefits, like:</p><ol><li>Multiplexes many requests using same connection.</li><li>Support for typical client-server request-response as well as duplex streaming.</li><li>Usage of a fast, very light, binary protocol with structured data as the communication medium among services.</li></ol><p>More about <a href="https://www.infracloud.io/blogs/understanding-grpc-concepts-best-practices/">gRPC</a>.</p><p>All above make gRPC a very attractive deal but there is some consideration with gRPC particularly load balancing.</p><blockquote><strong>The issue</strong></blockquote><p>Lets delve deep into the issue.</p><p>For this we will require a setup. The setup includes below:</p><ul><li>a gRPC server, we call it `Greet Server`.</li><li>a client that acts as a REST gateway and internally it is a gRPC client as well. We call it `Greet Client`.</li></ul><p>We are also using kubernetes for the demonstration, hence there are a bunch of YAML manifest files. Let me explain them below:</p><pre>## greetserver-deploy.yaml ##<br>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: greetserver-deploy<br>spec:<br>  replicas: 3<br>  selector:<br>    matchLabels:<br>      run: greetserver<br>  template:<br>    metadata:<br>      labels:<br>        run: greetserver<br>    spec:<br>      containers:<br>        - image: hiteshpattanayak/greet-server:1.0<br>          imagePullPolicy: IfNotPresent<br>          name: greetserver<br>          ports:<br>            - containerPort: 50051<br>          env:<br>            - name: POD_IP<br>              valueFrom:<br>                fieldRef:<br>                  fieldPath: status.podIP<br>            - name: POD_NAME<br>              valueFrom:<br>                fieldRef:<br>                  fieldPath: metadata.name</pre><p>The above is a deployment mainfest of `Greet Server`, that spins up 3 replicas of `Greet Server`.</p><p>The `Greet Server` uses `hiteshpattanayak/greet-server:1.0` image.</p><p>Also each pod of the deployment exposes `50051` port.</p><p>Environment variables: POD_IP and POD_NAME are injected into the pods.</p><p>What does each pod in the above server do?</p><p>They expose an `rpc` or service that expects a `first_name` and a `last_name`, in response they return a message in this format:</p><pre>reponse from Greet rpc: Hello, &lt;first_name&gt; &lt;last_name&gt; from pod: name(&lt;pod_name&gt;), ip(&lt;pod_ip&gt;).</pre><p>From the response, we can deduce in which pod did our request land in.</p><pre>## greet.svc.yaml ##<br>apiVersion: v1<br>kind: Service<br>metadata:<br>  labels:<br>    run: greetserver<br>  name: greetserver<br>  namespace: default<br>spec:<br>  ports:<br>    - name: grpc<br>      port: 50051<br>      protocol: TCP<br>      targetPort: 50051<br>  selector:<br>    run: greetserver</pre><p>The above is a service manifest of `Greet server service`. This basically acts as a proxy to above `Greet Server` pods.</p><p>The `selector` section of the service matches with the `labels` section of each pod.</p><pre>## greetclient-deploy.yaml ##<br>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: greetclient-deploy<br>spec:<br>  replicas: 1<br>  selector:<br>    matchLabels:<br>      run: greetclient<br>  template:<br>    metadata:<br>      labels:<br>        run: greetclient<br>    spec:<br>      containers:<br>        - image: hiteshpattanayak/greet-client:4.0<br>          name: greetclient<br>          ports:<br>            - containerPort: 9091<br>          env:<br>            - name: GRPC_SERVER_HOST<br>              value: greetserver.default.svc.cluster.local<br>            - name: GRPC_SVC<br>              value: greetserver<br>            - name: POD_NAMESPACE<br>              valueFrom:<br>                fieldRef:<br>                  fieldPath: metadata.namespace</pre><p>The above is a deployment mainfest of `Greet Client`, that spins up 1 replica of `Greet Client`.</p><p>As mentioned above the pod runs an applications that acts as a rest gateway and reaches out to `Greet Server` in order to process the request.</p><p>This deployment is using `hiteshpattanayak/greet-client:4.0` image.</p><p>The `4.0` tagged image has the load balancing issue.</p><p>Also the pod(s) expose port `9091`.</p><pre>## greetclient-svc.yaml ##<br>apiVersion: v1<br>kind: Service<br>metadata:<br>  labels:<br>    run: greetclient<br>  name: greetclient<br>  namespace: default<br>spec:<br>  ports:<br>    - name: restgateway<br>      port: 9091<br>      protocol: TCP<br>      targetPort: 9091<br>  selector:<br>    run: greetclient<br>  type: LoadBalancer</pre><p>The above service is just to redirect traffic to the `Greet Client` pods.</p><pre>## greet-ingress.yaml ##<br>apiVersion: networking.k8s.io/v1<br>kind: Ingress<br>metadata:<br>  name: greet-ingress<br>  namespace: default<br>  annotations:<br>    nginx.ingress.kubernetes.io/ssl-redirect: &quot;false&quot;<br>spec:<br>  rules:<br>    - host: greet.com<br>      http:<br>        paths:<br>          - path: /<br>            pathType: Prefix<br>            backend:<br>              service:<br>                name: greetclient<br>                port:<br>                  name: restgateway</pre><p>The above ingress is to expose `Greet Client Service` to outside of the cluster.</p><p><em>Note</em>:</p><p><em>`minikube` by default does not have ingress enabled by default</em></p><ul><li><em>check enabled or not: `minikube addons list`</em></li><li><em>enable ingress addon: `minikube addons enable ingress`</em></li></ul><pre>## greet-clusterrole.yaml ##<br>kind: ClusterRole<br>apiVersion: rbac.authorization.k8s.io/v1<br>metadata:<br>  namespace: default<br>  name: service-reader<br>rules:<br>  - apiGroups: [&quot;&quot;]<br>    resources: [&quot;services&quot;]<br>    verbs: [&quot;get&quot;, &quot;watch&quot;, &quot;list&quot;]<br><br>---<br><br>## greet-clusterrolebinding.yaml ##<br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: ClusterRoleBinding<br>metadata:<br>  name: service-reader-binding<br>roleRef:<br>  apiGroup: rbac.authorization.k8s.io<br>  kind: ClusterRole<br>  name: service-reader<br>subjects:<br>  - kind: ServiceAccount<br>    name: default<br>    namespace: default</pre><p>The cluster role and cluster role binding are required because the `default` service account does not have permission to fetch service details.</p><p>And the greet client pod internally tries to fetch service details, hence the binding is required.</p><p>Create the setup in below sequence:</p><pre>kubectl create -f greet-clusterrole.yaml<br><br>kubectl create -f greet-clusterrolebinding.yaml<br><br>kubectl create -f greetserver-deploy.yaml<br><br>kubectl get po -l &#39;run=greetserver&#39; -o wide<br>NAME                                  READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES<br>greetserver-deploy-7595ccbdd5-67bmd   1/1     Running   0          91s   172.17.0.4   minikube   &lt;none&gt;           &lt;none&gt;<br>greetserver-deploy-7595ccbdd5-k6zbl   1/1     Running   0          91s   172.17.0.3   minikube   &lt;none&gt;           &lt;none&gt;<br>greetserver-deploy-7595ccbdd5-l8kmv   1/1     Running   0          91s   172.17.0.2   minikube   &lt;none&gt;           &lt;none&gt;<br><br><br>kubectl create -f greet.svc.yaml<br>kubectl get svc<br>NAME          TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)     AGE<br>greetserver   ClusterIP   None         &lt;none&gt;        50051/TCP   77s<br><br><br>kubectl create -f greetclient-deploy.yaml<br>kubectl get po -l &#39;run=greetclient&#39; -o wide<br>NAME                                 READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES<br>greetclient-deploy-6bddb94df-jwr25   1/1     Running   0          35s   172.17.0.6   minikube   &lt;none&gt;           &lt;none&gt;<br><br><br>kubectl create -f greet-client.svc.yaml<br>kubectl get svc<br>NAME          TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE<br>greetclient   LoadBalancer   10.110.255.115   &lt;pending&gt;     9091:32713/TCP   22s<br>greetserver   ClusterIP      None             &lt;none&gt;        50051/TCP        5m14s<br><br><br>kubectl create -f greet-ingress.yaml<br>kubectl get ingress<br>NAME            CLASS   HOSTS       ADDRESS        PORTS   AGE<br>greet-ingress   nginx   greet.com   192.168.49.2   80      32s</pre><p>since we have exposed the `Greet Client` to outside of cluster via `greet-ingress`, the endpoint can be accessed on: `http://greet.com/greet`.</p><p>so when we make a curl request:</p><blockquote>Request#1</blockquote><pre><br>curl --request POST \<br>  --url http://greet.com/greet \<br>  --header &#39;Content-Type: application/json&#39; \<br>  --data &#39;{<br> &quot;first_name&quot;: &quot;Hitesh&quot;,<br> &quot;last_name&quot;: &quot;Pattanayak&quot;<br>}<br><br>## Response<br>## reponse from Greet rpc: Hello, Hitesh Pattanayak from pod: name(greetserver-deploy-7595ccbdd5-l8kmv), ip(172.17.0.2).</pre><blockquote>Request#2</blockquote><pre>curl --request POST \<br>  --url http://greet.com/greet \<br>  --header &#39;Content-Type: application/json&#39; \<br>  --data &#39;{<br> &quot;first_name&quot;: &quot;Hitesh&quot;,<br> &quot;last_name&quot;: &quot;Pattanayak&quot;<br>}<br><br>## Response<br>## reponse from Greet rpc: Hello, Hitesh Pattanayak from pod: name(greetserver-deploy-7595ccbdd5-l8kmv), ip(172.17.0.2).</pre><blockquote>Request#3</blockquote><pre>curl --request POST \<br>  --url http://greet.com/greet \<br>  --header &#39;Content-Type: application/json&#39; \<br>  --data &#39;{<br> &quot;first_name&quot;: &quot;Hitesh&quot;,<br> &quot;last_name&quot;: &quot;Pattanayak&quot;<br>}<br><br>## Response<br>## reponse from Greet rpc: Hello, Hitesh Pattanayak from pod: name(greetserver-deploy-7595ccbdd5-l8kmv), ip(172.17.0.2).</pre><p>So the <strong>ISSUE</strong> is no matter hw many request I make, the request lands up in the same server. This is happending because of sticky nature of HTTP/2.</p><p>The advantage of gRPC becomes it own peril.</p><p>The codebase to replicate the issue can be found <a href="https://github.com/HiteshRepo/grpc-loadbalancing/commit/dd31d2628d4ee1e47b07b5737ff51cfc43c76d4e">here</a>.</p><blockquote><strong>gRPC Client side load balancing</strong></blockquote><p>We have discussed earlier about one of the challenges with gRPC which is load balancing.</p><p>That happens due to the sticky nature of gRPC connections.</p><p>Now we shall discuss how to resolve the issue.</p><p>This particular solution is quite simple.</p><p>The onus to load balance falls on the client itself.</p><p>To be particular, client does not mean end user. All gRPC servers have a REST gateway that is used by end users.</p><p>This is because HTTP2, which is the protocol used by gRPC, is yet to have browser support.</p><p>Hence the REST gateway acts as a gRPC client to gRPC servers. And thats why gRPC is mostly used for internal communications.</p><p>Earlier we had used `hiteshpattanayak/greet-client:4.0` image for `Greet Client` which had the normal gRPC setup without client side load balancing.</p><p>The code can be referred <a href="https://github.com/HiteshRepo/grpc-loadbalancing/commit/dd31d2628d4ee1e47b07b5737ff51cfc43c76d4e">here</a>.</p><h4>Code changes to achieve this</h4><p>For this solution we use `hiteshpattanayak/greet-client:11.0` image. The <a href="https://github.com/HiteshRepo/grpc-loadbalancing/pull/1/files">codebase</a> has below changes:</p><p><em>Updated client deployment manifest</em></p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: greetclient-deploy<br>spec:<br>  replicas: 1<br>  selector:<br>    matchLabels:<br>      run: greetclient<br>  template:<br>    metadata:<br>      labels:<br>        run: greetclient<br>    spec:<br>      containers:<br>        - image: hiteshpattanayak/greet-client:11.0<br>          name: greetclient<br>          ports:<br>            - containerPort: 9091<br>          env:<br>            - name: GRPC_SVC<br>              value: greetserver<br>            - name: POD_NAMESPACE<br>              valueFrom:<br>                fieldRef:<br>                  fieldPath: metadata.namespace</pre><p><em>Configuring load balancing policy while making dialing to the server &amp; configuring to terminate connection while dialing to the server</em></p><pre>a.conn, err = grpc.Dial(<br>  servAddr,<br>  grpc.WithDefaultServiceConfig(`{&quot;loadBalancingPolicy&quot;:&quot;round_robin&quot;}`),<br>  grpc.WithBlock(),<br>  opts,<br> )</pre><p><em>The server address used while dialing needs to the dns address of the server</em></p><pre>var serverHost string<br>if host := kubernetes.GetServiceDnsName(client, os.Getenv(&quot;GRPC_SVC&quot;), os.Getenv(&quot;POD_NAMESPACE&quot;)); len(host) &gt; 0 {<br>  serverHost = host<br> }<br><br>servAddr := fmt.Sprintf(&quot;%s:%s&quot;, serverHost, serverPort)</pre><blockquote><strong>Headless service</strong></blockquote><p>Also earlier while replicating the issue the service (greetserver) we created for `Greet server pods` was of normal `ClusterIP` type. But here we would require a headless ClusterIP service is required for this solution.</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  labels:<br>    run: greetserver<br>  name: greetserver<br>  namespace: default<br>spec:<br>  ports:<br>    - name: grpc<br>      port: 50051<br>      protocol: TCP<br>      targetPort: 50051<br>  selector:<br>    run: greetserver<br>  clusterIP: None</pre><p>One significant thing to notice over here is that this is a special type of `ClusterIP` service called `Headless` service.</p><p>In this `service` kind, the type of service is not specified. By default the type becomes `ClusterIP`. Which means the service becomes available within cluster.</p><p>You can set `.spec.clusterIP`, if you already have an existing DNS entry that you wish to reuse.</p><p>In case you set `.spec.clusterIP` to `None`, it makes the service `headless`, which means when a client sends a request to a headless Service, it will get back a list of all Pods that this Service represents (in this case, the ones with the label `run: greetserver`).</p><p>Kubernetes allows clients to discover pod IPs through DNS lookups. Usually, when you perform a DNS lookup for a service, the DNS server returns a single IP — the service’s cluster IP. But if you tell Kubernetes you don’t need a cluster IP for your service (you do this by setting the clusterIP field to None in the service specification ), the DNS server will return the pod IPs instead of the single service IP. Instead of returning a single DNS A record, the DNS server will return multiple A records for the service, each pointing to the IP of an individual pod backing the service at that moment. Clients can therefore do a simple DNS A record lookup and get the IPs of all the pods that are part of the service. The client can then use that information to connect to one, many, or all of them.</p><p>Basically, the Service now lets the client decide on how it wants to connect to the Pods.</p><p><strong>Verify headless service DNS lookup</strong></p><p>create the headless service:</p><pre>kubectl create -f greet.svc.yaml</pre><p>create an utility pod:</p><pre>kubectl run dnsutils --image=tutum/dnsutils --command -- sleep infinity</pre><p>verify by running `nslookup` command on the pod</p><pre>kubectl exec dnsutils --  nslookup greetserver<br><br>## Result<br>Server:         10.96.0.10<br>Address:        10.96.0.10#53<br>Name:   greetserver.default.svc.cluster.local<br>Address: 172.17.0.4<br>Name:   greetserver.default.svc.cluster.local<br>Address: 172.17.0.3<br>Name:   greetserver.default.svc.cluster.local<br>Address: 172.17.0.2</pre><p>As you can see headless service resolves into the IP address of all pods connected through service.</p><p>Contrast this with the output returned for non-headless service.</p><pre>kubectl exec dnsutils --  nslookup greetclient<br><br>## Result<br>Server:  10.96.0.10<br>Address: 10.96.0.10#53<br><br>Name: greetclient.default.svc.cluster.local<br>Address: 10.110.255.115</pre><p>Now lets test the changes by making curl requests to the exposed ingress.</p><blockquote>Request#1</blockquote><pre>curl --request POST \<br>  --url http://greet.com/greet \<br>  --header &#39;Content-Type: application/json&#39; \<br>  --data &#39;{<br> &quot;first_name&quot;: &quot;Hitesh&quot;,<br> &quot;last_name&quot;: &quot;Pattanayak&quot;<br>}<br><br>## Response<br>## reponse from Greet rpc: Hello, Hitesh Pattanayak from pod: name(greetserver-deploy-7595ccbdd5-k6zbl), ip(172.17.0.3).</pre><blockquote>Request#2</blockquote><pre>curl --request POST \<br>  --url http://greet.com/greet \<br>  --header &#39;Content-Type: application/json&#39; \<br>  --data &#39;{<br> &quot;first_name&quot;: &quot;Hitesh&quot;,<br> &quot;last_name&quot;: &quot;Pattanayak&quot;<br>}<br><br>## Response<br>## reponse from Greet rpc: Hello, Hitesh Pattanayak from pod: name(greetserver-deploy-7595ccbdd5-67bmd), ip(172.17.0.4).</pre><blockquote>Request#3</blockquote><pre>curl --request POST \<br>  --url http://greet.com/greet \<br>  --header &#39;Content-Type: application/json&#39; \<br>  --data &#39;{<br> &quot;first_name&quot;: &quot;Hitesh&quot;,<br> &quot;last_name&quot;: &quot;Pattanayak&quot;<br>}<br><br>## Response<br>## reponse from Greet rpc: Hello, Hitesh Pattanayak from pod: name(greetserver-deploy-7595ccbdd5-l8kmv), ip(172.17.0.2).</pre><p>The issue no longer exists.</p><p>But what we are losing here is the capability of gRPC to retain connections for a longer period of time and multiplex several requests through them thereby reducing latency.</p><blockquote><strong>gRPC lookaside load balancing</strong></blockquote><p>Earlier we discussed about:</p><ul><li>Load balancing challenge with gRPC</li><li>How to address the above challenge via client side load balancing</li></ul><p>Even though we were able to resolve the load balancing issue but we traded off one of the major advantage of gRPC which is long duration connections.</p><p>So in this post we would like the achive load balancing (still client side) but we are gonna not trade off the above mentionde gRPC’s advantage.</p><p>I would like to re-iterate when I say onus to load balance falls on `client side`, client does not mean end user. All gRPC servers have a REST gateway that is used by end users. gRPC services are not directly exposed because of lack of browser support.</p><blockquote><strong>Lookaside load balancer</strong></blockquote><p>The purpose of this load balancer is to resolve which gRPC server to connect.</p><p>At the moment this load balancer works in two ways: round robin and random.</p><p>Load balancer itself is gRPC based and since the load is not going to be too much only one pod would suffice.</p><p>It exposes a service called `lookaside` and an rpc called `Resolve` which expects the type of routing along with some details about the gRPC servers like kubernetes service name and namespace they exist in.</p><p>Using the service name and namespace, it is going to fetch kubernetes endpoints object associated with it. From the endpoint object server IPs can be found.</p><p>Those IPs are going to be stored in memory. Every now and then those IPs would be refreshed based on interval set. For every request to resolve IP, it is going to rotate the IPs based on the routing type in the request.</p><p>Code for lookaside load balancer can be found <a href="https://github.com/HiteshRepo/grpc-loadbalancing/tree/lookaside/internal/app/lookaside">here</a>.</p><p>We are using the image `hiteshpattanayak/lookaside:9.0` for lookaside pod.</p><p>The pod manifest would be like this:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  labels:<br>    run: lookaside<br>  name: lookaside<br>  namespace: default<br>spec:<br>  containers:<br>    - image: hiteshpattanayak/lookaside:9.0<br>      name: lookaside<br>      ports:<br>        - containerPort: 50055<br>      env:<br>        - name: LB_PORT<br>          value: &quot;50055&quot;</pre><p>since it is too a gRPC server, the exposed port is `50055`.</p><p>The service manifest that exposes the pod is as below:</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  labels:<br>    run: lookaside-svc<br>  name: lookaside-svc<br>  namespace: default<br>spec:<br>  ports:<br>    - port: 50055<br>      protocol: TCP<br>      targetPort: 50055<br>  selector:<br>    run: lookaside<br>  clusterIP: None</pre><p>I chose `headless` service for this as well but there is no such need for this.</p><p>Updated the `ClusterRole` to include ability to fetch `endpoints` and `pod` details</p><pre>kind: ClusterRole<br>apiVersion: rbac.authorization.k8s.io/v1<br>metadata:<br>  namespace: default<br>  name: service-reader<br>rules:<br>  - apiGroups: [&quot;&quot;]<br>    resources: [&quot;services&quot;, &quot;pods&quot;, &quot;endpoints&quot;]<br>    verbs: [&quot;get&quot;, &quot;watch&quot;, &quot;list&quot;]</pre><blockquote><strong>Changes with Greet Client</strong></blockquote><p>`Greet Client` is now <a href="https://github.com/HiteshRepo/grpc-loadbalancing/blob/177c0fdccad06a76d7d6ce221ee267a47244dc43/internal/app/greetclient/app.go#L38">integrated</a> with lookaside loadbalancer.</p><p>The client is <a href="https://github.com/HiteshRepo/grpc-loadbalancing/blob/177c0fdccad06a76d7d6ce221ee267a47244dc43/internal/app/greetclient/app.go#L131">set</a> to use `RoundRobin` routing type but can be made configurable via configmap or environment variables.</p><p>Removed setting `load-balancing` policy and forcefully terminating connection by setting `WithBlock` option while dialing.</p><p>from</p><pre>conn, err := grpc.Dial(<br>    servAddr,<br>    grpc.WithDefaultServiceConfig(`{&quot;loadBalancingPolicy&quot;:&quot;round_robin&quot;}`),<br>    grpc.WithBlock(),<br>    opts,<br>)</pre><p>to</p><pre>conn, err := grpc.Dial(<br>    servAddr,<br>    opts,<br>)</pre><p>So how does it solve the earlier load balancing problem where we traded off terminating long duration connections for the sake of load balancing.</p><p>What we did was to store the previous connections to the server and reuse it but rotate for each request.</p><pre>if c, ok := a.greetClients[host]; !ok {<br>    servAddr := fmt.Sprintf(&quot;%s:%s&quot;, host, serverPort)<br><br>    fmt.Println(&quot;dialing greet server&quot;, servAddr)<br><br>    conn, err := grpc.Dial(<br>        servAddr,<br>        opts,<br>    )<br>    if err != nil {<br>        log.Printf(&quot;could not connect greet server: %v&quot;, err)<br>        return err<br>    }<br><br>    a.conn[host] = conn<br><br>    a.currentGreetClient = proto.NewGreetServiceClient(conn)<br>    a.greetClients[host] = a.currentGreetClient<br>} else {<br>    a.currentGreetClient = c<br>}</pre><blockquote>Conclusion</blockquote><p>gRPC is a great solution for microservice internal communication because of efficiency, speed and parity. But the long duration connections though an advantage results in tricky load balancing. With the help of this article we found ways to handle it.</p><p>There are ways to handle via service meshes like Linkerd and Istio. But it would be handy to have solutions incase where service meshes are not setup.</p><p>Folks, if you like my content, would you consider following me on <a href="https://www.linkedin.com/in/hitesh-pattanayak-52290b160/">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24d738b079e5" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/grpc-dynamic-loadbalancing-24d738b079e5">gRPC dynamic loadbalancing</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Blue Green Deployment with Kubernetes]]></title>
            <link>https://blog.devgenius.io/blue-green-deployment-with-kubernetes-b7595b17fe17?source=rss-183cc8bcd17d------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7595b17fe17</guid>
            <category><![CDATA[services]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[blue-green-deployment]]></category>
            <category><![CDATA[strategy-deployment]]></category>
            <category><![CDATA[k8s]]></category>
            <dc:creator><![CDATA[Hitesh Pattanayak]]></dc:creator>
            <pubDate>Sun, 02 Apr 2023 07:50:23 GMT</pubDate>
            <atom:updated>2023-04-02T15:52:20.016Z</atom:updated>
            <content:encoded><![CDATA[<p>Blue-green deployment is a software deployment strategy that allows the release of new features or updates without any downtime or service disruption. In this approach, two identical environments are created, one being the active or live environment (blue) and the other being the idle environment (green) where the new changes are deployed and tested before switching the traffic to the new environment.</p><p>The process of blue-green deployment involves the following steps:</p><p>1. The initial production environment (blue) is running and actively serving customer traffic.</p><p>2. A new environment (green) is created with the updated codebase or application version.</p><p>3. The new environment is thoroughly tested to ensure that everything is working correctly and that the application is stable.</p><p>4. Once the new environment is deemed stable, traffic is switched from the old environment to the new environment, and the old environment is decommissioned.</p><p>The main advantages of blue-green deployment are:</p><p>1. Reduced downtime: Since the new environment is tested thoroughly before deploying to the live environment, there is minimal to no downtime during the actual deployment process.</p><p>2. Risk mitigation: In case of any issues or bugs that might have been missed during testing, the old environment can be quickly switched back to, reducing the risk of downtime or service disruption.</p><p>3. Fast rollback: If any problems occur after the new environment is deployed, the switch back to the old environment can be done instantly, ensuring fast rollback with minimal impact to the end-users.</p><p>4. Increased reliability: Blue-green deployment enables a reliable and consistent approach to application updates, ensuring that there is minimal disruption to the end-users.</p><p>5. Testing in production: Blue-green deployment also allows for testing in production, which is an efficient way of testing new changes while avoiding any impact on the live environment.</p><blockquote><strong>Blue-Green vs Canary</strong></blockquote><p>There is another popular deployment strategy called ‘<a href="https://medium.com/@hitesh-pattanayak/canary-deployment-with-kubernetes-f49fbc6ec04b">Canary Deployment</a>’.</p><p>Although they share some similarities, there are significant differences between the two.</p><p>Canary deployment is a technique that allows you to deploy new changes to a small subset of users or servers, often referred to as the “canary group,” and then gradually roll out the changes to the rest of the users or servers. This approach allows you to test the new changes on a small scale before making them available to the entire user base.</p><p>One of the main differences between the two is that in blue-green deployment, both the old and new versions of the application are running at the same time, while in canary deployment, only one version of the application is running at a time. Another difference is that blue-green deployment typically involves switching the entire traffic from one environment to another, while canary deployment gradually increases the traffic to the new version.</p><p>Both deployment strategies have their advantages and disadvantages, and the choice between the two depends on various factors such as the size of the user base, the criticality of the application, and the available resources. Blue-green deployment is preferred when there are a large number of users, and the application needs to be highly available, while canary deployment is a good option when you want to test new changes on a small scale before rolling them out to the entire user base.</p><blockquote><strong>Steps to demonstrate blue-green deployment</strong></blockquote><p><strong>Create Blue deployment</strong></p><ul><li>with pod label `app: blue`</li><li>with an busybox initContainer</li></ul><pre>initContainers:<br>    - name: install<br>    image: busybox:1.28<br>    command:<br>    - /bin/sh<br>    - -c<br>    - &quot;echo blue &gt; /work-dir/index.html&quot;<br>    volumeMounts:<br>    - name: workdir<br>        mountPath: &quot;/work-dir&quot;</pre><p><strong>Final blue deployment</strong></p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: blue-1.10<br>spec:<br>  replicas: 3<br>  selector:<br>    matchLabels:<br>      name: nginx<br>      app: blue<br>  template:<br>    metadata:<br>      labels:<br>        name: nginx<br>        app: blue<br>    spec:Create Blue deployment<br><br><br>      initContainers:<br>      - name: install<br>        image: busybox:1.28<br>        command:<br>        - /bin/sh<br>        - -c<br>        - &quot;echo blue &gt; /work-dir/index.html&quot;<br>        volumeMounts:<br>        - name: workdir<br>          mountPath: &quot;/work-dir&quot;<br>      containers: <br>        - name: nginx<br>          image: nginx:1.10<br>          ports:<br>          - name: http<br>            containerPort: 80<br>          volumeMounts:<br>          - name: workdir<br>            mountPath: /usr/share/nginx/html<br>      volumes:<br>      - name: workdir<br>        emptyDir: {}</pre><p><strong>Create a service to interact with the blue deployment</strong></p><pre>apiVersion: v1<br>kind: Service<br>metadata: <br>  name: nginx<br>  labels: <br>    name: nginx<br>spec:<br>  ports:<br>    - name: http<br>      port: 80<br>      targetPort: 80<br>  selector: <br>    name: nginx<br>    app: blue</pre><p><strong>Create both deployment and service</strong></p><pre>kubectl create -f blue-deploy.yaml<br>kubectl create -f blue-green-svc.yaml</pre><p><strong>Validate both the objects</strong></p><pre>kubectl get deploy<br><br>NAME         READY   UP-TO-DATE   AVAILABLE   AGE<br>blue-1.10   3/3     3            3           4m37s<br><br><br>kubectl get svc<br><br>NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE<br>nginx        ClusterIP   10.109.140.77    &lt;none&gt;        80/TCP    57s<br><br>kubectl get po<br><br>NAME                          READY   STATUS    RESTARTS   AGE<br>blue-1.10-579857b89c-jvthn   1/1     Running   0          5m51s<br>blue-1.10-579857b89c-thq2z   1/1     Running   0          5m51s<br>blue-1.10-579857b89c-vhrvc   1/1     Running   0          5m51s</pre><p><strong>Test the deployment</strong></p><pre>kubectl run -it --rm --restart=Never busybox --image=gcr.io/google-containers/busybox --command -- wget -qO- nginx<br><br># o/p<br>blue<br>pod &quot;busybox&quot; deleted</pre><p><strong>Create Green deployment</strong></p><p>Green deployment is same as blue except for label `app=green` and command echoing `green` instead of `blue`.</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: green-1.10<br>spec:<br>  replicas: 3<br>  selector:<br>    matchLabels:<br>      name: nginx<br>      app: green<br>  template:<br>    metadata:<br>      labels:<br>        name: nginx<br>        app: green<br>    spec:<br>      initContainers:<br>      - name: install<br>        image: busybox:1.28<br>        command:<br>        - /bin/sh<br>        - -c<br>        - &quot;echo green &gt; /work-dir/index.html&quot;<br>        volumeMounts:<br>        - name: workdir<br>          mountPath: &quot;/work-dir&quot;<br>      containers: <br>        - name: nginx<br>          image: nginx:1.10<br>          ports:<br>          - name: http<br>            containerPort: 80<br>          volumeMounts:<br>          - name: workdir<br>            mountPath: /usr/share/nginx/html<br>      volumes:<br>      - name: workdir<br>        emptyDir: {}</pre><p><strong>Create and validate green deployment</strong></p><pre>kubectl create -f green-deploy.yaml<br><br>kubectl get deployments<br><br>NAME         READY   UP-TO-DATE   AVAILABLE   AGE<br>blue-1.10    3/3     3            3           3m33s<br>green-1.10   3/3     3            3           2m29s<br><br>kubectl get po<br><br>NAME                          READY   STATUS    RESTARTS   AGE<br>blue-1.10-6c6ff57655-9p5pf    1/1     Running   0          3m52s<br>blue-1.10-6c6ff57655-knpf5    1/1     Running   0          3m52s<br>blue-1.10-6c6ff57655-wpgqp    1/1     Running   0          3m52s<br>green-1.10-7f754b7675-kfj89   1/1     Running   0          2m48s<br>green-1.10-7f754b7675-nd749   1/1     Running   0          2m48s<br>green-1.10-7f754b7675-nrd9m   1/1     Running   0          2m48s</pre><p><strong>Update the selector of service nginx</strong></p><p>Change selector of service `nginx` from `app: blue` to `app: green`</p><pre>apiVersion: v1<br>kind: Service<br>metadata: <br>  name: nginx<br>  labels: <br>    name: nginx<br>spec:<br>  ports:<br>    - name: http<br>      port: 80<br>      targetPort: 80<br>  selector: <br>    name: nginx<br>    app: green</pre><p><strong>Re-Apply the service object</strong></p><pre>kubectl apply -f blue-green-svc.yaml</pre><p><strong>Test the deployment</strong></p><pre>kubectl run -it --rm --restart=Never busybox --image=gcr.io/google-containers/busybox --command -- wget -qO- nginx<br><br># o/p<br>green<br>pod &quot;busybox&quot; deleted</pre><blockquote><strong>Simple bash script to automate blue to green switch</strong></blockquote><pre>#!/bin/bash<br><br># bg-deploy.sh &lt;service-name&gt; &lt;blue-deployment-name&gt; &lt;green-deployment-yaml-path&gt; &lt;green-deployment-name&gt;<br><br>SERVICE=$1<br>BLUEDEPLOYMENTNAME=$2<br>GREENDEPLOYMENTFILE=$3<br>GREENDEPLOYMENTNAME=$2<br><br>kubectl apply -f $GREENDEPLOYMENTFILE<br><br># Wait until the Green Deployment is ready by checking the MinimumReplicasAvailable condition.<br>READY=$(kubectl get deploy $GREENDEPLOYMENTNAME -o json | jq &#39;.status.conditions[] | select(.reason == &quot;MinimumReplicasAvailable&quot;) | .status&#39; | tr -d &#39;&quot;&#39;)<br>while [[ &quot;$READY&quot; != &quot;True&quot; ]]; do<br>    READY=$(kubectl get deploy $GREENDEPLOYMENTNAME -o json | jq &#39;.status.conditions[] | select(.reason == &quot;MinimumReplicasAvailable&quot;) | .status&#39; | tr -d &#39;&quot;&#39;)<br>    sleep 5<br>done<br><br># Update the service selector with the new version<br>kubectl patch svc $SERVICE -p &quot;{\&quot;spec\&quot;:{\&quot;selector\&quot;: {\&quot;name\&quot;: \&quot;${SERVICE}\&quot;, \&quot;app\&quot;: \&quot;green\&quot;}}}&quot;<br><br># Delete blue deploy [optional]<br>kubectl delete deploy $BLUEDEPLOYMENTNAME<br><br>echo &quot;Done.&quot;</pre><p>In conclusion, blue-green deployment is an efficient and reliable approach to software deployment, allowing for the release of new features or updates with minimal to no downtime. With its ability to test in production, fast rollback, and reduced risk of service disruption, blue-green deployment is a preferred choice for applications that need to be highly available. While canary deployment shares some similarities with blue-green deployment, it differs in its approach to gradual rollout and testing on a small scale. The choice between the two deployment strategies depends on various factors, including the size of the user base, criticality of the application, and available resources. Overall, blue-green deployment is an excellent option for large applications that require high availability and reliability.</p><p>Folks, if you like my content, would you consider following me on linked in at: <a href="https://www.linkedin.com/in/hitesh-pattanayak-52290b160/">https://www.linkedin.com/in/hitesh-pattanayak-52290b160/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7595b17fe17" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/blue-green-deployment-with-kubernetes-b7595b17fe17">Blue Green Deployment with Kubernetes</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Canary Deployment with Kubernetes]]></title>
            <link>https://blog.devgenius.io/canary-deployment-with-kubernetes-f49fbc6ec04b?source=rss-183cc8bcd17d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f49fbc6ec04b</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[canary-deployments]]></category>
            <category><![CDATA[load-balancing]]></category>
            <category><![CDATA[k8s]]></category>
            <category><![CDATA[deployment]]></category>
            <dc:creator><![CDATA[Hitesh Pattanayak]]></dc:creator>
            <pubDate>Sat, 01 Apr 2023 03:27:53 GMT</pubDate>
            <atom:updated>2023-04-02T15:54:02.209Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post, we will learn how to do canary deployment using Kubernetes. In a canary deployment, a small set of users or requests are directed to a new version of the software while the majority of the traffic is still being handled by the old version. This allows us to test new versions of software in production without risking the entire system.</p><h4>Advantage of Canary Deployment</h4><p>Canary deployment is a deployment strategy where a new version of an application is gradually rolled out to a small subset of users or servers before it is released to the entire user base. The advantages of canary deployment include:</p><ol><li>Early detection of issues: Canary deployment allows you to test new features or changes on a small scale before rolling them out to your entire user base. This helps you to detect and fix any issues or bugs early on, minimizing the impact on your users.</li><li>Reduced risk: With canary deployment, you are reducing the risk of deploying new features or changes by limiting the scope of the rollout. This makes it easier to recover from any issues that may arise.</li><li>Better user experience: By gradually rolling out changes to a small subset of users, you can gather feedback and make adjustments before releasing the changes to your entire user base. This ensures a better user experience for your customers.</li><li>Improved performance: Canary deployment can improve the performance of your application by allowing you to test and optimize new features or changes on a small scale before rolling them out to your entire user base.</li><li>Increased agility: Canary deployment enables you to be more agile in your development process by allowing you to release new features or changes more frequently and with less risk. This can help you to stay ahead of the competition and meet the changing needs of your users.</li></ol><h4>Steps to demontrate Canary Deployment</h4><p>Let’s start by creating two nginx deployments with labels version=v1 and version=v2 to differentiate. We will call our first deployment nginx-app-1 and use the following command:</p><pre>kubectl create deploy nginx-app-1 --image=nginx --replicas=3 --dry-run=client -o yaml &gt; deploy-1.yaml</pre><p>Next, we will edit the deploy-1.yaml file and add a label app=v1 to the metadata.labels section. We will also add an initContainers section for the busybox pod:</p><pre>initContainers:<br>  - name: install<br>    image: busybox:1.28<br>    command:<br>      - /bin/sh<br>      - -c<br>      - &quot;echo version-1 &gt; /work-dir/index.html&quot;<br>    volumeMounts:<br>      - name: workdir<br>        mountPath: &quot;/work-dir&quot;</pre><p>Now, let’s create a service for the deployment to be accessible:</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: nginx-app-svc<br>  labels:<br>    app: nginx-app<br>spec:<br>  type: ClusterIP<br>  ports:<br>    - name: http<br>      port: 80<br>      targetPort: 80<br>  selector:<br>    app: nginx-app</pre><p>We can test the deployment by running:</p><pre>kubectl run -it --rm --restart=Never busybox --image=gcr.io/google-containers/busybox --command -- wget -qO- nginx-app-svc</pre><p>Now, let’s create another similar deployment with one replica, but with a label value of app=v2. We will call this deployment nginx-app-2:</p><pre>kubectl create deploy nginx-app-2 --image=nginx --replicas=1 --dry-run=client -o yaml &gt; deploy-2.yaml</pre><p>We will also edit the deploy-2.yaml file and add a label app=v2 to the metadata.labels section. We will deploy this new version of the software by running:</p><pre>kubectl apply -f deploy-2.yaml</pre><p>We can continuously call the service to see how the load balancer diverts the traffic between the two versions. To do this, we can run:</p><pre>kubectl run -it --rm --restart=Never busybox --image=gcr.io/google-containers/busybox -- /bin/sh -c &#39;while sleep 1; do wget -qO- nginx-app-svc; done&#39;</pre><p>Once we determine that nginx-app-2 is stable and we would like to deprecate nginx-app-1, we can delete the old deployment by running:</p><pre>kubectl delete deploy nginx-app-1</pre><p>All traffic will then be directed to nginx-app-2. We can also scale nginx-app-2 to four replicas by running:</p><pre>kubectl scale --replicas=4 deploy nginx-app-2</pre><p>To check the traffic, we can run the following command:</p><pre>while sleep 0.1; do curl $(kubectl get svc nginx-app-svc -o jsonpath=&quot;{.spec.clusterIP}&quot;); done</pre><p>In summary, canary deployment is a powerful tool that can help you to deploy new features or changes with more confidence and less risk. By gradually rolling out changes and gathering feedback, you can improve the user experience, performance, and agility of your application.</p><p>Folks, if you like my content, would you consider following me on linked in at: <a href="https://www.linkedin.com/in/hitesh-pattanayak-52290b160/">https://www.linkedin.com/in/hitesh-pattanayak-52290b160/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f49fbc6ec04b" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/canary-deployment-with-kubernetes-f49fbc6ec04b">Canary Deployment with Kubernetes</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Managing Kubernetes Resources with Resource Quotas]]></title>
            <link>https://blog.devgenius.io/managing-kubernetes-resources-with-resource-quotas-dbbe04fe43f6?source=rss-183cc8bcd17d------2</link>
            <guid isPermaLink="false">https://medium.com/p/dbbe04fe43f6</guid>
            <category><![CDATA[namespaces]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[k8s]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[resource-quota]]></category>
            <dc:creator><![CDATA[Hitesh Pattanayak]]></dc:creator>
            <pubDate>Fri, 31 Mar 2023 07:57:36 GMT</pubDate>
            <atom:updated>2023-03-31T11:27:14.304Z</atom:updated>
            <content:encoded><![CDATA[<p>When you’re working with Kubernetes, it is often the case that multiple teams or users share a cluster with a fixed number of nodes. In this case, we restrict teams to individual namespaces in order to prevent interference. But the overall resources are still the same for all teams put together. In this scenario, one team might consume more than its fair share of resources, which can be problematic for other teams or users. Resource quotas are a tool that Kubernetes administrators can use to ensure that each namespace, which can be thought of as a virtual cluster, receives a fair share of resources.</p><p>In this article, we will look at how resource quotas work, how to define them, and how to use them. We will also cover a YAML file that contains two Kubernetes objects: a Namespace object and a List object that contains two ResourceQuota objects. We will demonstrate how to create the YAML file, apply it to Kubernetes, and verify that the ResourceQuota objects are working as expected.</p><h4>What are Resource Quotas?</h4><p>Resource quotas are constraints that limit the amount of compute resources and objects that can be created in a namespace. A ResourceQuota object defines the usage limits for a namespace and can limit the quantity of objects that can be created in a namespace by type, as well as the total amount of compute resources that may be consumed by resources in that namespace.</p><p>Here’s an example of how resource quotas work:</p><ol><li>Different teams work in different namespaces, which can be enforced with Role-Based Access Control (RBAC).</li><li>The administrator creates one ResourceQuota for each namespace.</li><li>Users create resources (pods, services, etc.) in the namespace, and the quota system tracks usage to ensure it does not exceed hard resource limits defined in a ResourceQuota.</li><li>If creating or updating a resource violates a quota constraint, the request will fail with an HTTP status code 403 FORBIDDEN with a message explaining the constraint that would have been violated.</li><li>If quota is enabled in a namespace for compute resources like CPU and memory, users must specify requests or limits for those values. Otherwise, the quota system may reject pod creation. The LimitRanger admission controller can be used to force defaults for pods that make no compute resource requirements.</li></ol><h4>Example: resource_quota.yaml File</h4><pre>apiVersion: v1<br>kind: Namespace<br>metadata:<br>  name: mynamespace<br>---<br>apiVersion: v1<br>kind: List<br>items:<br>  - apiVersion: v1<br>    kind: ResourceQuota<br>    metadata:<br>      name: user-compute-quota<br>      namespace: mynamespace<br>    spec:<br>      hard:<br>        requests.cpu: &quot;1&quot;<br>        requests.memory: 1Gi<br>        limits.cpu: &quot;2&quot;<br>        limits.memory: 2Gi<br>  - apiVersion: v1<br>    kind: ResourceQuota<br>    metadata:<br>      name: user-object-quota<br>      namespace: mynamespace<br>    spec:<br>      hard:<br>        configmaps: &quot;10&quot;<br>        persistentvolumeclaims: &quot;4&quot;<br>        replicationcontrollers: &quot;20&quot;<br>        secrets: &quot;10&quot;<br>        services: &quot;10&quot;<br>        services.loadbalancers: &quot;2&quot;</pre><p>This file contains two Kubernetes objects: a Namespace object and a List object that contains two ResourceQuota objects.</p><p>The first object is of kind “Namespace” and has a metadata field with the name “mynamespace”. This is creating a namespace named “mynamespace” in Kubernetes.</p><p>The second object is of kind “List” and contains a list of two “ResourceQuota” objects. The first “ResourceQuota” object has a metadata field with the name “user-compute-quota” and namespace set to “mynamespace”. The “spec” field defines resource usage limits for the namespace, such as CPU and memory requests and limits. Specifically, this quota allows for a maximum of 1 CPU and 1Gi of memory for requests and a maximum of 2 CPUs and 2Gi of memory for limits.</p><p>The second “ResourceQuota” object has a metadata field with the name “user-object-quota” and namespace set to “mynamespace”. The “spec” field defines limits on the number of objects of different types that can be created in the namespace. Specifically, this quota allows for a maximum of 10 configmaps, 4 persistentvolumeclaims, 20 replicationcontrollers, 10 secrets, 10 services, and 2 load balancer services.</p><h4>Steps to create ‘Resource Quota’ objects and observe their working</h4><blockquote>Prerequisites</blockquote><p>To follow along with this tutorial, you will need:</p><ul><li>A Kubernetes cluster (easy one would be <a href="https://www.linuxtechi.com/how-to-install-minikube-on-ubuntu/">minikube</a>)</li><li>kubectl installed and configured to interact with the cluster</li><li>Basic knowledge of Kubernetes resources such as Deployments and ReplicaSets</li></ul><blockquote>Step 1 — Create resource quota objects</blockquote><p>The first step in setting resource quotas is to create resource quota objects. A resource quota object is a Kubernetes resource that defines the maximum resource usage that a user or group can consume in a namespace.</p><p>Here is an example YAML file (say ‘resource_quota.yaml’)for a resource quota object:</p><pre>apiVersion: v1<br>kind: ResourceQuota<br>metadata:<br>  name: user-compute-quota<br>spec:<br>  hard:<br>    requests.cpu: &quot;1&quot;<br>    requests.memory: 1Gi<br>    limits.cpu: &quot;2&quot;<br>    limits.memory: 2Gi</pre><p>This YAML file defines a resource quota object named user-compute-quota that limits the CPU requests and memory usage of a user or group in the namespace. The hard field specifies the maximum usage limits for CPU requests, CPU limits, memory requests, and memory limits.</p><p>You can create the resource quota object by running the following command:</p><pre>kubectl apply -f resource_quota.yaml</pre><blockquote>Step 2 — Verify created resource quota objects</blockquote><p>After creating the resource quota objects, you can verify that they are created successfully by running the following command:</p><pre>kubectl get quota -n mynamespace</pre><p>This command lists all resource quota objects in the mynamespace namespace. The output shows the resource quota object name, age, and the maximum usage limits for each resource.</p><blockquote>Step 3 — Create a Deployment without a resource quota</blockquote><p>To test the resource quotas, you can create a Deployment without specifying any resource quotas. Here is an example YAML file (say dep_without_quota.yaml) for a Deployment:</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: nginx-deployment<br>spec:<br>  replicas: 3<br>  selector:<br>    matchLabels:<br>      app: nginx<br>  template:<br>    metadata:<br>      labels:<br>        app: nginx<br>    spec:<br>      containers:<br>      - name: nginx<br>        image: nginx<br>        ports:<br>        - containerPort: 80</pre><p>This YAML file creates a Deployment with three replicas of an Nginx container. The Deployment doesn’t specify any resource quotas.</p><p>You can create the Deployment by running the following command:</p><pre>kubectl create -f dep_without_quota.yaml</pre><blockquote>Step 4 — Verify the Deployment</blockquote><p>After creating the Deployment, you can verify that it’s running by running the following command:</p><pre>kubectl get deploy -n mynamespace</pre><p>This command lists all Deployments in the mynamespace namespace. The output shows the Deployment name, the number of replicas, and the number of replicas that are available and ready.</p><blockquote>Step 5 — Explore the reason for the unavailability of the replicas</blockquote><p>In this step, we will explore why the replicas are not ready. You can run the following command to get the ReplicaSet that manages the Deployment:</p><pre>kubectl get rs -n mynamespace</pre><p>This command lists all ReplicaSets in the mynamespace namespace. The output shows the ReplicaSet name, the desired number of replicas, the current number of replicas, and the number of replicas that are ready.</p><pre>NAME                          DESIRED   CURRENT   READY   AGE<br>nginx-deployment-64ff9dbcdf   3         0         0       2m51s</pre><p>Now do a describe on the above rsto observe the events</p><pre>kubectl describe rs nginx-deployment-64ff9dbcdf<br><br>...<br>Events:<br>  Type     Reason        Age                 From                   Message<br>  ----     ------        ----                ----                   -------<br>  Warning  FailedCreate  3m9s                replicaset-controller  Error creating: pods &quot;nginx-deployment-64ff9dbcdf-wkr8s&quot; is forbidden: failed quota: user-compute-quota: must specify limits.cpu for: k8s-demo; limits.memory for: k8s-demo; requests.cpu for: k8s-demo; requests.memory for: k8s-demo<br>  Warning  FailedCreate  3m9s                replicaset-controller  Error creating: pods &quot;nginx-deployment-64ff9dbcdf-mhn6c&quot; is forbidden: failed quota: user-compute-quota: must specify limits.cpu for: k8s-demo; limits.memory for: k8s-demo; requests.cpu for: k8s-demo; requests.memory for: k8s-demo<br>  Warning  FailedCreate  3m9s                replicaset-controller  Error creating: pods &quot;nginx-deployment-64ff9dbcdf-77n7m&quot; is forbidden: failed quota: user-compute-quota: must specify limits.cpu for: k8s-demo; limits.memory for: k8s-demo; requests.cpu for: k8s-demo; requests.memory for: k8s-demo<br>  Warning  FailedCreate  3m9s                replicaset-controller  Error creating: pods &quot;nginx-deployment-64ff9dbcdf-t5m7q&quot; is forbidden: failed quota: user-compute-quota: must specify limits.cpu for: k8s-demo; limits.memory for: k8s-demo; requests.cpu for: k8s-demo; requests.memory for: k8s-demo<br>  Warning  FailedCreate  3m9s                replicaset-controller  Error creating: pods &quot;nginx-deployment-64ff9dbcdf-j6974&quot; is forbidden: failed quota: user-compute-quota: must specify limits.cpu for: k8s-demo; limits.memory for: k8s-demo; requests.cpu for: k8s-demo; requests.memory for: k8s-demo<br>  Warning  FailedCreate  3m9s                replicaset-controller  Error creating: pods &quot;nginx-deployment-64ff9dbcdf-ldtx8&quot; is forbidden: failed quota: user-compute-quota: must specify limits.cpu for: k8s-demo; limits.memory for: k8s-demo; requests.cpu for: k8s-demo; requests.memory for: k8s-demo<br>  Warning  FailedCreate  3m9s                replicaset-controller  Error creating: pods &quot;nginx-deployment-64ff9dbcdf-jzfqg&quot; is forbidden: failed quota: user-compute-quota: must specify limits.cpu for: k8s-demo; limits.memory for: k8s-demo; requests.cpu for: k8s-demo; requests.memory for: k8s-demo<br>  Warning  FailedCreate  3m8s                replicaset-controller  Error creating: pods &quot;nginx-deployment-64ff9dbcdf-xrp62&quot; is forbidden: failed quota: user-compute-quota: must specify limits.cpu for: k8s-demo; limits.memory for: k8s-demo; requests.cpu for: k8s-demo; requests.memory for: k8s-demo<br>  Warning  FailedCreate  3m8s                replicaset-controller  Error creating: pods &quot;nginx-deployment-64ff9dbcdf-s29ht&quot; is forbidden: failed quota: user-compute-quota: must specify limits.cpu for: k8s-demo; limits.memory for: k8s-demo; requests.cpu for: k8s-demo; requests.memory for: k8s-demo<br>  Warning  FailedCreate  25s (x7 over 3m6s)  replicaset-controller  (combined from similar events): Error creating: pods &quot;nginx-deployment-64ff9dbcdf-9gxvc&quot; is forbidden: failed quota: user-compute-quota: must specify limits.cpu for: k8s-demo; limits.memory for: k8s-demo; requests.cpu for: k8s-demo; requests.memory for: k8s-demo<br>```</pre><p>since quota is not specified in the deployment file, it fails to provision the pods.</p><blockquote>Step 6 — Create deployment (dep_with_quota.yaml)</blockquote><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: helloworld-deployment<br>  namespace: mynamespace<br>  labels:<br>    app: nginx-ssl<br>spec:<br>  replicas: 3<br>  selector:<br>    matchLabels:<br>      app: nginx-ssl<br>  template:<br>    metadata:<br>      labels:<br>        app: nginx-ssl<br>    spec:<br>      containers:<br>        - name: k8s-demo<br>          image: nginx:1.16<br>          ports:<br>            - name: nginxssl-port<br>              containerPort: 8081<br>          resources:<br>            requests:<br>              cpu: 200m<br>              memory: 0.5Gi<br>            limits:<br>              cpu: 400m<br>              memory: 1Gi</pre><pre>kubectl create -f dep_with_quota.yaml</pre><blockquote>Step 7 — Verify above deployment</blockquote><pre>kubectl get rs -n mynamespace <br><br>NAME                               DESIRED   CURRENT   READY   AGE<br>helloworld-deployment-7986cbf64d   3         2         2       45s<br>nginx-deployment-64ff9dbcdf        3         0         0       8m52s</pre><p>only 2 out of 3 are in ready state for helloworld deployment.</p><blockquote>Step 8 — Explore reason</blockquote><pre>kubectl get rs -n mynamespace <br><br>NAME                               DESIRED   CURRENT   READY   AGE<br>helloworld-deployment-7986cbf64d   3         2         2       45s<br>nginx-deployment-64ff9dbcdf        3         0         0       8m52s<br><br>kubectl describe rs helloworld-deployment-7986cbf64d -n mynamespace<br><br>Events:<br>  Type     Reason            Age                From                   Message<br>  ----     ------            ----               ----                   -------<br>  Normal   SuccessfulCreate  59s                replicaset-controller  Created pod: helloworld-deployment-7986cbf64d-k46hw<br>  Warning  FailedCreate      59s                replicaset-controller  Error creating: pods &quot;helloworld-deployment-7986cbf64d-cxjqx&quot; is forbidden: exceeded quota: user-compute-quota, requested: limits.memory=1Gi,requests.memory=512Mi, used: limits.memory=2Gi,requests.memory=1Gi, limited: limits.memory=2Gi,requests.memory=1Gi<br>  Normal   SuccessfulCreate  59s                replicaset-controller  Created pod: helloworld-deployment-7986cbf64d-r68vf<br>  Warning  FailedCreate      59s                replicaset-controller  Error creating: pods &quot;helloworld-deployment-7986cbf64d-sx2vj&quot; is forbidden: exceeded quota: user-compute-quota, requested: limits.memory=1Gi,requests.memory=512Mi, used: limits.memory=2Gi,requests.memory=1Gi, limited: limits.memory=2Gi,requests.memory=1Gi<br>  Warning  FailedCreate      59s                replicaset-controller  Error creating: pods &quot;helloworld-deployment-7986cbf64d-zx6pq&quot; is forbidden: exceeded quota: user-compute-quota, requested: limits.memory=1Gi,requests.memory=512Mi, used: limits.memory=2Gi,requests.memory=1Gi, limited: limits.memory=2Gi,requests.memory=1Gi<br>  Warning  FailedCreate      59s                replicaset-controller  Error creating: pods &quot;helloworld-deployment-7986cbf64d-khchm&quot; is forbidden: exceeded quota: user-compute-quota, requested: limits.memory=1Gi,requests.memory=512Mi, used: limits.memory=2Gi,requests.memory=1Gi, limited: limits.memory=2Gi,requests.memory=1Gi<br>  Warning  FailedCreate      59s                replicaset-controller  Error creating: pods &quot;helloworld-deployment-7986cbf64d-z8j4s&quot; is forbidden: exceeded quota: user-compute-quota, requested: limits.memory=1Gi,requests.memory=512Mi, used: limits.memory=2Gi,requests.memory=1Gi, limited: limits.memory=2Gi,requests.memory=1Gi<br>  Warning  FailedCreate      59s                replicaset-controller  Error creating: pods &quot;helloworld-deployment-7986cbf64d-7rqj4&quot; is forbidden: exceeded quota: user-compute-quota, requested: limits.memory=1Gi,requests.memory=512Mi, used: limits.memory=2Gi,requests.memory=1Gi, limited: limits.memory=2Gi,requests.memory=1Gi<br>  Warning  FailedCreate      59s                replicaset-controller  Error creating: pods &quot;helloworld-deployment-7986cbf64d-sb8ml&quot; is forbidden: exceeded quota: user-compute-quota, requested: limits.memory=1Gi,requests.memory=512Mi, used: limits.memory=2Gi,requests.memory=1Gi, limited: limits.memory=2Gi,requests.memory=1Gi<br>  Warning  FailedCreate      59s                replicaset-controller  Error creating: pods &quot;helloworld-deployment-7986cbf64d-pxsdv&quot; is forbidden: exceeded quota: user-compute-quota, requested: limits.memory=1Gi,requests.memory=512Mi, used: limits.memory=2Gi,requests.memory=1Gi, limited: limits.memory=2Gi,requests.memory=1Gi<br>  Warning  FailedCreate      58s                replicaset-controller  Error creating: pods &quot;helloworld-deployment-7986cbf64d-jdnfp&quot; is forbidden: exceeded quota: user-compute-quota, requested: limits.memory=1Gi,requests.memory=512Mi, used: limits.memory=2Gi,requests.memory=1Gi, limited: limits.memory=2Gi,requests.memory=1Gi<br>  Warning  FailedCreate      39s (x8 over 57s)  replicaset-controller  (combined from similar events): Error creating: pods &quot;helloworld-deployment-7986cbf64d-pjms2&quot; is forbidden: exceeded quota: user-compute-quota, requested: limits.memory=1Gi,requests.memory=512Mi, used: limits.memory=2Gi,requests.memory=1Gi, limited: limits.memory=2Gi,requests.memory=1Gi</pre><p>failed to provision 3rd pod as it exceeded quota</p><blockquote>Step 9 — How much quota exhausted:</blockquote><pre>kubectl describe quota user-compute-quota -n mynamespace <br><br>Name:            user-compute-quota<br>Namespace:       mynamespace<br>Resource         Used  Hard<br>--------         ----  ----<br>limits.cpu       800m  2<br>limits.memory    2Gi   2Gi<br>requests.cpu     400m  1<br>requests.memory  1Gi   1Gi</pre><p>Only 2 pods out of 3 got scheduled.</p><p>And they consumed resources as specified in resources specs of the helloworlddeployment.</p><blockquote>Step 10 — Create a limit ranger object:</blockquote><pre>apiVersion: v1<br>kind: LimitRange<br>metadata:<br>  name: limits-quota<br>  namespace: mynamespace<br>spec:<br>  limits:<br>    - default:<br>        cpu: 200m<br>        memory: 512Mi<br>      defaultRequest:<br>        cpu: 100m<br>        memory: 256Mi<br>      type: Container</pre><pre>kubectl create -f limit_range.yaml</pre><blockquote>Step 11 — Now retry dep_without_quota.yaml</blockquote><pre>kubectl create -f dep_without_quota.yaml</pre><blockquote>Step 12 — Verify again dep_without_quota replicas/pods:</blockquote><pre>kubectl get deploy -n mynamespace <br><br>NAME               READY   UP-TO-DATE   AVAILABLE   AGE<br>nginx-deployment   3/3     3            3           13s</pre><p>All 3 are in ready states</p><p>Finally check the consumption of quota</p><pre>kubectl describe quota user-compute-quota -n mynamespace <br><br>Name:            user-compute-quota<br>Namespace:       mynamespace<br>Resource         Used  Hard<br>--------         ----  ----<br>limits.cpu       600m    2<br>limits.memory    1536Mi  2Gi<br>requests.cpu     300m    1<br>requests.memory  768Mi   1Gi</pre><p>All 3 pods got scheduled using default limits specified in limit_range.yaml This is because the deployment ‘nginx-deployment’ does not have resources spec specified hence the pods created out of it use default specs from LimitRange object.</p><p>Folks, if you like my content, would you consider following me on linked in at: <a href="https://www.linkedin.com/in/hitesh-pattanayak-52290b160/">https://www.linkedin.com/in/hitesh-pattanayak-52290b160/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbbe04fe43f6" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/managing-kubernetes-resources-with-resource-quotas-dbbe04fe43f6">Managing Kubernetes Resources with Resource Quotas</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding gRPC Concepts, Use Cases & Best Practices]]></title>
            <link>https://blog.devgenius.io/understanding-grpc-concepts-use-cases-best-practices-6af18339ef50?source=rss-183cc8bcd17d------2</link>
            <guid isPermaLink="false">https://medium.com/p/6af18339ef50</guid>
            <category><![CDATA[grpc]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[rest]]></category>
            <category><![CDATA[k8s]]></category>
            <dc:creator><![CDATA[Hitesh Pattanayak]]></dc:creator>
            <pubDate>Sun, 15 Jan 2023 18:19:10 GMT</pubDate>
            <atom:updated>2023-01-18T13:29:11.481Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*umFvT22zfXHDGg4GW8QwnA.png" /></figure><p><a href="https://www.infracloud.io/blogs/understanding-grpc-concepts-best-practices/"><strong><em>Original post link</em></strong></a></p><p>As we progress with application development, among various things, there is one primary thing we are less worried about i.e. computing power. With the advent of cloud providers, we are less worried about managing data centers. Everything is available within seconds, and that too on-demand. This leads This leads to an increase in the size of data as well. Big data is generated and transported using various mediums in single requests.</p><p>With the increase in the size of data, we have activities like serializing, deserializing and transportation costs added to it. Though we are not worried about computing resources, the latency becomes an overhead. We need to cut down on transportation. A lot of messaging protocols have been developed in the past to address this. SOAP was bulky, and REST is a trimmed-down version, but we need an even more efficient framework. That’s where Remote Procedure Calls (RPC) comes in.</p><p>In this blog post, we will understand what RPC is and the various implementations of RPC with a focus on gRPC, which is Google’s implementation of RPC. We’ll also compare REST with RPC and understand various aspects of gRPC, including security, tooling, and much more. So, let’s get started!</p><p><strong>What is RPC?</strong></p><p>RPC stands for ‘Remote Procedure Calls’. The definition is in the name itself. Procedure calls simply mean function/method calls; it’s the ‘Remote’ word that makes all the difference. What if we can make a function call remotely?</p><p>Simply put, if a function resides on a ‘server’ and in order to be invoked from the ‘client’ side, could we make it as simple as a method/function call? Essentially what an RPC does is it gives the ‘illusion’ to the client that it is invoking a local method, but in reality, it invokes a method in a remote machine that abstracts the network layer tasks. The beauty of this is that the contract is kept very strict and transparent (we will discuss this later in the article).</p><p>Steps involved in an RPC call:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z3ecSdkFD1rJfQ5IwPL5_Q.png" /><figcaption>RPC Sequence Flow</figcaption></figure><p>This is how a typical REST process looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*JjBry_3s2B58kEpBdI2QMA.png" /><figcaption>Rest Flow</figcaption></figure><p>RPCs boil down the process to below:##</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*VV7QQkim-WzBmqZIlyDqiQ.png" /><figcaption>GRPC Flow</figcaption></figure><p>This is because all the complications associated with making a request are now abstracted from us (we will discuss this in code-generation). All we need to worry about is the data and logic.</p><p><strong>gRPC — what, why, and how of it</strong></p><p>So far, we discussed RPC, which essentially means making function/method calls remotely. Thereby giving us the benefits like ‘strict contract definition’, ‘abstracting transmission and conversion of data’, ‘reducing latency’, etc. Which we will be discussing as we proceed with this post. What we would really like to dive deep into is one of the implementations of RPC. RPC is a concept, and gRPC is a framework based on it.</p><p>There are various implementations of RPCs. They are:</p><ul><li>gRPC (Google)</li><li>Thrift (Facebook)</li><li>Finagle (Twitter)</li></ul><p>Google’s version of RPC is referred to as gRPC which was introduced in 2015 and has been gaining traction since. It is one of the most chosen communication mechanisms in a microservice architecture.</p><p>gRPC uses <a href="https://developers.google.com/protocol-buffers)%20(it%20is%20an%20open%20source%20message%20format">protocol buffers</a> as the default method of communication between client and server. Also, gRPC uses HTTP/ 2 as the default protocol.</p><p>There are again four types of communication that gRPC supports:</p><ul><li><a href="https://grpc.io/docs/what-is-grpc/core-concepts/#unary-rpc)">Unary</a> (typical client and server communication)</li><li><a href="https://grpc.io/docs/what-is-grpc/core-concepts/#client-streaming-rpc">Client</a> side streaming</li><li><a href="https://grpc.io/docs/what-is-grpc/core-concepts/#server-streaming-rpc">Server side streaming</a></li><li><a href="https://grpc.io/docs/what-is-grpc/core-concepts/#bidirectional-streaming-rpc">Bidirectional streaming</a></li></ul><p>Coming on to the message format that is being used widely in gRPC — protocol buffers a.k.a protobufs. A protobuf message looks something like below:</p><pre>message Person {<br>  string name = 1;<br>  string id = 2;<br>  string email = 3;<br>}</pre><p>Here, `Person` is the message we would like to transfer (as a part of request/response), which has fields `name` (string type), `id` (string type) and `email` (string type). The numbers 1, 2, 3 represent the position of the data (as in `name`, `id`, and `has_ponycopter`) when it is serialized to binary format.</p><p>Once the developer has created the Protocol Buffer file(s) with all messages, we can use a ‘protocol buffer compiler’ (a binary) to compile the written protocol buffer file, which will generate all the utility classes and methods which are needed to work with the message. For example, as shown in the above `Person` message, depending on the chosen language, the <a href="https://github.com/infracloudio/grpc-blog/blob/master/proto/example/person.pb.go">generated code</a> will look like this.</p><p><strong>How do we define services?</strong></p><p>We need to define services that use the above messages to be sent/received.</p><p>After writing the necessary request and response message types, the next step is to write the service itself.</p><p>gRPC services are also defined in Protocol Buffers and they use the ‘service’ and ‘rpc’ keywords to define a service.</p><p>Take a look at the content of the below proto file:</p><pre>message HelloRequest {<br>  string name = 1;<br>  string description = 2;<br>  int32 id = 3;<br>}<br>message HelloResponse {<br>  string processedMessage = 1;<br>}<br>service HelloService {<br>  rpc SayHello (HelloRequest) returns (HelloResponse);<br>}</pre><p>Here, `HelloRequest` and `HelloResponse` are the messages and `HelloService` is exposing one unary RPC called `SayHello` which takes `HelloRequest` as input and gives `HelloResponse` as output.</p><p>As mentioned, `HelloService` at the moment contains a single unary RPC. But it could contain more than one RPC. Also, it can contain a variety of RPCs (unary/client-side streaming/server-side streaming/Bidirectional).</p><p>In order to define a streaming RPC, all you have to do is prefix ‘stream’ before the request/response argument, <a href="https://github.com/infracloudio/grpc-blog/tree/master/proto/streaming">Streaming RPCs proto definitions, and generated code</a>.</p><p>In the above code-base link:</p><ul><li><a href="https://github.com/infracloudio/grpc-blog/blob/master/proto/streaming/streaming.proto">streaming.proto</a>: this file is user defined</li><li><a href="https://github.com/infracloudio/grpc-blog/blob/master/proto/streaming/streaming.pb.go">streaming.pb.go</a> &amp; <a href="https://github.com/infracloudio/grpc-blog/blob/master/proto/streaming/streaming_grpc.pb.go">streaming_grpc.pb.go</a>: these files are auto-generated on running <a href="https://github.com/infracloudio/grpc-blog/blob/883e25e207b8e7d3fdf8384b98fb0828a982d5b3/proto/Taskfile.yaml#L18">proto compiler command</a>.</li></ul><p><strong>gRPC vs REST</strong></p><p>We did talk about gRPC a fair bit. Also, there was a mention of REST. What we missed was discussing the difference. I mean when we have a well-established, lightweight communication framework in the form of REST, why was there a need to look for another communication framework? Let us understand more about gRPC with respect to REST along with the pros and cons of each of it.</p><p>In order to compare what we require are parameters. So let’s break down the comparison into the below parameters:</p><p><strong>Message format: protocol buffers vs JSON</strong></p><ul><li>Serialization and deserialization speed is way better in the case of protocol buffers across all data sizes (small/medium/large). <a href="https://github.com/infracloudio/grpc-blog/blob/master/proto/test.out">Benchmark-Test-Results</a>.</li><li>Post serialization JSON is human readable while protobufs (in binary format) are not. Not sure if this is a disadvantage or not because sometimes you would like to see the request details in the Google developers tool or Kafka topics and in the case of protobufs you can’t make out anything.</li></ul><p><strong>Communication protocol: HTTP 1.1 vs HTTP/2</strong></p><ul><li>REST is based on HTTP 1.1; communication between a REST client and server would require an established TCP connection which in turn has a 3-way handshake involved. When we get a response from the server upon sending a request from the client, the TCP connection does not exist after that. A new TCP connection needs to be spun up in order to process another request. This establishment of a TCP connection on each and every request adds up to the latency.</li><li>So gRPC which is based on HTTP 2 has encountered this challenge by having a persistent connection. We must remember that persistent connections in HTTP 2 are different from that in web sockets where a TCP connection is hijacked and the data transfer is unmonitored. In a gRPC connection, once a TCP connection is established, it is reused for several requests. All requests from the same client and server pair are multiplexed onto the same TCP connection.</li></ul><p><strong>Just worrying about data and logic: Code generation being a first-class citizen</strong></p><ul><li>Code generation features are native to gRPC via its in-built protoc compiler. With REST APIs, it’s necessary to use a third-party tool such as Swagger to auto-generate the code for API calls in various languages.</li><li>In the case of gRPC, it abstracts the process of marshaling/unmarshalling, setting up a connection, and sending/receiving messages; what we all need to worry about is the data that we want to send or receive and the logic.</li></ul><p><strong>Transmission speed</strong></p><ul><li>Since the binary format is much lighter than JSON format, the transmission speed in the case of gRPC is 7 to 10 times faster than that of REST.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/838/1*HayqlHfVR4HgB2lQmyr0uw.png" /></figure><p>Again for the time being gRPC does not have browser support since most of the UI frameworks still have limited or no support for gRPC. Although gRPC is an automatic choice in most cases when it comes to internal microservices communication, it is not the same for external communication that requires UI integration.</p><p>Now that we have done a comparison of both the frameworks: gRPC and REST. Which one to use and when?</p><ul><li>In a microservice architecture with multiple lightweight microservices, where the efficiency of data transmission is paramount, gRPC would be an ideal choice.</li><li>If code generation with multiple language support is a requirement, gRPC should be the go-to framework.</li><li>With gRPC’s streaming capabilities, real-time apps like trading or OTT would benefit from it rather than polling using REST.</li><li>If bandwidth is a constraint, gRPC would provide much lower latency and throughput.</li><li>If quicker development and high-speed iteration is a requirement, REST should be a go-to option.</li></ul><h3><strong>gRPC Concepts</strong></h3><blockquote><strong>Load balancing</strong></blockquote><p>Even though the persistent connection solves the latency issue, it props up another challenge in the form of load balancing. Since gRPC (or HTTP2) creates persistent connections, even with the presence of a load balancer, the client forms a persistent connection with the server which is behind the load balancer. This is analogous to a sticky session.</p><p>We can understand the challenge via a demo &amp; the code and deployment files for the same are present in this <a href="https://github.com/infracloudio/grpc-blog/tree/master/grpc-loadbalancing">repository</a>.</p><p>From the above demo code base, we can find out that the onus of load balancing falls on the client. This leads to the fact that the advantage of gRPC i.e. persistent connection does not exist with this change. But gRPC can still be used for its other benefits.</p><p>Read more about <a href="https://grpc.io/blog/grpc-load-balancing/">load balancing in gRPC</a>.</p><p>In the above demo code-base, only a ‘round-robin’ load balancing strategy is used/showcased. But gRPC does support another client-based load balancing strategy OOB called ‘pick-first’.</p><p>Furthermore, <a href="https://learn.microsoft.com/en-us/aspnet/core/grpc/loadbalancing?view=aspnetcore-6.0">custom client-side load balancing</a> is also supported.</p><blockquote><strong>Clean contract</strong></blockquote><p>In REST, the contract between the client and server is documented but not strict. If we go back even further to SOAP, contracts were exposed via wsdl files. In REST we expose contracts via Swagger and other provisions. But the strictness is lacking, we cannot for sure know if the contract has changed on the server’s side while the client code is being developed.</p><p>With gRPC, the contract is shared with both the client and server either directly via proto files or generated stub from proto files. This is like making a function call but remotely. And since we are making a function call we exactly know what we need to send and what we are expecting as a response. The complexity of making connections with the client, taking care of security, serialization-deserialization, etc are abstracted. All we care about is the data.</p><p>Lets consider the code base for <a href="https://github.com/infracloudio/grpc-blog/tree/master/greet_app">Greet App</a>.</p><p>The client uses the <a href="https://github.com/infracloudio/grpc-blog/blob/883e25e207b8e7d3fdf8384b98fb0828a982d5b3/greet_app/internal/app/client/client.go#L6">stub</a> (generated code from proto file) to create a client object and invoke remote function call:</p><pre>import greetpb &quot;github.com/infracloudio/grpc-blog/greet_app/internal/pkg/proto&quot;<br>cc, err := grpc.Dial(&quot;&lt;server-address&gt;&quot;, opts)<br>if err != nil {<br>log.Fatalf(&quot;could not connect: %v&quot;, err)<br>}<br>c := greetpb.NewGreetServiceClient(cc)<br>res, err := c.Greet(context.Background(), req)<br>if err != nil {<br>log.Fatalf(&quot;error while calling greet rpc : %v&quot;, err)<br>}</pre><p>Similarly, the server too uses the same <a href="https://github.com/infracloudio/grpc-blog/blob/883e25e207b8e7d3fdf8384b98fb0828a982d5b3/greet_app/internal/app/server/server.go#L6">stub</a> (generated code from proto file) to receive request object and create response object:</p><pre>import greetpb &quot;github.com/infracloudio/grpc-blog/greet_app/internal/pkg/proto&quot;<br>func (*server) Greet(_ context.Context, req *greetpb.GreetingRequest) (*greetpb.GreetingResponse, error) {<br>// do something with &#39;req&#39;<br>return &amp;greetpb.GreetingResponse{<br>Result: result,<br>}, nil<br>}</pre><p>Both of them are using the same stub generated from the proto file <a href="https://github.com/infracloudio/grpc-blog/blob/master/greet_app/internal/pkg/proto/greet.proto">greet.proto</a>.</p><p>And the stub was generated using ‘proto’ compiler and the command to generate is <a href="https://github.com/infracloudio/grpc-blog/blob/883e25e207b8e7d3fdf8384b98fb0828a982d5b3/greet_app/Taskfile.yaml#L10">this</a>.</p><pre>protoc - go_out=. - go_opt=paths=source_relative - go-grpc_out=. - go-grpc_opt=paths=source_relative internal/pkg/proto/*.proto</pre><blockquote><strong>Security</strong></blockquote><p>gRPC authentication and authorization works on two levels:</p><ul><li>Call-level authentication/authorization is usually handled through tokens that are applied in metadata when the call is made. <a href="https://github.com/infracloudio/grpc-blog/compare/master...secure_token)">Token based authentication example</a>.</li><li>Channel-level authentication uses a client certificate that’s applied at the connection level. It can also include call-level authentication/authorization credentials to be applied to every call on the channel automatically. <a href="https://github.com/infracloudio/grpc-blog/compare/secure_grpc">Certificate based authentication example</a>.</li></ul><p>Either or both of these mechanisms can be used to help secure services.</p><blockquote><strong>Middlewares</strong></blockquote><p>In REST, we use middlewares for various purposes like:</p><ul><li>Rate limiting</li><li>Pre/Post request/response validation</li><li>Address security threats</li></ul><p>We can achieve the same with gRPC as well. The verbiage is different in gRPC, they are referred as ‘interceptors’ but they do similar activities.</p><p>In the <a href="https://github.com/infracloudio/grpc-blog/tree/middlewares/greet_app/internal/app">middlewares</a> branch of the `greet_app` code base, we have integrated logger and Prometheus interceptors.</p><p>Look how the interceptors are configured to use Prometheus and logging packages in <a href="https://github.com/infracloudio/grpc-blog/blob/7700323e1e488eb8777a06ca762e4d29602d2424/greet_app/internal/pkg/middleware/middleware.go#L29">middleware.go</a>.</p><pre>// add middleware<br>AddLogging(&amp;zap.Logger{}, &amp;uInterceptors, &amp;sInterceptors)<br>AddPrometheus(&amp;uInterceptors, &amp;sInterceptors)</pre><p>But we can integrate other packages to interceptors for purposes like preventing panic and recovery (to handle exceptions), tracing, even authentication, etc.</p><p>Supported <a href="https://github.com/grpc-ecosystem/go-grpc-middleware">middlewares</a> by gRPC framework.</p><blockquote><strong>Packaging, versioning and code practices of proto files</strong></blockquote><p><strong>Packaging</strong></p><p>Let’s follow the <a href="https://github.com/infracloudio/grpc-blog/blob/packaging/proto/packaging/processor.proto">packaging</a> branch.</p><p>First start with `Taskfile.yaml`, the task `gen-pkg` says `protoc — proto_path=packaging packaging/*.proto — go_out=packaging`. This means `protoc` (the compiler) will convert all files in `packaging/*.proto` into its equivalent Go files as denoted by flag ` — go_out=packaging` in the `packaging` directory itself.</p><p>Secondly in the ‘processor.proto’ file, 2 messages have been defined namely ‘CPU’ and ‘GPU’. While CPU is a simple message with 3 fields of in-built data types, GPU message on the other hand has an additional custom data type called ‘Memory’ along with in-built data types same as CPU message. ‘Memory’ is a separate message and is defined in a different file altogether.</p><p>So how do you use the ‘Memory’ message in the ‘processor.proto’ file? By using <a href="https://github.com/infracloudio/grpc-blog/blob/436d84358868f463ea7929eb14120eb80801fde1/proto/packaging/processor.proto#L6">import</a>.</p><pre>syntax = &quot;proto3&quot;;<br>package laptop_pkg;<br>option go_package = &quot;/pb&quot;;<br>import &quot;memory.proto&quot;;<br>message CPU {<br>  string brand = 1;<br>  string name = 2;<br>  uint32 cores = 3;<br>}<br>message GPU {<br>  string brand = 1;<br>  string name = 2;<br>  uint32 cores = 3;<br>  Memory memory = 4;<br>}</pre><pre>syntax = &quot;proto3&quot;;<br>package laptop_pkg;<br>option go_package = &quot;/pb&quot;;<br>message Memory {<br>  enum Unit {<br>    UNKNOWN = 0;<br>    BIT = 1;<br>    BYTE = 2;<br>    KILOBYTE = 3;<br>    MEGABYTE = 4;<br>    GIGABYTE = 5;<br>  }<br>  uint64 value = 1;<br>  Unit unit = 2;<br>}</pre><p>Even if you try to generate a proto file by running task `gen-pkg` after mentioning import, it will throw an error. As by default `protoc` assumes both files `memory.proto` and `processor.proto` to be in different packages. So you need to mention the same package name in both files.</p><p>The optional `go_package` indicates the compiler to create a package name as `pb` for Go files. If any other language-d proto files were to be created, the package name would be `laptop_pkg`.</p><p><strong>Versioning</strong></p><p>There can be two kinds of changes in gRPC breaking and non-breaking changes:</p><ul><li>Non-breaking changes include adding a new service, adding a new method to a service, adding a field to request or response proto, and adding a value to enum</li><li>Breaking changes like renaming a field, changing field data type, field number, renaming or removing a package, service or methods require versioning of services</li><li>In order to distinguish between same name messages or services across proto files, <a href="https://developers.google.com/protocol-buffers/docs/proto#packages">optional packaging</a> can be implemented.</li></ul><p><strong>Code practices</strong></p><ul><li>Request message must suffix with request `CreateUserRequest`.</li><li>Response message must suffix with request `CreateUserResponse`.</li><li>In case the response message is empty, you can either use an empty object `CreateUserResponse` or use the `google.protobuf.Empty`.</li><li>Package name must make sense and must be versioned, for example: package `com.ic.internal_api.service1.v1`.</li></ul><p><strong>Tooling</strong></p><p>gRPC ecosystem supports an array of tools to make life easier in non-developmental tasks like documentation, rest gateway for a gRPC server, integrating custom validators, linting, etc. Here are some tools that can help us achieve the same:</p><ul><li><a href="https://github.com/grpc-ecosystem/grpc-gateway">protoc-gen-grpc-gateway</a> — plugin for creating a gRPC REST API gateway. It allows gRPC endpoints as REST API endpoints and performs the translation from JSON to proto. Basically, you define a gRPC service with some custom annotations and it makes those gRPC methods accessible via REST using JSON requests.</li><li><a href="https://github.com/grpc-ecosystem/grpc-gateway">protoc-gen-swagger</a>— a companion plugin for grpc-gateway. It is able to generate swagger.json based on the custom annotations required for gRPC gateway. You can then import that file into your REST client of choice (such as [Postman](https://www.postman.com/)) and perform REST API calls to the methods you exposed.</li><li><a href="https://github.com/grpc-ecosystem/grpc-gateway">protoc-gen-grpc-web</a> — a plugin that allows our front end to communicate with the backend using gRPC calls. A separate blog post on this coming up in the future.</li><li><a href="https://github.com/mwitkow/go-proto-validators">protoc-gen-go-validators</a>— a plugin that allows to define validation rules for proto message fields. It generates a `Validate() error` method for proto messages you can call in Go to validate if the message matches your predefined expectations.</li><li><a href="https://github.com/yoheimuta/protolint">protolint</a> — a plugin to add lint rules to proto files.</li></ul><h3><strong>Testing using Postman</strong></h3><p>Unlike testing REST APIs with Postman or any equivalent tools like Insomnia, it is not quite comfortable to test gRPC services.</p><p><strong>**Note:**</strong> gRPC services can also be tested from CLI using tools like <a href="https://github.com/ktr0731/evans">evans-cli</a>. But for that reflection needs (if not enabled the path to the proto file is <a href="https://github.com/infracloudio/grpc-blog/blob/ed390485e12ce6b63fd9fd53f867cf6e818a5407/greet_app/Taskfile.yaml#L82">required</a> to be enabled in gRPC servers. This [compare link](https://github.com/infracloudio/grpc-blog/compare/evans) shows the way to enable reflection and how to enter into evans-cli repl mode. Post entering repl mode of evans-cli, gRPC services can be tested from CLI itself and the process is described in <a href="https://github.com/ktr0731/evans">evans-cli GitHub page</a>.</p><p>Postman has a <a href="https://blog.postman.com/postman-now-supports-grpc/">beta version</a> of testing gRPC services.</p><p>Here are the steps of how you can do it:</p><p>1. Open Postman, goto ‘APIs’ in the left sidebar and click on ‘+’ sign to create new api. In the popup window, enter ‘Name’, ‘Version’, and ‘Schema Details’ and click on create [unless you need to import from some sources like GitHub, Bitbucket].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZyrNQK7wX3jtDNIKWzPR7g.png" /><figcaption>Create new API</figcaption></figure><p>2. Once Your API gets created then go to definition and enter your proto contract.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SgvtIDmJ0FbGl9KkmECrIQ.png" /><figcaption>Enter proto contract</figcaption></figure><p>3. Remember importing does not work here, so it would be better to keep all dependent protos at one place.</p><p>4. The above steps will help to retain contracts for future use.</p><p>5. Then click on ‘New’ and select ‘gRPC request’, enter the URI and choose the proto from the list of saved ‘APIs’ and finally enter your request message and hit ‘Invoke’</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vB3_9uTogTyc0wNJGqgAKQ.png" /><figcaption>Create gRPC request</figcaption></figure><p>In the above steps we figured out the process to test our gRPC APIs via Postman. The process to test gRPC endpoints is different from that of REST endpoints using Postman. One thing to remember is that while creating and saving proto contract as in 5, all proto message and service definitions need to be in the same place. As there is no provision to access proto messages across versions in Postman.</p><p><strong>## Conclusion</strong></p><p>In this post, we developed an idea about RPC, drew parallels with REST as well as discussed their differences, then we went on to discuss an implementation of RPC i.e. gRPC developed by Google.</p><p>gRPC as a framework can be crucial, especially for microservice-based architecture for internal communication. It can be used for external communication as well but will require a REST gateway. gRPC is a must for streaming and real-time apps.</p><p>The way Go is proving itself as a server-side scripting language, gRPC is proving itself as a de-facto communication framework.</p><p>That’s it folks! Feel free to reach out to <a href="https://www.linkedin.com/in/hitesh-pattanayak-52290b160/">Hitesh</a>/<a href="https://www.linkedin.com/in/pranoy-kundu-74b179167">Pranoy</a> for any feedback and thoughts on this topic.</p><p>Looking for help with building your DevOps strategy or want to outsource DevOps to the experts? Learn why so many startups &amp; enterprises consider us as one of the <a href="https://www.infracloud.io/devops-consulting-services/">best DevOps consulting &amp; services companies</a>.</p><p><strong>**Further reads**</strong></p><ul><li><a href="https://grpc.io/docs/">gRPC official documentation</a></li><li><a href="https://developers.google.com/protocol-buffers/docs/gotutorial">Protobuff golang documentation</a></li><li><a href="https://github.com/grpc-ecosystem">gRPC ecosystem</a></li><li><a href="https://www.baeldung.com/rest-vs-grpc">REST vs gRPC</a></li><li><a href="https://www.ibm.com/docs/en/aix/7.1?topic=concepts-remote-procedure-call">RPC concepts</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6af18339ef50" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/understanding-grpc-concepts-use-cases-best-practices-6af18339ef50">Understanding gRPC Concepts, Use Cases &amp; Best Practices</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>