ingress-nginx高可用安装和使用
网盘链接
# 网盘里面是更改好的ingress-nginx的yaml文件,可以直接使用
链接:https://pan.baidu.com/s/1jswoZ2X4ziTw3oHFR4pFFQ?pwd=dAOf
提取码:dAOf
1、ingress-nginx下载链接
https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.3.0/deploy/static/provider/baremetal/deploy.yaml
2、镜像
- 打开下载后的yaml,可以看到ingress-nginx需要两个镜像文件
- nginx-ingress-controller
- kube-webhook-certgen
- 从阿里云镜像仓库地址下载
- https://cr.console.aliyun.com/cn-hangzhou/instances/images?accounttraceid=e3a7830ffbbd4f1f933753c2c742196epojw
- https://cr.console.aliyun.com/cn-hangzhou/instances/images?accounttraceid=e3a7830ffbbd4f1f933753c2c742196epojw
# 需要在所有节点上都下载这两个镜像
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.1.0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1
3、替换镜像
# 共有3个地方要更换。找到对应的image,替换成如下镜像
image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.1.0
image: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1
image: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1
4、ingress-nginx修改成Daemonset模式和节点打标签
# 新增三个地方
apiVersion: apps/v1
kind: DaemonSet # 修改
metadata:
labels:
helm.sh/chart: ingress-nginx-4.0.10
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/instance: ingress-nginx
app.kubernetes.io/version: 1.1.0
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/component: controller
name: ingress-nginx-controller
namespace: ingress-nginx
spec:
selector:
matchLabels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/instance: ingress-nginx
app.kubernetes.io/component: controller
revisionHistoryLimit: 10
minReadySeconds: 0
template:
metadata:
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/instance: ingress-nginx
app.kubernetes.io/component: controller
spec:
hostNetwork: true # 启用hostNetwork网络
dnsPolicy: ClusterFirst
containers:
- name: controller
image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.1.0
imagePullPolicy: IfNotPresent
xxxxxxxxxxx
xxxxxxxxxxx
resources:
requests:
cpu: 100m
memory: 90Mi
nodeSelector:
isIngress: "true" # 新增标签信息
kubernetes.io/os: linux
serviceAccountName: ingress-nginx
terminationGracePeriodSeconds: 300
[root@k8s-master1 k8s]# kubectl label node k8s-master1 isIngress="true"
[root@k8s-master1 k8s]# kubectl label node k8s-node1 isIngress="true"
[root@k8s-master1 k8s]# kubectl label node k8s-node2 isIngress="true"
[root@k8s-master1 k8s]# kubectl apply -f ingress-controller-1.1.yaml
# 如果遇到如下错误,请尝试如下解决方案
# 1、检查物理机80和443端口有没有被占用!!!!!
[root@k8s-master1 k8s]# kubectl describe pod -n ingress-nginx ingress-nginx-controller-6hgb8
Normal Scheduled 9m9s default-scheduler Successfully assigned ingress-nginx/ingress-nginx-controller-6hgb8 to k8s-node1
Warning FailedMount 9m8s (x3 over 9m10s) kubelet MountVolume.SetUp failed for volume "webhook-cert" : secret "ingress-nginx-admission" not found
Normal Pulled 8m16s (x4 over 9m6s) kubelet Container image "registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.1.0" already present on machine
# 2、检查secret名字
[root@k8s-master1 k8s]# kubectl get secret -n ingress-nginx
NAME TYPE DATA AGE
default-token-n2p5l kubernetes.io/service-account-token 3 3m37s
ingress-nginx-admission-token-qfhmj kubernetes.io/service-account-token 3 3m37s
ingress-nginx-token-kr86s kubernetes.io/service-account-token 3 3m37s
[root@k8s-master1 k8s]# vim ingress-controller-1.1.yaml
secret:
#secretName: ingress-nginx-admission #(原来secret的名字)
secretName: ingress-nginx-admission-token-qfhmj #(将ingress-nginx-admission替换成使用的名字,在重新部署)
[root@k8s-master1 k8s]# kubectl get pod -n ingress-nginx
NAME READY STATUS RESTARTS AGE
ingress-nginx-admission-create-dwm6c 0/1 Completed 0 39m
ingress-nginx-admission-patch-nf5sp 0/1 Completed 1 39m
ingress-nginx-controller-6hgb8 1/1 Running 7 (33m ago) 39m
ingress-nginx-controller-7mxrq 1/1 Running 0 39m
ingress-nginx-controller-8q9cq 1/1 Running 0 39m
5、ingress-nginx的使用
[root@k8s-master1 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
gateway NodePort 10.0.0.193 <none> 80:32457/TCP 15d
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 33d
nginx-service NodePort 10.0.0.50 <none> 88:31153/TCP 38m
[root@k8s-master1 test]# vim ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress
annotations:
spec:
ingressClassName: nginx # 是基于nginx实现的,所以是nginx控制器。不同控制器标识不一样,需要去官网查询
rules:
- host: www.dada.com # 域名
http:
paths:
- path: / # 路径
pathType: Prefix
backend:
service:
name: nginx-service # 需要将哪一组应用给暴露出去
port:
number: 80
[root@k8s-master1 ~]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress nginx www.dada.com 192.168.211.131,192.168.211.132,192.168.211.133 80 24m
6、浏览器访问域名
至此,ingress高可用部署和使用到此以结束。