0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

KubernetesでIngressを試してみた

0
Posted at

Docker-DesktopのKubernetesでIngressを試してみた。

環境
macOS
Docker Desktop
Kubernetes(Docker Desktopで有効化)
nginx Deployment作成済み
Service作成済み

1. Ingress Controllerをインストール

まずはIngress Controllerを導入します。

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.15.1/deploy/static/provider/cloud/deploy.yaml

2. Controllerが起動したことを確認

kubectl get pods -n ingress-nginx

RunningになればOK。

NAME                                        READY   STATUS
ingress-nginx-controller-xxxxx              1/1     Running

Serviceも確認。

kubectl get svc -n ingress-nginx

例

NAME                                 TYPE           PORT(S)
ingress-nginx-controller             LoadBalancer   80:30535/TCP,443:31109/TCP

3. IngressClassを確認

kubectl get ingressclass

例

NAME    CONTROLLER
nginx   k8s.io/ingress-nginx

4. Ingressを作成

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nekogoya-ingress
spec:
  ingressClassName: nginx
  rules:
  - host: nekogoya.local
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nekogoya
            port:
              number: 80

適用。

kubectl apply -f ingress.yaml

5. Ingressを確認

kubectl get ingress

例

NAME               CLASS   HOSTS            ADDRESS
nekogoya-ingress   nginx   nekogoya.local   172.19.0.5

6. hostsファイルを編集

macOSではnekogoya.localをlocalhostへ向けます。

sudo nano /etc/hosts

追加

127.0.0.1    nekogoya.local

保存

  • Ctrl + O
  • Enter
  • Ctrl + X

7. 動作確認

まずはcurl。

curl -H "Host: nekogoya.local" http://localhost

結果

<h1>Cat Ops Dashboard v2</h1>

その後ブラウザで

http://nekogoya.local

へアクセスすると表示できました。


ハマった点

最初は

curl -H "Host: nekogoya.local" http://172.19.0.5

を試しましたが、

Couldn't connect to server

となりました。

Docker Desktopではkubectl get ingressに表示されるADDRESSへ直接アクセスするのではなく、

http://localhost

へHostヘッダーを付けてアクセスするのが正解でした。

そのため/etc/hostsには

127.0.0.1    nekogoya.local

を設定します。


Ingressの流れ(イメージ)

ブラウザ
      │
      ▼
nekogoya.local
      │
      ▼
Ingress Controller
      │
      ▼
Ingress
      │
      ▼
Service
      │
      ▼
Pod

この構成で、ローカル環境でもhttp://nekogoya.localという独自ホスト名でアプリへアクセスできるようになりました。


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?