1. 程式人生 > 其它 >kubernetes常用命令

kubernetes常用命令

檢視叢集狀態

檢視master元件狀態: kubectl get cs

檢視node狀態: kubectl get node

檢視Apiserver代理的URL:

kubectl cluster-info

檢視叢集詳細資訊:

kubectl cluster-info dump

檢視資源資訊:

kubectl describe <資源> <名稱>


檢視節點資源使用率

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:e61918875a41f1a5e4a489592a0076be8ae7471ab112f8afde67be3fdc3c3ed7

























































使用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 #以後間隔多少秒檢查一次 livenessProbe: 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