LoginSignup
1
1

More than 3 years have passed since last update.

NGINX Ingress Controllerで簡易的なNginxコンテナの起動

Posted at

目的

NGINX Ingress Controllerを触っておきたい!

環境

macOS
Docker for Mac

参照

Installation Guide - NGINX Ingress Controller

NGINX Ingress Controllerとは?

NGINX Ingress Controllerは、OSSのIngressコントローラーです。

Ingressリソースが機能するためには、クラスターでIngressコントローラーが実行されている必要があります。
Ingress Controllers | Kubernetes

  1. DeploymentやService等のリソースを準備
  2. NGINX Ingress Controllerをインストール
  3. Ingressリソースを準備
  4. Ingressリソースをローカルにdeploy

準備

Ingress Controllers | Kubernetes
👆よく読む

作業スペース確保

$ mkdir practice
$ cd practice

Deploymentの編集

$ vi nginx-deployment.yml
nginx-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx-sample
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-sample
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nginx-sample
    spec:
      containers:
      - name: nginx-deployment
        image: nginx:latest
        ports:
        - containerPort: 80

Deploymentの作成

$ kubectl apply -f nginx-deployment.yml

Serviceの編集

$ vi nginx-service.yml
nginx-service.yml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  ports:
    - port: 9999
      targetPort: 80
  selector:
    app: nginx-sample

Serviceの作成

$ kubectl apply -f nginx-service.yml

NGINX Ingress Controllerのインストール

参照: Installation Guide - NGINX Ingress Controller

Docker for Macの場合👇

$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/cloud/deploy.yaml

インストールできたか確認

$ kubectl get pods -n ingress-nginx \
  -l app.kubernetes.io/name=ingress-nginx --watch

Ingressの編集

nginx-ingress.yml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  backend:
    serviceName: nginx-service
    servicePort: 9999

Ingressの作成

$ kubectl apply -f nginx-ingress.yml

アクセスしてみる

スクリーンショット 2020-06-27 19.31.36.png

いろいろ見てみる

作成したリソースの操作

リソース名の省略形

たぶん触っていると沢山書くことになるので、省略形を覚えておくと便利です。

項目 省略
deployment deploy
service svc
ingress ing

PodのNAMEやレプリカ数などを取得

$ kubectl get deploy
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   0/1     1            0           109m

リソース名とオブジェクト名を指定してPodの詳細を表示

$ kubectl describe deploy nginx-deployment

ファイル名を指定してリソースの削除

$ kubectl delete -f nginx-deployment.yml

deploy 部分を、 svcing などに置き換えて使用します。

それぞれのリソースにおけるportの関係

Ingressで、service名とserviceのポートを指定します。
- spec.backend.serviceName
- spec.backend.servicePort

Serviceで、targetPortを指定します。
Deploymentで、containerPortにて待ち受けます。

Untitled Diagram (1).png

1
1
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
1
1