moriaki3193
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- lang: ja-jp tags: Infrastructure, Kubernetes title: Kubernetes Ingress --- # Kubernetes Ingress - [KubernetesにIngressを導入する方法](https://qiita.com/Hirata-Masato/items/8e6b4536b6f1b23c5270) - [learnk8s: A visual guide on troubleshooting Kubernetes deployments](https://learnk8s.io/troubleshooting-deployments) - [Ingress TLS](https://kubernetes.io/docs/concepts/services-networking/ingress/#tls) - [Kubernetes docs: Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/) ## Overview アプリケーションを構成するリソースは(基本的に)以下の通り: 1. **Pods**: 同一のIPアドレスが割り当てられた1つ以上のコンテナから構成される基本単位 2. **ReplicaSet**: Podの複製を作成し、サービスを構成するアプリケーションの個数を維持 3. **Deployment**: ReplicaSetを管理し、ローリングアップデートといったデプロイの機能を提供 クラスタに展開するDeploymentを、外部ネットワークに公開するには、基本的には**Service**リソースを利用することになる。`type=LoadBalancer`のServiceを作成することで、外部からのアクセスを受け付けることが可能になる。 しかし、Serviceリソースでは実現できない機能がいくつか存在する: - IPアドレスはServiceごとに生成されるため、アクセスポイントが変わりうる - HTTPSへのリダイレクトを実現できない - 同一Pod内に存在する(i.e. ローカルホストが同じ)コンテナに別々のルーティングを割り当てる場合に、Nginxのようなリバースプロキシを追加するひつようがある このような問題を解決するためのリソースとして、アプリケーションレイヤのロードバランスとして**Ingress**リソースが存在する。 ## Infra as Code: Service, Deployment - Serviceリソースの種類を`type=NodePort`にする - Deploymentの`spec.template.spec.containers.ports[]`にServiceの`spec.ports.targetPort`に一致するポート番号を入れる - Deploymentの`spec.template.metadata.labels`のラベル付けを正しく行う ```yaml= apiVersion: v1 kind: Service metadata: name: my-api-service namespace: my-namespace labels: app: my-api spec: type: NodePort ports: - port: 8080 targetPort: 8080 # コンテナのポート protocol: TCP selector: app: my-api-pod # どのラベルが割り当てられたPodと接続するかのセレクタ --- apiVersion: apps/v1beta kind: Deployment metadata: name: my-api-deployment namespace: my-namespace spec: replicas: 1 revisionHistoryLimit: 3 template: metadata: namespace: my-namespace labels: app: my-api-pod # このラベルをServiceリソースが探す version: latest spec: containers: - name: my-api image: gcr.io/moriaki3193/my-api:latest ports: - containerPort: 8080 # IngressとServiceの連携まで時間がかかるため # ヘルスチェックの設定をうまくやるには以下を参照 # https://qiita.com/Hirata-Masato/items/8e6b4536b6f1b23c5270#%E8%BF%BD%E8%A8%98201823 ``` ## Infra as Code: Ingress - `spec.rules[].http.paths[].<pathname>.serviceName`にServiceに割り当てた`metadata.name`の値を設定する ```yaml= apiVersion: extensions/v1beta1 kind: Ingress metadata: name: my-ingress namespace: my-namespace annotations: # optional kubernetes.io/ingress.global-static-ip-name: my-ipaddress spec: rules: - host: myapi.example.com http: paths: - <pathname>: serviceName: my-api servicePort: 8080 # Serviceのポートを指定 ``` **optional**な箇所については、Ingressを削除し、再設定した際にIPアドレスが変わりうるため、固定するようにするための設定となっている。 ## Terminology - **Node**: Kubernetesクラスタのワーカマシンのこと。 - **Cluster**: Kubernetesにより管理される、コンテナ化されたアプリケーションを実行するNodeの集合。クラスタに属するノードは外部ネットワークには接続されていない。 - **Edge Router**: クラスタのファイアウォールのポリシーを強制するルータのこと。クラウドベンダーにより管理されるゲートウェイであったり、物理的なハードウェアであったりする。 - **Cluster network**: クラスタ内部のやりとりを執り行う論理的あるいは物理的なリンクの集合のこと。詳細は[K8s networking model](https://kubernetes.io/docs/concepts/cluster-administration/networking/)を参考のこと。 - **Service**: ラベルセレクタを利用してPodの集合を識別するK8sのサービスのこと。特に他のリソースで指定されない限り、サービスはクラスタネットワーク内部でのみ接続可能な仮想IPアドレスを持つ。 ## What is Ingress? クラスタ外部からのHTTPやHTTPSによる通信をクラスタ内部のサービスに接続する役割を担うリソースのこと。**トラフィックルーティングはIngressリソースにより定義されるルールによってコントロールされる**。 ``` internet | [ Ingress ] --|-----|-- [ Services ] ``` IngressはServiceに外部からアクセス可能なURLを設定したり、トラフィックをロードバランシングしたり、SSL/TLS通信のエンドポイントとなったりするように設定できる。Ingress ControllerがIngressの実現に責務を負っていて、大抵の場合はロードバランサと合わせて利用される。 Ingressは任意のポートやプロトコルを公開することはない。HTTPとHTTPS以外の場合にインターネットにサービスを公開する際には、Serviceリソースがその責任を負う。`type=NodePort`または`type=LoadBalancer`が該当する。 ## Prerequisites Ingressリソースを立ち上げるには、`ingress controller`が必要になる。Ingressリソースを作成するだけでは、クラスタにはなんら影響はない。Ingress Controllerについては[こちら](https://kubernetes.io/docs/concepts/services-networking/ingress-controllers)を参考にする。 `ingress-nginx`といったようなIngress Controllerをデプロイする必要があるかもしれない。種類は数多くあるので、用途にあったものを選択すればよい。 ## The Ingress Resource 最低限のIngressリソースの具体例は次の通り: ```yaml= apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: test-ingress annotations: nginx.ingress.kubernetes.io/rewrite-target: / spec: rules: - http: paths: - path: /testpath backend: serviceName: test servicePort: 80 ``` ### Service type: NodePort > NodePortは、全てのKubernetes Nodeの`ipaddr:port`で受信したトラフィックをコンテナに転送する形で、外部疎通性を確立します 内部的には、マシンワーカとしてのノードに割り当てられた仮想的なソケットを指定して、Pod内のコンテナに通信を引き渡す役割を担っている。 ### ingress-nginx [kuberntes/ingress-nginx](https://github.com/kubernetes/ingress-nginx/blob/master/docs/examples/rewrite/README.md)に詳細が記載されている。 #### ingress.class annotations - `nginx.ingress.kubernetes.io/rewrite-target` - トラフィックがリダイレクトされなければならないターゲットURIを指定できる - `nginx.ingress.kubernetes.io/app-root` - '/'コンテキストである場合にコントローラがリダイレクトしなければならないApplication Rootを定義できる #### Rewrite Target ```yaml= apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: annotations: nginx.ingress.kubernetes.io/rewrite-target: /$2 name: rewrite namespace: default spec: rules: - host: rewrite.bar.com http: paths: - backend: serviceName: http-svc servicePort: 80 path: /something(/|$)(.*) ``` このIngressリソースの定義では、`(.*)`文字列でキャプチャされる文字がすべて`$2`プレースホルダに代入される。 このような設定を行うことで、次のような結果が得られる。 - `rewrite.bar.com/something` → `rewrite.bar.com/` - `rewrite.bar.com/something/` → `rewrite.bar.com/` - `rewrite.bar.com/something/new` → `rewrite.bar.com/new` #### App Root `spec.rules[].http.paths[].path`で指定されたパスのパターンに対してリダイレクトをかける。 ```yaml= apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: annotations: nginx.ingress.kubernetes.io/app-root: /app1 name: approot namespace: default spec: rules: - host: approot.bar.com http: paths: - backend: serviceName: http-svc servicePort: 80 path: / ``` - `http://approot.bar.com/` → 302 Moved Temporarily - `http://approot.bar.com/app1`になる ### メモ ``` # / にアクセスされたら3000番ポートに # /api にアクセスされたら、apiの文字列を削除して5000番ポートに # :80/api/estimate → :5000/estimate ということ # フロントエンドのアプリケーションの静的ファイルがすべてルーティングされていないので、Ingressを修正する # Flaskの方のエンドポイントを修正する ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully