K8S命令行总结

重启所有pods,变向重启或更新

kubectl delete pod $(kubectl get pods | grep -v NAME | awk '{print $1}')

清理所有的Evicted状态的POD

kubectl get pods | grep Evicted | awk '{print $1}' | xargs kubectl delete pod

更新deployment数量

kubectl scale deployment depName --replicas 1

kubectl scale deployment --replicas 1 -n pos-prd $(kubectl get deploy -n pos-prd|grep -v NAME|awk '{print $1}')

kubectl scale deployment --replicas 1 -n omni-prd $(kubectl get deploy -n omni-prd|grep -v NAME|awk '{print $1}')

获取异常容器

kubectl get pods | grep -v Running

查看pod状态信息

kubectl describe pod podName

查看pod日志信息

kubectl logs --tail 10 -f podName

查看整个所有的POS日志信息,并自动获取NAME

kubectl logs -n pos-uat --tail 10 -f deploy/$(kubectl get -n pos-uat deploy|grep nginx|awk '{print $1}') 

将k8s node变为不可用状态

kubectl patch node nodeIP -p '{"spec":{"unschedulable":true}}'

将k8s node变为可用状态

kubectl patch node nodeIP -p '{"spec":{"unschedulable":false}}'

查看 RC 和 service 列表, -o wide 查看详细信息

kubectl get pod,svc -o wide

查看K8S节点信息

kubectl describe node app-server-62

进入容器里面,查看

kubectl exec -it -n pos-prd crv-order-85557bb9d4-4rj77 /bin/sh

容器自动缩缩功能

kubectl autoscale deployment amp-register -n pos-prd --min=1 --max=1

创建一个加密的变量并被POD引用

kubectl create secret generic uat-secrets --from-file=uat.env -n omni-uat

uat.env 新建文件
SERVICE_OMP_REST_URL=OMNI-NGINX-ENTRY
SERVICE_OMNI_REST_URL=OMNI-NGINX-ENTRY
SERVICE_OMP_MEMCACHED_URI=OMP-MEMCACHED:11211
SERVICE_MYSQL_URI=10.249.1.247:3306
SERVICE_MYSQL_USER=root
SERVICE_MYSQL_PASS=Cxz12h7qlw2yLtdoAFbmMUv

PODS引用示例:
nginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-nginx-entry
  namespace: omni-uat
spec:
  selector:
    matchLabels:
      app: test-nginx-entry
  replicas: 1
  revisionHistoryLimit: 2
  template:
    metadata:
      labels:
        app: test-nginx-entry
    spec:
      containers:
      - image: 10.249.6.188/pos/nginx
        name: test-nginx-entry
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        envFrom:
        - secretRef:
            name: uat-secrets
        volumeMounts:
        - name: nginx-conf
          mountPath: /opt/nginx/conf/nginx.conf
      volumes:
      - name: nginx-conf
        hostPath:
          path: /data/nginx/conf/nginx-omni.conf

更新变量方法, 先删除,后创建:
kubectl delete secret app-secret -n omni-uat