在 centos7 搭建 kubernetes1.16.7 集群 I 中,环境搭建进度如下
k8s-master :192.168.1.127
k8s-node1 :192.168.1.122
k8s-node2 :192.168.1.132
此次将搭建 ingress ,并用 mediawiki 做测试。
拉取 mediawiki
# 每个节点都要拉取
docker pull mufengziyu/mediawiki:1.18.0
下载 Ingress 相关 yaml
# 下载创建 Ingress 用到的相关 yaml 文件
ingress-nginx for file in namespace.yaml configmap.yaml rbac.yaml tcp-services-configmap.yaml with-rbac.yaml udp-services-configmap.yaml default-backend.yaml; do wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.17.0/deploy/$file; done
下载的 yaml 做相应修改
修改 yaml
# 修改 with-rbac.yaml # 如下,在serviceAccountName上方添加hostNetwork: true: spec: hostNetwork: true serviceAccountName: nginx-ingress-serviceaccount containers: - name: nginx-ingress-controller image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.14.0 args: - /nginx-ingress-controller - --default-backend-service=$(POD_NAMESPACE)/default-http-backend - --configmap=$(POD_NAMESPACE)/nginx-configuration - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services - --udp-services-configmap=$(POD_NAMESPACE)/udp-services - --annotations-prefix=nginx.ingress.kubernetes.io
#修改 with-rbac.yaml # 在其env部分加入如下环境变量 env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace - name: KUBERNETES_MASTER value: http://192.168.56.101:8080
创建 Ingress-controller
ingress-nginx for file in namespace.yaml configmap.yaml rbac.yaml tcp-services-configmap.yaml with-rbac.yaml udp-services-configmap.yaml default-backend.yaml; do kubectl apply -f $file; done
创建后查看 pod
[root@k8s-node1 ~]# kubectl get pod -n ingress-nginx -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES default-http-backend-7b7fd455bd-ptsrt 1/1 Running 3 39h 10.244.1.7 k8s-node1 <none> <none> nginx-ingress-controller-657bbdc77-l4jps 1/1 Running 5 39h 192.168.1.122 k8s-node1 <none> <none>
创建自定义 Ingress
# 创建 deployment , service
[root@k8s-master ~]# vi yaml/media.yaml [root@k8s-master ~]# cat yaml/media.yaml apiVersion: apps/v1 kind: Deployment metadata: labels: app: mediawiki-pod name: mediawiki-pod spec: replicas: 1 selector: matchLabels: app: mediawiki-pod template: metadata: labels: app: mediawiki-pod spec: containers: - image: mufengziyu/mediawiki:1.18.0 name: mediawiki-pod ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: mediawiki-pod-service labels: app: mediawiki-pod # 对应pod得label spec: type: NodePort # 类型ExternalName, ClusterIP, NodePort, and LoadBalancer ports: - port: 80 targetPort: 80 nodePort: 30002 # 端口范围【30000-32767】 selector: app: mediawiki-pod
[root@k8s-master ~] kubectl apply -f media.yaml
# 创建 ingress # host :ingress-controller 所在节点的域名,不能是 ip # 建立一个本地DNS服务即可 # serviceName 和 servicePort 分别对应 service 的 Name 和 port [root@k8s-master ~]# vi yaml/media-ingress.yaml [root@k8s-master ~]# cat yaml/media-ingress.yaml apiVersion: extensions/v1beta1 kind: Ingress metadata: name: mediawiki-ingress namespace: default spec: rules: - host: k8s-node1.com http: paths: - path: / backend: serviceName: mediawiki-pod-service servicePort: 80 [root@k8s-master ~]# kubectl apply -f media-ingress.yaml
# 修改 本地 dsn 服务
# 打开 /etc/host,插入 ip 域名 映射 # 我添加的如下 192.168.1.122 k8s-node1.com 192.168.1.127 k8s-master.com 192.168.1.132 k8s-node2.com
查看结果
[root@k8s-master ~]# kubectl get pod --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE default mediawiki-pod-74c76684d-z9ss5 1/1 Running 1 16h ingress-nginx default-http-backend-7b7fd455bd-ptsrt 1/1 Running 3 39h ingress-nginx nginx-ingress-controller-657bbdc77-l4jps 1/1 Running 5 39h kube-system coredns-58cc8c89f4-955zb 1/1 Running 12 2d19h kube-system coredns-58cc8c89f4-bp746 1/1 Running 12 2d19h kube-system etcd-k8s-master 1/1 Running 5 2d19h kube-system kube-apiserver-k8s-master 1/1 Running 5 2d19h kube-system kube-controller-manager-k8s-master 1/1 Running 5 2d19h kube-system kube-flannel-ds-amd64-5f28r 1/1 Running 3 41h kube-system kube-flannel-ds-amd64-9s6v2 1/1 Running 4 41h kube-system kube-flannel-ds-amd64-fvrmj 1/1 Running 5 2d19h kube-system kube-proxy-cddqh 1/1 Running 4 41h kube-system kube-proxy-d8sxd 1/1 Running 5 2d19h kube-system kube-proxy-gfvhf 1/1 Running 3 41h kube-system kube-scheduler-k8s-master 1/1 Running 5 2d19h [root@k8s-master ~]# kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE mediawiki-pod 1/1 1 1 16h [root@k8s-master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d19h mediawiki-pod-service NodePort 10.98.86.108 <none> 80:30002/TCP 16h [root@k8s-master ~]# kubectl get ingress NAME HOSTS ADDRESS PORTS AGE mediawiki-ingress k8s-node1.com 80 15h
# 访问 服务 # 未添加数据库,所以显示 这个页面 [root@k8s-master ~]# curl k8s-node1.com <h1>Sorry! This site is experiencing technical difficulties.</h1><p>Try waiting a few minutes and reloading.</p><p><small>(Can't contact the database server: <span dir="ltr">No route to host (192.168.1.200)</span>)</small></p><hr /><div style="margin: 1.5em">You can try searching via Google in the meantime.<br /> <small>Note that their indexes of our content may be out of date.</small></div> <!-- SiteSearch Google --> <form method="get" action="http://www.google.com/search" id="googlesearch"> <input type="hidden" name="domains" value="" /> <input type="hidden" name="num" value="50" /> <input type="hidden" name="ie" value="UTF-8" /> <input type="hidden" name="oe" value="UTF-8" /> <input type="text" name="q" size="31" maxlength="255" value="" /> <input type="submit" name="btnG" value="Search" /> <div> <input type="radio" name="sitesearch" id="gwiki" value="" checked="checked" /><label for="gwiki">test</label> <input type="radio" name="sitesearch" id="gWWW" value="" /><label for="gWWW">WWW</label> </div> </form>
若用windows访问,修改 C:\Windows\System32\drivers\etc\hosts 文件,加入相应域名 ip 映射
测试结果