摘要:所以,我們使用或訪問(wèn)是不合適的,我們需要提供一個(gè)固定及端口。創(chuàng)建一個(gè)用法創(chuàng)建現(xiàn)在,我們就可以在節(jié)點(diǎn)上通過(guò)訪問(wèn)。我們創(chuàng)建一個(gè)客戶端,來(lái)測(cè)試查看的解析,發(fā)現(xiàn)是。在瀏覽器訪問(wèn)發(fā)現(xiàn)是可以訪問(wèn)的未截圖使用任意的均可
創(chuàng)建一個(gè)deployment
[root@master ~]# kubectl run --help Create and run a particular image, possibly replicated. Creates a deployment or job to manage the created container(s). Usage: kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...] [options]
使用如下命令:
[root@master ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 --dry-run=true deployment.apps/nginx-deploy created (dry run) [root@master ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 deployment.apps/nginx-deploy created [root@master ~]# kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deploy 1 1 1 1 7s [root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE nginx-deploy-5b595999-nzlmj 1/1 Running 0 1m 10.244.2.14 node02
創(chuàng)建了一個(gè)名為 nginx-deploy 的deployment,副本設(shè)置為1, 查看pod,發(fā)現(xiàn)生成了一個(gè)nginx的pod,且該pod運(yùn)行在node02上,ip為 10.244.2.14 ,檢查node02的網(wǎng)絡(luò)
[root@node02 ~]# ip a|grep -A5 cni0 5: cni0:mtu 1450 qdisc noqueue state UP qlen 1000 link/ether 0a:58:0a:f4:02:01 brd ff:ff:ff:ff:ff:ff inet 10.244.2.1/24 scope global cni0 valid_lft forever preferred_lft forever inet6 fe80::980e:b1ff:fee7:a26f/64 scope link valid_lft forever preferred_lft forever 8: veth23ea238e@if3: mtu 1450 qdisc noqueue master cni0 state UP link/ether 32:26:f6:cb:a0:9f brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet6 fe80::3026:f6ff:fecb:a09f/64 scope link valid_lft forever preferred_lft forever
可以發(fā)現(xiàn),pod是跑在cni網(wǎng)橋上的。
Pod此時(shí)可以在節(jié)點(diǎn)端通過(guò)Pod ip訪問(wèn)。
當(dāng)我們刪除這個(gè)pod后,Deployment會(huì)重新創(chuàng)建一個(gè)pod,該pod的ip就會(huì)變化。
[root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE nginx-deploy-5b595999-nzlmj 1/1 Running 0 1m 10.244.2.14 node02[root@master ~]# kubectl delete pod nginx-deploy-5b595999-nzlmj pod "nginx-deploy-5b595999-nzlmj" deleted [root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE nginx-deploy-5b595999-z76bn 1/1 Running 0 4s 10.244.1.16 node01
所以,我們使用pod ip或pod hostname訪問(wèn)是不合適的,我們需要提供一個(gè)固定ip及端口。service可以滿足要求。
創(chuàng)建一個(gè)service用法
[root@master ~]# kubectl expose --help Expose a resource as a new Kubernetes service. Usage: kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP] [--target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type] [options]
創(chuàng)建service
[root@master ~]# kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP service/nginx exposed [root@master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1443/TCP 8h nginx ClusterIP 10.101.41.108 80/TCP 7s
現(xiàn)在,我們就可以在節(jié)點(diǎn)上通過(guò)ip 10.101.41.108 訪問(wèn)。在節(jié)點(diǎn)外部無(wú)法訪問(wèn)。
這類地址,更多時(shí)候是被pod客戶端訪問(wèn)的。
我們創(chuàng)建一個(gè)pod客戶端,來(lái)測(cè)試:
[root@master ~]# kubectl run client --image=busybox --replicas=1 -it --restart=Never If you don"t see a command prompt, try pressing enter. / # cat /etc/resolv.conf nameserver 10.96.0.10 search default.svc.cluster.local svc.cluster.local cluster.local options ndots:5 / #
查看pod的dns解析,發(fā)現(xiàn)nameserver是 10.96.0.10 。這個(gè)ip是coredns的service ip。
[root@master ~]# kubectl get svc -n kube-system -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR kube-dns ClusterIP 10.96.0.1053/UDP,53/TCP 9h k8s-app=kube-dns
此時(shí),在busybox這個(gè)pod里,我們可以直接通過(guò)service name nginx 訪問(wèn)之前創(chuàng)建的service,而不是service的ip:port。
# busybox里 / # wget nginx Connecting to nginx (10.101.41.108:80) index.html 100% |**************************************************************************************| 612 0:00:00 ETA / # wget -O - -q http://nginx:80Welcome to nginx!
注意: 10.101.41.108是nginx的service的ip。
此時(shí),我們?nèi)藶閷ginx的pod搞掛。
[root@master ~]# kubectl delete pod nginx-deploy-5b595999-z76bn pod "nginx-deploy-5b595999-z76bn" deleted [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE client 1/1 Running 0 8m nginx-deploy-5b595999-j6p8g 1/1 Running 0 4s
再次在busybox里面訪問(wèn)nginx
# busybox里 / # wget -O - -q http://nginx:80Welcome to nginx!
nginx依舊可以訪問(wèn)。
可以通過(guò)如下命令,獲取service后端的pod ip
[root@master ~]# kubectl describe service nginx Name: nginx Namespace: default Labels: run=nginx-deploy Annotations:動(dòng)態(tài)修改Selector: run=nginx-deploy Type: ClusterIP IP: 10.101.41.108 Port: 80/TCP TargetPort: 80/TCP Endpoints: 10.244.2.17:80 Session Affinity: None Events:
我們創(chuàng)建一個(gè) myapp 的deployment
[root@master ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2 deployment.apps/myapp created [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE client 1/1 Running 0 17m myapp-848b5b879b-4dz7h 1/1 Running 0 7s myapp-848b5b879b-wjktr 1/1 Running 0 7s nginx-deploy-5b595999-j6p8g 1/1 Running 0 8m [root@master ~]# kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE myapp 2 2 2 2 28s nginx-deploy 1 1 1 1 38m
創(chuàng)建一個(gè) myapp-svc 的service
[root@master ~]# kubectl expose deployment myapp --name=myapp-svc --port=80 service/myapp-svc exposed [root@master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1443/TCP 9h myapp-svc ClusterIP 10.105.155.237 80/TCP 7s nginx ClusterIP 10.101.41.108 80/TCP 28m
這時(shí)候,在busybox pod里可以通過(guò)myapp-svc來(lái)訪問(wèn)
# busybox pod里 # 10.244.1.17是pod的ip / # wget -O - -q 10.244.1.17 Hello MyApp | Version: v1 | Pod Name / # wget -O - -q myapp-svc Hello MyApp | Version: v1 | Pod Name
/ # while true; do wget -O - -q myapp-svc/hostname.html; sleep 1;done myapp-848b5b879b-wjktr myapp-848b5b879b-wjktr myapp-848b5b879b-4dz7h myapp-848b5b879b-wjktr myapp-848b5b879b-4dz7h myapp-848b5b879b-4dz7h
可以發(fā)現(xiàn),后端的pod會(huì)有兩個(gè)。
動(dòng)態(tài)修改將deployment myapp的副本修改為5個(gè)。
[root@master ~]# kubectl scale --replicas=5 deployment myapp deployment.extensions/myapp scaled [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE client 1/1 Running 0 24m myapp-848b5b879b-28crc 1/1 Running 0 6s myapp-848b5b879b-4dz7h 1/1 Running 0 7m myapp-848b5b879b-9dp4m 1/1 Running 0 6s myapp-848b5b879b-g4pzd 1/1 Running 0 6s myapp-848b5b879b-wjktr 1/1 Running 0 7m nginx-deploy-5b595999-j6p8g 1/1 Running 0 16m
此時(shí),在busybox pod里訪問(wèn):
/ # while true; do wget -O - -q myapp-svc/hostname.html; sleep 1;done myapp-848b5b879b-g4pzd # 1 myapp-848b5b879b-9dp4m # 2 myapp-848b5b879b-9dp4m myapp-848b5b879b-wjktr # 3 myapp-848b5b879b-28crc # 4 myapp-848b5b879b-4dz7h # 5 myapp-848b5b879b-28crc myapp-848b5b879b-4dz7h
縮減為3個(gè)
[root@master ~]# kubectl scale --replicas=3 deployment myapp deployment.extensions/myapp scaled [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE client 1/1 Running 0 26m myapp-848b5b879b-28crc 0/1 Terminating 0 2m myapp-848b5b879b-4dz7h 1/1 Running 0 9m myapp-848b5b879b-9dp4m 1/1 Running 0 2m myapp-848b5b879b-g4pzd 0/1 Terminating 0 2m myapp-848b5b879b-wjktr 1/1 Running 0 9m nginx-deploy-5b595999-j6p8g 1/1 Running 0 18m滾動(dòng)更新
現(xiàn)在更新pod的鏡像
## 鏡像的具體信息可以通過(guò)kubectl describe pod pod_name獲取 [root@master ~]# kubectset image deployment myapp myapp=ikubernetes/myapp:v2 deployment.extensions/myapp image updated [root@master ~]# kubectl rollout status deployment myapp Waiting for deployment "myapp" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "myapp" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "myapp" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "myapp" rollout to finish: 4 of 5 updated replicas are available... deployment "myapp" successfully rolled out
在busybox pod上觀察
Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v2 | Pod Name Hello MyApp | Version: v2 | Pod Name Hello MyApp | Version: v2 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v2 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v2 | Pod Name Hello MyApp | Version: v2 | Pod Name Hello MyApp | Version: v2 | Pod Name Hello MyApp | Version: v2 | Pod Name Hello MyApp | Version: v2 | Pod Name
可以發(fā)現(xiàn),會(huì)有一段時(shí)間v1和v2版本會(huì)共存,后續(xù)更新完畢后,所有pod版本都會(huì)變?yōu)関2版本。
回滾可以直接修改image
也可以使用undo
master節(jié)點(diǎn)上執(zhí)行undo
[root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE client 1/1 Running 0 34m myapp-74c94dcb8c-5hflp 1/1 Running 0 3m myapp-74c94dcb8c-6klpb 1/1 Running 0 3m myapp-74c94dcb8c-gsrbj 1/1 Running 0 3m myapp-74c94dcb8c-jfzzr 1/1 Running 0 3m myapp-74c94dcb8c-p55sz 1/1 Running 0 3m nginx-deploy-5b595999-j6p8g 1/1 Running 0 26m [root@master ~]# kubectl rollout undo deployment myapp deployment.extensions/myapp [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE client 1/1 Running 0 36m myapp-848b5b879b-5bvtk 1/1 Running 0 34s myapp-848b5b879b-8dh6r 1/1 Running 0 33s myapp-848b5b879b-dzsxq 1/1 Running 0 35s myapp-848b5b879b-gbrqg 1/1 Running 0 35s myapp-848b5b879b-vh4l9 1/1 Running 0 35s nginx-deploy-5b595999-j6p8g 1/1 Running 0 28m
可以發(fā)現(xiàn)myapp的pod全部改變了。去busybox pod中查看:
Hello MyApp | Version: v2 | Pod Name Hello MyApp | Version: v2 | Pod Name Hello MyApp | Version: v2 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name
自動(dòng)擴(kuò)縮容: 依賴資源使用狀態(tài)。
在集群外訪問(wèn)修改svc myapp-svc
kubectl edit svc myapp-svc # 將type: ClusterIP修改為 type: NodePort
之后,查看svc
[root@master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1443/TCP 9h myapp-svc NodePort 10.105.155.237 80:30724/TCP 24m nginx ClusterIP 10.101.41.108 80/TCP 53m
發(fā)現(xiàn)myapp-svc綁定了30724端口。
在windows瀏覽器訪問(wèn)url nodeIP:30724 發(fā)現(xiàn)是可以訪問(wèn)的(未截圖)
使用任意node的ip均可
[root@master ~]# while true; do wget -O - -q 192.168.18.128:30724; sleep 0.3;done Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name ^C [root@master ~]# while true; do wget -O - -q 192.168.18.129:30724; sleep 0.3;done Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name ^C [root@master ~]# while true; do wget -O - -q 192.168.18.130:30724; sleep 0.3;done Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name Hello MyApp | Version: v1 | Pod Name
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/32701.html
摘要:解決了密碼密鑰等敏感數(shù)據(jù)的配置問(wèn)題,使用可以避免把這些敏感數(shù)據(jù)以明文的形式暴露到鏡像或者中。可以以或者環(huán)境變量的方式使用。 Secret解決了密碼、token、密鑰等敏感數(shù)據(jù)的配置問(wèn)題,使用Secret可以避免把這些敏感數(shù)據(jù)以明文的形式暴露到鏡像或者Pod Spec中。 Secret可以以Volume或者環(huán)境變量的方式使用。 使用如下命令行創(chuàng)建一個(gè)secret: kubectl cre...
摘要:解決了密碼密鑰等敏感數(shù)據(jù)的配置問(wèn)題,使用可以避免把這些敏感數(shù)據(jù)以明文的形式暴露到鏡像或者中。可以以或者環(huán)境變量的方式使用。 Secret解決了密碼、token、密鑰等敏感數(shù)據(jù)的配置問(wèn)題,使用Secret可以避免把這些敏感數(shù)據(jù)以明文的形式暴露到鏡像或者Pod Spec中。 Secret可以以Volume或者環(huán)境變量的方式使用。 使用如下命令行創(chuàng)建一個(gè)secret: kubectl cre...
摘要:解決了密碼密鑰等敏感數(shù)據(jù)的配置問(wèn)題,使用可以避免把這些敏感數(shù)據(jù)以明文的形式暴露到鏡像或者中。可以以或者環(huán)境變量的方式使用。 Secret解決了密碼、token、密鑰等敏感數(shù)據(jù)的配置問(wèn)題,使用Secret可以避免把這些敏感數(shù)據(jù)以明文的形式暴露到鏡像或者Pod Spec中。 Secret可以以Volume或者環(huán)境變量的方式使用。 使用如下命令行創(chuàng)建一個(gè)secret: kubectl cre...
摘要:相關(guān)基于項(xiàng)目和項(xiàng)目,并遵循應(yīng)用的十二因素風(fēng)格。相關(guān)在設(shè)計(jì)上,項(xiàng)目盡量保持驅(qū)動(dòng)和模塊化,以便模塊支持不同的實(shí)現(xiàn)方案。相關(guān)不僅可以管理眾多虛擬機(jī),其計(jì)算服務(wù)還支持對(duì)的驅(qū)動(dòng),管理引擎的子項(xiàng)目還可用于通過(guò)模板管理容器。現(xiàn)已整合公司所支持的項(xiàng)目。 整理自《Docker技術(shù)入門(mén)與實(shí)踐》 PaaS(Platform as a Service) PaaS 是希望提供一個(gè)統(tǒng)一的可供所有軟件直接運(yùn)行而無(wú)需...
摘要:的本身是無(wú)狀態(tài)的生命周期通常比較短,只要出現(xiàn)了異常,就會(huì)自動(dòng)創(chuàng)建一個(gè)新的來(lái)代替它。為了實(shí)現(xiàn)內(nèi)數(shù)據(jù)的存儲(chǔ)管理,引入了兩個(gè)資源持久卷,以下簡(jiǎn)稱和持久卷申請(qǐng),以下簡(jiǎn)稱。跟里的卷類似,不過(guò)會(huì)有獨(dú)立于的生命周期。 Kubernetes的pod本身是無(wú)狀態(tài)的(stateless),生命周期通常比較短,只要出現(xiàn)了異常,Kubernetes就會(huì)自動(dòng)創(chuàng)建一個(gè)新的Pod來(lái)代替它。 而容器產(chǎn)生的數(shù)據(jù),會(huì)隨著...
閱讀 2161·2021-09-04 16:40
閱讀 1453·2021-08-13 15:07
閱讀 3605·2019-08-30 15:53
閱讀 3194·2019-08-30 13:11
閱讀 1069·2019-08-29 17:22
閱讀 1811·2019-08-29 12:47
閱讀 1469·2019-08-29 11:27
閱讀 2223·2019-08-26 18:42