一.调试工具利器 kubectl-debug使用
1.安装
- https://github.com/aylei/kubectl-debug
- https://github.com/aylei/kubectl-debug/releases
export PLUGIN_VERSION=0.1.1
wget -c https://github.com/aylei/kubectl-debug/releases/download/v0.1.1/kubectl-debug_0.1.1_linux_amd64.tar.gz
tar -zxvf kubectl-debug.tar.gz kubectl-debug
mv kubectl-debug /usr/local/bin/
kubectl apply -f https://raw.githubusercontent.com/aylei/kubectl-debug/master/scripts/agent_daemonset.yml
2.基本的使用
#使用daemonset agent模式(关闭agentless模式)
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
debug-agent-fnjtn 1/1 Running 0 3h43m
debug-agent-lxbrq 1/1 Running 0 3h43m
debug-agent-ngk6t 1/1 Running 0 3h43m
nginx-ds-7jzlj 1/1 Running 1 26h
nginx-ds-snvcp 1/1 Running 1 26h
nginx-test-6fbf8d98b8-h58z7 1/1 Running 0 59m
[root@k8s-master ~]# kubectl debug --agentless=false debug-agent-fnjtn
pulling image nicolaka/netshoot:latest...
latest: Pulling from nicolaka/netshoot
Digest: sha256:ea3757c995a3b538c45724cd537beeb5363cdc094209920896826082509c26a3
Status: Image is up to date for nicolaka/netshoot:latest
starting debug container...
container created, open tty...
bash-5.0# exit
exit
## 查询有哪些Pod
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
debug-agent-fnjtn 1/1 Running 0 45s
debug-agent-lxbrq 1/1 Running 0 45s
debug-agent-ngk6t 1/1 Running 0 45s
nginx-ds-7jzlj 1/1 Running 1 23h
nginx-ds-snvcp 1/1 Running 1 23h
## 开始测试目标为pod nginx-ds-snvcp
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
debug-agent-fnjtn 1/1 Running 0 22m
debug-agent-lxbrq 1/1 Running 0 22m
debug-agent-ngk6t 1/1 Running 0 22m
nginx-ds-7jzlj 1/1 Running 1 23h
nginx-ds-snvcp 1/1 Running 1 23h
[root@k8s-master ~]# kubectl debug nginx-ds-7jzlj
pulling image nicolaka/netshoot:latest...
latest: Pulling from nicolaka/netshoot
31603596830f: Pull complete
4c6128bf03c4: Pull complete
506f1f10e7b8: Pull complete
5223980a1846: Pull complete
57718fc36e2b: Pull complete
6d106e36f096: Pull complete
d1a0a123ce03: Pull complete
4fcf9ea161ff: Pull complete
Digest: sha256:ea3757c995a3b538c45724cd537beeb5363cdc094209920896826082509c26a3
Status: Downloaded newer image for nicolaka/netshoot:latest
starting debug container...
container created, open tty...
bash-5.0# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master pro
bash-5.0# ifconfig
eth0 Link encap:Ethernet HWaddr E2:BC:45:D2:76:35
inet addr:10.200.2.5 Bcast:0.0.0.0 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1450 Metric:1
RX packets:50 errors:0 dropped:0 overruns:0 frame:0
TX packets:1 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2692 (2.6 KiB) TX bytes:42 (42.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
bash-5.0# ping -c 3 www.baidu.com
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=127 time=78.8 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=127 time=35.5 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=3 ttl=127 time=53.4 ms
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 35.474/55.904/78.793/17.770 ms
bash-5.0# ps aux
PID USER TIME COMMAND
1 root 0:00 nginx: master process nginx -g daemon off;
6 101 0:00 nginx: worker process
24 root 0:00 bash
29 root 0:00 ps aux
bash-5.0# exit
exit
二.简单操作
1.创建一个应用程序
- https://juejin.im/search?query=kubernetes-Deployment&type=all
- https://www.cnblogs.com/kenken2018/p/10334565.html
- https://kubernetes.io/zh/docs/tasks/run-application/run-stateless-application-deployment/
- https://www.qikegu.com/docs/5502
## 编配.yaml文件,创建容器
[root@k8s-master ~]# kubectl apply -f nginx-1.16.1.yaml
deployment.apps/nginx-deployment created
[root@k8s-master ~]# cat nginx-1.16.1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 4
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16.1
ports:
- containerPort: 80
## 获取我刚刚部署的列表
[root@k8s-master ~]# kubectl get deployment ## 可以看到我这里部署的
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 4/4 4 4 2m12s
nginx-test 1/1 1 1 71m
[root@k8s-master ~]# kubectl describe deployment nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Fri, 29 May 2020 21:49:46 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.16.1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment-7b45d69949 (4/4 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 4m11s deployment-controller Scaled up replica set nginx-deployment-7b45d69949 to 4
##--dry-run=true:表示测试,不真正执行命令
[root@k8s-master ~]# kubectl run nginx-test --image=nginx:1.16 --port=80 --replicas=2 --dry-run=true
Flag --replicas has been deprecated, has no effect and will be removed in the future.
W0529 18:27:05.350016 15582 helpers.go:549] --dry-run=true is deprecated (boolean value) and can be replaced with --dry-run=client.
pod/nginx-test created (dry run)
##真正开始创建
[root@k8s-master ~]# kubectl run nginx-test --image=nginx:1.16 --port=80 --replicas=2
Flag --replicas has been deprecated, has no effect and will be removed in the future.
pod/nginx-test created
##查看创建的应用
##查看其生成的pod
[root@k8s-master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
debug-agent-fnjtn 1/1 Running 0 4h 10.200.2.7 k8s-node-2 <none> <none>
debug-agent-lxbrq 1/1 Running 0 4h 10.200.1.7 k8s-node-1 <none> <none>
debug-agent-ngk6t 1/1 Running 0 4h 10.200.0.6 k8s-master <none> <none>
nginx-deployment-7b45d69949-6j9dp 1/1 Running 0 6m41s 10.200.2.11 k8s-node-2 <none> <none>
nginx-deployment-7b45d69949-ll6qr 1/1 Running 0 6m41s 10.200.1.12 k8s-node-1 <none> <none>
nginx-deployment-7b45d69949-wgwdv 1/1 Running 0 6m41s 10.200.2.12 k8s-node-2 <none> <none>
nginx-deployment-7b45d69949-xcpcx 1/1 Running 0 6m41s 10.200.1.11 k8s-node-1 <none> <none>
nginx-ds-7jzlj 1/1 Running 1 27h 10.200.2.5 k8s-node-2 <none> <none>
nginx-ds-snvcp 1/1 Running 1 27h 10.200.1.6 k8s-node-1 <none> <none>
nginx-test-6fbf8d98b8-h58z7 1/1 Running 0 76m 10.200.1.10 k8s-node-1 <none> <none>
2.访问一下具体Pod地址
[root@k8s-master ~]# curl http://10.200.2.11
<!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>
3.如何让集群外部可以访问呢???
这需要创建service,然后暴露端口
##创建service,它的端口为8100,对应Pod的端口80,并使用NodePort方式暴露到集群外部
[root@k8s-master ~]# kubectl expose deployment nginx-deployment --name=nginx --port=8880 --target-port=80 --type=NodePort
service/nginx exposed
##查看创建的service,可以看到暴露到集群外的端口为32402
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.2.0.1 <none> 443/TCP 27h
nginx NodePort 10.2.207.143 <none> 8880:30206/TCP 11s
nginx-ds NodePort 10.2.100.15 <none> 80:31350/TCP 27h
4.扩容及缩减Pod数量
##扩容前先查看
[root@k8s-master ~]# kubectl get deployment nginx-deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 4/4 4 4 21m
##扩容到5个副本
[root@k8s-master ~]# kubectl scale --replicas=5 deployment nginx-deployment
deployment.apps/nginx-deployment scaled
[root@k8s-master ~]# kubectl get deployment nginx-deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 5/5 5 5 22m
##缩减到2个副本
[root@k8s-master ~]# kubectl scale --replicas=2 deployment nginx-deployment
deployment.apps/nginx-deployment scaled
[root@k8s-master ~]# kubectl get deployment nginx-deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 23m
5.滚动升级
## 修改部署的镜像版本:
[root@k8s-master ~]# kubectl set image deployment/nginx-deployment nginx=nginx:1.17.1 --record
deployment.apps/nginx-deployment image updated
## 查看修改生效状态
[root@k8s-master ~]# kubectl rollout status deployment.apps/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out
## ##观察滚动升级情况
[root@k8s-master ~]# kubectl get pod --watch
NAME READY STATUS RESTARTS AGE
debug-agent-fnjtn 1/1 Running 0 4h28m
debug-agent-lxbrq 1/1 Running 0 4h28m
debug-agent-ngk6t 1/1 Running 0 4h28m
nginx-deployment-f8c77cd9d-6gjrq 1/1 Running 0 8m10s
nginx-deployment-f8c77cd9d-xc4mx 1/1 Running 0 8m43s
nginx-ds-7jzlj 1/1 Running 1 27h
nginx-ds-snvcp 1/1 Running 1 27h
nginx-test-6fbf8d98b8-h58z7 1/1 Running 0 105m
nginx-deployment-7b45d69949-mkpxl 0/1 Pending 0 0s
nginx-deployment-7b45d69949-mkpxl 0/1 Pending 0 0s
nginx-deployment-7b45d69949-mkpxl 0/1 ContainerCreating 0 0s
nginx-deployment-7b45d69949-mkpxl 1/1 Running 0 2s
nginx-deployment-f8c77cd9d-6gjrq 1/1 Terminating 0 10m
nginx-deployment-7b45d69949-mwl6m 0/1 Pending 0 0s
nginx-deployment-7b45d69949-mwl6m 0/1 Pending 0 0s
nginx-deployment-7b45d69949-mwl6m 0/1 ContainerCreating 0 0s
nginx-deployment-f8c77cd9d-6gjrq 0/1 Terminating 0 10m
nginx-deployment-f8c77cd9d-6gjrq 0/1 Terminating 0 10m
nginx-deployment-f8c77cd9d-6gjrq 0/1 Terminating 0 10m
nginx-deployment-7b45d69949-mwl6m 1/1 Running 0 2s
nginx-deployment-f8c77cd9d-xc4mx 1/1 Terminating 0 10m
nginx-deployment-f8c77cd9d-xc4mx 0/1 Terminating 0 10m
nginx-deployment-f8c77cd9d-xc4mx 0/1 Terminating 0 10m
nginx-deployment-f8c77cd9d-xc4mx 0/1 Terminating 0 10m
##查看任意一个Pod详细信息
[root@k8s-master ~]# kubectl describe pod nginx-deployment-f8c77cd9d-xc4mx
Name: nginx-deployment-f8c77cd9d-xc4mx
Namespace: default
Priority: 0
Node: k8s-node-2/10.0.0.32
Start Time: Fri, 29 May 2020 22:16:27 +0800
Labels: app=nginx
pod-template-hash=f8c77cd9d
Annotations: <none>
Status: Running
IP: 10.200.2.14
IPs:
IP: 10.200.2.14
Controlled By: ReplicaSet/nginx-deployment-f8c77cd9d
Containers:
nginx:
Container ID: docker://32c4c5862b75f349f2f6cb0d0ddf8936202253653d161779dbd5355d4af17fca
Image: nginx:1.17.1
Image ID: docker-pullable://nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Fri, 29 May 2020 22:16:58 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-plhfn (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-plhfn:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-plhfn
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled <unknown> default-scheduler Successfully assigned default/nginx-deployment-f8c77cd9d-xc4mx to k8s-node-2
Normal Pulling 8m15s kubelet, k8s-node-2 Pulling image "nginx:1.17.1"
Normal Pulled 7m46s kubelet, k8s-node-2 Successfully pulled image "nginx:1.17.1"
Normal Created 7m45s kubelet, k8s-node-2 Created container nginx
Normal Started 7m45s kubelet, k8s-node-2 Started container nginx
6.删除以上创建的server及deployment
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.2.0.1 <none> 443/TCP 28h
nginx NodePort 10.2.207.143 <none> 8880:30206/TCP 26m
nginx-ds NodePort 10.2.100.15 <none> 80:31350/TCP 27h
[root@k8s-master ~]# kubectl delete svc nginx
service "nginx" deleted
[root@k8s-master ~]# kubectl delete svc deployment nginx-deployment
deployment.apps "nginx-deployment" deleted
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
debug-agent-fnjtn 1/1 Running 0 4h34m
debug-agent-lxbrq 1/1 Running 0 4h34m
debug-agent-ngk6t 1/1 Running 0 4h34m
nginx-ds-7jzlj 1/1 Running 1 27h
nginx-ds-snvcp 1/1 Running 1 27h
7.伸缩部署
[root@k8s-master ~]# kubectl scale deployment.apps/nginx-deployment --replicas=20
deployment.apps/nginx-deployment scaled