您好,登錄后才能下訂單哦!
《深入剖析Kubernetes - 12? 牛刀小試:我的第一個容器化應用》
主要包括2個步驟:制作鏡像 和 編寫yaml配置文件(或json)
以 nginx 的deployment 為例
apiVersion:?apps/v1? kind:?Deployment? metadata:? ??name:?nginx-deployment? spec:? ??selector:? ????matchLabels:? ??????app:?nginx? ??replicas:?2? ?template:? ???metadata:?labels:? ?????app:?nginx? ???spec:? ?????containers:? ?????-?name:?nginx? ???????image:?nginx:1.7.9? ???????ports:? ???????-?containerPort:?80
其中kind 字段聲明類型,例如deployment、daemonset 等,metadata 是具體描述,例如name、namespace、labels、annotations等
spec.template 描述了pod 的細節,即容器配置的模板
創建deployment
$?kubectl?create?-f?nginx-deployment.yaml
查看deployment運行情況
$?kubectl?get?pods?-l?app=nginx? NAME?READY?STATUS?RESTARTS?AGE?n ginx-deployment-67594d6bf6-9gdvr?1/1?Running?0?10m? nginx-deployment-67594d6bf6-v6j7w?1/1?Running?0?10m
查看API 對象細節
kubectl?describe?pod?nginx-deployment-67594d6bf6-9gdvr Name:???????????????nginx-deployment-67594d6bf6-9gdvr Namespace:??????????default Priority:???????????0 PriorityClassName:??<none> Node:???????????????node-1/10.168.0.3 Start?Time:?????????Thu,?16?Aug?2018?08:48:42?+0000 Labels:?????????????app=nginx ????????????????????pod-template-hash=2315082692 Annotations:????????<none> Status:?????????????Running IP:?????????????????10.32.0.23 Controlled?By:??????ReplicaSet/nginx-deployment-67594d6bf6 ... Events: ??Type?????Reason??????????????????Age????????????????From???????????????Message ??----?????------??????????????????----???????????????----???????????????------- ?? ??Normal???Scheduled???????????????1m?????????????????default-scheduler??Successfully?assigned?default/nginx-deployment-67594d6bf6-9gdvr?to?node-1 ??Normal???Pulling?????????????????25s????????????????kubelet,?node-1????pulling?image?"nginx:1.7.9" ??Normal???Pulled??????????????????17s????????????????kubelet,?node-1????Successfully?pulled?image?"nginx:1.7.9" ??Normal???Created?????????????????17s????????????????kubelet,?node-1????Created?container ??Normal???Started?????????????????17s????????????????kubelet,?node-1????Started?container
其中Events 字段可用于后期問題定位,常見的有
(1)找不到合適的node節點調度pod,包括
node節點label匹配不上;
node節點資源不夠分配;
node節點設置了污點;
node節點網絡異常;
(2) 鏡像拉取失敗
升級容器鏡像版本
...???? ????spec: ??????containers: ??????-?name:?nginx ????????image:?nginx:1.8?#?這里被從?1.7.9?修改為?1.8 ????????ports: ??????-?containerPort:?80
執行
$?kubectl?replace?-f?nginx-deployment.yaml
更常見的方式是,通過apply 執行相關操作,無論是創建還是修改都可以通過該命令完成
$?kubectl?apply?-f?nginx-deployment.yaml? #?修改?nginx-deployment.yaml?的內容? $?kubectl?apply?-f?nginx-deployment.yaml
下面是一個volume掛載的例子
通常分為emptyDir 和 hostPath 兩種,兩者的區別在于前者不需要指定宿主機目錄(源目錄),k8s 會在宿主機上創建一個臨時目錄并掛載進去;而后者,則需要顯式聲明掛載的源目錄,例如將宿主機的/usr/local/nginx/html 目錄掛載到容器同樣的位置
emptyDir的例子
volumes: ??????-?name:?nginx-vol ????????emptyDir:?{} hostPath的例子 ...??? ????volumes: ??????-?name:?nginx-vol ????????hostPath:? ??????????path:?/var/data
進入容器
$?kubectl?exec?-it?nginx-deployment-5c678cfb6d-lg9lw?--?/bin/bash? #?ls?/usr/share/nginx/html
刪除容器
$?kubectl?delete?-f?nginx-deployment.yaml
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。