K8s浣跨敤YAML鏂囦欢鍒涘缓璧勬簮
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了K8s浣跨敤YAML鏂囦欢鍒涘缓璧勬簮相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/port' title='port'>port 榛樿 鐢熸垚 鍚庣 targe 鏍煎紡 鏌ヨ 鑾峰彇 娌℃湁
鐢变簬K8s鎶€鏈殑鐏垎锛屽鑷寸幇鍦ㄥぇ澶у皬灏忕殑浼佷笟閮藉湪浣跨敤锛岃櫧鐒舵病鏈夋櫘鍙婏紝浣嗘湰浜虹浉淇℃椂杩熸棭鐨勪簨鎯咃紝鎵€浠ユ姄绱ф椂闂村瀛︿範涓€涓嬪惂锛?/p>鍦↘8s涓垱寤鸿祫婧愮殑鏂瑰紡鏈変袱绉嶏細鍛戒护琛屽拰YAML鏂囦欢锛屾湰娆″崥鏂囦富瑕佷粙缁嶄娇鐢╕AML鏂囦欢鐨勬柟寮忥紝濡傞渶浣跨敤鍛戒护琛屽垱寤鸿祫婧愯鍙傝€?a href="https://blog.51cto.com/14157628/2464824">K8s璧勬簮瀵硅薄鐨勫熀鏈鐞?/a>
Kubernetes涓殑YAML鏂囦欢涓庨厤缃竻鍗曟槸涓€鏍风殑锛屾牴鎹釜浜轰範鎯€傛湰娆″崥鏂囩粺绉颁负YAML鏂囦欢锛?/p>
涓€銆乊AML鏂囦欢鍩虹
YAML鏄笓闂ㄧ敤鏉ラ厤缃枃浠剁殑璇█锛岄潪甯哥畝娲佸拰寮哄ぇ銆備笌浜嗚В鐨刾roperties銆乆ML銆乯son绛夋暟鎹牸寮忥紝涔犳儻涔嬪悗灏变細鍙戠幇瓒婃潵瓒婂ソ鐢ㄣ€傚叾瀹瀁AML灏辨槸缁撳悎浜嗗ぇ閮ㄥ垎鐨勬爣璁拌瑷€鐨勭壒鎬э紝鏁村悎鏂板紑鍙戠殑銆?/p>
YAML鏂囦欢鐨勭壒鐐癸細
- 灞傛鍒嗘槑銆佺粨鏋勬竻鏅帮紱
- 浣跨敤绠€鍗曘€佷笂鎵嬪鏄擄紱
- 鍔熻兘寮哄ぇ銆佽涔変赴瀵岋紱
闇€瑕佺壒鍒敞鎰忕殑鏄細- 澶у皬鍐欐晱鎰燂紱
- 涓ユ牸瑕佹眰缂╄繘锛?/li>
浜屻€乊AML鏂囦欢浣跨敤
1锛塝AML鏂囦欢鐨勭粍鎴?/h3>
Kubernetes涓殑YAML鏂囦欢涓昏鐢变簲涓竴绾у瓧娈电粍鎴愶紝鍒嗗埆鏄細
- apiVersion锛歛pi鐗堟湰淇℃伅锛?/li>
- kind锛氭寚瀹氬垱寤鸿祫婧愬璞$殑绫诲瀷锛?/li>
- metadata锛氬厓鏁版嵁鍐呴儴鐨勫祵濂楀瓧娈碉紝瀹氫箟浜嗚祫婧愬璞$殑鍚嶇О銆佸悕绉扮┖闂寸瓑锛?/li>
- spec锛氳鑼冨畾涔夎祫婧愬簲璇ユ嫢鏈変粈涔堟牱鐨勭壒鎬э紝渚濋潬鎺у埗鍣ㄧ‘淇濇€ц兘鍙互婊¤冻锛屾弧瓒崇敤鎴锋湡鏈涚殑鐘舵€併€?/li>
- status锛氭樉绀鸿祫婧愮殑褰撳墠鐘舵€侊紝K8s灏辨槸纭繚褰撳墠鐘舵€佸悜鐩爣鐘舵€佹棤闄愰潬杩戜粠鑰屾弧瓒崇敤鎴锋湡鏈涖€備唬琛ㄨ祫婧愬綋鍓嶇殑鐘舵€侊紱
2锛夎幏鍙栫紪鍐橸AML鏂囦欢鐨勫府鍔?/h3>
Kubernetes涓殑YAML鏂囦欢涓昏鐢变簲涓竴绾у瓧娈电粍鎴愶紝鍒嗗埆鏄細
- apiVersion锛歛pi鐗堟湰淇℃伅锛?/li>
- kind锛氭寚瀹氬垱寤鸿祫婧愬璞$殑绫诲瀷锛?/li>
- metadata锛氬厓鏁版嵁鍐呴儴鐨勫祵濂楀瓧娈碉紝瀹氫箟浜嗚祫婧愬璞$殑鍚嶇О銆佸悕绉扮┖闂寸瓑锛?/li>
- spec锛氳鑼冨畾涔夎祫婧愬簲璇ユ嫢鏈変粈涔堟牱鐨勭壒鎬э紝渚濋潬鎺у埗鍣ㄧ‘淇濇€ц兘鍙互婊¤冻锛屾弧瓒崇敤鎴锋湡鏈涚殑鐘舵€併€?/li>
- status锛氭樉绀鸿祫婧愮殑褰撳墠鐘舵€侊紝K8s灏辨槸纭繚褰撳墠鐘舵€佸悜鐩爣鐘舵€佹棤闄愰潬杩戜粠鑰屾弧瓒崇敤鎴锋湡鏈涖€備唬琛ㄨ祫婧愬綋鍓嶇殑鐘舵€侊紱
铏界劧鐭ラ亾浜哬AML鏂囦欢涓殑涓€绾у瓧娈甸兘鏄粈涔堬紝浣嗘槸杩樻槸涓嶇煡閬撳簲璇ユ€庝箞鍐欍€傚彲浠ュ€熷姪浠ヤ笅鍛戒护鏉ヨ幏鍙栦竴浜涘府鍔╀俊鎭€?/p>
[root@master ~]# kubectl api-versions
//鑾峰彇褰撳墠闆嗙兢鏀寔鐨?apiserver鐗堟湰
[root@master ~]# kubectl api-resources
//鑾峰彇鍏ㄩ儴鐨刟pi璧勬簮瀵硅薄
[root@master ~]# kubectl explain deployment
//鏌ョ湅k8s鏌愪釜瀵硅薄鐨勯厤缃竻鍗曟牸寮忥紝搴旇鍖呭惈鍝簺瀛楁鍙婁娇鐢ㄦ柟娉?[root@master ~]# kubectl explain deployment.spec
//杩欎釜鍛戒护鏄潪甯搁噸瑕佺殑锛屽畠鍙互涓€绾т竴绾ф潵鑾峰彇甯姪
3锛塝AML鏂囦欢鐨勫熀鏈牸寮?/h3>
[root@master ~]# cat web.yaml
kind: Deployment //鎸囧畾瑕佸垱寤虹殑璧勬簮瀵硅薄
apiVersion: extensions/v1beta1 //鎸囧畾deployment鎵€瀵瑰簲鐨凙PI鐗堟湰淇℃伅
metadata:
name: web //瀹氫箟deployment鐨勫悕绉?spec:
replicas: 2 //鎸囧畾鍓湰鏁伴噺
template:
metadata:
labels: //鎸囧畾pod鐨勬爣绛? app: web_server
spec:
containers:
- name: nginx //鎸囧畾pod杩愯鐨勫鍣ㄧ殑鍚嶇О
image: nginx //鎸囧畾杩愯瀹瑰櫒鎵€闇€鐨勯暅鍍?/code>
4锛塧pply鍒涘缓鎴栨洿鏂?/h3>
[root@master ~]# kubectl apply -f web.yaml
//浣跨敤鈥?f鈥濇潵鎸囧畾yaml鏂囦欢锛屾牴鎹畒aml鏂囦欢涓畾涔夌殑鍐呭鐢熸垚鎵€闇€鐨勮祫婧?/code>
[root@master ~]# cat web.yaml
kind: Deployment //鎸囧畾瑕佸垱寤虹殑璧勬簮瀵硅薄
apiVersion: extensions/v1beta1 //鎸囧畾deployment鎵€瀵瑰簲鐨凙PI鐗堟湰淇℃伅
metadata:
name: web //瀹氫箟deployment鐨勫悕绉?spec:
replicas: 2 //鎸囧畾鍓湰鏁伴噺
template:
metadata:
labels: //鎸囧畾pod鐨勬爣绛? app: web_server
spec:
containers:
- name: nginx //鎸囧畾pod杩愯鐨勫鍣ㄧ殑鍚嶇О
image: nginx //鎸囧畾杩愯瀹瑰櫒鎵€闇€鐨勯暅鍍?/code>
[root@master ~]# kubectl apply -f web.yaml
//浣跨敤鈥?f鈥濇潵鎸囧畾yaml鏂囦欢锛屾牴鎹畒aml鏂囦欢涓畾涔夌殑鍐呭鐢熸垚鎵€闇€鐨勮祫婧?/code>
apply鍙互鎸囧畾澶氭锛屽鏋滃彂鐜版枃浠朵笉鍚岋紝鍒欐洿鏂?/p>
5锛塪elete鍒犻櫎
[root@master ~]# kubectl delete -f web.yaml
//鍒犻櫎yaml鏂囦欢涓畾涔夌殑璧勬簮
6锛夐獙璇?/h3>
[root@master ~]# kubectl get deployments. web
//鏌ョ湅web鎺у埗鍣ㄦ墍浜х敓鐨刾od
NAME READY UP-TO-DATE AVAILABLE AGE
web 2/2 2 2 5m50s
[root@master ~]# kubectl describe deployments. web
//鏌ョ湅web鎺у埗鍣ㄧ殑璇︾粏淇℃伅
[root@master ~]# kubectl get deployments. web
//鏌ョ湅web鎺у埗鍣ㄦ墍浜х敓鐨刾od
NAME READY UP-TO-DATE AVAILABLE AGE
web 2/2 2 2 5m50s
[root@master ~]# kubectl describe deployments. web
//鏌ョ湅web鎺у埗鍣ㄧ殑璇︾粏淇℃伅
杩斿洖鐨勭粨鏋滃涓嬶細
杩欐牱涓€鏉ワ紝Kubernetes宸茬粡鏍规嵁YAML鏂囦欢鐢熸垚浜嗘垜浠墍闇€鐨刾od璧勬簮锛?/p>
[root@master ~]# kubectl get pod -o wide //鏌ョ湅pod鐨勮缁嗕俊鎭?NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-d6ff6c799-7jtvd 1/1 Running 0 17m 10.244.2.2 node02 <none> <none>
web-d6ff6c799-7tpdc 1/1 Running 0 17m 10.244.1.2 node01 <none> <none>
K8s闆嗙兢鍐呴儴娴嬭瘯璁块棶锛?br/>
涓夈€佸垱寤篩AML鏂囦欢浣縆8s涓璸od鐨勬湇鍔¤兘澶熻澶栭儴璁块棶
[root@master ~]# cat web-svc.yaml
kind: Service
apiVersion: v1
metadata:
name: web-svc
spec:
type: NodePort //鎸囧畾绫诲瀷涓篘odePort锛屽彲浠ヨ澶栭儴璁块棶锛屽惁鍒欓粯璁ゆ儏鍐典笅鏄痗luster IP锛屼粎闄愰泦缇ゅ唴閮ㄨ闂? selector:
app: web_server //蹇呴』涓巇eployment璧勬簮瀵硅薄鐨勬爣绛捐繘琛屽叧鑱? ports:
- protocol: TCP
port: 80 //鎸囧畾瑕佹槧灏勫埌鐨凜luster IP鐨勭鍙? targetPort: 80 //鎸囧畾鐨勬槸瑕佹槧灏刾od涓殑绔彛
nodePort: 31000 //鎸囧畾鏄犲皠鍒板涓绘満鐨勭鍙o紝鑼冨洿鏄?0000~32767
[root@master ~]# kubectl apply -f web-svc.yaml
//鐢熸垚service鐨勬帶鍒舵枃浠讹紙yaml涓凡缁忓畾涔夊叾鍚嶇О涓簑eb-svc锛?[root@master ~]# kubectl get svc web-svc //鏌ョ湅service鎺у埗鍣?NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web-svc NodePort 10.99.32.22 <none> 80:31000/TCP 12m
//TYPE锛氫负NodePort锛屽彲浠ヤ娇澶栭儴璁块棶锛?//PORT锛氭槧灏勫嚭鐨勭鍙d笌鎴戜滑瀹氫箟鐨勪竴鏍?/code>
娴嬭瘯璁块棶锛?br/>
娉ㄦ剰锛氭槸璁块棶缇ら泦涓换鎰忚妭鐐归兘鍙互璁块棶k8s闆嗙兢涓璸od鎵€鎻愪緵鐨勬湇鍔★紒
鍥涖€佸簳灞傝礋杞藉潎琛″疄鐜板師鐞?/h2>
[root@master ~]# kubectl describe svc web-svc
//鏌ョ湅service鐨勮缁嗕俊鎭?/code>
[root@master ~]# kubectl describe svc web-svc
//鏌ョ湅service鐨勮缁嗕俊鎭?/code>
杩斿洖鐨勪俊鎭涓嬶細
鏃㈢劧璇村埌锛孍ndpoints鎸囧畾鐨勬槸鍚庣pod鐨処P鍦板潃锛岄偅涔堜笅闈㈣繘琛岄獙璇侊細
[root@master ~]# kubectl get pod -o wide | awk 鈥榹print $6}鈥?//鎻愬彇鍚庣pod鐨処P鍦板潃
IP
10.244.2.2
10.244.1.2
//涓庝笂杩版煡璇㈢殑缁撴灉涓€鏍凤紒
鎴戜滑鐭ラ亾service鏄湁璐熻浇鍧囪 鐨勮兘鍔涳紝閭d箞鏄€庝箞瀹炵幇鐨勶紵
鍏跺疄锛岃儗鍚庣殑鍘熺悊骞舵病鏈夐偅涔堥珮澶т笂锛宬ube-proxy閫氳繃iptables鐨勮浆鍙戞満鍒舵潵瀹炵幇璐熻浇鍧囪 鐨勬晥鏋滅殑锛屽厛瀹氫箟鐩爣IP鏄痵ervice鎻愪緵鐨勭兢闆咺P锛岀劧鍚庝娇鐢ㄢ€?j鈥濋€夐」杞彂鍒板叾浠杋ptables瑙勫垯锛屾帴涓嬫潵楠岃瘉涓€涓嬶細
[root@master ~]# kubectl get svc web-svc | awk 鈥榹print $3}鈥?//棣栧厛鏌ョ湅service鐨勭兢闆咺P鍦板潃
CLUSTER-IP
10.99.32.22
[root@master ~]# iptables-save | grep 10.99.32.22
//鏌ョ湅iptables瑙勫垯涓笌缇ら泦IP鍦板潃鐩稿叧鐨勫唴瀹?-A KUBE-SERVICES ! -s 10.244.0.0/16 -d 10.99.32.22/32 -p tcp -m comment --comment "default/web-svc: cluster IP" -m tcp --dport 80 -j KUBE-MARK-MASQ
-A KUBE-SERVICES -d 10.99.32.22/32 -p tcp -m comment --comment "default/web-svc: cluster IP" -m tcp --dport 80 -j KUBE-SVC-3RBUQ3B6P3MTQ3S7
浠庝笂杩扮粨鏋滀腑鍙互鐪嬪嚭锛屽綋鐩爣鍦板潃鏄兢闆咺P鏃讹紝灏变細杞彂鍒癒UBE-SVC-3RBUQ3B6P3MTQ3S7瑙勫垯涓?[root@master ~]# iptables-save | grep KUBE-SVC-3RBUQ3B6P3MTQ3S7
:KUBE-SVC-3RBUQ3B6P3MTQ3S7 - [0:0]
-A KUBE-NODEPORTS -p tcp -m comment --comment "default/web-svc:" -m tcp --dport 31000 -j KUBE-SVC-3RBUQ3B6P3MTQ3S7
-A KUBE-SERVICES -d 10.99.32.22/32 -p tcp -m comment --comment "default/web-svc: cluster IP" -m tcp --dport 80 -j KUBE-SVC-3RBUQ3B6P3MTQ3S7
-A KUBE-SVC-3RBUQ3B6P3MTQ3S7 -m statistic --mode random --probability 0.50000000000 -j KUBE-SEP-E3SP5QDRAUFB55IC
-A KUBE-SVC-3RBUQ3B6P3MTQ3S7 -j KUBE-SEP-3T3LUFAKMOTS5BKN
//浠庢煡璇㈢粨鏋滀腑鍙互鐪嬪嚭鍏惰礋杞藉潎琛$殑鏁堟灉锛屽洜涓哄悗绔彧鍒涘缓浜嗕袱涓猵od锛屾墍浠ュ叾姒傜巼涓?.5
鏈夊叧K8s鐨勮缁嗕粙缁嶈繕鏄缓璁弬鑰?a href="https://awesome-kubernetes-notes.readthedocs.io/en/latest/awesome-kubernetes-notes.html#" rel="nofollow">K8s涓枃鏂囨。
鈥斺€斺€斺€斺€斺€斺€斺€旀湰鏂囧埌姝や负姝紝鎰熻阿闃呰鈥斺€斺€斺€斺€斺€斺€斺€?/p>
以上是关于K8s浣跨敤YAML鏂囦欢鍒涘缓璧勬簮的主要内容,如果未能解决你的问题,请参考以下文章
浣跨敤HiBurn鐑у綍楦胯挋.bin鏂囦欢鍒癏i3861寮€鍙戞澘