集群中所有机器之间网络互通
可以访问外网,需要拉取镜像
禁止swap分区
同步时间服务器
2. 准备环境 安装基础包 yum -y install bash-completion wget ntpdate nfs-utils 关闭防火墙: systemctl stop firewalld systemctl disable firewalld 关闭selinux: sed -i \'s/enforcing/disabled/\' /etc/selinux/config setenforce 0 关闭swap: swapoff -a # 临时 sed -i \'/ swap / s/^\(.*\)$/#\1/g\' /etc/fstab # 永久 时间同步: crontab -e 0 */2 * * * /usr/sbin/ntpdate time.windows.date hostnamectl set-hostname k8s-master --static添加主机名与IP对应关系(记得设置主机名):
$ cat >> /etc/hosts<<EOF 9.110.187.120 k8s-master 9.110.187.125 k8s-node1 9.110.187.126 k8s-node2 EOF将桥接的IPv4流量传递到iptables的链:
$ cat > /etc/sysctl.d/k8s.conf << EOF net.ipv4.ip_nonlocal_bind = 1 net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_local_port_range = 10000 65000 fs.file-max = 2000000 net.ipv4.ip_forward = 1 vm.swappiness = 0 EOF $ sysctl --system 3. 所有节点安装Docker/kubeadm/kubeletKubernetes默认CRI(容器运行时)为Docker,因此先安装Docker。
3.1 安装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-18.06.1.ce-3.el7 $ yum list docker-ce --showduplicates | sort -r # 确保网络模块开机自动加载 lsmod | grep overlay lsmod | grep br_netfilter # 若上面命令无返回值输出或提示文件不存在,需执行以下命令: cat > /etc/modules-load.d/docker.conf <<EOF overlay br_netfilter EOF modprobe overlay modprobe br_netfilter # 配置Docker mkdir /etc/docker # 修改cgroup驱动为systemd[k8s官方推荐]、限制容器日志量、修改存储类型,最后的docker家目录可修改 cat > /etc/docker/daemon.json <<EOF { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2", "storage-opts": [ "overlay2.override_kernel_check=true" ], "registry-mirrors": ["https://7uuu3esz.mirror.aliyuncs.com"], "data-root": "/data/docker" } EOF #添加开机自启,立即启动 systemctl enable --now docker systemctl daemon-reload systemctl restart docker 3.2 添加阿里云YUM软件源 $ cat > /etc/yum.repos.d/kubernetes.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 # 查看版本 yum list |grep kubelet yum list |grep kubeadm yum list |grep kubectl 3.3 安装kubeadm,kubelet和kubectl由于版本更新频繁,这里指定版本号部署:
yum install -y kubelet-1.19.3 kubeadm-1.19.3 kubectl-1.19.3 systemctl enable kubelet 4. 部署Kubernetes Master在9.110.187.120(Master)执行。
$ kubeadm init \ --apiserver-advertise-address=9.110.187.120 \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.19.3 \ --service-cidr=10.1.0.0/16 \ --pod-network-cidr=10.244.0.0/16由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址。
使用kubectl工具:
$ mkdir -p $HOME/.kube $ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config $ sudo chown $(id -u):$(id -g) $HOME/.kube/config $ kubectl get nodes 5. 安装Pod网络插件(CNI) $ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml如果下载失败,可以改成这个镜像地址:lizhenliang/flannel:v0.11.0-amd64
6. 加入Kubernetes Node在9.110.187.125/126(Node)执行。
向集群添加新节点,执行在kubeadm init输出的kubeadm join命令:
$ kubeadm join 9.110.187.120:6443 --token esce21.q6hetwm8si29qxwn \ --discovery-token-ca-cert-hash sha256:00603a05805807501d7181c3d60b478788408cfe6cedefedb1f97569708be9c5 7. 测试kubernetes集群在Kubernetes集群中创建一个pod,验证是否正常运行:
kubectl create deployment nginx --image=nginx:1.16 kubectl expose deployment nginx --port=80 --type=NodePort kubectl get pods -A -o wide kubectl describe pod nginx-86c57db685-frwwt访问地址::Port
8. Kubernetes V1.19.3部署Dashboard V2.0(beta5) $ wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta5/aio/deploy/recommended.yaml修改recommended.yaml文件内容(vi recommended.yaml):
--- #增加直接访问端口 kind: Service apiVersion: v1 metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kubernetes-dashboard spec: type: NodePort #增加 ports: - port: 443 targetPort: 8443 nodePort: 30001 #增加 selector: k8s-app: kubernetes-dashboard --- #因为自动生成的证书很多浏览器无法使用,所以我们自己创建,注释掉kubernetes-dashboard-certs对象声明 #apiVersion: v1 #kind: Secret #metadata: # labels: # k8s-app: kubernetes-dashboard # name: kubernetes-dashboard-certs # namespace: kubernetes-dashboard #type: Opaque --- 创建证书 mkdir dashboard-certs cd dashboard-certs/ #创建命名空间 # kubectl create namespace kubernetes-dashboard # 创建key文件 openssl genrsa -out dashboard.key 2048 #证书请求 openssl req -days 36000 -new -out dashboard.csr -key dashboard.key -subj \'/CN=dashboard-cert\' #自签证书 openssl x509 -req -in dashboard.csr -signkey dashboard.key -out dashboard.crt #创建kubernetes-dashboard-certs对象 kubectl create secret generic kubernetes-dashboard-certs --from-file=dashboard.key --from-file=dashboard.crt -n kubernetes-dashboard