------------------------------------------
kubectl命令:通过此命令对pod、service、replicaset、statefulet、daemonset、job、cronjob、node等完成增删查。
[root@master ~]# kubectl
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
Basic Commands (Beginner):
create Create a resource from a file or from stdin.
expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
run Run a particular image on the cluster
set Set specific features on objects
Basic Commands (Intermediate):
explain Documentation of resources
get Display one or many resources
edit Edit a resource on the server
delete Delete resources by filenames, stdin, resources and names, or by resources and label selector
Deploy Commands:
rollout Manage the rollout of a resource
scale Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job
autoscale Auto-scale a Deployment, ReplicaSet, or ReplicationController
Cluster Management Commands:
certificate Modify certificate resources.
cluster-info Display cluster info
top Display Resource (CPU/Memory/Storage) usage.
cordon Mark node as unschedulable
uncordon Mark node as schedulable
drain Drain node in preparation for maintenance
taint Update the taints on one or more nodes 污点
Troubleshooting and Debugging Commands: 解决问题的
describe Show details of a specific resource or group of resources
logs Print the logs for a container in a pod
attach Attach to a running container
exec Execute a command in a container
port-forward Forward one or more local ports to a pod
proxy Run a proxy to the Kubernetes API server
cp Copy files and directories to and from containers.
auth Inspect authorization
Advanced Commands:
diff Diff live version against would-be applied version
apply Apply a configuration to a resource by filename or stdin
patch Update field(s) of a resource using strategic merge patch
replace Replace a resource by filename or stdin
wait Experimental: Wait for a specific condition on one or many resources.
convert Convert config files between different API versions
Settings Commands:
label Update the labels on a resource
annotate Update the annotations on a resource
completion Output shell completion code for the specified shell (bash or zsh)
Other Commands:
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of "group/version"
config Modify kubeconfig files
plugin Provides utilities for interacting with plugins.
version Print the client and server version information
Usage:
kubectl [flags] [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
==========================================
查看版本信息
kubectl version
查看集群信息
kubectl cluster-info
查看节点详细信息
kubectl describe node master/node01/node02
kubectl describe svc nginx
查看pod
kubectl get pods/nodes
kubectl get pods -o wide
kubectl get deployments
删除pod
kubectl delete pod nginx-deploy-84cbfc56b6-9h9wm
kubectl delete pod nginx-5c7588df-9xvbn
kubectl delete svc nginx-deploy
kubectl delete deployment wolf
kubectl get pods -o yaml | kubectl delete -f - 删除全部
创建控制器
kubectl run --help
kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 --dry-run=true
kubectl run nginx --image=nginx:1.14-alpine --port=80 --replicas=2
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl run client --image=busybox --replicas=1 -it --restart=Never
kubectl run wolf --image=busybox --replicas=2
暴露端口
kubectl expose deployment nginx-deploy --name=nginx-deploy --port=80 --protocol=TCP
动态伸缩
kubectl scale --replicas=5 deployment nginx
kubectl scale --replicas=3 deployment nginx
滚动升级
kubectl set image deployment nginx nginx=nginx 这个环境没准备容器,感受一下过程
kubectl rollout status deployment nginx
默认回滚到上一个版本
kubectl rollout undo deployment nginx
-----------------------------------------
service_ip:service_pod---->pod_ip:port_port
被集群内部访问
Type for this service: ClusterIP, NodePort, LoadBalancer, or ExternalName. Default is 'ClusterIP'
kubectl expose --help
kubectl expose deployment nginx-deploy --name=nginx-deploy --port=80 --protocol=TCP
kubectl get pod,svc
------------------------------------------
修改镜像站
kubeadm config images pull --config image.yaml
------------------------------------------
测试
[root@master ingress-nginx]# kubectl version
Client Version: version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.3", GitCommit:"721bfa751924da8d1680787490c54b9179b1fed0", GitTreeState:"clean", BuildDate:"2019-02-01T20:08:12Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.3", GitCommit:"721bfa751924da8d1680787490c54b9179b1fed0", GitTreeState:"clean", BuildDate:"2019-02-01T20:00:57Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"linux/amd64"}
[root@master ingress-nginx]# kubectl cluster-info
Kubernetes master is running at https://10.249.6.100:6443
KubeDNS is running at https://10.249.6.100:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
[root@master ingress-nginx]# kubectl describe node master
Name: master
Roles: master
Labels: beta.kubernetes.io/arch=amd64
beta.kubernetes.io/os=linux
kubernetes.io/hostname=master
node-role.kubernetes.io/master=
Annotations: flannel.alpha.coreos.com/backend-data: {"VtepMAC":"6a:63:a1:96:b6:66"}
flannel.alpha.coreos.com/backend-type: vxlan
flannel.alpha.coreos.com/kube-subnet-manager: true
flannel.alpha.coreos.com/public-ip: 10.249.6.100
kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
node.alpha.kubernetes.io/ttl: 0
volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp: Thu, 28 Feb 2019 06:23:41 -0500
Taints: node-role.kubernetes.io/master:NoSchedule
Unschedulable: false
Conditions:
Type Status LastHeartbeatTime LastTransitionTime Reason Message
---- ------ ----------------- ------------------ ------ -------
MemoryPressure False Thu, 28 Feb 2019 09:29:34 -0500 Thu, 28 Feb 2019 06:23:33 -0500 KubeletHasSufficientMemory kubelet has sufficient memory available
DiskPressure False Thu, 28 Feb 2019 09:29:34 -0500 Thu, 28 Feb 2019 06:23:33 -0500 KubeletHasNoDiskPressure kubelet has no disk pressure
PIDPressure False Thu, 28 Feb 2019 09:29:34 -0500 Thu, 28 Feb 2019 06:23:33 -0500 KubeletHasSufficientPID kubelet has sufficient PID available
Ready True Thu, 28 Feb 2019 09:29:34 -0500 Thu, 28 Feb 2019 06:41:02 -0500 KubeletReady kubelet is posting ready status
Addresses:
InternalIP: 10.249.6.100
Hostname: master
Capacity:
cpu: 40
ephemeral-storage: 266110Mi
hugepages-1Gi: 0
hugepages-2Mi: 0
memory: 32612544Ki
pods: 110
Allocatable:
cpu: 40
ephemeral-storage: 251132903009
hugepages-1Gi: 0
hugepages-2Mi: 0
memory: 32510144Ki
pods: 110
System Info:
Machine ID: 904f126630e44103b404c0adc7b66d6f
System UUID: 4C023761-E90B-E611-9DB5-7CD30AB13074
Boot ID: e316bea7-ec44-4d1e-b9b6-e46e0cd74087
Kernel Version: 3.10.0-693.el7.x86_64
OS Image: CentOS Linux 7 (Core)
Operating System: linux
Architecture: amd64
Container Runtime Version: docker://18.9.2
Kubelet Version: v1.13.3
Kube-Proxy Version: v1.13.3
PodCIDR: 10.244.0.0/24
Non-terminated Pods: (8 in total)
Namespace Name CPU Requests CPU Limits Memory Requests Memory Limits AGE
--------- ---- ------------ ---------- --------------- ------------- ---
kube-system coredns-86c58d9df4-fltl9 100m (0%) 0 (0%) 70Mi (0%) 170Mi (0%) 3h5m
kube-system coredns-86c58d9df4-kdm2h 100m (0%) 0 (0%) 70Mi (0%) 170Mi (0%) 3h5m
kube-system etcd-master 0 (0%) 0 (0%) 0 (0%) 0 (0%) 3h4m
kube-system kube-apiserver-master 250m (0%) 0 (0%) 0 (0%) 0 (0%) 3h4m
kube-system kube-controller-manager-master 200m (0%) 0 (0%) 0 (0%) 0 (0%) 3h4m
kube-system kube-flannel-ds-amd64-px2rk 100m (0%) 100m (0%) 50Mi (0%) 50Mi (0%) 168m
kube-system kube-proxy-zndjb 0 (0%) 0 (0%) 0 (0%) 0 (0%) 3h5m
kube-system kube-scheduler-master 100m (0%) 0 (0%) 0 (0%) 0 (0%) 3h4m
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
Resource Requests Limits
-------- -------- ------
cpu 850m (2%) 100m (0%)
memory 190Mi (0%) 390Mi (1%)
ephemeral-storage 0 (0%) 0 (0%)
Events: <none>
查看系统内部组件信息
[root@master ingress-nginx]# kubectl get pods -n kube-system -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
coredns-86c58d9df4-fltl9 1/1 Running 0 4h 10.244.0.3 master <none> <none>
coredns-86c58d9df4-kdm2h 1/1 Running 0 4h 10.244.0.2 master <none> <none>
etcd-master 1/1 Running 0 3h59m 10.249.6.100 master <none> <none>
kube-apiserver-master 1/1 Running 0 3h59m 10.249.6.100 master <none> <none>
kube-controller-manager-master 1/1 Running 0 3h59m 10.249.6.100 master <none> <none>
kube-flannel-ds-amd64-78tgg 1/1 Running 0 3h43m 10.249.6.102 node02 <none> <none>
kube-flannel-ds-amd64-px2rk 1/1 Running 1 3h43m 10.249.6.100 master <none> <none>
kube-flannel-ds-amd64-qdbzq 1/1 Running 1 3h43m 10.249.6.101 node01 <none> <none>
kube-proxy-6fp6m 1/1 Running 0 3h50m 10.249.6.102 node02 <none> <none>
kube-proxy-wv6gg 1/1 Running 0 3h50m 10.249.6.101 node01 <none> <none>
kube-proxy-zndjb 1/1 Running 0 4h 10.249.6.100 master <none> <none>
kube-scheduler-master 1/1 Running 0 3h59m 10.249.6.100 master <none> <none>
[root@master ingress-nginx]# kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP 4h4m
------------------------------------------
创建控制器
[root@master ingress-nginx]#kubectl run --help
[root@master ingress-nginx]#kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 --dry-run=true
[root@master ingress-nginx]#kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1
[root@master ingress-nginx]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-5c7588df-9xvbn 1/1 Running 0 44m
nginx-deploy-84cbfc56b6-9h9wm 1/1 Running 0 59s
[root@master ingress-nginx]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-5c7588df-9xvbn 1/1 Running 0 46m 10.244.2.2 node02 <none> <none>
nginx-deploy-84cbfc56b6-9h9wm 1/1 Running 0 2m40s 10.244.2.3 node02 <none> <none>
[root@node01 ~]# curl 10.244.2.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
[root@master ingress-nginx]# kubectl delete pod nginx-deploy-84cbfc56b6-9h9wm
pod "nginx-deploy-84cbfc56b6-9h9wm" deleted
这里删除pod,容器随之删除,然后新建一个pod,新建一个容器到节点上
[root@master ingress-nginx]#
[root@master ingress-nginx]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-5c7588df-9xvbn 1/1 Running 0 49m 10.244.2.2 node02 <none> <none>
nginx-deploy-84cbfc56b6-87sgf 0/1 ContainerCreating 0 8s <none> node01 <none> <none>
[root@master ingress-nginx]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-5c7588df-9xvbn 1/1 Running 0 50m 10.244.2.2 node02 <none> <none>
nginx-deploy-84cbfc56b6-87sgf 1/1 Running 0 42s 10.244.1.2 node01 <none> <none>
暴露端口
[root@master ingress-nginx]# kubectl expose deployment nginx-deploy --name=nginx-deploy --port=80 --protocol=TCP
service/nginx-deploy exposed
[root@master ingress-nginx]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-5c7588df-9xvbn 1/1 Running 0 72m
pod/nginx-deploy-84cbfc56b6-87sgf 1/1 Running 0 22m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3h57m
service/nginx NodePort 10.98.181.177 <none> 80:31884/TCP 72m
service/nginx-deploy ClusterIP 10.98.81.246 <none> 80/TCP 55s 地址自动生产
这里访问是service地址,还是集群内部的节点访问
[root@node01 ~]# curl 10.98.81.246
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
[root@master ingress-nginx]# kubectl run client --image=busybox --replicas=1 -it --restart=Never
----------------------------------------------
内部dns
其他机器查看状态
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 41s
nginx-5c7588df-9xvbn 1/1 Running 0 92m
nginx-deploy-84cbfc56b6-87sgf 1/1 Running 0 42m
[root@master ingress-nginx]# 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 这里的内部dns信息
options ndots:5
[root@node01 ~]# dig -t nginx.default.svc.cluster.local @10.96.0.10 使用这个dns就可以解析
;; Warning, ignoring invalid type nginx.default.svc.cluster.local
; <<>> DiG 9.9.4-RedHat-9.9.4-73.el7_6 <<>> -t nginx.default.svc.cluster.local @10.96.0.10
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53567
;; flags: qr rd ra; QUERY: 1, ANSWER: 13, AUTHORITY: 0, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;. IN NS
;; ANSWER SECTION:
. 30 IN NS f.root-servers.net.
. 30 IN NS k.root-servers.net.
. 30 IN NS e.root-servers.net.
. 30 IN NS g.root-servers.net.
. 30 IN NS j.root-servers.net.
. 30 IN NS b.root-servers.net.
. 30 IN NS i.root-servers.net.
. 30 IN NS c.root-servers.net.
. 30 IN NS m.root-servers.net.
. 30 IN NS h.root-servers.net.
. 30 IN NS l.root-servers.net.
. 30 IN NS a.root-servers.net.
. 30 IN NS d.root-servers.net.
;; ADDITIONAL SECTION:
a.root-servers.net. 30 IN A 198.41.0.4
b.root-servers.net. 30 IN AAAA 2001:500:200::b
;; Query time: 5 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Thu Feb 28 10:45:43 EST 2019
;; MSG SIZE rcvd: 511
[root@node01 ~]#
/ # wget nginx
Connecting to nginx (10.98.181.177:80)
index.html 100% |***********************************************************************************************************************************************|
这里我把pod删除,他会自动重新创建,在内部访问nginx 还是通,说明他内部dns ok
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 41s
nginx-5c7588df-9xvbn 1/1 Running 0 92m
nginx-deploy-84cbfc56b6-87sgf 1/1 Running 0 42m
[root@master ~]# kubectl delete pod nginx-5c7588df-9xvbn
pod "nginx-5c7588df-9xvbn" deleted
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 13m
nginx-5c7588df-d6bfp 1/1 Running 0 31s
nginx-deploy-84cbfc56b6-87sgf 1/1 Running 0 55m
index.html 100% |***********************************************************************************************************************************************| 612 0:00:0
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 16m
nginx-5c7588df-d6bfp 1/1 Running 0 4m29s
nginx-deploy-84cbfc56b6-87sgf 1/1 Running 0 59m
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
client 1/1 Running 0 17m 10.244.2.4 node02 <none> <none>
nginx-5c7588df-d6bfp 1/1 Running 0 4m38s 10.244.1.3 node01 <none> <none>
nginx-deploy-84cbfc56b6-87sgf 1/1 Running 0 59m 10.244.1.2 node01 <none> <none>
[root@master ~]# kubectl describe svc nginx
Name: nginx
Namespace: default
Labels: app=nginx
Annotations: <none>
Selector: app=nginx 标签
Type: NodePort
IP: 10.98.181.177
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 31884/TCP
Endpoints: 10.244.1.3:80 这里对应关系很清楚,后端是那个
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
pod和service之间的关系如上
-----------------------------------------
随时可以改变对外地址
kubectl eidt svc nginx
将上面的IP10.98.181.177修改为188
这里没有变化,不知道是不是不支持动态修改???
换一个方式测试
[root@master ~]# kubectl delete svc nginx-deploy
service "nginx-deploy" deleted
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h50m
nginx NodePort 10.98.181.177 <none> 80:31884/TCP 125m
[root@master ~]# kubectl expose deployment nginx-deploy --name=nginx
Error from server (AlreadyExists): services "nginx" already exists
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h51m
nginx NodePort 10.98.181.177 <none> 80:31884/TCP 126m
nginx-deploy ClusterIP 10.100.251.191 <none> 80/TCP 5s
/ # wget -O - -q nginx-deploy
Connecting to nginx-deploy (10.100.251.191:80)
index.html 100% |***********************************************************************************************************************************************| 612 0:00:00 ETA
/ #
service地址存在的意义就是这个,只要服务在,这些信息就动态反映到dns中去
标签选择器
[root@master ~]# kubectl describe deployment nginx-deploy
Name: nginx-deploy
Namespace: default
CreationTimestamp: Thu, 28 Feb 2019 09:51:39 -0500
Labels: run=nginx-deploy
Annotations: deployment.kubernetes.io/revision: 1
Selector: run=nginx-deploy 标签选择器
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: run=nginx-deploy
Containers:
nginx-deploy:
Image: nginx:1.14-alpine
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deploy-84cbfc56b6 (1/1 replicas created)
Events: <none>
创建两台
[root@master ~]# kubectl run wolf --image=busybox --replicas=2
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/wolf created
[root@master ~]# kubectl get svc,pods
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 27h
service/nginx NodePort 10.98.181.177 <none> 80:31884/TCP 25h
service/nginx-deploy ClusterIP 10.100.251.191 <none> 80/TCP 23h
NAME READY STATUS RESTARTS AGE
pod/client 1/1 Running 0 23h
pod/myapp-9b4987d5-7qrh4 1/1 Running 0 17m
pod/nginx-5c7588df-d6bfp 1/1 Running 0 23h
pod/nginx-deploy-84cbfc56b6-87sgf 1/1 Running 0 24h
pod/wolf-57dbdfc67c-8pc96 0/1 CrashLoopBackOff 7 14m
pod/wolf-57dbdfc67c-wzmn8 0/1 CrashLoopBackOff 7 14m
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 23h
myapp-9b4987d5-7qrh4 1/1 Running 0 17m
nginx-5c7588df-d6bfp 1/1 Running 0 23h
nginx-deploy-84cbfc56b6-87sgf 1/1 Running 0 24h
wolf-57dbdfc67c-8pc96 0/1 CrashLoopBackOff 7 14m
wolf-57dbdfc67c-wzmn8 0/1 CrashLoopBackOff 7 14m
[root@master ~]# kubectl delete pods wolf-57dbdfc67c-8pc96
pod "wolf-57dbdfc67c-8pc96" deleted
[root@master ~]# kubectl delete pods wolf-57dbdfc67c-wzmn8
pod "wolf-57dbdfc67c-wzmn8" deleted
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 23h
myapp-9b4987d5-7qrh4 1/1 Running 0 18m
nginx-5c7588df-d6bfp 1/1 Running 0 23h
nginx-deploy-84cbfc56b6-87sgf 1/1 Running 0 24h
wolf-57dbdfc67c-c85vg 0/1 Completed 2 33s
wolf-57dbdfc67c-pqcsb 0/1 CrashLoopBackOff 1 20s
[root@master ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
myapp 1/1 1 1 32m
nginx 1/1 1 1 25h
nginx-deploy 1/1 1 1 24h
wolf 0/2 2 0 29m
[root@master ~]# kubectl delete deployment wolf
deployment.extensions "wolf" deleted
[root@master ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
myapp 1/1 1 1 34m
nginx 1/1 1 1 25h
nginx-deploy 1/1 1 1 24h
[root@master ~]#
从新创建两个
[root@master ~]# kubectl run nginx --image=nginx:1.14-alpine --port=80 --replicas=2
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created
[root@master ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 2/2 2 2 11s
nginx-deploy 1/1 1 1 25h
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-7849c4bbcd-d492t 1/1 Running 0 53s 10.244.2.11 node02 <none> <none>
nginx-7849c4bbcd-g7r2n 1/1 Running 0 53s 10.244.1.9 node01 <none> <none>
nginx-deploy-84cbfc56b6-mjcw5 1/1 Running 0 43m 10.244.2.9 node02 <none> <none>
找一台docker容器进去ping
[root@node02 ~]# docker exec -it 9c3bd3d9c78c /bin/sh
/ # ping 10.244.2.11
PING 10.244.2.11 (10.244.2.11): 56 data bytes
64 bytes from 10.244.2.11: seq=0 ttl=64 time=0.222 ms
64 bytes from 10.244.2.11: seq=1 ttl=64 time=0.068 ms
^C
--- 10.244.2.11 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.068/0.145/0.222 ms
/ # ping 10.244.1.9
PING 10.244.1.9 (10.244.1.9): 56 data bytes
64 bytes from 10.244.1.9: seq=0 ttl=62 time=0.525 ms
^C
--- 10.244.1.9 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.525/0.525/0.525 ms
[root@master ~]# kubectl expose deployment nginx --name=nginx --port=80
service/nginx exposed
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 28h
nginx ClusterIP 10.105.107.102 <none> 80/TCP 6s
nginx-deploy ClusterIP 10.100.251.191 <none> 80/TCP 24h
wget -O - -q nginx
先进入容器编辑nginx index.html文件
随便进入一个容器
[root@node02 ~]# docker exec -it 9c3bd3d9c78c /bin/sh
/ # wget -O - -q nginx
wolf1
/ # wget -O - -q nginx
laolang
/ # wget -O - -q nginx
laolang
/ #
/ # wget -O - -q nginx
laolang
/ # while true;do wget -O - -q nginx/index.html;sleep 1;done
laolang
wolf1
laolang
wolf1
wolf1
laolang
laolang
wolf1
wolf1
动态扩展
[root@master ~]# kubectl scale --replicas=5 deployment nginx
deployment.extensions/nginx scaled
/ # while true;do wget -O - -q nginx/index.html;sleep 1;done
1
1
1
laolang
laolang
laolang
2
5
2
wolf1
5
1
2
wolf1
2
动态缩减
[root@master ~]# kubectl scale --replicas=3 deployment nginx
deployment.extensions/nginx scaled
/ # while true;do wget -O - -q nginx/index.html;sleep 1;done
2
laolang
wolf1
wolf1
wolf1
wolf1
2
wolf1
wolf1
wolf1
laolang
wolf1
wolf1
2
laolang
wolf1
wolf1
2
laolang
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-7849c4bbcd-d492t 1/1 Running 0 24m
nginx-7849c4bbcd-g7r2n 1/1 Running 0 24m
nginx-7849c4bbcd-szsvq 1/1 Running 0 6m39s
nginx-deploy-84cbfc56b6-mjcw5 1/1 Running 0 67
这里就说明了server ip的作用了
滚动升级
kubectl set image deployment nginx nginx=nginx 这个环境没准备容器,感受一下过程
kubectl rollout status deployment nginx
[root@master ~]# kubectl set image deployment nginx nginx=nginx
deployment.extensions/nginx image updated
[root@master ~]# kubectl rollout status deployment nginx
deployment "nginx" successfully rolled out
再访问,已经覆盖我刚才手工修改的文件了
/ # while true;do wget -O - -q nginx/index.html;sleep 1;done
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
pods名称全部改变
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-775ff75bc8-lzrts 1/1 Running 0 2m58s
nginx-775ff75bc8-nj2m6 1/1 Running 0 2m56s
nginx-775ff75bc8-q857h 1/1 Running 0 3m
nginx-deploy-84cbfc56b6-mjcw5 1/1 Running 0 74m
默认回滚到上一个版本
kubectl rollout undo deployment nginx
[root@master ~]# kubectl rollout undo deployment nginx
deployment.extensions/nginx rolled back
群名称:k8s学习群 群 号:153144292