1. 环境准备

   安装nginx-ingress-controller和backend

   cd /etc/yum.repos.d/mainfests

   下载镜像的脚本 vi ingressnginx.sh

#!/bin/bash
images=(
    nginx-ingress-controller:0.9.0-beta.10 
    defaultbackend:1.4)

for imageName in ${images[@]} ; do
    docker pull docker.io/chenliujin/$imageName
    docker tag docker.io/chenliujin/$imageName gcr.io/google_containers/$imageName 
    docker rmi docker.io/chenliujin/$imageName
done

为了防止在执行 default-backend.yaml发现意外错误,在所有节点执行以下操作

docker pull docker.io/chenliujin/defaultbackend:1.4
docker tag docker.io/chenliujin/defaultbackend:1.4 gcr.io/google_containers/defaultbackend:1.4
docker rmi docker.io/chenliujin/defaultbackend:1.4

2. 下载必需的yaml文件并执行

cd /home/k8s/ingressimage

编写下载yaml的脚本  vi downimages.sh

for file in namespace.yaml configmap.yaml rbac.yaml tcp-services-configmap.yaml with-rbac.yaml udp-services-configmap.yaml default-backend.yaml;
do wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.17.0/deploy/$file; done

kubectl apply -f namespace.yaml

kubectl get ns

kubectl apply -f ./

kubectl get pods -n ingress-nginx --show-labels

3. 编写测试文件

cd /home/k8s/ingress   vi deploy-demo.yaml

apiVersion: v1
kind: Service
metadata:
  name: myapp
  namespace: default
spec:
  selector:
    app: myapp
    release: canary
  ports:
  - name: http
    targetPort: 80
    port: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      release: canary
  template:
    metadata:
      labels:
        app: myapp
        release: canary
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v2
        ports:
        - name: http
          containerPort: 80

kubectl apply -f deploy-demo.yaml

kubectl get svc

kubectl get pods

  cd /home/k8s/ingresstest

  wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.17.0/deploy/provider/baremetal/service-nodeport.yaml

  修改其中的内容为

apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    targetPort: 80
    protocol: TCP
    nodePort: 30080
  - name: https
    port: 443
    targetPort: 443
    protocol: TCP
    nodePort: 30443
  selector:
    app: ingress-nginx

kubectl apply -f service-nodeport.yaml

  最后访问在浏览器访问:http://47.102.46.176:30080/   如果出现  default backend - 404 表示成功

 4. 通过修改域名访问

vi ingress-myapp.yaml
# 内容是 
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-myapp
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: myapp.klvchen.com
    http:
      paths:
      - path:
        backend:
          serviceName: myapp
          servicePort: 80

# 创建 ingress
kubectl apply -f ingress-myapp.yaml 

# 查看 ingress
kubectl get ingress

# 查详细信息 ingress
kubectl describe ingress ingress-myapp

# 通过修改 host 可以通过域名访问,修改 hosts 文件,添加下面记录
192.168.0.205       myapp.klvchen.com

# 访问 http://myapp.klvchen.com:30080/

 

转载于:https://www.cnblogs.com/mutong1228/p/10949506.html

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐