k8s 1.22.10 Ingress-nginx 的部署
1. 创建ingress
GitHub:ingress-nginx/index.md at controller-v1.1.3 · kubernetes/ingress-nginx (github.com)
官网:Installation Guide - NGINX Ingress Controller (kubernetes.github.io)
mkdir -p /root/ingress-install && cd /root/ingress-install
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.3/deploy/static/provider/baremetal/deploy.yaml
cat deploy.yaml | grep image:
image: k8s.gcr.io/ingress-nginx/controller:v1.1.3@sha256:31f47c1e202b39fadecf822a9b76370bd4baed199a005b3e7d4d1455f4fd3fe2
image: k8s.gcr.io/ingress-nginx/kube-webhook-certgen:v1.1.1@sha256:64d8c73dca984af206adf9d6d7e46aa550362b1d7a01f3a0a91b20cc67868660
image: k8s.gcr.io/ingress-nginx/kube-webhook-certgen:v1.1.1@sha256:64d8c73dca984af206adf9d6d7e46aa550362b1d7a01f3a0a91b20cc67868660
# 替换镜像
sed -i 's#k8s.gcr.io/ingress-nginx/controller:v1.1.3@sha256:31f47c1e202b39fadecf822a9b76370bd4baed199a005b3e7d4d1455f4fd3fe2#registry.cn-hangzhou.aliyuncs.com/imges/controller:v1.1.3#' deploy.yaml
sed -i 's#k8s.gcr.io/ingress-nginx/kube-webhook-certgen:v1.1.1@sha256:64d8c73dca984af206adf9d6d7e46aa550362b1d7a01f3a0a91b20cc67868660#registry.cn-hangzhou.aliyuncs.com/chenby/kube-webhook-certgen:v1.1.1#' deploy.yaml
# 起pod
kubectl apply -f .
POD_NAMESPACE=ingress-nginx
POD_NAME=$(kubectl get pods -n $POD_NAMESPACE -l app.kubernetes.io/name=ingress-nginx --field-selector=status.phase=Running -o name)
kubectl exec $POD_NAME -n $POD_NAMESPACE -- /nginx-ingress-controller --version
kubectl get pod -n ingress-nginx
NAME READY STATUS RESTARTS AGE
ingress-nginx-admission-create-shff4 0/1 Completed 0 22m
ingress-nginx-admission-patch-chhcw 0/1 Completed 0 22m
ingress-nginx-controller-78cc7f865b-pd4xw 1/1 Running 0 22m
注:镜像已经从外网上传到自己在阿里云的镜像中心。
2. 启用后端,写入配置文件执行
cat > backend.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: default-http-backend
labels:
app.kubernetes.io/name: default-http-backend
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: default-http-backend
template:
metadata:
labels:
app.kubernetes.io/name: default-http-backend
spec:
terminationGracePeriodSeconds: 60
containers:
- name: default-http-backend
image: registry.cn-hangzhou.aliyuncs.com/imges/defaultbackend-amd64:1.5
livenessProbe:
httpGet:
path: /healthz
port: 8080
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 5
ports:
- containerPort: 8080
resources:
limits:
cpu: 10m
memory: 20Mi
requests:
cpu: 10m
memory: 20Mi
---
apiVersion: v1
kind: Service
metadata:
name: default-http-backend
namespace: kube-system
labels:
app.kubernetes.io/name: default-http-backend
spec:
ports:
- port: 80
targetPort: 8080
selector:
app.kubernetes.io/name: default-http-backend
EOF
mkdir -p /root/ingress-install/test && cd /root/ingress-install/test
# Deployment
cat > mynginx-Template.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
spec:
replicas: 3
selector:
matchLabels:
app: mynginx
release: nginxapp
template:
metadata:
labels:
app: mynginx
release: nginxapp
env: test
spec:
containers:
- name: nginx
image: nginx
EOF
# svc
cat > mynginx-svc.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
selector:
app: mynginx
ports:
- port: 80
targetPort: 80
protocol: TCP
EOF
# Ingress
cat > mynginx-Ingress.yaml <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-test
namespace: default
spec:
ingressClassName: nginx # 使用 nginx 的 IngressClass(关联的 ingress-nginx 控制器)
rules:
- host: www.mynginx.com # 将域名映射到 my-nginx 服务
http:
paths:
- path: /
pathType: Prefix
backend:
service: # 将所有请求发送到 my-nginx 服务的 80 端口
name: nginx-svc
port:
number: 80
EOF
访问:
kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.104.240.22 <none> 80:30037/TCP,443:32306/TCP 35m
ingress-nginx-controller-admission ClusterIP 10.105.87.198 <none> 443/TCP 35m
修改Windows本地文件C:\Windows\System32\drivers\etc\hosts
参考: