查看master组件状态: kubectl get cs
查看node状态: kubectl get node
查看Apiserver代理的URL:
kubectl cluster-info
查看集群详细信息:
kubectl cluster-info dump
查看资源信息:
kubectl describe <资源> <名称>
kubectl get pod -n kube-system
kubectl logs pod名字 注意如果pod不在默认的命名空间里后面需要-n指定命名空间,如: kubectl logs eureka-0 -f -n ms
kubectl logs --tail=1000 pod的名称 | less
kubectl get ns
查看pod运行在哪个节点下 kubectl get pods -o wide 提示:如果不是默认的pod要指定命名空间名字加 -n 指定kubectl get pods -n 命名空间名字
在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
kubectl delete svc service名字 -n 命名空间
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
kubectl -n xyzp cp default.conf web-pod-84d4bbd7f9-pcpsq:/etc/nginx/conf.d/default.conf
kubectl -n xyzp cp nginx-pod-5898d94f67-62knh:etc/nginx/nginx.conf /root/nginx.conf
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
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
vim /etc/kubernetes/manifests/kube-apiserver.yaml
修改yaml文件,放在image:镜像下面
查看节点资源使用率
kubectl describe node k8s-node4
kubectl patch pv xxx -p '{"metadata":{"finalizers":null}}'
kubectl create configmap app-config --from-file=nginx.conf
kubectl get configmap
kubectl describe cm redis-conf
master执行
kubectl drain k8s-node5 --delete-local-data --force --ignore-daemonsets node/k8s-node5
kubectl delete node k8s-node5
node节点执行
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
第一步:给节点添加标签格式:kubectl label nodes <node-name> <label-key>=<label-value>
例如:kubectl label nodes k8s-node1 disktype=ssd
验证:kubectl get nodes --show-labels
第二步:添加nodeSelector字段到Pod配置中
apiVersion: v1kind: Pod
metadata:
name: pod-example
spec:
nodeSelector:
disktype: “ssd”
containers:
name: nginximage: nginx:1.19
最后,验证: kubectl get pods -o wide
kubectl create deployment web --image=lizhenliang/java-demo
kubectl get deploy,pods
kubectl expose deployment web --port=80 --target-port=8080 --type=NodePort
kubectl get service
访问应用:
http://NodeIP:Port # 端口随机生成,通过get svc获取
kubectl create namespace xinpod
kubectl run nginx --image=nginx --namespace=xinpod
kubectl get pods -n kube-system
kubectl get pods -l app=nginx -n default
kubectl create deployment nginx --image=nginx:1.16 -o yaml --dry-run=client > my-deploy.yaml
kubectl get deployment nginx -o yaml > my-deploy.yaml
kubectl explain pods.spec.containers
kubectl explain deployment #查Deployment版本使用
name: probe-demo
namespace: demo
name: webimage: nginx
ports:
containerPort: 80readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 30 #启动容器后多少秒健康检查
periodSeconds: 10 #以后间隔多少秒检查一次
livenessProbe:
startupProbe:
命令创建configmap
kubectl create configmap nginx-conf --from-file=nginx.conf
生产环境nginx.yaml
apiVersion: apps/v1beta2
kind: Deployment
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
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
path: /nginx/ssl/
- configMap:
defaultMode: 420
items:
- key: nginx.conf
mode: 420
path: etc/nginx/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
type: Available
observedGeneration: 15
readyReplicas: 3
updatedReplicas: 3