使用kubeadm方式部署k8s集群

一次使用kubeadm搭建k8s集群的记录。

环境准备

预先安装好3台主机,需要有上网权限,此处为VM,与主机网络模式为NAT

HostName OS IP
k8s-master CentOS Linux release 7.9.2009 (Core) 172.16.252.88
k8s-node1 CentOS Linux release 7.9.2009 (Core) 172.16.252.89
k8s-node2 CentOS Linux release 7.9.2009 (Core) 172.16.252.90

 

所有节点均需设置

  • 关闭 主机防火墙
systemctl stop firewalld
systemctl disable firewalld

 

  • 关闭SELinux
sed -i 's/enforcing/disabled/' /etc/selinux/config #永久关闭
setenforce 0 #临时关闭

 

  • 关闭swap分区
swapoff -a #临时关闭
sed -ri 's/.*swap.* /#&/' /etc/fstab #永久关闭

 

  • 设置主机名
hostnamectl set-hostname [hostname]

 

  • master节点中添加hosts
cat >> /etc/hosts <

 

  • 将桥接的网卡流量传到iptables
cat > /etc/sysctl.d/k8s.conf <

 

  • 时间同步
yum -y install ntpdate
ntpdate time.windows.com

#如有时区错误参照
#将etc目录下时区文件备份
mv /etc/localtime /etc/localtime.bak 
#将/usr/share/zoneinfo/Asia/Shanghai 文件软链接到/etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
#查看时间
date -R

 


所有节点安装Docker/Kubeadm/Kublet

 

  • 安装Docker
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-20.10.6-3.el7.x86_64
systemctl enable docker && systemctl start docker

#配置镜像加速器
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors":[https://kod2rnzj.mirror.aliyuncs.com] #可使用自己唯一的阿里加速器
}
systemctl restart docker

 

  • 添加阿里云YUM源
#可用
cat > /etc/yum.repos.d/kubeadm.repo << EOF
 

[kubernetes]

name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=0 repo_gpgcheck=0 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF

  • Kubeadm/Kubelet/Kubectl 安装
yum install -y kubelet-1.20.1 kubeadm-1.20.1 kubectl-1.20.1
systemctl enable kubelet

部署Kubernetes Master

#master节点部署
kubeadm init --apiserver-advertise-address=172.16.252.88 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.20.1 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16 
#拉取Kubernetes 核心组件apiserver,contorll,etcd,proxy,pause,coredns,scheduller 镜像
#配置Kubernetes master
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u) $HOME/.kube/config
chown $(id -g) $HOME/.kube/config
kubectl get nodes #查看集群节点
  • node节点加入集群
#node节点加入集群
kubeadm join 172.16.252.88:6443 --token xxx.xxxxxxxxxxx  #随机生成token
	--discovery-token-ca-cert-hash sha256 xxxxxxxxxxxxxxx
#默认token有效期为24小时,过期后需重新创建token
kubeadm token create --print-join-command
  • 部署CNI网络插件
#github地址
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
#应用flannel生成
kubectl apply -f kube-flannel.yml 
#检查kube-system系统命名空间内组件
kubectl get pods -n kube-system

kube-flannel.yml 内容,没有科学上网的情况下很难拉取下来,此处贴出来,如有需要直接复制新建kube-flannel.yml文件即可。


---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: psp.flannel.unprivileged
  annotations:
    seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default
    seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default
    apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default
    apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default
spec:
  privileged: false
  volumes:
  - configMap
  - secret
  - emptyDir
  - hostPath
  allowedHostPaths:
  - pathPrefix: "/etc/cni/net.d"
  - pathPrefix: "/etc/kube-flannel"
  - pathPrefix: "/run/flannel"
  readOnlyRootFilesystem: false
  # Users and groups
  runAsUser:
    rule: RunAsAny
  supplementalGroups:
    rule: RunAsAny
  fsGroup:
    rule: RunAsAny
  # Privilege Escalation
  allowPrivilegeEscalation: false
  defaultAllowPrivilegeEscalation: false
  # Capabilities
  allowedCapabilities: ['NET_ADMIN', 'NET_RAW']
  defaultAddCapabilities: []
  requiredDropCapabilities: []
  # Host namespaces
  hostPID: false
  hostIPC: false
  hostNetwork: true
  hostPorts:
  - min: 0
    max: 65535
  # SELinux
  seLinux:
    # SELinux is unused in CaaSP
    rule: 'RunAsAny'
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
rules:
- apiGroups: ['extensions']
  resources: ['podsecuritypolicies']
  verbs: ['use']
  resourceNames: ['psp.flannel.unprivileged']
- 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-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: flannel
  namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: kube-flannel-cfg
  namespace: kube-system
  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-system
  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
        image: quay.io/coreos/flannel:v0.14.0-rc1
        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: quay.io/coreos/flannel:v0.14.0-rc1
        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
        volumeMounts:
        - name: run
          mountPath: /run/flannel
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      volumes:
      - name: run
        hostPath:
          path: /run/flannel
      - name: cni
        hostPath:
          path: /etc/cni/net.d
      - name: flannel-cfg
        configMap:
          name: kube-flannel-cfg

测试K8s集群

  • 查看集群状态
#查看集群状态,如遇node节点NotReady状态,可尝试重启该节点docker
kubectl get nodes
NAME         STATUS     ROLES                  AGE   VERSION
k8s-master   Ready      control-plane,master   48m   v1.20.1
k8s-node1    Ready                       39m   v1.20.1
k8s-node2    Ready                       37m   v1.20.1
  • 查看集群内容器分布状态以及集群内服务状态
#查看集群内容器分布状态以及集群内服务状态
kubectl get pods,svc -n kube-system -o wide
NAME                                     READY   STATUS                  RESTARTS   AGE   IP              NODE         NOMINATED NODE   READINESS GATES
pod/coredns-7f89b7bc75-5b47p             1/1     Running                 0          50m   10.244.0.3      k8s-master              
pod/coredns-7f89b7bc75-ncpds             1/1     Running                 0          50m   10.244.0.2      k8s-master              
pod/etcd-k8s-master                      1/1     Running                 0          50m   172.16.252.88   k8s-master              
pod/kube-apiserver-k8s-master            1/1     Running                 0          50m   172.16.252.88   k8s-master              
pod/kube-controller-manager-k8s-master   1/1     Running                 0          50m   172.16.252.88   k8s-master              
pod/kube-flannel-ds-49pjb                1/1     Running                 0          17m   172.16.252.88   k8s-master              
pod/kube-flannel-ds-ktpfk                0/1     Init:ImagePullBackOff   0          17m   172.16.252.90   k8s-node2               
pod/kube-flannel-ds-zpvmw                0/1     Init:0/1                0          17m   172.16.252.89   k8s-node1               
pod/kube-proxy-29n6x                     1/1     Running                 0          50m   172.16.252.88   k8s-master              
pod/kube-proxy-nbvqf                     1/1     Running                 0          42m   172.16.252.89   k8s-node1               
pod/kube-proxy-x7lbt                     1/1     Running                 0          39m   172.16.252.90   k8s-node2               
pod/kube-scheduler-k8s-master            1/1     Running                 0          50m   172.16.252.88   k8s-master              

NAME               TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE   SELECTOR
service/kube-dns   ClusterIP   10.96.0.10           53/UDP,53/TCP,9153/TCP   50m   k8s-app=kube-dns

集群搭建完成,后续正常实验跑pod即可。


给TA买糖
共{{data.count}}人
人已赞赏
文章教程

MacOS Big Sur app 转换 CDR 镜像文件

2021-5-3 10:22:42

文章教程

VMware vSphere Esxi 升级教程

2021-6-11 11:01:00

个人中心
今日签到
搜索