helm 安装 bitnami/nginx-ingress-controller

镜像源环境

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 镜像源
helm repo list
NAME    URL
bitnami https://charts.bitnami.com/bitnami

# 搜索 nginx-ingress
helm search repo nginx
NAME                                    CHART VERSION   APP VERSION     DESCRIPTION
bitnami/nginx                           16.0.6          1.25.5          NGINX Open Source is a web server that can be a...
bitnami/nginx-ingress-controller        11.1.1          1.10.1          NGINX Ingress Controller is an Ingress controll...
bitnami/nginx-intel                     2.1.15          0.4.9           DEPRECATED NGINX Open Source for Intel is a lig...

拉取的镜像

如果已经拉取可以直接使用,或者在部署时自动拉取指定版本。

1
2
3
4
5
6
docker images
REPOSITORY                         TAG                   IMAGE ID       CREATED         SIZE
fastapi_shortcode                  v0.1                  4285802c2307   3 days ago      1.09GB
nginx                              latest                e784f4560448   3 weeks ago     188MB
bitnami/nginx-ingress-controller   1.10.1-debian-12-r1   adbb738199b6   4 weeks ago     287MB
bitnami/nginx                      1.25.5-debian-12-r1   eff9e5aff5bf   5 weeks ago     185MB

安装

拉取nginx-ingress chart到本地

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
helm pull bitnami/nginx-ingress-controller

# 拉取到的文件
nginx-ingress-controller-11.1.1.tgz

# 解压文件
tar -xf nginx-ingress-controller-11.1.1.tgz

# 解压后查看
ls nginx-ingress-controller
Chart.lock  Chart.yaml  README.md   charts      templates   values.yaml

修改 values.yaml 配置

  1. 修改ingress-controller服务Deployment的NodeSelector;此处为了把资源部署到打了此标签的节点
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## @param kind Install as Deployment or DaemonSet
##
kind: Deployment

# ...

## @param nodeSelector Node labels for pod assignment. Evaluated as a template.
## ref: https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/
##
nodeSelector: {}

修改结果如下:

1
2
3
4
5
6
447 ## @param nodeSelector Node labels for pod assignment. Evaluated as a template.
448 ## ref: https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/      449 ##
450 nodeSelector:.
451   kubernetes.io/os: linux
452   ignode: 'true'

  1. 修改默认后端defaultBackend的NodeSelector;此处为了把资源部署到打了此标签的节点
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## Default 404 backend
##
defaultBackend:

# ...

  ## @param defaultBackend.nodeSelector Node labels for pod assignment
  ## ref: https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/
  ##
  nodeSelector: {}

修改后内容如下:

1
2
3
4
5
6
7
  ## @param defaultBackend.nodeSelector Node labels for pod assignment
  ## ref: https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/
  ##
  nodeSelector: 
    kubernetes.io/os: linux
    ignode: 'true'

本地测试没有节点可选择时,可以不需要修改。

  1. 修改ingress-controller服务service的类型,默认是LoadBalance
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
## Service parameters
##
service:
  ## @param service.type Kubernetes Service type for Controller
  ##
  type: NodePort # LoadBalancer
  ## @param service.ports [object] Service ports
  ##
  ports:
    http: 80
    https: 443

用于本地测试,选择NodePort类型。

创建namespace

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
kubectl create ns bitnami-ingress-nginx
namespace/bitnami-ingress-nginx created

## 查看
kubectl get pods,rc,svc,deployment,ns -n default
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.43.0.1    <none>        443/TCP   24d

NAME                              STATUS   AGE
namespace/kube-system             Active   24d
namespace/kube-public             Active   24d
namespace/kube-node-lease         Active   24d
namespace/default                 Active   24d
namespace/bitnami-ingress-nginx   Active   11s

资源节点标签

1
2
3
4
5
6
7
8
9
# 查看节点
kubectl get nodes
NAME               STATUS   ROLES                  AGE   VERSION
colima-helm-test   Ready    control-plane,master   24d   v1.28.3+k3s2

# 查看节点标签
kubectl get nodes --show-labels
NAME               STATUS   ROLES                  AGE   VERSION        LABELS
colima-helm-test   Ready    control-plane,master   24d   v1.28.3+k3s2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/instance-type=k3s,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=colima-helm-test,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=true,node-role.kubernetes.io/master=true,node.kubernetes.io/instance-type=k3s

添加 values.yaml 中 nodeSelector 修改的标签

1
2
3
4
5
6
7
8
kubectl label nodes colima-helm-test ignode=true
node/colima-helm-test labeled

# 查看节点标签
kubectl get nodes --show-labels
NAME               STATUS   ROLES                  AGE   VERSION        LABELS
colima-helm-test   Ready    control-plane,master   24d   v1.28.3+k3s2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/instance-type=k3s,beta.kubernetes.io/os=linux,ignode=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=colima-helm-test,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=true,node-role.kubernetes.io/master=true,node.kubernetes.io/instance-type=k3s

添加节点后资源会部署到有该标签的节点上

部署资源

执行部署命令如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
helm install myingress ./nginx-ingress-controller -f ./nginx-ingress-controller/values.yaml --namespace bitnami-ingress-nginx
NAME: myingress
LAST DEPLOYED: Wed May 29 23:04:31 2024
NAMESPACE: bitnami-ingress-nginx
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: nginx-ingress-controller
CHART VERSION: 11.1.1
APP VERSION: 1.10.1

** Please be patient while the chart is being deployed **

The nginx-ingress controller has been installed.

Get the application URL by running these commands:
    export HTTP_NODE_PORT=$(kubectl --namespace bitnami-ingress-nginx get services -o jsonpath="{.spec.ports[0].nodePort}" myingress-nginx-ingress-controller)
    export HTTPS_NODE_PORT=$(kubectl --namespace bitnami-ingress-nginx get services -o jsonpath="{.spec.ports[1].nodePort}" myingress-nginx-ingress-controller)
    export NODE_IP=$(kubectl --namespace bitnami-ingress-nginx get nodes -o jsonpath="{.items[0].status.addresses[1].address}")

    echo "Visit http://$NODE_IP:$HTTP_NODE_PORT to access your application via HTTP."
    echo "Visit https://$NODE_IP:$HTTPS_NODE_PORT to access your application via HTTPS."

An example Ingress that makes use of the controller:

  apiVersion: networking.k8s.io/v1
  kind: Ingress
  metadata:
    name: example
    namespace: bitnami-ingress-nginx
  spec:
    ingressClassName: nginx
    rules:
      - host: www.example.com
        http:
          paths:
            - backend:
                service:
                  name: example-service
                  port:
                    number: 80
              path: /
              pathType: Prefix
    # This section is only required if TLS is to be enabled for the Ingress
    tls:
        - hosts:
            - www.example.com
          secretName: example-tls

If TLS is enabled for the Ingress, a Secret containing the certificate and key must also be provided:

  apiVersion: v1
  kind: Secret
  metadata:
    name: example-tls
    namespace: bitnami-ingress-nginx
  data:
    tls.crt: <base64 encoded cert>
    tls.key: <base64 encoded key>
  type: kubernetes.io/tls

WARNING: There are "resources" sections in the chart not set. Using "resourcesPreset" is not recommended for production. For production installations, please set the following values according to your workload needs:
  - defaultBackend.resources
  - resources
+info https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/

  1. helm查看部署
1
2
3
4
helm ls -A
NAME            NAMESPACE               REVISION        UPDATED                                 STATUS          CHART                           APP VERSION
myingress       bitnami-ingress-nginx   1               2024-05-29 23:04:31.635548 +0800 CST    deployed        nginx-ingress-controller-11.1.1 1.10.1

  1. kubectl查看部署资源
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
kubectl get pods,rc,svc,deployment,ns -n bitnami-ingress-nginx
NAME                                                                  READY   STATUS    RESTARTS   AGE
pod/myingress-nginx-ingress-controller-default-backend-68f4ddflqv7m   1/1     Running   0          115s
pod/myingress-nginx-ingress-controller-7c6cd975b-sbbcn                1/1     Running   0          115s

NAME                                                         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                      AGE
service/myingress-nginx-ingress-controller-default-backend   ClusterIP   10.43.193.40   <none>        80/TCP                       116s
service/myingress-nginx-ingress-controller                   NodePort    10.43.82.69    <none>        80:31758/TCP,443:32577/TCP   116s

NAME                                                                 READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/myingress-nginx-ingress-controller-default-backend   1/1     1            1           116s
deployment.apps/myingress-nginx-ingress-controller                   1/1     1            1           116s

NAME                              STATUS   AGE
namespace/kube-system             Active   24d
namespace/kube-public             Active   24d
namespace/kube-node-lease         Active   24d
namespace/default                 Active   24d
namespace/bitnami-ingress-nginx   Active   17m

  1. 部署成功可以在浏览器中打开: http://localhost:31758/
1
2
3
4
5
6
curl -I http://localhost:31758/
HTTP/1.1 404 Not Found
Date: Wed, 29 May 2024 15:15:32 GMT
Content-Type: text/html
Content-Length: 146
Connection: keep-alive

但是暂时没有服务在运行。

创建一个web服务资源

创建web服务资源namespace

1
2
kubectl create ns shortcode-ns
namespace/shortcode-ns created

web服务使用本地镜像服务

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 镜像源
helm repo list
NAME    URL
piaohua https://piaohua.github.io/helm-charts/

# 安装镜像
helm install shortcode ./fastapi-short-code -f ./fastapi-short-code/values.yaml --namespace shortcode-ns
NAME: shortcode
LAST DEPLOYED: Thu May 30 22:23:37 2024
NAMESPACE: shortcode-ns
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace shortcode-ns -l "app.kubernetes.io/name=fastapi-short-code,app.kubernetes.io/instance=shortcode" -o jsonpath="{.items[0].metadata.name}")
  export CONTAINER_PORT=$(kubectl get pod --namespace shortcode-ns $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace shortcode-ns port-forward $POD_NAME 8080:$CONTAINER_PORT

# 安装后查看资源
kubectl get all -n shortcode-ns
NAME                                                READY   STATUS    RESTARTS   AGE
pod/shortcode-fastapi-short-code-6bb7d4b8cc-8xdc5   1/1     Running   0          99s

NAME                                   TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
service/shortcode-fastapi-short-code   ClusterIP   10.43.85.216   <none>        80/TCP    99s

NAME                                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/shortcode-fastapi-short-code   1/1     1            1           99s

NAME                                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/shortcode-fastapi-short-code-6bb7d4b8cc   1         1         1       99s

创建Ingress资源

bitnami-ingress.yaml 内容:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example
  namespace: shortcode-ns
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /*
spec:
  ingressClassName: nginx
  rules:
    - host: www.example.com
      http:
        paths:
          - path: /*
            pathType: Prefix
            backend:
              service:
                name: shortcode-fastapi-short-code
                port:
                  number: 80

创建Ingress资源:

1
2
3
4
5
6
7
8
# 创建Ingress资源
kubectl apply -f bitnami-ingress.yaml 
ingress.networking.k8s.io/bitnami-ingress created

# 查看资源
kubectl get ingress -A
NAMESPACE      NAME      CLASS   HOSTS             ADDRESS       PORTS   AGE
shortcode-ns   example   nginx   www.example.com   192.168.5.3   80      122m

接口请求测试

请求头必须带上Host,否则路由到默认的后端nginx服务。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
curl -H "Host: www.example.com" http://127.0.0.1:31758/
{"Hello":"World"}%

curl -H "Host: www.example.com" http://127.0.0.1:31758/items/5
{"item_id":5,"q":null}

url -H "Host: www.example.com" http://127.0.0.1:31758/items/5\?q\=a
{"item_id":5,"q":"a"}

curl -H "Host: www.example.com" http://127.0.0.1:31758/short/code
{"code":"jRcFf1"}

curl -X POST -H "Host: www.example.com" -H "Content-Type:application/json" -d '{"long_url":"https://google.com"}' http://127.0.0.1:31758/short/url
{"long_url":"https://google.com","short_url":"zTgQx3"}

浏览器中访问,修改/etc/hosts添加如下配置

1
127.0.0.1 www.example.com

然后在浏览器中打开:http://www.example.com:31758/docs

最后打包发布到私有仓库

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
helm package nginx-ingress-controller --debug --version 11.1
.1
Successfully packaged chart and saved it to: /Users/piao/data/golang/helm-charts/nginx-ingress-controller-11.1.1.tgz

helm package fastapi-short-code --debug --version 0.1.1
Successfully packaged chart and saved it to: /Users/piao/data/golang/helm-charts/fastapi-short-code-0.1.1.tgz

# 更新仓库
helm repo index --url https://piaohua.github.io/helm-charts/ --merge index.yaml .

参考