Kubernetes安装配置Ingress

Kubernetes安装配置Ingress。

跟官方教程略有不同,改了两个地方, 一是github上的文件地址,二是ingress.yaml文件。

首先加hosts

127.0.0.1  kubernetes.docker.internal
127.0.0.1  localhost

安装Ingress

需要注意的是Ingress默认使用的是80端口,如果已有容器使用了该端口,先停掉并取消其自启,不然Docker重启后80端口就不一定是Ingress占用了,会无法访问。

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

验证安装

kubectl get pods --all-namespaces -l app.kubernetes.io/name=ingress-nginx

配置文件

创建几个配置文件,放任意位置即可

apple.yaml,内容如下

kind: Pod
apiVersion: v1
metadata:
  name: apple-app
  labels:
    app: apple
spec:
  containers:
    - name: apple-app
      image: hashicorp/http-echo
      args:
        - "-text=apple"

---

kind: Service
apiVersion: v1
metadata:
  name: apple-service
spec:
  selector:
    app: apple
  ports:
    - port: 5678 # Default port for image

banana.yaml,内容如下

kind: Pod
apiVersion: v1
metadata:
  name: banana-app
  labels:
    app: banana
spec:
  containers:
    - name: banana-app
      image: hashicorp/http-echo
      args:
        - "-text=banana"

---

kind: Service
apiVersion: v1
metadata:
  name: banana-service
spec:
  selector:
    app: banana
  ports:
    - port: 5678 # Default port for image

ingress.yaml,内容如下

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - http:
      paths:
        - path: /apple
          backend:
            serviceName: apple-service
            servicePort: 5678
        - path: /banana
          backend:
            serviceName: banana-service
            servicePort: 5678

annotations官方的写法是ingress.kubernetes.io/rewrite-target: /,如果按它的来,访问http://localhost/apple404

如果报Warning:

Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress

忽略即可。

创建Pods和服务

kubectl create -f apple.yaml
kubectl create -f banana.yaml
kubectl create -f ingress.yaml

然后访问http://localhost/applehttp://localhost/banana就可以正常输出了。

如何卸载

卸载的命令如下,一个一个来

kubectl delete -f apple.yaml
kubectl delete -f banana.yaml
kubectl delete -f ingress.yaml
kubectl delete -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yam

请耐心等待命令执行完成,不要提前退出。

1 thought on “Kubernetes安装配置Ingress”

Leave a Comment

豫ICP备19001387号-1