十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
這篇文章主要介紹“CentOS7.2如何基于Kubernetes部署簡(jiǎn)單的應(yīng)用”,在日常操作中,相信很多人在CentOS7.2如何基于Kubernetes部署簡(jiǎn)單的應(yīng)用問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”CentOS7.2如何基于Kubernetes部署簡(jiǎn)單的應(yīng)用”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到牟定網(wǎng)站設(shè)計(jì)與牟定網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋牟定地區(qū)。
以下面的圖來(lái)安裝一個(gè)簡(jiǎn)單的靜態(tài)內(nèi)容的nginx應(yīng)用:
首先,我們用復(fù)制器啟動(dòng)一個(gè)2個(gè)備份的nginx Pod。然后在前面掛Service,一個(gè)service只能被集群內(nèi)部訪問(wèn),一個(gè)能被集群外的節(jié)點(diǎn)訪問(wèn)。
1. 部署nginx pod 和復(fù)制器
#cat nginx-rc.yaml apiVersion: v1 kind: ReplicationController metadata: name: nginx-controller spec: replicas: 2 selector: name: nginx template: metadata: labels: name: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80
我們定義了一個(gè)nginx pod復(fù)制器,復(fù)制份數(shù)為2,我們使用nginx docker鏡像。
執(zhí)行下面的操作創(chuàng)建nginx pod復(fù)制器:
[root@master test]# kubectl create -f nginx-rc.yaml replicationcontrollers/nginx-controller
記得先去下載gcr.io鏡像,然后改名,否則會(huì)提示失敗。由于還會(huì)下載nginx鏡像,所以所創(chuàng)建的Pod需要等待一些時(shí)間才能處于running狀態(tài)。
[root@master test]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx 1/1 Running 0 1d nginx-controller-dkl3v 1/1 Running 0 14s nginx-controller-hxcq8 1/1 Running 0 14s
我們可以使用describe 命令查看pod的相關(guān)信息:
[root@master test]# kubectl describe pod nginx-controller-dkl3v Name: nginx-controller-dkl3v Namespace: default Image(s): nginx Node: 192.168.32.17/192.168.32.17 Labels: name=nginx Status: Running Reason: Message: IP: 172.17.67.2 Replication Controllers: nginx-controller (2/2 replicas created) Containers: nginx: Image: nginx State: Running Started: Wed, 30 Dec 2015 02:03:19 -0500 Ready: True Restart Count: 0 Conditions: Type Status Ready True Events: FirstSeen LastSeen Count From SubobjectPath Reason Message Wed, 30 Dec 2015 02:03:14 -0500 Wed, 30 Dec 2015 02:03:14 -0500 1 {scheduler } scheduled Successfully assigned nginx-controller-dkl3v to 192.168.32.17 Wed, 30 Dec 2015 02:03:15 -0500 Wed, 30 Dec 2015 02:03:15 -0500 1 {kubelet 192.168.32.17} implicitly required container POD pulled Pod container image "kubernetes/pause" already present on machine Wed, 30 Dec 2015 02:03:16 -0500 Wed, 30 Dec 2015 02:03:16 -0500 1 {kubelet 192.168.32.17} implicitly required container POD created Created with docker id e88dffe46a28 Wed, 30 Dec 2015 02:03:17 -0500 Wed, 30 Dec 2015 02:03:17 -0500 1 {kubelet 192.168.32.17} implicitly required container POD started Started with docker id e88dffe46a28 Wed, 30 Dec 2015 02:03:18 -0500 Wed, 30 Dec 2015 02:03:18 -0500 1 {kubelet 192.168.32.17} spec.containers{nginx} created Created with docker id 25fcb6b4ce09 Wed, 30 Dec 2015 02:03:19 -0500 Wed, 30 Dec 2015 02:03:19 -0500 1 {kubelet 192.168.32.17} spec.containers{nginx} started Started with docker id 25fcb6b4ce09
2. 部署節(jié)點(diǎn)內(nèi)部可訪問(wèn)的nginx service
Service的type有ClusterIP和NodePort之分,缺省是ClusterIP,這種類型的Service只能在集群內(nèi)部訪問(wèn)。配置文件如下:
#cat nginx-service-clusterip.yaml apiVersion: v1 kind: Service metadata: name: nginx-service-clusterip spec: ports: - port: 8001 targetPort: 80 protocol: TCP selector: name: nginx
執(zhí)行下面的命令創(chuàng)建service:
[root@master test]# kubectl create -f ./nginx-service-clusterip.yaml services/nginx-service-clusterip
查看所創(chuàng)建的service:
[root@master test]# kubectl get service NAME LABELS SELECTOR IP(S) PORT(S) kubernetes component=apiserver,provider=kubernetes10.254.0.1 443/TCP nginx-service-clusterip name=nginx 10.254.234.255 8001/TCP
上面的輸出告訴我們這個(gè) Service的Cluster IP是10.254.234.255,端口是8001。下面我們驗(yàn)證這個(gè)PortalNet IP的工作情況:
在192.168.32.16上執(zhí)行以下命令:
[root@minion1 ~]# curl -s 10.254.234.255:8001Welcome to nginx! Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.
從前面部署復(fù)制器的部分我們知道nginx Pod運(yùn)行在17節(jié)點(diǎn)上。上面我們特意從16代理節(jié)點(diǎn)上訪問(wèn)我們的服務(wù)來(lái)體現(xiàn)Service Cluster IP在所有集群代理節(jié)點(diǎn)的可到達(dá)性。
3. 部署外部可訪問(wèn)的nginx service
下面我們創(chuàng)建NodePort類型的Service,這種類型的Service在集群外部是可以訪問(wèn)。配置文件如下:
cat nginx-service-nodeport.yaml apiVersion: v1 kind: Service metadata: name: nginx-service-nodeport spec: ports: - port: 8000 targetPort: 80 protocol: TCP type: NodePort selector: name: nginx
執(zhí)行如下命令創(chuàng)建service并進(jìn)行查看:
[root@master test]# kubectl create -f ./nginx-service-nodeport.yaml You have exposed your service on an external port on all nodes in your cluster. If you want to expose this service to the external internet, you may need to set up firewall rules for the service port(s) (tcp:31000) to serve traffic. See http://releases.k8s.io/HEAD/docs/user-guide/services-firewalls.md for more details. services/nginx-service-nodeport [root@master test]# kubectl get service NAME LABELS SELECTOR IP(S) PORT(S) kubernetes component=apiserver,provider=kubernetes10.254.0.1 443/TCP nginx-service-clusterip name=nginx 10.254.234.255 8001/TCP nginx-service-nodeport name=nginx 10.254.210.68 8000/TCP
創(chuàng)建service時(shí)提示需要設(shè)置firewall rules,不用去管,不影響后續(xù)操作。
查看創(chuàng)建的service:
[root@master test]# kubectl describe service nginx-service-nodeport Name: nginx-service-nodeport Namespace: default Labels:Selector: name=nginx Type: NodePort IP: 10.254.210.68 Port: 8000/TCP NodePort: 31000/TCP Endpoints: 172.17.67.2:80,172.17.67.3:80 Session Affinity: None No events.
這個(gè) Service的節(jié)點(diǎn)級(jí)別端口是31000。下面我們驗(yàn)證這個(gè) Service的工作情況:
[root@master test]# curl -s 192.168.32.16:31000Welcome to nginx! Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.
[root@master test]# curl -s 192.168.32.17:31000Welcome to nginx! Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.
不管是從16還是17,都能訪問(wèn)到我們的服務(wù)。
4. 總結(jié)
本文只是一個(gè)簡(jiǎn)單的應(yīng)用,在應(yīng)用部署時(shí),了解Kubernetes的應(yīng)用模型是非常重要的。還需要對(duì)Kubernetes進(jìn)行更深入的研究。
到此,關(guān)于“CentOS7.2如何基于Kubernetes部署簡(jiǎn)單的應(yīng)用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!