查看集群状态
查看master组件状态: kubectl get cs
查看node状态: kubectl get node
查看Apiserver代理的URL:
kubectl cluster-info
查看集群详细信息:
kubectl cluster-info dump
查看资源信息:
kubectl describe <资源> <名称>
查看所有组件pod
kubectl get pod -n kube-system
查看 pod日志
kubectl logs pod名字 注意如果pod不在默认的命名空间里后面需要-n指定命名空间,如: kubectl logs eureka-0 -f -n ms
kubectl logs --tail=1000 pod的名称 | less
查看节点资源和日志
kubectl describe nodes k8s-node2
查看节点日志
journalctl -f -u kubelet
获取所有命名空间
kubectl get ns
获取所有命名空间
kubectl get ns
查看Pod节点信息。
查看pod运行在哪个节点下 kubectl get pods -o wide 提示:如果不是默认的pod要指定命名空间名字加 -n 指定kubectl get pods -n 命名空间名字
命令创建一个pod
在Kubernetes集群中创建一个pod,验证是否正常运行 kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
查看pod和
service kubectl get pod,svc
查看节点标签
kubectl get nodes --show-labels
创建命名空间
kubectl create namespace xyzp
查看所有命名空间
kubectl get pods -A kubectl get pod --all-namespaces -o wide
删除service
kubectl delete svc service名字 -n 命名空间
进入一个pod
kubectl exec -ti <your-pod-name> -n <your-namespace> -- /bin/sh
一个pod里多个容器进入指定容器
kubectl exec [-c CONTAINER] -- bash
例子:
kubectl exec -it spcadmin-77cc666ffb-42kvm -n xiaoyuanzhaopin -- /bin/bash
复制文件到pod里
kubectl -n xyzp cp default.conf web-pod-84d4bbd7f9-pcpsq:/etc/nginx/conf.d/default.conf
复制pod文件到主机
kubectl -n xyzp cp nginx-pod-5898d94f67-62knh:etc/nginx/nginx.conf /root/nginx.conf
删除pod
kubectl delete pod nginx -n xinpod
强制删除
kubectl delete pod scheduletask-pod-575944b694-t4vnd --force --grace-period=0 -n zhxy
查看资源
kubectl api-resources
k8s配置文件
/root/.kube/config
查看pod标签
kubectl get pods --show-labels
查看节点污点
kubectl describe node |grep Taint
添加污点
kubectl taint node k8s-node1 gpu=yes:NoSchedule
删除污点
kubectl taint node k8s-node1 gpu-
yum -y install bash-completion
source <(kubectl completion bash)
bash
修改k8s端口范围
vim /etc/kubernetes/manifests/kube-apiserver.yaml
- --service-node-port-range=1-65535 #添加这一句不用重启生效
- --advertise-address=192.168.1.111
挂载nfs 共享目录
修改yaml文件,放在image:镜像下面
#必须放在资源限制后面不然会报错
volumeMounts:
- name: gongxina
mountPath: /opt/software/Resources/
volumes:
- name: gongxina
nfs:
server: 192.168.1.111
path: /ifs/kubernetes/zhihuixiaoyuan
kubectl describe node k8s-node4
kubernetes 删除删不掉的pv
kubectl patch pv xxx -p '{"metadata":{"finalizers":null}}'
命令创建ConfigMap
kubectl create configmap app-config --from-file=nginx.conf
查看已有configmap
kubectl get configmap
查看创建的configmap内容
kubectl describe cm redis-conf
退出K8S集群
master执行
kubectl drain k8s-node5 --delete-local-data --force --ignore-daemonsets node/k8s-node5
kubectl delete node k8s-node5
重新加入K8S集群
node节点执行
一:停掉kubelet
systemctl stop kubelet
kubeadm reset iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
二:删除之前的相关文件
rm -rf /etc/kubernetes/*
echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables
master执行生成token
kubeadm token create --print-join-command
node执行上面生成的token重新加入
kubeadm join 192.168.1.111:6443 --token c6f5ua.98wz2iu1rebems6m --discovery-token-ca-cert-hash sha256:e61918875a41f1a5e4a489592a0076be8ae7471ab112f8afde67be3fdc3c3ed
pod指定节点
第一步:给节点添加标签 格式:kubectl label nodes <node-name> <label-key>=<label-value> 例如:kubectl label nodes k8s-node1 disktype=ssd 验证:kubectl get nodes --show-labels
第二步:添加nodeSelector字段到Pod配置中
apiVersion: v1 kind: Pod metadata: name: pod-example spec: nodeSelector: containers:
name: nginx image: nginx:1.19
最后,验证: kubectl get pods -o wide
使用Deployment控制器部署镜像
kubectl create deployment web --image=lizhenliang/java-demo
kubectl get deploy,pods
使用Service将Pod暴露出去
kubectl expose deployment web --port=80 --target-port=8080 --type=NodePort
kubectl get service
访问应用:
http://NodeIP:Port # 端口随机生成,通过get svc获取
新建命名空间,在该命名空间创建一个pod
kubectl create namespace xinpod
kubectl run nginx --image=nginx --namespace=xinpod
kubectl get pods -n kube-system
列出空间下指定标签pod
kubectl get pods -l app=nginx -n default
导出YAML文件
用create命令生成
kubectl create deployment nginx --image=nginx:1.16 -o yaml --dry-run=client > my-deploy.yaml
用get命令导出
kubectl get deployment nginx -o yaml > my-deploy.yaml
Pod容器的字段拼写忘记了
kubectl explain pods.spec.containers
kubectl explain deployment #查Deployment版本使用
重启策略+健康检查(应用自修复)
apiVersion: v1 kind: Pod metadata: name: probe-demo namespace: demo spec: containers:
name: web image: nginx ports:
containerPort: 80 readinessProbe: tcpSocket: port: 80 initialDelaySeconds: 30 #启动容器后多少秒健康检查 periodSeconds: 10 #以后间隔多少秒检查一次 tcpSocket: port: 80 startupProbe: tcpSocket: port: 80
ConfigMap
命令创建configmap
kubectl create configmap nginx-conf --from-file=nginx.conf
生产环境nginx.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "10"
description: web
creationTimestamp: "2021-06-18T02:33:09Z"
generation: 15
labels:
k8s-app: web-nginx
qcloud-app: web-nginx
name: web-nginx
namespace: default
resourceVersion: "36599305"
selfLink: /apis/apps/v1beta2/namespaces/default/deployments/web-nginx
uid: 762e0372-bd03-467b-9aaa-5058cf3ebfc4
spec:
progressDeadlineSeconds: 600
replicas: 3
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: web-nginx
qcloud-app: web-nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
k8s-app: web-nginx
qcloud-app: web-nginx
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- 192.168.3.46
containers:
- env:
- name: PATH
value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- name: NGINX_VERSION
value: 1.21.0
- name: NJS_VERSION
value: 0.5.3
- name: PKG_RELEASE
value: 1~buster
image: ccr.gz.govcloud.tencent.com/tsf_100004603197/nginx:v1
imagePullPolicy: IfNotPresent
name: nginx
resources:
limits:
cpu: 500m
memory: 1Gi
requests:
cpu: 250m
memory: 256Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /opt/software/Resources/www
name: web
- mountPath: /etc/nginx/conf.d/
name: nginx-ssl
- mountPath: /etc/nginx/nginx.conf
name: nginx-conf
subPath: etc/nginx/nginx.conf
dnsPolicy: ClusterFirst
imagePullSecrets:
- name: qcloudregistrykey
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- name: web
nfs:
path: /www
server: 192.168.3.45
- name: nginx-ssl
nfs:
path: /nginx/ssl/
server: 192.168.3.45
- configMap:
defaultMode: 420
items:
- key: nginx.conf
mode: 420
path: etc/nginx/nginx.conf
name: nginx-conf
name: nginx-conf
status:
availableReplicas: 3
conditions:
- lastTransitionTime: "2021-06-18T02:33:09Z"
lastUpdateTime: "2021-06-18T03:35:55Z"
message: ReplicaSet "web-nginx-6747db4c58" has successfully progressed.
reason: NewReplicaSetAvailable
status: "True"
type: Progressing
- lastTransitionTime: "2021-07-29T13:24:38Z"
lastUpdateTime: "2021-07-29T13:24:38Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
observedGeneration: 15
readyReplicas: 3
replicas: 3
updatedReplicas: 3