软件版本
ubuntu 22.04 LTS
containerd 1.7.17
kubernetes v1.30.3
下载
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.10.1/deploy/static/provider/cloud/deploy.yaml
修改国内镜像源:
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/registry.k8s.io/ingress-nginx/controller:v1.10.1
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/registry.k8s.io/ingress-nginx/kube-webhook-certgen:v1.4.1
registry.k8s.io/ingress-nginx/kube-webhook-certgen:v1.4.1
—增加配置
422 spec:
423 hostNetwork: true ###与宿主机共享网络
424 containers:
425 - args:
426 - /nginx-ingress-controller
427 - --publish-service=$(POD_NAMESPACE)/ingress-nginx-controller
428 - --election-id=ingress-nginx-leader
429 - --controller-class=k8s.io/ingress-nginx
430 - --ingress-class=nginx
431 - --configmap=$(POD_NAMESPACE)/ingress-nginx-controller
432 - --validating-webhook=:8443
433 - --validating-webhook-certificate=/usr/local/certificates/cert
434 - --validating-webhook-key=/usr/local/certificates/key
435 - --enable-metrics=false
#支持master节点部署
我这里要支持在master节点部署,所以要加一个配置:
tolerations: #设置能在master上部署
- key: node-role.kubernetes.io/master
operator: Exists
修改后的结果如下:
spec:
hostNetwork: true #与宿主机共享网络
tolerations: #设置能在master上部署
- key: node-role.kubernetes.io/master
operator: Exists
containers:
- args:
- /nginx-ingress-controller
- --publish-service=$(POD_NAMESPACE)/ingress-nginx-controller
- --election-id=ingress-nginx-leader
- --controller-class=k8s.io/ingress-nginx
- --ingress-class=nginx
- --configmap=$(POD_NAMESPACE)/ingress-nginx-controller
- --validating-webhook=:8443
- --validating-webhook-certificate=/usr/local/certificates/cert
- --validating-webhook-key=/usr/local/certificates/key
root@k8s-master01:~/ingress# kubectl apply -f deploy.yaml
namespace/ingress-nginx configured
serviceaccount/ingress-nginx created
serviceaccount/ingress-nginx-admission created
role.rbac.authorization.k8s.io/ingress-nginx created
role.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrole.rbac.authorization.k8s.io/ingress-nginx created
clusterrole.rbac.authorization.k8s.io/ingress-nginx-admission created
rolebinding.rbac.authorization.k8s.io/ingress-nginx created
rolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
configmap/ingress-nginx-controller created
service/ingress-nginx-controller created
service/ingress-nginx-controller-admission created
deployment.apps/ingress-nginx-controller created
job.batch/ingress-nginx-admission-create created
job.batch/ingress-nginx-admission-patch created
ingressclass.networking.k8s.io/nginx created
validatingwebhookconfiguration.admissionregistration.k8s.io/ingress-nginx-admission created
root@k8s-master01:~/ingress# kubectl get all -n ingress-nginx
NAME READY STATUS RESTARTS AGE
pod/ingress-nginx-admission-create-6tfzn 0/1 Completed 0 21h
pod/ingress-nginx-admission-patch-tzl4b 0/1 Completed 0 21h
pod/ingress-nginx-controller-6bdfdfc4c8-dglc5 1/1 Running 0 5m40s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/ingress-nginx-controller LoadBalancer 10.99.84.218 <pending> 80:30553/TCP,443:31030/TCP 21h
service/ingress-nginx-controller-admission ClusterIP 10.108.67.14 <none> 443/TCP 21h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/ingress-nginx-controller 1/1 1 1 21h
NAME DESIRED CURRENT READY AGE
replicaset.apps/ingress-nginx-controller-64d75bd7b7 0 0 0 21h
replicaset.apps/ingress-nginx-controller-6bdfdfc4c8 1 1 1 5m40s
NAME STATUS COMPLETIONS DURATION AGE
job.batch/ingress-nginx-admission-create Complete 1/1 4s 21h
job.batch/ingress-nginx-admission-patch Complete 1/1 3s 21h
查看pod状态
root@k8s-master01:~/ingress# kubectl get pod -n ingress-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
ingress-nginx-admission-create-6gzxm 0/1 Completed 0 46s 10.10.135.136 k8s-node03 <none> <none>
ingress-nginx-admission-patch-r6dr7 0/1 Completed 2 46s 10.10.58.201 k8s-node02 <none> <none>
ingress-nginx-controller-64d75bd7b7-hhwf4 1/1 Running 0 46s 10.10.85.196 k8s-node01 <none> <none>
测试应用
创建应用yaml
vim tomcat.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat-deployment
labels:
app: tomcat
spec:
replicas: 2
selector:
matchLabels:
app: tomcat
minReadySeconds: 1
progressDeadlineSeconds: 60
revisionHistoryLimit: 2
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
template:
metadata:
labels:
app: tomcat
spec:
containers:
- name: tomcat
image: wenlongxue/tomcat:tomcat-demo-62-8fe6052
imagePullPolicy: Always
ports:
- containerPort: 8080
resources:
requests:
memory: "2Gi"
cpu: "80m"
limits:
memory: "2Gi"
cpu: "80m"
readinessProbe:
httpGet:
path: /
port: 8080
initialDelaySeconds: 180
periodSeconds: 5
timeoutSeconds: 3
successThreshold: 1
failureThreshold: 30
---
apiVersion: v1
kind: Service
metadata:
name: tomcat-service
labels:
app: tomcat
spec:
selector:
app: tomcat
ports:
- name: tomcat-port
protocol: TCP
port: 8080
targetPort: 8080
type: ClusterIP
部署 tomcat 应用
kubectl apply -f tomcat.yaml
创建 ingress yaml
vim tomcat-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: tomcat
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: tomcat.cctbb.com
http:
paths:
- path: "/"
pathType: Prefix
backend:
service:
name: tomcat-service
port:
number: 8080
部署 tomcat ingress yaml
kubectl apply -f tomcat-ingress.yaml
查看 ingress 对应节点的端口
kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.96.88.139 <none> 80:30497/TCP,443:32581/TCP 54m
ingress-nginx-controller-admission ClusterIP 10.96.193.26 <none> 443/TCP 54m
添加 hosts
在 hosts 文件最后追加 ingress 节点的 IP 地址
192.168.2.67 tomcat.cctbb.com
然后在浏览器中访问 tomcat.cctbb.com:30497。