1 installation minikube

2 Start the K8S cluster using minikube

minikube start
Copy the code

through

3 Create a Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        ports:
        - containerPort: 80
Copy the code
kubectl apply -f nginx-dep.yml
Copy the code

To ensure that the nginx service within the cluster has been started, we can go to the corresponding POD to check

Kubectl exec nginx-deployment-7fb7fd49b4-29cdk-it -- /bin/sh // /bin/shCopy the code
curl localhost
Copy the code

If the output is as follows, the Nginx service in pod has been started

<! DOCTYPE html> <html> <head> <title>Welcome to nginx! </title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx! </h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>Copy the code

4 Create the Service to expose the Pod

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 30080
  type: NodePort
Copy the code
kubectl apply -f nginx-svc.yml
Copy the code

5 If you are using minikube, you can get the access via minikube service nginx-service –url. NodePort is usually used with externalIPs.

If you want to use it locally (accessed via localhost), you can use port forwarding. The following command sets up a port forwarding

kubectl port-forward nginx-deployment-7d9d7464fb-mrc4h 30001:80
Copy the code

30001 is the local port, 80 is the cluster internal access port (port).

Use port forwarding to access applications in the cluster

K8S Indicates the relationship between ports

  • Port: indicates the k8S internal port for accessing services. ClusterIP: port is used to access a service
  • NodePort: specifies the external port for accessing services in the K8S cluster. NodeIP: nodePort is used to access a service
  • TargetPort: is the port of POD. The traffic from port and nodePort flows through kube-proxy to the targetPort of back-end POD, and finally to the container
  • ContainerPort: Is the port of the pod’s internal container. TargetPort maps to containerPort

Ingress

  • Check whether the Ingress is enabled
minikube addons list
Copy the code
  • To enable the Ingress Controller in minikube, simply execute the following command
minikube addons enable ingress
Copy the code
  • Create example – ingress. Yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: hello-world.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web
                port:
                  number: 8080
Copy the code
  • Create the Ingress resource by running the following command
kubectl apply -f example-ingress.yaml
Copy the code
  • Verify that the IP address is set
kubectl get ingress
Copy the code
  • Add the following at the end of the /etc/hosts file

Note: If you are running the Minikube environment locally, you need to use the Minikube IP to get the external IP address. The IP addresses displayed in the Ingress list are internal IP addresses.

// Configure host 172.17.0.15 hello-world.infoCopy the code
  • Verify that the Ingress controller can forward request traffic:
curl hello-world.info
Copy the code

Visual interface

In addition to using the command line to view pod and service information, we can also use a visual interface

  • View the built-in Minikube plugins. The Dashboard plugin is not enabled by default
minikube addons list
Copy the code

|—————————–|———-|————–|

ADDON NAME PROFILE STATUS
dashboard minikube disabled
default-storageclass minikube Enabled ✅
  • Enabling the Dashboard plug-in
minikube addons enable dashboard
Copy the code
  • When you start Dashboard, the — URL parameter does not open the admin page and the access path is available on the console
minikube dashboard --url
Copy the code
  • To access Dashboard from outside, you need to set the proxy from kubectl, –address is set to your server address;
Kubectl proxy --port=44469 --address='192.168.5.94' --accept-hosts='^.*' &Copy the code
  • You need to enable the firewall port to access the server from the outside.
Firewall - CMD --zone=public --add-port=44469/ TCP --permanentCopy the code
  • You can access the Dashboard using the following address:
http://192.168.5.94:44469/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
Copy the code

Refer to the article

  • Configure the Ingress using the NGINX Ingress controller in a Minikube environment