20230326-k8s容器编排

课程贴

安装K8S集群

参考:使用kubeadm在Centos8上部署kubernetes1.18_Kubernetes中文社区

配置源
cat /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
安装工具
yum install -y kubelet-1.17.17-0 kubectl-1.17.17-0 kubeadm-1.17.17-0

初始化集群
kubeadm init \--image-repository registry.aliyuncs.com/google_containers \--kubernetes-version v1.17.17 \--pod-network-cidr=10.244.0.0/16

安装集群网络

---
kind: Namespace
apiVersion: v1
metadata:
  name: kube-flannel
  labels:
    pod-security.kubernetes.io/enforce: privileged
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
- apiGroups:
  - ""
  resources:
  - nodes
  verbs:
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - nodes/status
  verbs:
  - patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: flannel
subjects:
- kind: ServiceAccount
  name: flannel
  namespace: kube-flannel
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: flannel
  namespace: kube-flannel
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: kube-flannel-cfg
  namespace: kube-flannel
  labels:
    tier: node
    app: flannel
data:
  cni-conf.json: |
    {
      "name": "cbr0",
      "cniVersion": "0.3.1",
      "plugins": [
        {
          "type": "flannel",
          "delegate": {
            "hairpinMode": true,
            "isDefaultGateway": true
          }
        },
        {
          "type": "portmap",
          "capabilities": {
            "portMappings": true
          }
        }
      ]
    }
  net-conf.json: |
    {
      "Network": "10.244.0.0/16",
      "Backend": {
        "Type": "vxlan"
      }
    }
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: kube-flannel-ds
  namespace: kube-flannel
  labels:
    tier: node
    app: flannel
spec:
  selector:
    matchLabels:
      app: flannel
  template:
    metadata:
      labels:
        tier: node
        app: flannel
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/os
                operator: In
                values:
                - linux
      hostNetwork: true
      priorityClassName: system-node-critical
      tolerations:
      - operator: Exists
        effect: NoSchedule
      serviceAccountName: flannel
      initContainers:
      - name: install-cni-plugin
       #image: flannelcni/flannel-cni-plugin:v1.1.0 for ppc64le and mips64le (dockerhub limitations may apply)
        image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0
        command:
        - cp
        args:
        - -f
        - /flannel
        - /opt/cni/bin/flannel
        volumeMounts:
        - name: cni-plugin
          mountPath: /opt/cni/bin
      - name: install-cni
       #image: flannelcni/flannel:v0.19.2 for ppc64le and mips64le (dockerhub limitations may apply)
        image: docker.io/rancher/mirrored-flannelcni-flannel:v0.19.2
        command:
        - cp
        args:
        - -f
        - /etc/kube-flannel/cni-conf.json
        - /etc/cni/net.d/10-flannel.conflist
        volumeMounts:
        - name: cni
          mountPath: /etc/cni/net.d
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      containers:
      - name: kube-flannel
       #image: flannelcni/flannel:v0.19.2 for ppc64le and mips64le (dockerhub limitations may apply)
        image: docker.io/rancher/mirrored-flannelcni-flannel:v0.19.2
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            cpu: "100m"
            memory: "50Mi"
          limits:
            cpu: "100m"
            memory: "50Mi"
        securityContext:
          privileged: false
          capabilities:
            add: ["NET_ADMIN", "NET_RAW"]
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: EVENT_QUEUE_DEPTH
          value: "5000"
        volumeMounts:
        - name: run
          mountPath: /run/flannel
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
        - name: xtables-lock
          mountPath: /run/xtables.lock
      volumes:
      - name: run
        hostPath:
          path: /run/flannel
      - name: cni-plugin
        hostPath:
          path: /opt/cni/bin
      - name: cni
        hostPath:
          path: /etc/cni/net.d
      - name: flannel-cfg
        configMap:
          name: kube-flannel-cfg
      - name: xtables-lock
        hostPath:
          path: /run/xtables.lock
          type: FileOrCreate

删除污点
kubectl get nodes 查看节点
kubectl taint nodes 节点名称 node-role.kubernetes.io/master:NoSchedule-

远程访问

在/etc/hosts添加如下记录:

47.93.32.161 kubernetes

kubeconfig配置:保存到$HOME/.kube/config

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJek1ETXlOakEyTWpJeE5Gb1hEVE16TURNeU16QTJNakl4TkZvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTmFHCldFMTV1RnpBMjJCYXZITUFQdU1yNS9LK3Z1dEwyMVpSaTZPOTN5cklqMlZ4Wmo5bndxT2Z4dVJlQmFNeFVsYWUKc1pYQXlyWWJDU3pja2VsNVhST0xvSjlTZlF1dVp2SDhlYm1VL1MrNk04c0EyaVRFN3A2TGZpSFdZRDJrcVFIQQpONWpiL3VyZGdlY0doZ28vSDY1UVJqdGxwb0wzN2NUVHQxSGM3alFqYm1IK1JMZTJ3Z0VTc00ra1hjOGNwMXJuCk4vWC80TjNQOE14L1RPV2FmQi9WYk9lTkJTb250YzJhcnJDUnJ1T3RSSU5BS2xVa0lUVWhlV2hBamptMTg5YlAKczJ0bVJMNEd2SG9WK1dINWZHQmdPeDdocGowOW91VFdTT3FqMXVDaDhOTndjRWYxYWJXQkhOTEJveGpxanp6Wgp1dWcrTlp5V1Y4eVZzdUJkSFIwQ0F3RUFBYU1qTUNFd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFFN0VybnEzTGwyb2IzSEIrZWIxUEJZUUNJd00KVTNJTjh1d3VOZkZXU0dnNHlWSXRoS1NRVEdoZUhUc004c2lCR2hpejVtNnh2akNhV2tZUUZ1anRTN1FUQS95eQpJSnpLYnBUUHR0UWxJbGx4aUNnNlNZRjhhejlyRXpiZFh2RWNtZi81MlJ0Y2RXWmx1T2hzWDVVbzhCRXdnb3VZCjZlYTZVVkVsWWhGT2NGbVp2YXIxaVZyMitGYVUzQ1dNcUxONVNtQjVsWitUN2JrcDdDRnc0T2ZCb0lCbzlwRmkKMlVyVTRBNTNmN0VaeUtTTTBYNlZHQ0dHVDJPTVpHOFR6ckRTd0hSUnJsVGVZbkxYNGM0enQvK3N0eDNWcVB1SQoySXJUSC81NGRsTWJ1T0lrdktiUUlqQTc2dG1ZWlZ3UWZETTRpdXRveGZweXJnVTUrdnBpRE5RYXgzND0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
    server: https://kubernetes:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJYVFhNks0MElHdW93RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzV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XcytLL0hsZ0svMHY5YgpWRGxtcmlPUk50V29DSC9DSzE2R2JlVjRZWVpsRldNTXY5eVBqNXM1eUFXalFNd2p4bnJwNHpWamdDU0VYRUJQCnIyd1dTUUtCZ1FDRnZ3c3ZxaUU2MS9sUWtZUTU5d0UrS1pRNms4SndvL0RMN1BoWkZvUjNwUGVjS3Z1dStlaU8KQjlTdkJwRU83M2RDdGZiSHNJVUlzQ3hNSEY5dm5aMitmbUUrSTNQUFZjUWlrVlRZejV6L1lRZklKMnZvR2xZdwo4NCtLQ3Z3STkxNTIrQy9HazZVbHBQb2R0M1h2bUlnbkhUSGg4MFZHNXVSNitVakhEaUxsSVE9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=

#在k8s中部署服务

apiVersion: apps/v1
kind: Deployment
metadata:
  name: selenium-hub
  labels:
    name: selenium-hub
spec:
  replicas: 1
  selector:
    matchLabels:
      name: selenium-hub
  template:
    metadata:
      labels:
        name: selenium-hub
    spec:
      containers:
        - name: selenium-hub
          image: selenium/hub:4.0.0-rc-2-prerelease-20210923
          imagePullPolicy: IfNotPresent
          resources:
            limits:
              cpu: "0.5"
            requests:
              cpu: "0.5"
          ports:
            - containerPort: 4444
            - containerPort: 4442
            - containerPort: 4443
            - containerPort: 5557
          env:
            - name: TZ
              value: "Asia/Shanghai"
          volumeMounts:
            - mountPath: "/etc/localtime"
              name: "host-time"
          livenessProbe:
            httpGet:
              path: /grid/console
              port: 4444
            initialDelaySeconds: 30
            timeoutSeconds: 1
            periodSeconds: 5
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /grid/console
              port: 4444
            initialDelaySeconds: 30
            timeoutSeconds: 1
            periodSeconds: 5
            failureThreshold: 3
      volumes:
        - name: "host-time"
          hostPath:
            path: "/etc/localtime"
---
apiVersion: v1
kind: Service
metadata:
  name: selenium-hub
  labels:
    name: selenium-hub
spec:
  type: NodePort
  ports:
    - name: port1
      protocol: TCP
      port: 4442
      targetPort: 4442
    - name: port2
      protocol: TCP
      port: 4443
      targetPort: 4443
    - name: port3
      protocol: TCP
      port: 5557
      targetPort: 5557
    - port: 4444
      targetPort: 4444
      name: port0
      nodePort: 32757
  selector:
    name: selenium-hub
  sessionAffinity: None