一、环境准备
centos7两台机器搭建一个master一个worker集群
master-ip:192.168.200.30
node-ip:192.168.200.31
二、关闭selinux
setenforce 0
永久关闭:
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
三、关闭防火墙
systemctl stop firewalld
四、设置hostname
hostnamectl set-hostname master hostnamectl set-hostname node
reboot #重启生效
将master&node写入hosts文件中
五、关闭交换分区
swapoff -a
永久生效:
sed -ri 's/.*swap.*/#&/' /etc/fstab
六、允许 iptables 检查桥接流量
cat <生效方式:
sysctl --system
七、安装dockeryum remove docker* yum install -y yum-utils yum install docker-ce
配置镜像加速器(自行注册一个阿里账号,在镜像服务中找到对应得加速文件)sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://xxx.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker sudo systemctl enable docker
八、配置k8s源cat </etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF 九、安装kubelet、kubeadm、kubectl
yum install -y kubelet kubeadm kubectl #最好不要指定版本,默认更新为最新 #指定版本 yum install -y --nogpgcheck kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0 systemctl enable --now kubelet systemctl start kubelet出现以下问题解决:
解决方法:下载yum源,安装依赖包 cd /etc/yum.repos.d/ wget http://mirrors.aliyun.com/repo/epel-7.repo wget http://mirrors.aliyun.com/repo/Centos-7.repo yum install epel-release yum install conntrack-tools再次执行安装命令即可
检查安装:kubelet --version
十、kubeadm安装集群
kubeadm初始化集群 #在master上执行
需要修改为master的IP地址, --apiserver-advertise-address 192.168.200.30#apiserver-advertise-address 192.168.200.100为master节点IP,根据个人master IP修改 kubeadm init --apiserver-advertise-address 192.168.200.30 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr 10.244.0.0/16 --service-cidr 10.96.0.0/12 #--kubernetes-version v1.23.4 #本行,可以不添加,默认使用最新的版本 #pod、service的网段为官网默认
以上为初始化成功
#根据提示写入环境变量mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config#查看node
kubectl get no状态为notready,需要安装网络插件
#master上下载网络插件wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.ymlvim 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-plugin #image: flannelcni/flannel-cni-plugin:v1.0.1 for ppc64le and mips64le (dockerhub limitations may apply) image: rancher/mirrored-flannelcni-flannel-cni-plugin:v1.0.1 command: - cp args: - -f - /flannel - /opt/cni/bin/flannel volumeMounts: - name: cni-plugin mountPath: /opt/cni/bin - name: install-cni #image: flannelcni/flannel:v0.16.3 for ppc64le and mips64le (dockerhub limitations may apply) image: rancher/mirrored-flannelcni-flannel:v0.16.3 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.16.3 for ppc64le and mips64le (dockerhub limitations may apply) image: rancher/mirrored-flannelcni-flannel:v0.16.3 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/ - 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: FileOrCreatekubectl apply -f ./kube-flannel.yml #此处应用后node状态由NotReady --> Ready将node节点加入master:
kubeadm join 192.168.200.30:6443 --token 574e7g.uo9ko4bn8cr3figa
–discovery-token-ca-cert-hash sha256:49f0a014dd1d5f6e3f0830d169bd7a37df4e9da14468cfd2e4a2741b9b6a866b
加入成功
#工作负载worker节点node查看集群scp -r /etc/kubernetes/admin.conf node:/etc/kubernetes/ node上执行 echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile source ~/.bash_profile#初始化报错:
The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp [::1]:10248: connect: connection refused.
解决方式:# 添加以下内容 vim /etc/docker/daemon.json { "exec-opts": ["native.cgroupdriver=systemd"] } # 重启docker systemctl restart docker # 重新初始化 kubeadm reset # 先重置,再次初始化
#查看k8s所需镜像kubeadm config images list#token生成
kubeadm token create#查看列表
kubeadm token list#获取ca证书sha256编码hash值
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'node上加入
kubeadm join 192.168.10.39:6443 --token vyaztf.snjf2za5636nekcv --discovery-token-ca-cert-hash sha256:21c1254f9da1bf4c46a6841b603ca1c21193eb32c27639ebf775f6fab8ba39de#允许master进行调度
kubectl taint node master node-role.kubernetes.io/master-#所有节点不允许调度
kubectl taint nodes --all node-role.kubernetes.io/master-#重新设置master不允许调度
kubectl taint node master node-role.kubernetes.io/master=:NoSchedule



