虚拟机k8s搭建
1.永久关闭swap,命令行键入以下指令
systemctl stop firewalld systemctl disable firewalld setenforce 0 sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config swapoff -a sed -i 's/.*swap.*/#&/' /etc/fstab
2.修改网络配置,命令行键入以下指令
cat <<EOF > /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl -p /etc/sysctl.d/k8s.conf
2.1错误 : cannot stat /proc/sys/net/bridge/bridge-nf-call-ip6tables: 没有那个文件或目录...
# 临时解决,重启失效 modprobe br_netfilter # 为了开机加载上面这个模块 cat > /etc/rc.sysinit << EOF #!/bin/bash for file in /etc/sysconfig/modules/*.modules ; do [ -x $file ] && $file done EOF cat > /etc/sysconfig/modules/br_netfilter.modules << EOF modprobe br_netfilter EOF chmod 755 /etc/sysconfig/modules/br_netfilter.modules lsmod |grep br_netfilter
3.安装docker ,k8s运行依赖docker
# 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: 更新并安装Docker-CE sudo yum makecache fast # 注意: # 官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,您可以通过以下方式开启。同理可以开启各种测试版本等。 # vim /etc/yum.repos.d/docker-ee.repo # 将[docker-ce-test]下方的enabled=0修改为enabled=1 # Step 4: 查找Docker-CE的版本: yum list docker-ce.x86_64 --showduplicates | sort -r # Loading mirror speeds from cached hostfile # Loaded plugins: branch, fastestmirror, langpacks # docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable # docker-ce.x86_64 17.03.1.ce-1.el7.centos @docker-ce-stable # docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable # Available Packages # Step5: 安装指定版本的Docker-CE: (VERSION例如上面的17.03.0.ce.1-1.el7.centos) # sudo yum -y install docker-ce-[VERSION] sudo yum -y install docker-ce-18.06.0.ce-3.el7 # Step 6: 开启Docker服务 sudo service docker start # Step 7: 开机启动 sudo systemctl enable docker # Step 8: 查看状态 sudo systemctl status docker
3.1 docker加速
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' # xxxxxx换成自己的连接, 阿里云镜像容器服务可以找到(镜像加速) { "registry-mirrors": [ "https://xxxxxx.mirror.aliyuncs.com", "http://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn", "https://registry.docker-cn.com", "https://ung2thfc.mirror.aliyuncs.com" ] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
4. 配置kubernets国内源
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=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
5. 从kubeernets源下载kubeadm kubelet kubectl
# 指定版本,不推荐使用最新版 yum install -y kubelet-1.20.5 kubeadm-1.20.5 kubectl-1.20.5 --disableexcludes=kubernetes systemctl enable kubelet.service
5.1.1 修改kubelet配置
vim /etc/sysconfig/kubelet KUBELET_EXTRA_ARGS="--fail-swap-on=false"
完成以上可进行虚拟机克隆,以下步骤node节点不需要执行,无node节点也可
5.1.2 使用阿里云镜像初始化
运行kubeadm init时加上阿里云镜像的参数--image-repository=registry.aliyuncs.com/google_containers,如下:
kubeadm init --image-repository=registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.20.5 --v=6
6. 创建文件夹 拷贝配置信息(根据提示操作)
Your Kubernetes control-plane has initialized successfully! To start using your cluster, you need to run the following as a regular user: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config Alternatively, if you are the root user, you can run: export KUBECONFIG=/etc/kubernetes/admin.conf You should now deploy a pod network to the cluster. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/ Then you can join any number of worker nodes by running the following on each as root: kubeadm join 192.168.1.13:6443 --token su0cm4.0fgt1nuyq61yvknu \ --discovery-token-ca-cert-hash sha256:38dee19cd610ff56071e95399880cb68765674f85f83a13338870dc7d347da8c
7. 安装fannel,coredns组件需要,域名解析使用
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
7.1如果网络不通,无法下载也可以直接复制下面的yaml去直接创建,此版本与kubectl v1.20.5对应
--- 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.13.1-rc2 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.13.1-rc2 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
8. 查看安装情况
kubectl get pods -n kube-system
9. 开启主节点可以负载,默认情况是不会负载到主节点
# 查看hostname hostname # 开启主节点负载 kubectl taint node [主节点的hostname] node-role.kubernetes.io/master-
10. 部署可视化插件
sudo docker run -d \ --restart=unless-stopped \ --name=kuboard \ -p 9999:80/tcp \ -p 10081:10081/tcp \ -e KUBOARD_ENDPOINT="http://192.168.232.150:9999" \ -e KUBOARD_AGENT_SERVER_TCP_PORT="10081" \ -v /root/kuboard-data:/data \ eipwork/kuboard:v3
遇到的问题
1.failed to find plugin “flannel” in path [/opt/cni/bin],k8sNotReady
过命令在该节点查看相关kubectl日志提示如下错误:
journalctl -u kubelet -f
failed to find plugin “flannel” in path [/opt/cni/bin]
查看 /opt/cni/bin 缺少 flannel
解决办法
需要下载CNI插件:CNI plugins v0.8.6
github下载地址:
https://github.com/containernetworking/plugins/releases/tag/v0.8.6
https://gitee.com/llchaimiyouyan/resources/raw/master/cni-plugins-linux-amd64-v0.8.6.tgz
(在1.0.0版本后CNI Plugins中没有flannel)
下载后通过xftp 上传到Linux /home目录解压
tar zxvf cni-plugins-linux-amd64-v0.8.6.tgz
复制 flannel 到 /opt/cni/bin/
 cp flannel /opt/cni/bin/
节点由NotReady变为Ready
————————————————
版权声明:问题为CSDN博主「奈斯菟咪踢呦」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_34288630/article/details/127772813
2.为什么要关闭swap、selinux、firewalld
部署Kubernetes(k8s)时关闭swap、selinux和firewalld是为了确保集群的稳定性、安全性和正常运行。
关闭Swap:Swap是一种虚拟内存,当物理内存不足时,操作系统将数据转移到磁盘上。在Kubernetes集群中,应用程序的性能和稳定性对内存的需求很高。K8s的各个组件和容器都需要足够的内存来运行,而Swap的使用可能导致性能下降,甚至是应用程序的奔溃。关闭Swap可以确保集群的可预测性和稳定性,避免不必要的磁盘交换。
关闭SELinux(Security-Enhanced Linux):SELinux是Linux内核的安全模块,用于强化系统的安全性。然而,在Kubernetes环境中,SELinux可能干扰容器与宿主机之间的交互,导致意外的权限问题。K8s使用一种名为"Container Runtime"的组件来运行容器,而SELinux的策略可能会限制容器的正常操作。关闭SELinux可以减少由于安全策略冲突引起的问题,同时K8s本身已经提供了适当的隔离和安全机制。
关闭Firewalld(防火墙):防火墙是用于保护网络安全的重要组件,但在Kubernetes集群中,可能会干扰集群内部的网络通信。K8s集群内部有许多组件需要相互通信,例如节点之间的Pod通信、Master节点与Worker节点之间的通信等。如果防火墙设置不当,会阻止这些必要的通信,导致集群部署失败或者网络问题。关闭防火墙可以确保K8s集群内部的正常通信,同时需要在K8s的网络配置中设置适当的安全规则。需要注意的是,尽管关闭这些组件可能会带来一些好处,但也可能导致一些安全风险。
关闭SELinux和防火墙可能会降低系统的安全性,因此在执行这些步骤之前,务必考虑集群环境的安全需求,并确保在关闭这些组件之后采取适当的安全措施来保护集群。
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号