K8S 整理

kebectl 部署常用操作

服務管理

deployment、service、cronJob、ingress都是支持的

# 創建及更新服務(yaml文件形式)
kubectl apply -f web-click.yaml
# 刪除服務(配置文件形式)
kubectl delete -f web-click.yaml
# 刪除服務
kubectl delete deployment adc-web -n bigdata
kubectl delete svc adc-web -n bigdata
# 查看服務
kubectl get deployment --namespace=bigdata
kubectl get svc --namespace=bigdata
kubectl get ingress --namespace=bigdata
kubectl get cronjob smcheck-cronjob  -n bigdata
# 查看具體pod詳細信息
kubectl describe pods adc-web-click-5f64b5fc-5v5n4 -n bigdata
kubectl describe ingress adc-ingress -n bigdata

pod信息

# 包含pod運行的狀態
kubectl get pods --namespace=bigdata |grep adc
# 顯示詳細信息 如ip
kubectl get pods -o wide -n bigdata 	

查看pod日誌

kubectl logs adc-web-click-5f64b5fc-6qs6w  -n bigdata

問題排查

查看容器進入容器環境

kubectl exec -it [pod name] --container [container name] -n [namespace] [執行命令]

kubectl exec -it callbackconsume-job-758874f99d-pgmtf --container adc-callback -n bigdata ls config/debug

查看日誌

kubectl logs callbackconsume-job-758874f99d-pgmtf -n bigdata

將pod分配給節點

https://kubernetes.io/zh/docs/concepts/configuration/assign-pod-node/

查看節點 顯示標籤

kubectl get nodes --show-labels

添加標籤

kubectl label nodes <node-name> <label-key>=<label-value>

添加 nodeSelector 字段到 pod 配置中

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    env: test
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  nodeSelector:
    disktype: ssd

kubectl apply -f https://k8s.io/examples/pods/pod-nginx.yaml 命令,pod 將會調度到將標籤添加到的節點上。

可以通過運行 kubectl get pods -o wide 並查看分配給 pod 的 “NODE” 來驗證其是否有效

監控job

kubectl get jobs --watch -n bigdata

Replace "hello-4111706356" with the job name in your system

pods=$(kubectl get pods --selector=job-name=smcheck-cronjob-1589540820 --output=jsonpath={.items[*].metadata.name})

kubectl get pods --selector=job-name=smcheck-cronjob-1589540640

smcheck-cronjob-1589536140y

ingress使用

---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  namespace: bigdata
  name: adc-ingress
  annotations:
    ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: "nginx"					# ingress指定nginx		
    nginx.ingress.kubernetes.io/server-snippet: |     # 設置nginx 服務腳本配置
        set $flag 0;
        if ( $uri = /click) {
          set $flag 1;
        }
        if ( $args ~ project=ios ) {
          set $flag 1$flag;
        }
        if ( $flag = 11 ) {
          rewrite ^/(.*) $uri-ios break;
        }
spec:
  rules:
    - host: walking.sun.com
      http:
        paths:															# path對應nginx path
          - path: /click/google
            backend:
              serviceName: google-srv       # 指定path 請求會轉發到對應的svc
              servicePort: 8080
          - path: /click-ios
            backend:
              serviceName: web-ios-srv
              servicePort: 8080
          - path: /click
            backend:
              serviceName: web-srv
              servicePort: 8080
    - host: walking.sun123.com
      http:
        paths:
          - path: /click/google
            backend:
              serviceName: google-srv
              servicePort: 8080
  tls:																			# 開發tls
    - hosts:
        - walking.sun.com
      secretName: sun.com
    - hosts:
        - walking.sun123.com
      secretName: sun123.com

參考:http://nginx.org/en/docs/http/ngx_http_core_module.html#internal

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章