我們學習語言第一個想法就是在環境搭建好了之后寫一個Helloworld,我們學習如何在 k8s 上部署應用時,部署一個簡單的?nginx,能夠訪問到它的配置頁面。由于它五臟俱全,功能簡單,無狀態,可以當做 k8s 部署應用的?hello, world:
實驗目的:
利用ingress-nginx暴露服務供外網訪問,版本:0.30.0
實驗步驟:
1,下載ingress-nginx安裝配置文件
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml -O ingress-nginx.yaml
2,在ingress-nginx.yaml文件最后增加service的安裝配置
---
apiVersion: v1
kind: Service
metadata:
??? name: ingress-nginx
??? namespace: ingress-nginx
??? labels:
??????? app.kubernetes.io/name: ingress-nginx
??????? app.kubernetes.io/part-of: ingress-nginx
spec:
??? type: NodePort
??? ports:
??????? - name: http
????????? port: 80
????????? targetPort: 80
??? ??? ? nodePort: 30010
??? ??? ? protocol: TCP
??????? - name: https
??? ????? port: 443
??? ????? targetPort: 443
??? ????? nodePort: 30011
??? ????? protocol: TCP
selector:
??? app.kubernetes.io/name: ingress-nginx
??? app.kubernetes.io/part-of: ingress-nginx
3,安裝ingress-nginx,安裝了namespace,nginx-ingress-controller,ingress-nginx
kubectl apply -f ingress-nginx.yaml
輸出:
namespace/ingress-nginx created
configmap/nginx-configuration created
configmap/tcp-services created
configmap/udp-services created
serviceaccount/nginx-ingress-serviceaccount created
Warning: rbac.authorization.k8s.io/v1beta1 ClusterRole is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRole
clusterrole.rbac.authorization.k8s.io/nginx-ingress-clusterrole created
Warning: rbac.authorization.k8s.io/v1beta1 Role is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 Rolerole.rbac.authorization.k8s.io/nginx-ingress-role created
Warning: rbac.authorization.k8s.io/v1beta1 RoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 RoleBindingrolebinding.rbac.authorization.k8s.io/nginx-ingress-role-nisa-binding created
Warning: rbac.authorization.k8s.io/v1beta1 ClusterRoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRoleBindingclusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-clusterrole-nisa-binding createddeployment.apps/nginx-ingress-controller created
limitrange/ingress-nginx created
service/ingress-nginx created
4,查看安裝結果:
???? 1,namespace是k8s的命名空間,旨在對一組資源和對象創建一個抽象空間集合,比如可以用來將系統內部的對象劃分為不同的項目組或用戶組。常見的 pod, service, replication controller 和 deployment 等都是屬于某一個 namespace 的(默認是 default);
???? 2,pod是一組緊密關聯的容器集合,Pod就是共享IPC、Network和UTS namespace,是K8S調度的基本單位。Pod 的設計理念是支持多個容器在一個 Pod 中共享網絡和文件系統,可以通過進程間通信和文件共享這種簡單高效的方式組合完成服務。這個容器就是ingress-nginx的控制器;
???? 3,service是對一組提供相同功能的 Pods 的抽象,并為它們提供一個統一的入口。借助 Service,應用可以方便的實現服務發現與負載均衡,并實現應用的零宕機升級。Service 通過標簽來選取服務后端,一般配合 Replication Controller 或者 Deployment 來保證后端容器的正常運行。這些匹配標簽的 Pod IP 和端口列表組成 endpoints,由 kube-proxy 負責將服務 IP 負載均衡到這些 endpoints 上。Service有四種類型(ClusterIP,NodePort,LoadBalancer,ExternalName)為對外暴露的接口,說明了集群的機器均對外暴露30010和30011兩個接口;
//查看namespace
kubectl get namespaces
NAME STATUS AGE
default Active 7d18h
ingress-nginx Active 22s
kube-node-lease Active 7d18h
kube-public Active 7d18h
kube-system Active 7d18h
kubernetes-dashboard Active 41h//查看pod
kubectl get pods -n ingress-nginx
NAME READY STATUS RESTARTS AGE
nginx-ingress-controller-54b86f8f7b-lghw4 1/1 Running 0 66s//查看service
kubectl get services -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx NodePort 10.1.64.186 <none> 80:30010/TCP,443:30011/TCP 75s
5,安裝nginx pod并暴露80端口:
//nginx.yaml
apiVersion: v1
kind: Pod
metadata:
???? name: nginx
???? # 指定 label,便于檢索
???? labels:
??????? app: nginx
spec:
??? containers:
??? - name: nginx
??? # 指定鏡像
??? image: nginx:alpine
??? # 指定暴露端口
??? ports:
??? - containerPort: 80
//使用kubectl apply,部署 Pod
kubectl apply -f nginx.yaml
pod/nginx created
//校驗部署狀態,此時 STATUS 為?Running?表明部署成功
kubectl get pods nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 43h 10.244.3.4 pcno4 <none> <none>
//獲取更加詳細的信息
kubectl describe pod nginx
Name: nginx
Namespace: default
Priority: 0
Node: pcno4/10.10.30.74
Start Time: Wed, 06 Jan 2021 15:16:28 +0800
Labels: app=nginx
Annotations: <none>
Status: Running
IP: 10.244.3.4
IPs:
??? IP: 10.244.3.4
Containers:
??? nginx:
??????? Container ID: docker://fa7b8ef84d0c2cb9bc3c7bc1bab7e94912fecada7f901f762da3898a8bcf65cc
??????? Image: nginx:alpine
??????? Image ID: docker-pullable://nginx@sha256:c2ce58e024275728b00a554ac25628af25c54782865b3487b11c21cafb7fabda
??????? Port: 80/TCP
??????? Host Port: 0/TCP
??????? State: Running
?????????? Started: Wed, 06 Jan 2021 15:16:47 +0800
??????? Ready: True
??????? Restart Count: 0
??????? Environment: <none>
??????? Mounts:
???????????? /var/run/secrets/kubernetes.io/serviceaccount from default-token-k9grl (ro)
Conditions:
??? Type Status
??? Initialized True
??? Ready True
??? ContainersReady True
??? PodScheduled True
Volumes:
??? default-token-k9grl:
??? ??? Type: Secret (a volume populated by a Secret)
??? ??? SecretName: default-token-k9grl
??? ??? Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations:
??? node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
??? node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>
每個 pod 都有一個IP地址,直接訪問IP地址獲取內容
curl 10.244.3.4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
??? body {
??? ??? width: 35em;
??? ??? margin: 0 auto;
??? ??? font-family: Tahoma, Verdana, Arial, sans-serif;
??? }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
6,此時我們可以使用?kubectl exec?進入?Pod?的內部容器。如果?Pod?中有多個容器,使用?kubectl exec -c?指定容器
kubectl exec -it nginx sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead./ # vim /usr/share/nginx/html/index.html
//修改index.html文件改成自己希望加入的內容,然后退出重新使用curl命令查看
curl 10.244.3.4
#可以看到修改過的頁面
//進入pod還可以查看網絡情況,如下:
netstat -tan
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTENtcp 0 0 :::80 :::* LISTEN
wget -q -O - localhost
#可以看到頁面文件內容
7,部署nginx至集群,在k8s中管理?Pod?的稱作?Controller,我們可以使用?Deployment?這種?Controller?來為?Pod?進行擴容,當然它還可以滾動升級,回滾等等關于部署的事情,我們編寫一個Deployment的資源配置文件
//nginxdelop.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
???? name: nginx-deployment
spec:
???? #定位需要管理的Pod
???? selector:?
???????? matchLabels:
????????????? app: nginx
???? #需要部署的個數
???? replicas: 3
???? #指定要部署的Pod
???? template:
??????? metadata:
???????????? labels:
??????????????? app: nginx
??????? spec:
??????????? containers:
??????????? - name: nginx
????????????? image: nginx:alpine
????????????? ports:
????????????? - containerPort: 80
//kubectl apply?部署生效后查看?Pod?以及?Deployment?狀態
kubectl get pods -o wide -l 'app=nginx'
//nginx-deployment 部署的三個 pod 全部成功
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 46h 10.244.3.4 pcno4 <none> <none>
nginx-deployment-7fb7fd49b4-b6tcw 1/1 Running 0 46h 10.244.3.5 pcno4 <none> <none>
nginx-deployment-7fb7fd49b4-jxdx2 1/1 Running 0 46h 10.244.1.4 pcno5 <none> <none>
nginx-deployment-7fb7fd49b4-spxgr 1/1 Running 0 46h 10.244.1.5 pcno5 <none> <none>
//READY 3/3 表明全部部署成功
kubectl get deploy nginx-deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 46h
8,現在部署的這個nginx[pod],對應第二步里建立的ingress-nginx的service,這樣就可以通過下面的渠道被訪問了:
集群對外的ip(機器ip:10.10.30.76:80)--> ingress(ingress-nginx NodePort ClusterIP: 10.1.64.186:30010/30011) --> nginx-service(CluserIP:10.1.169.124:80) --> nginx-deployment(IP:10.244.1.4/1.5/3.5/
10.10.30.76:30010 --|???????????????????????????????? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?????????????????????????????????????????? ? ? ? |-> 10.244.1.4:80
??????????????????????? [serive:NodePort:ingress-nginx] ? [pod:nginx-ingress-controller]??? [service:nginx-service]
????????????????????????????????? |????? 10.1.64.186 ????? |??? --> ? |???? 10.244.1.7:80 ?? |???? -->??? |????? 10.1.169.124:80 ??? |-> 10.244.1.5:80
10.10.30.75:30010 --|????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? |-> 10.244.3.4:80
10.10.30.74:30010 --|????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? |-> 10.244.3.5:80
9,最后可以通過集群外機器直接用ip訪問(如果進入不同nginxpod機器修改不同的nginx主頁,然后多次訪問就能看出集群的負載均衡在自動調用不同的node處理)如下:
//編輯所有的pods,將內部部署的網頁全部添加node和ip相關的信息
kubectl exec -it nginx sh? #10.244.3.4 pcno4 根據第7步kubectl get pods -o wide -l 'app=nginx'命令的結果添加node和ip信息
kubectl exec -it nginx-deployment-7fb7fd49b4-jxdx2 sh???? #10.244.3.5 pcno4
kubectl exec -it nginx-deployment-7fb7fd49b4-b6tcw sh??? #10.244.1.4 pcno5
kubectl exec -it nginx-deployment-7fb7fd49b4-spxgr sh???? #10.244.1.5 pcno5//在外網訪問,可以看到四個不同的node的信息
curl http://10.10.30.76(75|74):30010?