Steps to deploy ingress-nginx on k8s

Steps to deploy ingress-nginx on k8s

Preface

After the k8s cluster service is deployed, you need to provide a domain name for external access. At this time, you need ingress-nginx. Today I will share with you

1. Deployment and Configuration of Ingress

1. Get the configuration file

#The file has been downloaded locally https://github.com/kubernetes/ingress-nginx/tree/nginx-0.20.0/deploy

2. Prepare the image

unzip ingress-nginx-nginx-0.20.0.zip
cd ingress-nginx-nginx-0.20.0/deploy/
vim mandatory.yaml #Collection of other files#Edit the mandatory.yaml file and change the defaultbackend image address to the Alibaba Cloud image address (as shown below)
image: registry.cn-hangzhou.aliyuncs.com/allinpay/defaultbackend-amd64:v1.5

insert image description here

3. Installation

kubectl apply -f mandatory.yaml 

insert image description here

#Wait a moment and use the following command to query kubectl get namespace
kubectl get pods -n ingress-nginx

insert image description here

4. Create backend pod and service (pod-B, service-B)

vim 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:
      app: myapp
      release: canary
  template:
    metadata:
      labels:
        app: myapp
        release: canary
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v2
        ports:
        - name: http
          containerPort: 80

#Application configuration kubectl apply -f deploy-demo.yaml
#View kubectl get pods

insert image description here

5. Create service-A

vim service-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: 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.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx

#Application configuration kubectl apply -f service-nodeport.yaml

#View kubectl get svc -n ingress-nginx

insert image description here

Access in an external browser: ip:30080

insert image description here

The reason for the error at this time is that no ingress rules are generated to associate ingress-controller with service-B;

6. Define ingress rules

Define ingress rules, which will be automatically injected into nginx.conf of ingress-controller (pod);
Ingress-controller is directly associated with service-B, but ingress generates various rules in the middle;

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

At this time, a server segment configuration will be automatically generated in the nginx.conf of ingress-controller, that is, a field for the nginx virtual host, including upstream configuration, etc.;
Upstream automatically proxies to the backend pod (pod-B). These are all automatically generated, so automatic configuration generation and automatic configuration change are achieved;
Just need to change this yaml file;

#Application configuration kubectl apply -f ingress-myapp.yaml

insert image description here

Configure the host file on the external host

insert image description here

Then visit it in the browser again and find that the backend pod can be accessed

insert image description here

2. Use https

1. Create a k8s certificate (note that the same namespace is used as the backend pod)

kubectl -n default create secret tls ingress-test --key /home/centos/cert/cash432.key --cert /home/centos/cert/cash432.crt

2. Create ingress rules

Pay attention to the same namespace, otherwise the certificate will not take effect

vim ingress-myapp-cash432.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-myapp
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
    kubernetes.io/secure-backends: "true"
    kubernetes.io/ssl-passthrough: "true"
spec:
  tls:
  - hosts:
    - myapp.cash432.xyz
    secretName: ingress-test
  rules:
  - host: myapp.cash432.xyz
    http:
      paths:
      - path:
        backend:
          serviceName: myapp
          servicePort: 80

#Application configuration kubectl apply -f ingress-myapp-cash432.yaml

Browser access

insert image description here

This is the end of this article about the steps of deploying ingress-nginx on k8s. For more relevant content about deploying ingress-nginx on k8s, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Ingress seven-layer routing mechanism implements domain name access to k8s
  • A detailed introduction to deploying Ingress in k8s and creating rules

<<:  A solution to a bug in IE6 with jquery-multiselect

>>:  How to install Element UI and use vector graphics in vue3.0

Recommend

How to optimize logic judgment code in JavaScript

Preface The logical judgment statements we use in...

Implementation of building Kubernetes cluster with VirtualBox+Ubuntu16

Table of contents About Kubernetes Basic environm...

Two ways to exit bash in docker container under Linux

If you want to exit bash, there are two options: ...

CSS solves the misalignment problem of inline-block

No more nonsense, post code HTML part <div cla...

Detailed explanation of jQuery chain calls

Table of contents Chain calls A small case Chain ...

JavaScript to achieve click image flip effect

I was recently working on a project about face co...

Detailed explanation of putting common nginx commands into shell scripts

1. Create a folder to store nginx shell scripts /...

Detailed installation process of nodejs management tool nvm

nvm nvm is responsible for managing multiple vers...

MySQL joint table query basic operation left-join common pitfalls

Overview For small and medium-sized projects, joi...

MySQL 8.0 WITH query details

Table of contents Learning about WITH queries in ...

Let's talk about bitwise operations in React source code in detail

Table of contents Preface Several common bit oper...

Teach you step by step to develop a brick-breaking game with vue3

Preface I wrote a few examples using vue3, and I ...