Nginx Ingress インストール

# 概要
 - k8sテスト環境構築<br>
<b>Nginx Ingress インストール</b>

# 構築目次
 - [全体目次] (https://qiita.com/prodigy413/items/89fcb8cfb71e80f541d8)

# 環境
 - Rancher: v2.6.3
 - kubernetes(Client): v1.22.4
 - kubernetes(Server): v1.22.4
 - Nginx Ingress Chart: v4.0.13
 - Nginx Ingress App: v1.1.0

# インストール
 - 作業場所: ClientPC
 - Nginx Ingress Install Page<br>
https://github.com/kubernetes/ingress-nginx/tree/main/charts/ingress-nginx<br>
 - Argumentなど参照<br>
https://kubernetes.github.io/ingress-nginx/user-guide/cli-arguments/<br>

### helmにingress repo追加
~~~
$ helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
$ helm repo update

## 確認 ##
$ helm search repo ingress-nginx/ingress-nginx
NAME                       	CHART VERSION	APP VERSION	DESCRIPTION                                       
ingress-nginx/ingress-nginx	4.0.13       	1.1.0      	Ingress controller for Kubernetes using NGINX a...
~~~

### helmでNginx Ingress インストール
~~~
## namespace(ingress-system) 作成
$ kubectl create ns ingress-system
namespace/ingress-system created

## 「ingress-nginx/ingress-nginx」を指定名前(nginx-ingress)で指定namespace(ingress-system)にインストール
## ssl passthrough を使うため、「--set controller.extraArgs.enable-ssl-passthrough=""」追加
$ helm install obi-test ingress-nginx/ingress-nginx -n ingress-system --set controller.extraArgs.enable-ssl-passthrough=""

## ssl passthrough 設定が不要の場合は以下だけで良い。
$ helm install obi-test ingress-nginx/ingress-nginx -n ingress-system

## インストール後、確認
$ helm list -n ingress-system
NAME    	NAMESPACE     	REVISION	UPDATED                                	STATUS  	CHART               	APP VERSION
obi-test	ingress-system	1       	2021-12-29 18:16:08.703864046 +0900 JST	deployed	ingress-nginx-4.0.13	1.1.0


$ kubectl get all -n ingress-system
NAME                                                     READY   STATUS    RESTARTS   AGE
pod/obi-test-ingress-nginx-controller-7b47d89bcb-srbfw   1/1     Running   0          2m3s

NAME                                                  TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
service/obi-test-ingress-nginx-controller             LoadBalancer   10.43.58.216    <pending>     80:30769/TCP,443:30528/TCP   2m3s
service/obi-test-ingress-nginx-controller-admission   ClusterIP      10.43.146.222   <none>        443/TCP                      2m3s

NAME                                                READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/obi-test-ingress-nginx-controller   1/1     1            1           2m3s

NAME                                                           DESIRED   CURRENT   READY   AGE
replicaset.apps/obi-test-ingress-nginx-controller-7b47d89bcb   1         1         1       2m3s


$ kubectl get svc -n ingress-system
NAME                                          TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
obi-test-ingress-nginx-controller             LoadBalancer   10.43.58.216    <pending>     80:30769/TCP,443:30528/TCP   22m
obi-test-ingress-nginx-controller-admission   ClusterIP      10.43.146.222   <none>        443/TCP                      22m
~~~

### 動作確認
 - マニフェスト作成

~~~yaml:test-ingress.yaml
---
### Ingress 作成
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  ingressClassName: nginx
  rules:
  - host: nginx.test.local
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-svc
            port:
              number: 80
---
### Service 作成
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  selector:
    app: nginx
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80
---
### Pod 作成
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.21.4
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 80
~~~

 - マニフェスト適用

~~~
$ kubectl apply -f test-ingress.yaml
~~~

 - hosts 設定追加  
Node IP(Master, Worker node中1個)とIngressで設定したhostを紐づけ

~~~
$ cat /etc/hosts
........
192.168.245.102 nginx.test.local
........
~~~

 - NodePort 確認  
nginx-ingress-controllerのNodePortを確認（ここでは「``30769``」）

~~~
$ kubectl get svc -n ingress-system
NAME                                          TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
obi-test-ingress-nginx-controller             LoadBalancer   10.43.58.216    <pending>     80:30769/TCP,443:30528/TCP   22m
obi-test-ingress-nginx-controller-admission   ClusterIP      10.43.146.222   <none>        443/TCP                      22m
~~~

 - 接続確認

~~~
$ curl -I http://nginx.test.local:30769
HTTP/1.1 200 OK
.........
~~~