k8s总结(共享存储nfs 亲和性等)

一、设置环境
设置节点污点与标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
   |  kubectl taint nodes k8s1 tag=heima:NoExecute
  kubectl taint nodes k8s1 tag:NoExecute-
  kubectl taint nodes gegewu node-role.kubernetes.io/control-plane:NoSchedule-
  kubectl describe nodes |grep Taints
 
 
  kubectl label nodes k8s1 nodeenv=pro kubectl get nodes --show-labels kubectl taint nodes k8s1 tag=heima:NoExecute
  kubectl taint nodes k8s1 tag:NoExecute-
  kubectl taint nodes gegewu node-role.kubernetes.io/control-plane:NoSchedule-
  kubectl describe nodes |grep Taints
 
 
  kubectl label nodes k8s1 nodeenv=pro kubectl get nodes --show-labels
 
  | 
二、配置yaml文件
以yaml配置方式总结 创建一个可以外部访问的nginx(zj.yaml):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
   | apiVersion: apps/v1   kind: Deployment      metadata:               name: deploy-nginx    namespace: dev        labels:                 version: "label-test"  spec:   replicas: 3           selector:               matchLabels:            version: label-test   template:                        metadata:       labels:                          version: label-test           spec:              containers:       - image: registry.cn-hangzhou.aliyuncs.com/zznn/mycentos:nginx-latest         imagePullPolicy: IfNotPresent          name: nginx-test                  ports:                - containerPort: 80           protocol: TCP             command: ["/bin/sh","-c","while true;do /bin/echo $(date +%T);sleep 60; done;"]           env:                                - name: "username"             value: "admin"           - name: "password"             value: "123456"                     resources:                         limits:                 cpu: "2"              memory: "10Gi"            requests:                     cpu: "1"                    memory: "10Mi"                      lifecycle:         postStart:            exec:              command: ["/bin/sh", "-c", "echo postStart... > /usr/share/nginx/html/index.html"]         preStop:           exec:              command: ["/usr/sbin/nginx","-s","quit"]       tolerations:               - key: "tag"                 operator: "Equal"          value: "heima"                 effect: "NoExecute"                    affinity:            nodeAffinity:            preferredDuringSchedulingIgnoredDuringExecution:            - weight: 1             preference:               matchExpressions:                - key: nodeenv                 operator: In                 values: ["pro","yyy"]
   | 
总结2加入共享存储挂载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
   | apiVersion: apps/v1   kind: Deployment      metadata:               name: deploy-nginx    namespace: dev        labels:                 version: "label-test"  spec:   replicas: 3           selector:               matchLabels:            version: label-test   template:                        metadata:       labels:                          version: label-test             spec:              containers:       - image: registry.cn-hangzhou.aliyuncs.com/zznn/mycentos:nginx-latest         imagePullPolicy: IfNotPresent          name: nginx-test                  ports:                  - containerPort: 80           protocol: TCP                   env:                            - name: "username"             value: "admin"           - name: "password"             value: "123456"                     command: ["/bin/sh","-c","while true;do /bin/echo $(date +%T);sleep 60; done;"]                  volumeMounts:         - name: logs-volume           mountPath: /var/log/nginx                  resources:                           limits:                 cpu: "2"              memory: "10Gi"            requests:                     cpu: "1"                    memory: "10Mi"                   lifecycle:           postStart:              exec:                command: ["/bin/sh", "-c", "echo postStart... > /usr/share/nginx/html/index.html"]           preStop:             exec:                command: ["/usr/sbin/nginx","-s","quit"]
               volumes:       - name: logs-volume         nfs:           server: 58.49.144.8             path: /srv/nfs4      
               tolerations:          - key: "tag"             operator: "Equal"          value: "heima"             effect: "NoExecute"                      affinity:            nodeAffinity:            preferredDuringSchedulingIgnoredDuringExecution:            - weight: 1             preference:               matchExpressions:                - key: nodeenv                 operator: In                 values: ["pro","yyy"]
 
   | 
验证nfs挂载是否成功
1 2 3 4 5 6 7 8 9 10 11
   |  kubectl exec -it deploy-nginx-7d9b78b676-dvbxh -n dev -- /bin/sh
  cd /var/log/nginx ls
  kubectl exec -it deploy-nginx-7d9b78b676-dvbxh -n dev -- /bin/sh
  cd /var/log/nginx ls
 
 
  | 
对外暴露端口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
   | apiVersion: v1 kind: Service metadata:   name: svc-nginx       namespace: dev      	labels:     version: label-test      spec:   clusterIP: 10.109.179.231    ports:                       - port: 80                     protocol: TCP     targetPort: 80             selector:                      version: label-test    type: NodePort            
   | 
效果
kubectl get pod -n dev –show-labels

# 只筛选出标签是version=2.0的pod
kubectl get pods -l “version=2.0” -n dev –show-labels
# 只筛选出标签是version=2.0的pod
kubectl get pods -l “version=2.0” -n dev –show-labels