下载Ingress-Nginx-0.30.0
docker pull registry.cn-beijing.aliyuncs.com/google_registry/nginx-ingress-controller:0.30.0
docker tag 89ccad40ce8e quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0
docker rmi registry.cn-beijing.aliyuncs.com/google_registry/nginx-ingress-controller:0.30.0
创建文件夹下载nginx-ingress包
mkdir ingress-nginx-0.30
cd ingress-nginx-0.30
wget https://github.com/kubernetes/ingress-nginx/archive/nginx-0.30.0.tar.gz
tar xf nginx-0.30.0.tar.gz
cp -a ingress-nginx-nginx-0.30.0/deploy/static/mandatory.yaml ./
vim mandatory.yaml
修改ingress的yaml:
#191行
apiVersion: apps/v1
kind: DaemonSet # 从Deployment改为DaemonSet
metadata:
name: nginx-ingress-controller
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
spec:
#replicas: 1 # 注释
#218行
nodeSelector:
kubernetes.io/hostname: k8s-master # 修改处
# 如下几行为新加行 作用【允许在master节点运行】
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
#251行
ports:
- name: http
containerPort: 80
hostPort: 80 # 添加处【可在宿主机通过该端口访问Pod】
protocol: TCP
- name: https
containerPort: 443
hostPort: 443 # 添加处【可在宿主机通过该端口访问Pod】
protocol: TCP
kubectl apply -f mandatory.yaml
kubectl get pod -n ingress-nginx -o wide
创建第一个服务
vim deply_service1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy1
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: myapp
release: v1
template:
metadata:
labels:
app: myapp
release: v1
env: test
spec:
containers:
- name: myapp
image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: myapp-clusterip1
namespace: default
spec:
type: ClusterIP # 默认类型
selector:
app: myapp
release: v1
ports:
- name: http
port: 80
targetPort: 80
kubectl apply -f deply_service1.yaml
kubectl get deploy -o wide
kubectl get rs -o wide
kubectl get pod -o wide --show-labels
kubectl get svc -o wide
curl 10.244.2.***/hostname.html
创建第二个服务
vim deply_service2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy2
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: myapp
release: v2
template:
metadata:
labels:
app: myapp
release: v2
env: test
spec:
containers:
- name: myapp
image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v2
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: myapp-clusterip2
namespace: default
spec:
type: ClusterIP # 默认类型
selector:
app: myapp
release: v2
ports:
- name: http
port: 80
targetPort: 80
kubectl apply -f deply_service2.yaml
kubectl get pod -o wide --show-labels -l "release=v2"
kubectl get svc -o wide
创建ingress外部访问yaml
vim ingress-http.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: nginx-http
namespace: default
spec:
rules:
- host: test.demo1.com
http:
paths:
- path: /
backend:
serviceName: myapp-clusterip1
servicePort: 80
- host: test.demo2.com
http:
paths:
- path: /
backend:
serviceName: myapp-clusterip2
servicePort: 80
kubectl apply -f ingress-http.yaml
kubectl get ingress -o wide
修改windos
C:\WINDOWS\System32\drivers\etc\hosts
(k8s-master的ip) test.demo1.com test.demo2.com
浏览器访问test.demo1.com
浏览器访问test.demo2.com