天天看點

kubernetes常用指令

檢視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

繼續閱讀