前提规划
主机规划
主机名称 | IP | 主机版本 |
master | 10.0.0.100 | centos7.3 |
node01 | 10.0.0.101 | centos7.3 |
node02 | 10.0.0.102 | centos7.3 |
软件规划
软件名称 | 版本信息 |
docker | 18.09.2 |
kube-apiserver | 1.13.3 |
kube-controller-manager | 1.13.3 |
kube-proxy | 1.13.3 |
kube-scheduler | 1.13.3 |
flannel | 0.11.0-amd64 |
coredns | 1.2.6 |
etcd | 3.2.24 |
pause | 3.1 |
快速入门
kubectl命令
[root@master ~]# kubectl --help
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 使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的
Kubernetes Service
run 在集群中运行一个指定的镜像
set 为 objects 设置一个指定的特征
Basic Commands (Intermediate):
explain 查看资源的文档
get 显示一个或更多 resources
edit 在服务器上编辑一个资源
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 为 Deployment, ReplicaSet, Replication Controller 或者 Job 设置一个新的副本数量
autoscale 自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController 的副本数量
Cluster Management Commands:
certificate 修改 certificate 资源.
cluster-info 显示集群信息
top Display Resource (CPU/Memory/Storage) usage.
cordon 标记 node 为 unschedulable
uncordon 标记 node 为 schedulable
drain Drain node in preparation for maintenance
taint 更新一个或者多个 node 上的 taints
Troubleshooting and Debugging Commands:
describe 显示一个指定 resource 或者 group 的 resources 详情
logs 输出容器在 pod 中的日志
attach Attach 到一个运行中的 container
exec 在一个 container 中执行一个命令
port-forward Forward one or more local ports to a pod
proxy 运行一个 proxy 到 Kubernetes API server
cp 复制 files 和 directories 到 containers 和从容器中复制 files 和 directories.
auth Inspect authorization
Advanced Commands:
diff Diff live version against would-be applied version
apply 通过文件名或标准输入流(stdin)对资源进行配置
patch 使用 strategic merge patch 更新一个资源的 field(s)
replace 通过 filename 或者 stdin替换一个资源
wait Experimental: Wait for a specific condition on one or many resources.
convert 在不同的 API versions 转换配置文件
Settings Commands:
label 更新在这个资源上的 labels
annotate 更新一个资源的注解
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 修改 kubeconfig 文件
plugin Provides utilities for interacting with plugins.
version 输出 client 和 server 的版本信息
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命令下的选项还有众多的子选项,查看方式如:[root@master ~]# kubectl get --help
查看master节点和node节点的版本信息
[root@master ~]# 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 ~]# kubectl version --help
Print the client and server version information for the current context
Examples:
# Print the client and server versions for the current context
kubectl version
Options:
--client=false: Client version only (no server required).
-o, --output='': One of 'yaml' or 'json'.
--short=false: Print just the version number.
Usage:
kubectl version [flags] [options]
Use "kubectl options" for a list of global command-line options (applies to all commands).
[root@master ~]# kubectl version --short=true
Client Version: v1.13.3
Server Version: v1.13.3
查看节点的所有信息:
[root@master ~]# 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":"8a:06:d9:01:6f:3f"}
flannel.alpha.coreos.com/backend-type: vxlan
flannel.alpha.coreos.com/kube-subnet-manager: true
flannel.alpha.coreos.com/public-ip: 192.168.43.117
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: Mon, 18 Feb 2019 10:47:30 +0800
Taints: node-role.kubernetes.io/master:NoSchedule 污点
Unschedulable: false
Conditions:
Type Status LastHeartbeatTime LastTransitionTime Reason Message
---- ------ ----------------- ------------------ ------ -------
MemoryPressure False Thu, 21 Feb 2019 09:17:44 +0800 Mon, 18 Feb 2019 10:47:20 +0800 KubeletHasSufficientMemory kubelet has sufficient memory available
DiskPressure False Thu, 21 Feb 2019 09:17:44 +0800 Mon, 18 Feb 2019 10:47:20 +0800 KubeletHasNoDiskPressure kubelet has no disk pressure
PIDPressure False Thu, 21 Feb 2019 09:17:44 +0800 Mon, 18 Feb 2019 10:47:20 +0800 KubeletHasSufficientPID kubelet has sufficient PID available
Ready True Thu, 21 Feb 2019 09:17:44 +0800 Mon, 18 Feb 2019 11:11:22 +0800 KubeletReady kubelet is posting ready status
Addresses:
InternalIP: 10.0.0.100 节点IP
Hostname: master
Capacity:
cpu: 2
ephemeral-storage: 18418Mi
hugepages-1Gi: 0
hugepages-2Mi: 0
memory: 2032156Ki
pods: 110
Allocatable:
cpu: 2
ephemeral-storage: 17381405463
hugepages-1Gi: 0
hugepages-2Mi: 0
memory: 1929756Ki
pods: 110
System Info:
Machine ID: 7e42852c170e4b5aba6718b05ca262c0
System UUID: B9A74D56-32D4-0917-51DE-01E6A67BB055
Boot ID: 62c0d7d6-4cba-4fb7-b7a4-04637ac989d1
Kernel Version: 3.10.0-514.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-62t8f 100m (5%) 0 (0%) 70Mi (3%) 170Mi (9%) 2d22h
kube-system coredns-86c58d9df4-k4twg 100m (5%) 0 (0%) 70Mi (3%) 170Mi (9%) 2d22h
kube-system etcd-master 0 (0%) 0 (0%) 0 (0%) 0 (0%) 2d22h
kube-system kube-apiserver-master 250m (12%) 0 (0%) 0 (0%) 0 (0%) 2d22h
kube-system kube-controller-manager-master 200m (10%) 0 (0%) 0 (0%) 0 (0%) 2d22h
kube-system kube-flannel-ds-amd64-zkn9k 100m (5%) 100m (5%) 50Mi (2%) 50Mi (2%) 2d22h
kube-system kube-proxy-4dqgs 0 (0%) 0 (0%) 0 (0%) 0 (0%) 2d22h
kube-system kube-scheduler-master 100m (5%) 0 (0%) 0 (0%) 0 (0%) 2d22h
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
Resource Requests Limits
-------- -------- ------
cpu 850m (42%) 100m (5%)
memory 190Mi (10%) 390Mi (20%)
ephemeral-storage 0 (0%) 0 (0%)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal NodeReady 2d22h kubelet, master Node master status is now: NodeReady
Normal Starting 2d21h kube-proxy, master Starting kube-proxy.
Normal Starting 35m kubelet, master Starting kubelet.
Normal NodeHasSufficientMemory 35m (x8 over 35m) kubelet, master Node master status is now: NodeHasSufficientMemory
Normal NodeHasNoDiskPressure 35m (x8 over 35m) kubelet, master Node master status is now: NodeHasNoDiskPressure
Normal NodeHasSufficientPID 35m (x7 over 35m) kubelet, master Node master status is now: NodeHasSufficientPID
Normal NodeAllocatableEnforced 35m kubelet, master Updated Node Allocatable limit across pods
Normal Starting 34m kube-proxy, master Starting kube-proxy.
集群DNS信息:
[root@master ~]# kubectl cluster-info
Kubernetes master is running at https://192.168.43.117:6443
KubeDNS is running at https://192.168.43.117:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
创建一个干跑的nginx应用,版本是nginx1.14-alpine,对外的端口是80,创建1个
[root@master ~]# kubectl run nginx-deploy --image=nginx1.14-alpine --port=80 --replicas=1 --dry-run=true
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generv1 or kubectl create instead.
deployment.apps/nginx-deploy created (dry run)
注: (dry run)->并没有执行,只是干跑一遍
创建一个nginx应用,版本是nginx1.14-alpine,对外的端口是80,创建1个
[root@master ~]# kubectl run nginx-deploy --image=nginx-1.4-alpine --port=80 --replicas=1
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-deploy created
查看创建的应用的情况
[root@master ~]# kubectl get deployment -w
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 0/1 1 0 21s
nginx-deploy 1/1 1 1 62s
-w:监控
DESIRED:期望创建数
CURRENT:创建了几个
UP-TO-DATE:最新状态
AVAILABLE:可以的是几个
查看pod信息
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deploy-84cbfc56b6-h5vd6 1/1 Running 0 3m44s
显示控制器的更多信息
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deploy-84cbfc56b6-h5vd6 1/1 Running 0 3m57s 10.244.2.2 node02 <none> <none>
注:上面可以看到nginx项目的地址是10.244.2.2,在任意的node节点上都可以访问,10.244.2.2在每个node上都可以访问,在kubernetes的外部是不能访问的
通过查看网络我们发现:
master:cni0 10.244.0.1
node1: cni0 10.244.1.1
node2: cni0 10.244.2.1
我们发现10.244.网络是一个大网,每个node上时不同的网段,每个node上都有一个10.244.x.1的子网,保证不会冲突!
删除一个pod
[root@master ~]# kubectl delete pods nginx-deploy-84cbfc56b6-h5vd6
pod "nginx-deploy-84cbfc56b6-h5vd6" deleted
注:删除后,再用kubectl get pods 查看会发现自动重新创建了一个nginx-deploy-84cbfc56b6-XXXXX的pod,这正好体现出了kubernetes的一个特性:自我修复
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deploy-84cbfc56b6-9cw87 1/1 Running 0 6m52s 10.244.1.3 node01 <none> <none>
查看DNS
[root@master ~]# 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 2d22h
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d22h
每次删除pod都会生成一个pod,但是ip也会更改,这样很不方便,所以一般要固定IP
[root@master ~]# kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP
service/nginx exposed
在master节点上安装dns解析工具
[root@master ~]# yum install bind-utils
[root@master ~]# dig -t nginx @10.96.0.10
;; Warning, ignoring invalid type nginx
; <<>> DiG 9.9.4-RedHat-9.9.4-73.el7_6 <<>> -t nginx @10.96.0.10
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 46424
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 13, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;. IN NS
;; ANSWER SECTION:
. 30 IN NS h.root-servers.net.
. 30 IN NS g.root-servers.net.
. 30 IN NS i.root-servers.net.
. 30 IN NS f.root-servers.net.
. 30 IN NS b.root-servers.net.
. 30 IN NS a.root-servers.net.
. 30 IN NS e.root-servers.net.
. 30 IN NS c.root-servers.net.
. 30 IN NS j.root-servers.net.
. 30 IN NS l.root-servers.net.
. 30 IN NS k.root-servers.net.
. 30 IN NS d.root-servers.net.
. 30 IN NS m.root-servers.net.
;; Query time: 100 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: 四 2月 21 09:47:21 CST 2019
;; MSG SIZE rcvd: 431
再创建一个pod测试连接
[root@master ~]# 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
options ndots:5
注:上面的没有给完整的域名,我们在master上重新设置一下域名的解析
再起一个窗口:
[root@master ~]# dig -t A nginx.default.svc.cluster.local @10.96.0.10
; <<>> DiG 9.9.4-RedHat-9.9.4-73.el7_6 <<>> -t A nginx.default.svc.cluster.local @10.96.0.10
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13027
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;nginx.default.svc.cluster.local. IN A
;; ANSWER SECTION:
nginx.default.svc.cluster.local. 5 IN A 10.111.232.218
;; Query time: 1 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: 四 2月 21 09:52:57 CST 2019
;; MSG SIZE rcvd: 96
再次回到busybox pod上
/ # wget nginx
Connecting to nginx (10.111.232.218:80)
index.html 100% |************************************************************************| 612 0:00:00 ETA
是可以解析nginx的
注:因为busybox里面没有curl,我们可以尝试wget -O - -q http://nginx:80 ,这样可以直接打印页面
/ # wget -O - -q http://nginx:80/
<!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>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
测试:删除nginx pod 后是否原地址访问成功
获取pods
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 7m32s
nginx-deploy-84cbfc56b6-9cw87 1/1 Running 0 20m
删除nginx pod
[root@master ~]# kubectl delete pods nginx-deploy-84cbfc56b6-9cw87
pod "nginx-deploy-84cbfc56b6-9cw87" deleted
自动重新创建nginx pod
[root@master ~]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 8m9s
nginx-deploy-84cbfc56b6-h5h7x 1/1 Running 0 16s
在busybox pod 中访问
/ # wget -O - -q http://nginx:80/
<!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>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
注:测试证明还是可以访问的,其实:我们关注的并不是Pod本身,而是service,创建的nginx会生成一个ipvs规则,把访问的pod地址都调度到lables标签选择器上
查看nginx pod的详细信息
[root@master ~]# kubectl describe svc nginx
Name: nginx
Namespace: default
Labels: run=nginx-deploy #我们删除pod时,自动创建pod会自动带有这个标签
Annotations: <none>
Selector: run=nginx-deploy #标签选择器
Type: ClusterIP
IP: 10.111.232.218
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.2.5:80
Session Affinity: None
Events: <none>
编辑nginx服务
[root@master ~]# kubectl edit svc 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-02-21T01:43:19Z"
labels:
run: nginx-deploy
name: nginx
namespace: default
resourceVersion: "14958"
selfLink: /api/v1/namespaces/default/services/nginx
uid: 10e6771a-357a-11e9-8e77-000c297bb055
spec:
clusterIP: 10.111.232.218
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: nginx-deploy
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
删除nginx服务
删除nginx服务
[root@master ~]# kubectl delete svc nginx
service "nginx" deleted
删除成功
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d23h
再次创建一个nginx 服务
[root@master ~]# kubectl expose deployment nginx-deploy --name=nginx
service/nginx exposed
在Buxybox pod上还是可以访问
/ # wget -O - -q http://nginx:80/
<h1>Welcome to nginx!</h1>
查看nginx 服务的详细信息
[root@master ~]# kubectl describe deployment nginx-deploy
Name: nginx-deploy
Namespace: default
CreationTimestamp: Thu, 21 Feb 2019 09:29:17 +0800
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:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 40m deployment-controller Scaled up replica set nginx-deploy-84cbfc56b6 to 1
创建两个pod进行测试
创建两个myapp pod
[root@master ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --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/myapp created
查看myapp 创建的情况
[root@master ~]# kubectl get deployment -w
NAME READY UP-TO-DATE AVAILABLE AGE
myapp 2/2 2 2 13s
nginx-deploy 1/1 1 1 45m
查看myapp 的详细信息
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
client 1/1 Running 0 24m 10.244.2.4 node02 <none> <none>
myapp-9b4987d5-bmms4 1/1 Running 0 29s 10.244.2.6 node02 <none> <none>
myapp-9b4987d5-f2m9d 1/1 Running 0 29s 10.244.1.4 node01 <none> <none>
nginx-deploy-84cbfc56b6-h5h7x 1/1 Running 0 16m 10.244.2.5 node02 <none> <none>
在buxybox上测试访问
/ # wget -O - -q 10.244.1.4
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
/ # wget -O - -q 10.244.2.6
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
/ # wget -O - -q 10.244.1.4/hostname.html
myapp-9b4987d5-f2m9d
给myapp创建一个service
[root@master ~]# kubectl expose deployment myapp --name=myapp --port=80
service/myapp exposed
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d23h
myapp ClusterIP 10.108.224.2 <none> 80/TCP 7s
nginx ClusterIP 10.107.43.244 <none> 80/TCP 6m26s
/ # wget -O - -q 10.108.224.2/hostname.html
myapp-9b4987d5-f2m9d
/ # wget -O - -q 10.108.224.2/hostname.html
myapp-9b4987d5-bmms4
/ # wget -O - -q 10.108.224.2/hostname.html
myapp-9b4987d5-f2m9d
/ # wget -O - -q myapp/hostname.html
myapp-9b4987d5-bmms4
/ # wget -O - -q myapp/hostname.html
myapp-9b4987d5-bmms4
/ # wget -O - -q myapp/hostname.html
myapp-9b4987d5-f2m9d
/ # wget -O - -q myapp/hostname.html
myapp-9b4987d5-f2m9d
循环执行:
/ # while true;do wget -O - -q myapp/hostname.html;sleep 2;done
myapp-9b4987d5-f2m9d
myapp-9b4987d5-bmms4
注:上述测试可以看出来kubernetes的另一个特性:服务发现和负载均衡
将myapp项目动态扩容到5个
[root@master ~]# kubectl scale --replicas=5 deployment myapp
deployment.extensions/myapp scaled
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 35m
myapp-9b4987d5-2bsq6 0/1 ContainerCreating 0 7s
myapp-9b4987d5-bmms4 1/1 Running 0 12m
myapp-9b4987d5-f2m9d 1/1 Running 0 12m
myapp-9b4987d5-gjgkt 0/1 ContainerCreating 0 7s
myapp-9b4987d5-wp5r2 0/1 ContainerCreating 0 7s
nginx-deploy-84cbfc56b6-h5h7x 1/1 Running 0 27m
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 35m
myapp-9b4987d5-2bsq6 1/1 Running 0 12s
myapp-9b4987d5-bmms4 1/1 Running 0 12m
myapp-9b4987d5-f2m9d 1/1 Running 0 12m
myapp-9b4987d5-gjgkt 1/1 Running 0 12s
myapp-9b4987d5-wp5r2 1/1 Running 0 12s
nginx-deploy-84cbfc56b6-h5h7x 1/1 Running 0 28m
/ # while true;do wget -O - -q myapp/hostname.html;sleep 2;done
myapp-9b4987d5-bmms4
myapp-9b4987d5-wp5r2
myapp-9b4987d5-wp5r2
myapp-9b4987d5-gjgkt
myapp-9b4987d5-gjgkt
myapp-9b4987d5-f2m9d
myapp-9b4987d5-bmms4
将myapp项目动态缩容到4个
[root@master ~]# kubectl scale --replicas=4 deployment myapp
deployment.extensions/myapp scaled
[root@master ~]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 37m
myapp-9b4987d5-2bsq6 1/1 Running 0 2m15s
myapp-9b4987d5-bmms4 1/1 Running 0 14m
myapp-9b4987d5-f2m9d 1/1 Running 0 14m
myapp-9b4987d5-wp5r2 1/1 Running 0 2m15s
nginx-deploy-84cbfc56b6-h5h7x 1/1 Running 0 30m
注:上述测试可以看出来kubernetes的另一个特性:水平扩展
做滚动升级和降级
在busybox上运行
/ # while true;do wget -O - -q myapp;sleep 2;done
查看某一个容器的详细信息
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 41m
myapp-9b4987d5-2bsq6 1/1 Running 0 5m29s
myapp-9b4987d5-bmms4 1/1 Running 0 17m
myapp-9b4987d5-f2m9d 1/1 Running 0 17m
myapp-9b4987d5-wp5r2 1/1 Running 0 5m29s
nginx-deploy-84cbfc56b6-h5h7x 1/1 Running 0 33m
[root@master ~]# kubectl describe pods myapp-9b4987d5-f2m9d
Name: myapp-9b4987d5-f2m9d
docker://834060a775c288d78ff4ddd5bd3b73d751ecda3b5a8dec30f3830b79dd6e53f0
Image: ikubernetes/myapp:v1
改变镜像的版本换成最新的
[root@master ~]# kubectl set image deployment myapp myapp=ikubernetes/myapp:v2
deployment.extensions/myapp image updated
查看更新情况
[root@master ~]# kubectl rollout status deployment myapp
deployment "myapp" successfully rolled out
在busybox pod中开始灰度发布
/ # while true;do wget -O - -q myapp;sleep 2;done
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
版本发布错误后回滚到上一个版本
1,kubectl rollout undo deployment myapp
2,执行刚刚发布v2的命令,将v2换成v1即可
[root@master ~]# kubectl rollout undo deployment myapp
deployment.extensions/myapp rolled back
[root@master ~]# kubectl rollout status deployment myapp
deployment "myapp" successfully rolled out
/ # while true;do wget -O - -q myapp;sleep 2;done
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
查看iptables的自动生成的规则
[root@master ~]# iptables -vnL -t nat
我们在外部访问,改怎么办呢?
将type: ClusterIP更改为NodePort
[root@master ~]# kubectl edit svc myapp
service/myapp edited
查看一下
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d23h
myapp NodePort 10.108.224.2 <none> 80:31318/TCP 18m
nginx ClusterIP 10.107.43.244 <none> 80/TCP 25m
我们发现多了一个端口
任意节点IP+随机端口号都可以在外部浏览器上访问!!!