brief introduction
Just take notes to prevent forgetting. What we want to achieve today is the access method above the article pictures.
k8s installation ingress nginx
First deploy the following yaml file
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml
Most of the tutorials are done by typing commands directly, but I like to download them first.
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml
Because it's in China, you need to make sure that the mirror can be downloaded to the machine. If not, I recommend the following method.
https://www.bboysoul.com/2019/08/19/k8s%E5%85%8Dfq%E4%B8%8B%E8%BD%BD%E9%95%9C%E5%83%8F/
Deployment after downloading the mirror
kubectl apply -f mandatory.yaml
It is noteworthy that this yaml file does not contain service. To create a service, use the following yaml file
https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/baremetal/service-nodeport.yaml
Download the yaml file locally and then view the content
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 - name: https port: 443 targetPort: 443 protocol: TCP selector: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx
Because we want to achieve the status above the article image, the service also writes a nodePort: 80 and nodePort: 443 fields, such as the following
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 nodePort: 80 protocol: TCP - name: https port: 443 targetPort: 443 nodePort: 443 protocol: TCP selector: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx
Before apply this service, we should pay attention to our apiserver's default node port range is 30000-32767, but the node port we need is not in this range, so we need to modify the node port port port of apiserver.
Because I am a cluster built using kubeadm, so
Edit the following file.
vim /etc/kubernetes/manifests/kube-apiserver.yaml
Added under command
- --service-node-port-range=30-3000
After saving, apiserver will restart automatically and apply configuration. After modifying one node, other master nodes need to be modified to ensure the same configuration of apiserver.
Then apply the yaml file.
Finally, check if the svc is working properly
kubectl get svc -A
If it's normal, let's test it.
Use the following yaml file
#deploy apiVersion: apps/v1 kind: Deployment metadata: name: nginx-ingress spec: selector: matchLabels: app: nginx-ingress replicas: 1 template: metadata: labels: app: nginx-ingress spec: containers: - name: nginx-ingress image: nginx:1.15 ports: - containerPort: 80 --- #service apiVersion: v1 kind: Service metadata: name: nginx-ingress spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: nginx-ingress --- #ingress apiVersion: extensions/v1beta1 kind: Ingress metadata: name: nginx-ingress spec: rules: - host: nginx.bboysoul.com http: paths: - path: / backend: serviceName: nginx-ingress servicePort: 80
There are three objects defined above, one is deployment, one is service, and the last is ingress ingress ress and service binding, so we can access the deployment by visiting nginx.bboysoul.com.
kubectl apply -f ingress-nginx.yaml
Add your k8smaster node and nginx.bboysoul.com parsing to the external dns, and then directly access it.
Welcome to Bboysoul's blog
Have Fun