創建 httpserver 服務
創建 Service 在集群內訪問 httpserver
apiVersion: v1
kind: Service
metadata:
name: httpserver-svc
namespace: mxs
spec:
type: ClusterIP
selector:
app: httpserver
ports:
- protocol: TCP
port: 8088
targetPort: 8080
通過 service 的 cluster ip 訪問 httpserver 服務
image.png
安裝 ingress-nginx
安裝 helm
wget https://get.helm.sh/helm-v3.8.0-linux-amd64.tar.gz
tar -zxvf helm-v3.8.0-linux-amd64.tar.gz
mv linux-amd64/helm /usr/local/bin/helm
helm version
使用 helm 安裝 ingress-nginx
helm repo add nginx-stable https://helm.nginx.com/stable
helm upgrade --install ingress-nginx ingress-nginx \
--repo https://kubernetes.github.io/ingress-nginx \
--namespace ingress-nginx --create-namespace
使用 yaml 安裝 ingress-nginx
如果 helm 安裝超時,可以使用 yaml 直接安裝
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.1/deploy/static/provider/cloud/deploy.yaml
image.png
安裝完 ingress-nginx 之后 EXTERNAL-IP 處于 pending 狀態,因為 svc 是 LoadBalancer 類型,需要安裝 metallb 進行支持
安裝 metallb
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.12.1/manifests/namespace.yaml
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.12.1/manifests/metallb.yaml
kubectl apply -f - <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
namespace: metallb-system
name: config
data:
config: |
address-pools:
- name: default
protocol: layer2
addresses:
- 192.168.56.120-192.168.56.200
EOF
注意 192.168.56.120-192.168.56.200 這個 ip 段要跟集群的 node 在同一個 ip 段內,但是不能包含集群 node 的 ip,否則可能導致集群 node 無法 ping 和 curl 通,我集群 node 的 ip 是 192.168.56.100 和 192.168.56.101 這兩個
安裝完成后 svc 的 EXTERNAL-IP 就可以了
image.png
安裝 coredns
下載 deploy.sh 和 coredns.yaml.sed
chmod +x deploy.sh
./deploy.sh > coredns.yaml
kubectl apply -f coredns.yaml
創建 Ingress 使用 http 和 域名訪問 httpserver 服務
將以下內容保存為 http-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: http-ingress
namespace: mxs
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: www.mxstest.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: httpserver-svc
port:
number: 8088
創建 ingress
k apply -f http-ingress.yaml
修改 /etc/hosts ,增加一行 " 192.168.56.120 www.mxstest.com "
訪問域名
image.png
支持 https 訪問
生成 https 證書
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=mxstest.com/O=mxstest" -addext "subjectAltName = DNS:mxstest.com"
創建 secret
kubectl create secret tls mxs-tls --cert=./tls.crt --key=./tls.key
k get secret
把以下文件保存為 https-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: https-ingress
namespace: mxs
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
tls:
- hosts:
- www.mxstest.com
secretName: mxs-tls
rules:
- host: www.mxstest.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: httpserver-svc
port:
number: 8088
創建 https-ingress :
需要先刪除 http-ingress,因為域名和路徑與 https-ingress 沖突了
k delete ingress http-ingress
k apply -f https-ingress.yaml
用 https 訪問 httpserver
curl -v -k https://www.mxstest.com
image.png