2024年9月12日(k8s环境及测试 常用命令)
一、环境准备及测试
1、报错处理:
kube-system calico-node-5wvln 0/1 Init:0/3 0 16h kube-system calico-node-d7xfb 0/1 Init:0/3 0 16h kube-system calico-node-k84tc 0/1 Init:0/3 0 16h
执行以下命令:更新内核版本
yum list kernel
yum update kernel
reboot
如果是cni报错,需要上传下面两个文件,然后重启
一直不成功挂起状态
再/etc/cni/net.d/中上传文件
kube-system calico-node-5wvln 0/1 Init:0/3 0 16h kube-system calico-node-d7xfb 0/1 Init:0/3 0 16h kube-system calico-node-k84tc 1/1 Running 0 16h
[root@k8s-master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master Ready control-plane 16h v1.28.2 k8s-node1 Ready <none> 16h v1.28.2 k8s-node2 Ready <none> 16h v1.28.2
现在就合适了
kubectl 无法补全报错ku-bash: _get_comp_words_by_ref: command not found
[root@k8s-master ~]# yum -y install bash-completion
[root@k8s-master ~]# bash /usr/share/bash-completion/bash_completion
[root@k8s-master ~]# bash
2、测试:
创建nginx镜像名称为nginx0
[root@k8s-master ~]# kubectl run nginx0 --image=nginx:latest
pod/nginx0 created
[root@k8s-master ~]# kubectl get po -Aowidedefault nginx0 0/1 ContainerCreating 0 29s <none> k8s-node2 <none> <none>
查看日志
[root@k8s-master ~]# kubectl logs nginx0
Error from server (BadRequest): container "nginx0" in pod "nginx0" is waiting to start: ContainerCreating
[root@k8s-master ~]# kubectl run nginx --image=nginx 不指定版本[root@k8s-master ~]# kubectl delete pod nginx 删除pod
[root@k8s-master ~]# kubectl get po -Aowide|grep nginx default nginx 0/1 ContainerCreating 0 5s <none> k8s-node2 <none> <none> default nginx0 0/1 ContainerCreating 0 6m59s <none> k8s-node2 <none> <none>
3、部署资源监控metries-server
(1)复制密钥文件到node1和node2
[root@k8s-master ~]# scp /etc/kubernetes/pki/front-proxy-ca.crt k8s-node1:/etc/kubernetes/pki/front-proxy-ca.crt
[root@k8s-master ~]# scp /etc/kubernetes/pki/front-proxy-ca.crt k8s-node2:/etc/kubernetes/pki/front-proxy-ca.crt
(2)上传文件
将components.yaml文件上传到家目录
[root@k8s-master ~]# mkdir pods
[root@k8s-master ~]# mv components.yaml pods/
(3)根据yaml文件创建pod
[root@k8s-master pods]# kubectl create -f components.yaml
[root@k8s-master pods]# kubectl top nodes
(4)查看pod列表状态
[root@k8s-master pods]# kubectl get pods -Aowide|grep metrics
kube-system metrics-server-79776b6d54-xfv8d 1/1 Running 0 5m58s 172.16.169.129 k8s-node2 <none> <none>
(5)查看节点资源监控
[root@k8s-master pods]# kubectl top nodes 查看节点
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% k8s-master 80m 4% 1087Mi 63% k8s-node1 34m 1% 859Mi 49% k8s-node2 32m 1% 825Mi 48%
[root@k8s-master pods]# kubectl top pods -n kube-system 查看kube-system命名空间的所有节点
NAME CPU(cores) MEMORY(bytes) calico-kube-controllers-6d48795585-pfwrj 2m 13Mi calico-node-5wvln 15m 93Mi calico-node-d7xfb 15m 90Mi calico-node-k84tc 16m 94Mi coredns-6554b8b87f-bbnh7 1m 11Mi coredns-6554b8b87f-z9rvq 1m 12Mi etcd-k8s-master 10m 79Mi kube-apiserver-k8s-master 24m 313Mi kube-controller-manager-k8s-master 7m 87Mi kube-proxy-644jq 1m 27Mi kube-proxy-65xl7 1m 25Mi kube-proxy-7thhh 1m 35Mi kube-scheduler-k8s-master 2m 49Mi metrics-server-79776b6d54-xfv8d 2m 17Mi
4、Dashboard部署
[root@k8s-master ~]# cd k8s-ha-install/
[root@k8s-master k8s-ha-install]# ls
[root@k8s-master k8s-ha-install]# cd dashboard/
[root@k8s-master dashboard]# ls
dashboard-user.yaml dashboard.yaml[root@k8s-master dashboard]# kubectl create -f . .表示用当前目录
[root@k8s-master dashboard]# kubectl get po -A|grep dashboard
[root@k8s-master dashboard]# kubectl get po -A|grep dashboard kubernetes-dashboard dashboard-metrics-scraper-7b554c884f-xnch2 1/1 Running 0 10m kubernetes-dashboard kubernetes-dashboard-54b699784c-z7444 1/1 Running 0 10m
5、更改svc模式
[root@k8s-master dashboard]# kubectl edit svc kubernetes-dashboard -n kubernetes-dashboard
[root@k8s-master dashboard]# kubectl get svc kubernetes-dashboard -n kubernetes-dashboard
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes-dashboard NodePort 10.96.160.87 <none> 443:30965/TCP 8m42s
6、创建登录token
[root@k8s-master dashboard]# kubectl create token admin-user -n kube-system
eyJhbGciOiJSUzI1NiIsImtpZCI6IkpfZklrNGlnY0JGWTB2Zmdxbm40N0JtMkdnQWR6RkphZ25KVEdUTnkxNWMifQ.eyJhdWQiOlsiaHR0cHM6Ly9rdWJlcm5ldGVzLmRlZmF1bHQuc3ZjLmNsdXN0ZXIubG9jYWwiXSwiZXhwIjoxNzI2MTE1NTQ2LCJpYXQiOjE3MjYxMTE5NDYsImlzcyI6Imh0dHBzOi8va3ViZXJuZXRlcy5kZWZhdWx0LnN2Yy5jbHVzdGVyLmxvY2FsIiwia3ViZXJuZXRlcy5pbyI6eyJuYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsInNlcnZpY2VhY2NvdW50Ijp7Im5hbWUiOiJhZG1pbi11c2VyIiwidWlkIjoiYTVlNDQ3OWYtNzFiOS00YWQyLTkxMTYtOWI5NTNlYmE3ODk1In19LCJuYmYiOjE3MjYxMTE5NDYsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTphZG1pbi11c2VyIn0.Xo98HIHqp_jSUhbkjyGDeCM0rzeleSi7HXDW-FRTS8DcqeY2Azgwxjjm_nEA9koxrJRtkr2F5Cv2IYTQYHVrsEbtnHk8aG_KXICeLdcKVcYEIzI7MMrKatinfLHwgrIvFM6xNgPEZleA7X0g49FRlZ7ZCHGLghIWpzyHn78ZDxuXWfsE-yaTxvVc4AbrsUR2c1xMGskXXIjmCj8yZ3HkUbDsuLhOFBinMi_BgWXTHh63wiveKpHmfClsKwhBb7Q3QjFqzdYr9veTxgbUbA95-eKZ4XLYZim1TEpvFgi4zDhkW8OFzaFfGlhxvP7DTmax7qp-46mRq_VBdDUiCdoQdw
7、更新 Kube-Proxy 的 Pod
[root@k8s-master ~]# kubectl edit cm kube-proxy -n kube-system
configmap/kube-proxy edited[root@k8s-master ~]# kubectl patch daemonset kube-proxy -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"`date +'%s'`\"}}}}}" -n kube-system
[root@k8s-master ~]# curl 127.0.0.1:10249/proxyMode
ipvs
查看service pod 宿主机的网段
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 21h
8、验证是否可正常创建参数
[root@k8s-master ~]# kubectl create deploy cluster-test1 --image=registry.cn-beijing.aliyuncs.com/dotbalo/debug-tools -- sleep 3600
[root@k8s-master ~]# kubectl get pocluster-test1-54575cf56c-9xwb5 1/1 Running 0 3m34s
[root@k8s-master ~]# kubectl get po -owide
cluster-test1-54575cf56c-9xwb5 1/1 Running 0 10m 172.16.36.72 k8s-node1 <none> <none>
9、Pod 必须能够解析 Service
(1)nslookup kubernetes
[root@k8s-master ~]# kubectl exec -it cluster-test1-54575cf56c-9xwb5 -- bash
(07:39 cluster-test1-54575cf56c-9xwb5:/) nslookup kubernetes Server: 10.96.0.10 Address: 10.96.0.10#53 Name: kubernetes.default.svc.cluster.local Address: 10.96.0.1
(2)nslookup kube-dns.kube-system
(07:39 cluster-test1-54575cf56c-9xwb5:/) nslookup kube-dns.kube-system Server: 10.96.0.10 Address: 10.96.0.10#53 Name: kube-dns.kube-system.svc.cluster.local Address: 10.96.0.10
[root@k8s-master ~]# ping 172.16.36.72 -c 3 PING 172.16.36.72 (172.16.36.72) 56(84) bytes of data. 64 bytes from 172.16.36.72: icmp_seq=1 ttl=63 time=0.435 ms 64 bytes from 172.16.36.72: icmp_seq=2 ttl=63 time=0.295 ms 64 bytes from 172.16.36.72: icmp_seq=3 ttl=63 time=0.357 ms --- 172.16.36.72 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2000ms rtt min/avg/max/mdev = 0.295/0.362/0.435/0.059 ms
二、常用指令
1、Kubernetes 自动补齐
[root@k8s-master ~]# yum -y install bash-completion
[root@k8s-master ~]# source <(kubectl completion bash) 临时开启自动补齐功能
[root@k8s-master ~]# echo "source <(kubectl completion bash)" >> ~/.bashrc 永久开启自动补齐功能
2、常用命令
创建
[root@k8s-master ~]# kubectl create namespace pes
[root@k8s-master ~]# kubectl get namespace删除
[root@k8s-master ~]# kubectl delete deployment clustertest
[root@k8s-master ~]# kubectl delete deployment cluster-test
3、编写yaml文件
[root@k8s-master ~]# vim pods/abc.yaml
apiVersion: v1
kind: Pod
metadata:
name: busybox-sleep
spec:
containers:
- name: busybox
image: busybox:1.28
args:
- sleep
- "1000"
4、创建
[root@k8s-master pods]# kubectl create -f abc.yaml
[root@k8s-master pods]# kubectl get po -A|grep bus
[root@k8s-master pods]# kubectl delete pod busybox-sleep