A, kubectl cluster deployment resources management and service Service
1, the relevant parameters
2, the mirror introduced
Second, the editors create nginx-deployment.yaml /nginx-svc.yaml
1. Create deployment.yaml
[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
2. Create servcie
vim nginx-svc.yaml
[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
3, several ports explained
nodePort: 31008 # --- Late user can access through this port on the node node nginx, public network interface
targetPort: 80 # --- designated nginx docker container port
port: 80 # --- pod port
4、create deployment/service
1) Create a
kubectl create -f nginx-deployment.yaml
kubectl create -f nginx-svc.yaml
2) View deployment / service / pod Details
kubectl get deploy
kubectl get svc
kubectl get pod -o wide
[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 ~]#
3) access
Above it has been known to run in the pod Which nodes and external monitor port , the next step is to access (it is labeled red field)
4) access by other nodes nginx
While nginx is running on node1, but also can be accessed by other nodes, as it has been doing load balancing
Three, kubectl Edit command
The main role is to modify the service value
1, using the output get -o parameter specifies the type of message yaml
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: {}
2, modify the external port 31009
kubectl edit service nginx
Similar operations and 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: {}
3, see the service, and verify
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
Access web end
四、kubectl replace
replace the replacement means
1, View service
kubectl get service
2, the redirection of a nginx_replace yaml file
kubectl get service nginx -O yaml> nginx_replace.yaml
3, edit, modify port 31010
vim nginx_replace.yaml
4, replace the implementation of
kubectl replace -f nginx_replace.yaml
5, check whether the service to take effect
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 ~
Five, kubectl Patch
When modifying part of the configuration, using the patch will be convenient point, such as the mirror image change pod
Here such as replacing the mirror support such nginx php
1, check the current mirror supports php
kubectl exec -it nginx-1011335894-853ql bash
php
2, upload a new image, and import
This can easily find a cloud in Ali
3, performing replacement patch
kubectl patch pod nginx-1011335894-673bv -p '{"spec":{"containers":[{"name":"nginx","image":"docker.io/zxg/nginx-php-fpm56:latest"}]}}'
4. Check whether to support php
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
Six, kubectl the Apply
It is to use the standard input file or change the configuration information
1, edit the file svc.yaml
vim nginx-svc.yaml
Change: nodePort: 31010
Is: nodePort: 31011
2, the apply command
kubectl apply -f nginx-svc.yaml
3, test results
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 ~]#
Seven, kubectl Scale (scale)
For horizontal expansion, it is one of the important functions such containers k8s or swarm editing platform
As used herein the replica copies to 3
1, in which node running nginx View
kubectl get pod -o wide
2, command execution scale
kubectl scale --current-replicas=1 --replicas=3 deployment/nginx
3, look again at what node running 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 ~]#
八、kubectl autoscale
Automatic confirmation for the expansion, with a different scale of the former or the need to manually perform, will mediate the autoscale according to the load, and this order can be set for Deployment / ReplicaSet / RC, by the specified minimum and maximum set up
1, 2 to set the minimum, maximum auto setting 5
kubectl autoscale deployment nginx --min=2 --max=5
2, view results
Should be no change, because in the third period 2-5 before a manual scale setting
kubectl get pod -o wide
3, the minimum set 2, maximum 2
kubectl autoscale deployment nginx --min=2 --max=2
Here on the error because 3 is set before
[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 ~]#
Nine, kubectl Cordon and uncordon
If one node is broken or maintenance, temporarily unable to make pod generated on this node to run, do not need to notify kubernetes let it come to create, use cordon command, is to cancel this setting if uncordon
1, running on node2 cordon command
kubectl node2 cord
2 view details pod, no change
kubectl get pod -o wide
3. View node details
kubectl get nodes -o wide
Node2 found status is Ready, SchedulingDisabled
3, a copy of increase relicas
kubectl scale --replicas=6 deployment/nginx
4, again pod View details
kubectl get pod -o wide
Discoveries are node1 establish, node2 has successfully blocked
5, using uncordon cancel cordon set
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 ~]#
Ten, kubectl Drain (expulsion)
For the maintenance of a certain node node
1, drain two effects:
1, may not be used to set this node (Cordon)
2, evict expel pod on his normal node to node
2, before the first delete nginx
kubectl delete deploy nginx
3, create a pod
kubectl create -f nginx-deployment.yaml
4, pod View details
kubectl get pod -o wide
5, execute the command drain drain node2
kubectl drain node2
6, see the pod
get pod -o wide
Mirror has drifted over the
7, see the node
node2 status of the Ready, SchedulingDisabled , to complete the configuration
[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>
Please indicate the source: https://www.cnblogs.com/zhangxingeng/p/11807083.html