k8s集群安装
说明:本次安装k8s使用1.20.15版本,docker使用19.03版本,版本依赖参考:https://github.com/kubernetes/kubernetes/blob/v1.20.15/build/dependencies.yaml
一、准备机器
192.168.5.200 master 192.168.5.201 node1 192.168.5.202 node2
二、关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
三、关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
或者
vi /etc/selinux/config
# 在修改SELINUX=disabled
四、关闭swap
swapoff -a # 临时 sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
五、修改主机名
hostnamectl set-hostname <hostname> # 3台机器修改为master/node1/node2
六、添加hosts
cat >> /etc/hosts << EOF 192.168.5.200 master 192.168.5.201 node1 192.168.5.202 node2 EOF
七、将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl --system
八、安装docker-ce(阿里云提供:https://developer.aliyun.com/mirror/docker-ce)
# step 1: 安装必要的一些系统工具 sudo yum install -y yum-utils device-mapper-persistent-data lvm2 # Step 2: 添加软件源信息 sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # Step 3 sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo # Step 4: 更新并安装Docker-CE sudo yum makecache fast sudo yum -y install docker-ce-19.03.15 docker-ce-cli-19.03.15 # Step 4: 开启Docker服务 sudo service docker start
九、阿里云镜像加速
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://zo5eqsmr.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
十、安装k8s(kubernetes),教程:https://developer.aliyun.com/mirror/kubernetes
cat <<EOF > /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 setenforce 0 yum install -y kubelet-1.20.15 kubeadm-1.20.15 kubectl-1.20.15
# yum install -y --nogpgcheck kubelet-1.20.15 kubeadm-1.20.15 kubectl-1.20.15 (由于官网未开放同步方式, 可能会有索引gpg检查失败的情况, 这时请用)
systemctl enable kubelet && systemctl start kubelet
十一、master执行安装
kubeadm init \ --apiserver-advertise-address=192.168.5.200 \ --image-repository=registry.aliyuncs.com/google_containers \ --kubernetes-version=v1.20.15 \ --service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
等待完成出现:
十二、在其他(node1/node2)节点执行上述图片
kubeadm join 192.168.5.200:6443 --token dpd1wd.sx3eh220tvgsb217 \ --discovery-token-ca-cert-hash sha256:fa149ffeb30f5b60d790ba4602afaf262ca65de9575534f6563fc8e8ccf3e190
十三、使用kubectl工具,查看节点
mkdir -p $HOME/.kubernetes sudo cp -i /etc/kubernetes/admin.conf $HOME/.kubernetes/config sudo chown $(id -u):$(id -g) $HOME/.kubernetes/config $ kubectl get nodes
问题:
解决:
vi /etc/profile #末尾添加 export KUBECONFIG=/etc/kubernetes/admin.conf source /etc/profile
十四、安装kube-flannel
1)查看节点状态,全是NotReady
2)问题是因为kube-flannel没有安装
可以看到是因为网络的组件加载一直存在问题,所以需要安装kube-flannel
3)下载文件:kube-flannel.yaml,由于FQ的问题,一般是不能下载的。
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
提供文件:kube-flannel.yaml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
--- 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
4)安装
kubectl apply -f kube-flannel.yaml
十五、查看节点状态
十六、阿里云镜像中心地址:
https://cr.console.aliyun.com/cn-hangzhou/instances/images
https://cr.console.aliyun.com/cn-hangzhou/instances/favourites
十七、总结:k8s的本地集群安装方式就可以了,在云上面使用建议直接买k8s的服务。