[root@master ~]# vim nginx-deployment.yaml kind: Deployment apiVersion: extensions/v1beta1 metadata: name: nginx spec: replicas: 1 template: metadata: labels: name: nginx spec: containers: - name: nginx image: docker.io/nginx:latest imagePullPolicy: IfNotPresent ports: - containerPort: 80 protocol: TCP
vim nginx-svc.yaml php
[root@master ~]# vim nginx-svc.yaml kind: Service apiVersion: v1 metadata: name: nginx labels: name: nginx spec: type: NodePort ports: - protocol: TCP nodePort: 31008 targetPort: 80 port: 80 selector: name: nginx
nodePort:31008 #---後期用戶能夠經過node節點上這個端口訪問nginx,公網接口html
targetPort:80 #---指定nginx docker容器的端口node
port:80 #---pod端口mysql
kubectl create -f nginx-deployment.yaml nginx
kubectl create -f nginx-svc.yamlweb
kubectl get deploysql
kubectl get svcdocker
kubectl get pod -o widevim
[root@master ~]# kubectl create -f nginx-deployment.yaml deployment "nginx" created [root@master ~]# kubectl create -f nginx-svc.yaml service "nginx" created [root@master ~]# kubectl get deploy NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE mysql 1 1 1 1 1h nginx 1 1 1 1 26s [root@master ~]# kubectl get svc NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.254.0.1 <none> 443/TCP 5d nginx 10.254.8.125 <nodes> 80:31008/TCP 24s [root@master ~]#
[root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE mysql-1971774246-2f905 1/1 Running 0 1h 10.255.36.2 node2 nginx-1011335894-9wd5h 1/1 Running 0 2m 10.255.41.2 node1 [root@master ~]#
上面已經知道pod運行在哪臺節點及對外監聽端口,接下來就是訪問(上面標紅字段)api
雖然nginx是在node1上運行,可是經過其餘節點也是能夠訪問,由於已經作負載均衡
主要做用是修改service值
kubectl get service nginx -o yaml
[root@master ~]# kubectl get service nginx -o yaml apiVersion: v1 kind: Service metadata: creationTimestamp: 2019-11-03T20:45:43Z labels: name: nginx name: nginx namespace: default resourceVersion: "122656916" selfLink: /api/v1/namespaces/default/services/nginx uid: e7775727-fe7a-11e9-bc69-000c291c8b39 spec: clusterIP: 10.254.8.125 ports: - nodePort: 31008 port: 80 protocol: TCP targetPort: 80 selector: name: nginx sessionAffinity: None type: NodePort status: loadBalancer: {}
kubectl edit service nginx
和vim相似的操做
[root@master ~]# kubectl edit service nginx # Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 kind: Service metadata: creationTimestamp: 2019-11-03T20:45:43Z labels: name: nginx name: nginx namespace: default resourceVersion: "122656916" selfLink: /api/v1/namespaces/default/services/nginx uid: e7775727-fe7a-11e9-bc69-000c291c8b39 spec: clusterIP: 10.254.8.125 ports: - nodePort: 31009 port: 80 protocol: TCP targetPort: 80 selector: name: nginx sessionAffinity: None type: NodePort status: loadBalancer: {}
kubectl get service
[root@master ~]# kubectl get service NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.254.0.1 <none> 443/TCP 5d nginx 10.254.8.125 <nodes> 80:31009/TCP 20m
訪問web端
replace 替換的意思
kubectl get service
kubectl get service nginx -o yaml >nginx_replace.yaml
vim nginx_replace.yaml
kubectl replace -f nginx_replace.yaml
kubectl get service
[root@master ~]# kubectl get service NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.254.0.1 <none> 443/TCP 5d nginx 10.254.8.125 <nodes> 80:31009/TCP 20m [root@master ~]# kubectl get service nginx -o yaml >nginx_replace.yaml [root@master ~]# vim nginx_replace.yaml apiVersion: v1 kind: Service metadata: creationTimestamp: 2019-11-03T20:45:43Z labels: name: nginx name: nginx namespace: default resourceVersion: "123205401" selfLink: /api/v1/namespaces/default/services/nginx uid: e7775727-fe7a-11e9-bc69-000c291c8b39 spec: clusterIP: 10.254.8.125 ports: - nodePort: 31010 port: 80 protocol: TCP targetPort: 80 selector: name: nginx sessionAffinity: None type: NodePort status: loadBalancer: {} ~ ~ [root@master ~]# kubectl get service NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.254.0.1 <none> 443/TCP 6d nginx 10.254.8.125 <nodes> 80:31010/TCP 17h ~
當修改一部分配置時,使用patch會方便點,如pod換個image鏡像
這裏好比更換鏡像使得nginx支持php
kubectl exec -it nginx-1011335894-853ql bash
php
這個能夠在阿里雲隨便找一個
kubectl patch pod nginx-1011335894-673bv -p '{"spec":{"containers":[{"name":"nginx","image":"docker.io/zxg/nginx-php-fpm56:latest"}]}}'
kubectl exec nginx-1011335894-853ql -it bash
[root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE mysql-1971774246-jwrfc 1/1 Running 0 2h 10.255.41.5 node1 nginx-1011335894-853ql 1/1 Running 0 13s 10.255.36.2 node2 nginx-1011335894-pzgsj 1/1 Running 0 2h 10.255.41.2 node1 [root@master ~]# kubectl exec -it nginx-1011335894-853ql bash root@nginx-1011335894-853ql:/# nginx -v nginx version: nginx/1.13.7 root@nginx-1011335894-853ql:/# php bash: php: command not found root@nginx-1011335894-853ql:/# exit exit [root@master ~]# kubectl patch pod nginx-1011335894-853ql -p '{"spec":{"containers":[{"name":"nginx","image":"docker.io/zxg/nginx-php-fpm56:latest"}]}}' "nginx-1011335894-853ql" patched [root@master ~]# kubectl get pod NAME READY STATUS RESTARTS AGE mysql-1971774246-jwrfc 1/1 Running 0 2h nginx-1011335894-853ql 1/1 Running 1 3m nginx-1011335894-pzgsj 1/1 Running 0 2h [root@master ~]# kubectl exec nginx-1011335894-853ql -it bash bash-4.3# php -v PHP 5.6.32 (cli) (built: Dec 1 2017 19:58:36) Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
是用來使用文件或者標準輸入來更改配置信息
vim nginx-svc.yaml
改:nodePort:31010
爲:nodePort:31011
kubectl apply -f nginx-svc.yaml
kubectl get svc
[root@master ~]# vim nginx-svc.yaml kind: Service apiVersion: v1 metadata: name: nginx labels: name: nginx spec: type: NodePort ports: - protocol: TCP nodePort: 31011 targetPort: 80 port: 80 selector: name: nginx [root@master ~]# kubectl apply -f nginx-svc.yaml service "nginx" configured [root@master ~]# kubectl get svc NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.254.0.1 <none> 443/TCP 6d nginx 10.254.8.125 <nodes> 80:31011/TCP 23h [root@master ~]#
用於橫向擴展、是k8s或swarm這類容器編輯平臺的重要功能之一
如這裏把replica副本改成3
kubectl get pod -o wide
kubectl scale --current-replicas=1 --replicas=3 deployment/nginx
kubectl get pod -o wide
[root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE mysql-1971774246-2f905 1/1 Running 0 1d 10.255.36.2 node2 nginx-1011335894-9wd5h 1/1 Running 0 23h 10.255.41.2 node1 [root@master ~]# kubectl scale --current-replicas=1 --replicas=3 deployment/nginx deployment "nginx" scaled [root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE mysql-1971774246-2f905 1/1 Running 0 1d 10.255.36.2 node2 nginx-1011335894-9wd5h 1/1 Running 0 23h 10.255.41.2 node1 nginx-1011335894-scm64 1/1 Running 0 5s 10.255.36.3 node2 nginx-1011335894-xtkqd 1/1 Running 0 5s 10.255.41.3 node1 [root@master ~]#
用於自動擴展確認,跟scale不一樣的是前者仍是須要手動執行,而autoscale則會根據負載進行調解,而這條命令能夠對Deployment/ReplicaSet/RC進行設定,經過最小值和最大值的指定進行設定
kubectl autoscale deployment nginx --min=2 --max=5
應該是沒變化,由於以前手動scale設置的3在這個2-5的區間
kubectl get pod -o wide
kubectl autoscale deployment nginx --min=2 --max=2
這裏就報錯了由於以前設置的是3
[root@master ~]# kubectl autoscale deployment nginx --min=2 --max=5 deployment "nginx" autoscaled [root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE mysql-1971774246-2f905 1/1 Running 0 1d 10.255.36.2 node2 nginx-1011335894-9wd5h 1/1 Running 0 23h 10.255.41.2 node1 nginx-1011335894-scm64 1/1 Running 0 3m 10.255.36.3 node2 nginx-1011335894-xtkqd 1/1 Running 0 3m 10.255.41.3 node1 [root@master ~]# kubectl autoscale deployment nginx --min=2 --max=2 Error from server (AlreadyExists): horizontalpodautoscalers.autoscaling "nginx" already exists [root@master ~]#
若是其中一臺node壞掉或者維護,暫時不能讓生成的pod在此node上運行,須要通知kubernetes讓其不要建立過來,就用cordon命令,若是uncordon就是取消這個設定
kubectl cordon node2
kubectl get pod -o wide
kubectl get nodes -o wide
發現node2狀態爲Ready,SchedulingDisabled
kubectl scale --replicas=6 deployment/nginx
kubectl get pod -o wide
發現都是node1創建,node2已經封鎖成功
kubectl uncordon node2
[root@master ~]# kubectl cordon node2 node "node2" cordoned [root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE mysql-1971774246-2f905 1/1 Running 0 1d 10.255.36.2 node2 nginx-1011335894-9wd5h 1/1 Running 0 23h 10.255.41.2 node1 nginx-1011335894-scm64 1/1 Running 0 9m 10.255.36.3 node2 nginx-1011335894-xtkqd 1/1 Running 0 9m 10.255.41.3 node1 [root@master ~]# kubectl get nodes -o wide NAME STATUS AGE EXTERNAL-IP node1 Ready 6d <none> node2 Ready,SchedulingDisabled 5d <none> [root@master ~]# kubectl scale --replicas=6 deployment/nginx deployment "nginx" scaled [root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE mysql-1971774246-2f905 1/1 Running 0 1d 10.255.36.2 node2 nginx-1011335894-7jvp0 1/1 Running 0 5s 10.255.41.5 node1 nginx-1011335894-8nd1q 1/1 Running 0 5s 10.255.41.6 node1 nginx-1011335894-9wd5h 1/1 Running 0 23h 10.255.41.2 node1 nginx-1011335894-lhtkm 1/1 Running 0 5s 10.255.41.4 node1 nginx-1011335894-scm64 1/1 Running 0 11m 10.255.36.3 node2 nginx-1011335894-xtkqd 1/1 Running 0 11m 10.255.41.3 node1 [root@master ~]#
[root@master ~]# kubectl uncordon node2 node "node2" uncordoned [root@master ~]# kubectl get node -o wide NAME STATUS AGE EXTERNAL-IP node1 Ready 6d <none> node2 Ready 5d <none> [root@master ~]#
用於對某個node結點進行維護
kubectl delete deploy nginx
kubectl create -f nginx-deployment.yaml
kubectl get pod -o wide
kubectl drain node2
get pod -o wide
鏡像已經漂移過來了
node2狀態爲Ready,SchedulingDisabled ,完成配置
[root@master ~]# kubectl create -f nginx-deployment.yaml deployment "nginx" created [root@master ~]# kubectl scale --replicas=4 deployment nginx deployment "nginx" scaled [root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE mysql-1971774246-2f905 1/1 Running 0 1d 10.255.36.2 node2 nginx-1011335894-4tpj5 1/1 Running 0 8s 10.255.41.3 node1 nginx-1011335894-673bv 1/1 Running 0 8s 10.255.36.3 node2 nginx-1011335894-hw8ld 1/1 Running 0 8s 10.255.36.4 node2 nginx-1011335894-pzgsj 1/1 Running 0 28s 10.255.41.2 node1 [root@master ~]# kubectl drain node2 node "node2" cordoned pod "nginx-1011335894-hw8ld" evicted pod "nginx-1011335894-673bv" evicted pod "mysql-1971774246-2f905" evicted node "node2" drained [root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE mysql-1971774246-jwrfc 1/1 Running 0 7s 10.255.41.5 node1 nginx-1011335894-4tpj5 1/1 Running 0 1m 10.255.41.3 node1 nginx-1011335894-d683g 1/1 Running 0 7s 10.255.41.6 node1 nginx-1011335894-gs3lg 1/1 Running 0 7s 10.255.41.4 node1 nginx-1011335894-pzgsj 1/1 Running 0 1m 10.255.41.2 node1 [root@master ~]# get nodes -o wide
NAME STATUS AGE EXTERNAL-IP
node1 Ready 6d <none>
node2 Ready,SchedulingDisabled 5d <none>