Contour インストール

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

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

# 環境
 - Rancher: v2.5.7
 - kubernetes(Client): v1.20.5
 - kubernetes(Server): v1.20.5
 - Contour Chart: v4.2.2
 - Contour App: v1.14.0

# インストール
 - 作業場所: ClientPC
 - Contour Install Page<br>
https://projectcontour.io/getting-started/<br>
<b>※インストール方法は3つあるが、ここではHelmでインストールする</b><br>
 - Contour Chart Page<br>
https://github.com/bitnami/charts/tree/master/bitnami/contour

### helmにcontour repo追加
~~~
$ helm repo add bitnami https://charts.bitnami.com/bitnami
$ helm repo update

## 確認 ##
$ helm search repo bitnami/contour
NAME           	CHART VERSION	APP VERSION	DESCRIPTION                              
bitnami/contour	4.2.2        	1.14.0     	Contour Ingress controller for Kubernetes
~~~

### helmでContour インストール
~~~
## namespace(projectcontour) 作成
$ kubectl create ns projectcontour
namespace/projectcontour created

## 「bitnami/contour」を指定名前(contour)で指定namespace(projectcontour)にインストール
$ helm install contour bitnami/contour -n projectcontour

## インストール後、確認
$ helm list -n projectcontour
NAME   	NAMESPACE     	REVISION	UPDATED                                	STATUS  	CHART        	APP VERSION
contour	projectcontour	1       	2021-04-08 23:18:38.944010107 +0900 JST	deployed	contour-4.2.2	1.14.0

$ kubectl get deploy -n projectcontour
NAME              READY   UP-TO-DATE   AVAILABLE   AGE
contour-contour   2/2     2            2           45s

$ kubectl get get ds -n projectcontour
NAME            DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
contour-envoy   3         3         3       3            3           <none>          52s

$ kubectl get pod -n projectcontour
NAME                               READY   STATUS    RESTARTS   AGE
contour-contour-56cd8f64bf-cpsxk   1/1     Running   0          57s
contour-contour-56cd8f64bf-djmkp   1/1     Running   0          57s
contour-envoy-dzb79                2/2     Running   0          57s
contour-envoy-gt65w                2/2     Running   0          57s
contour-envoy-jvthk                2/2     Running   0          57s

$ kubectl get svc -n projectcontour
NAME            TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
contour         ClusterIP      10.43.51.243    <none>        8001/TCP                     69s
contour-envoy   LoadBalancer   10.43.184.146   <pending>     80:31094/TCP,443:30716/TCP   69s

$ kubectl get sa -n projectcontour
NAME              SECRETS   AGE
contour-contour   1         83s
contour-envoy     1         83s
..........

$ kubectl secret -n projectcontour
NAME                            TYPE                                  DATA   AGE
contour-contour-token-jlr25     kubernetes.io/service-account-token   3      4h10m
contour-envoy-token-kks64       kubernetes.io/service-account-token   3      4h10m
contourcert                     kubernetes.io/tls                     3      4h10m
envoycert                       kubernetes.io/tls                     3      4h10m
sh.helm.release.v1.contour.v1   helm.sh/release.v1                    1      4h10m
..........

$ kubectl get cm -n projectcontour
NAME           DATA   AGE
contour        1      76s
leader-elect   0      74s

~~~

### 動作確認
 - PodとService作成

~~~yaml:test-nginx.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  selector:
    app: nginx
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80
---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.19.9
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 80
~~~

<br><p><b>※Contourが提供するHTTPProxyリソース(CRD)を使用<br>→ Ingressリソースを使っても良い</b></p>

 - HTTPProxy作成<br>

~~~yaml:test-httpproxy.yaml
apiVersion: projectcontour.io/v1
kind: HTTPProxy
metadata:
  name: contour-http-proxy
spec:
  virtualhost:
    fqdn: nginx-contour.test.local
  routes:
    - conditions:
      - prefix: /
      services:
        - name: nginx-svc
          port: 80
~~~

 - manifest適用

~~~
$ kubectl apply -f test-nginx.yaml
$ kubectl apply -f test-httpproxy.yaml
~~~

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

~~~
$ cat /etc/hosts
........
192.168.245.103 nginx-contour.test.local
........
~~~

 - NodePort 確認  
contour serviceのNodePortを確認（ここでは「``31094``」）

~~~
$ kubectl get svc -n projectcontour
NAME            TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
contour         ClusterIP      10.43.51.243    <none>        8001/TCP                     69s
contour-envoy   LoadBalancer   10.43.184.146   <pending>     80:31094/TCP,443:30716/TCP   69s
~~~

 - 接続確認

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

<br><b>Ingressで確認</b>

 - Ingress作成<br>

~~~yaml:test-ingress.yaml
## kubernetes v1.19以降用
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: contour-ingress
  annotations:
    kubernetes.io/ingress.class: contour ## contour classを指定
spec:
  rules:
  - host: nginx-contour.test.local
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-svc
            port:
              number: 80
~~~

 - manifest適用

~~~
## HTTPProxyリソース削除
$ kubectl delete -f test-httpproxy.yaml

## Ingressリソース作成
$ kubectl apply -f test-ingress.yaml
~~~

 - 接続確認

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