<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hayder Sharhan</title>
    <description>The latest articles on DEV Community by Hayder Sharhan (@hshar7).</description>
    <link>https://dev.to/hshar7</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F141404%2F690bf625-b1f4-47d3-8f65-05ff21672a66.jpeg</url>
      <title>DEV Community: Hayder Sharhan</title>
      <link>https://dev.to/hshar7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hshar7"/>
    <language>en</language>
    <item>
      <title>How to get an ALB running with kOps in 2021</title>
      <dc:creator>Hayder Sharhan</dc:creator>
      <pubDate>Fri, 22 Jan 2021 19:34:27 +0000</pubDate>
      <link>https://dev.to/hshar7/how-to-get-an-alb-running-with-kops-in-2021-3jke</link>
      <guid>https://dev.to/hshar7/how-to-get-an-alb-running-with-kops-in-2021-3jke</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;Yesterday was a painful day because even though the folks who created &lt;a href="https://github.com/kubernetes-sigs/aws-load-balancer-controller"&gt;https://github.com/kubernetes-sigs/aws-load-balancer-controller&lt;/a&gt; are great developers. They're really shit at &lt;a href="https://kubernetes-sigs.github.io/aws-load-balancer-controller/latest/"&gt;documenting&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I needed to use an ALB because ELBs don't support automatic http to https upgrades. And they have a ton more features that I'll use in the future. So in this guide I'll show you how to make your ALB do that auto redirect.&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting your cluster ready
&lt;/h1&gt;

&lt;p&gt;So let me guide you through the bs and save you 8+ hours of trial and error and intense research though github issues.&lt;/p&gt;

&lt;p&gt;Please keep in mind my kubernetes version is ~1.15 so I need to use some legacy things. You might need to use some more recent configs.&lt;/p&gt;

&lt;h2&gt;
  
  
  The subnets
&lt;/h2&gt;

&lt;p&gt;You need to make sure you have at least two subnets setup for your kubernetes cluster and these subnets need to be in different availability zones. Take a look at &lt;a href="https://kops.sigs.k8s.io/run_in_existing_vpc/"&gt;this&lt;/a&gt; for more information but basically you need to do &lt;code&gt;kops edit cluster&lt;/code&gt; and make sure your subnets look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  subnets:
  - cidr: 172.20.32.0/19
    name: us-east-1a
    type: Public
    zone: us-east-1a
  - cidr: 172.20.64.0/19
    name: us-east-1b
    type: Public
    zone: us-east-1b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then run &lt;code&gt;kops update cluster --yes&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The extra policies
&lt;/h2&gt;

&lt;p&gt;For aws-load-balancer-controller to run properly, it needs extra policies to be attached to your cluster nodes so it can configure some things for you. To do that first we need to download this file which contains the policy description: &lt;code&gt;curl -o iam-policy.json https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.1.0/docs/install/iam_policy.json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then creating a policy for it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws iam create-policy \
    --policy-name AWSLoadBalancerControllerIAMPolicy \
    --policy-document file://iam-policy.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Take note of the arn that gets returned.. something that looks like "aws:arn:iam:123456789000:policy:test-policy"&lt;/p&gt;

&lt;p&gt;Now go edit you cluster again &lt;code&gt;kops edit cluster&lt;/code&gt; and attach the policy like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spec:
  externalPolicies:
    node:
    - aws:arn:iam:123456789000:policy:test-policy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now hit it with a &lt;code&gt;kops update cluster --yes&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Installing dependencies on your cluster
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;First you need to install cert manager which will allow you to manage certs for the different services and deployments that we'll be doing:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl apply --validate=false -f https://github.com/jetstack/cert-manager/releases/download/v1.1.0/cert-manager-legacy.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Second installing the actual load balancer controller:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wget https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.1.0/docs/install/v2_1_0_full.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;open that file and go to where it says --cluster-name= and change it to your cluster's name.&lt;/p&gt;

&lt;p&gt;save and run &lt;code&gt;kubectl apply -f v2_1_0_full.yaml&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Configuring the ALB
&lt;/h1&gt;

&lt;p&gt;Make sure you have your deployment looking something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: my-app
  name: my-app-deployment
  namespace: dev
spec:
  replicas: 2
  selector:
    matchLabels:
      app.kubernetes.io/name: my-app
  template:
    metadata:
      labels:
        app.kubernetes.io/name: my-app
    spec:
      containers:
      - image: my-app:latest
        imagePullPolicy: Always
        name: my-app
        ports:
        - containerPort: 3000
          name: app-port
          protocol: TCP
      restartPolicy: Always

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you need a service classic LoadBalancer (I think this can be a NodePort instead) to point to this port:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: v1
kind: Service
metadata:
  namespace: dev
  name: my-app-service
spec:
  ports:
    - port: 3000
      targetPort: 3000
      protocol: TCP
  type: LoadBalancer
  selector:
    app.kubernetes.io/name: my-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the actual ALB ingress:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/certificate-arn: YOUR-ARN-CERT-HERE
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80}, {"HTTPS":443}]'
    alb.ingress.kubernetes.io/actions.ssl-redirect: '{"Type": "redirect", "RedirectConfig": { "Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}'
  name: my-app-alb
  namespace: dev
spec:
  rules:
  - http:
      paths:
      - path: /*
        backend:
          serviceName: ssl-redirect
          servicePort: use-annotation
      - path: /*
        backend:
          serviceName: my-app-service
          servicePort: 3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! Run &lt;code&gt;kubectl -n dev get ingress&lt;/code&gt; and go to that address. Enjoy it!&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>alb</category>
      <category>kops</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
