k8s-master:$ hostnamectl set-hostname k8s-master && bashk8s-slave1:$ hostnamectl set-hostname k8s-slave1 && bashk8s-slave2:$ hostnamectl set-hostname k8s-slave2 && bash
2、设置hosts解释(三台机)[root@k8s-master ~]# vi /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain610.3.153.200 k8s-master10.3.153.201 k8s-slave110.3.153.202 k8s-slave2
3、调整系统参数(三台机)1、关闭swap使用$ swapoff -a$ sed -i '/ swap / s/^(.*)$/#\1/g' /etc/fstab2、关闭selinux和防火墙$ sed -ri 's#(SELINUX=).*#\1disabled#' /etc/selinux/config setenforce 0 $ systemctl disable firewalld && systemctl stop firewalld3、修改内核参数vi /etc/sysctl.d/k8s.confnet.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward=1 vm.max_map_count=2621444、内核参数生效$ modprobe br_netfilter && sysctl -p /etc/sysctl.d/k8s.conf5、设置相应yun源(删除当前所有/etc/yum.repos.d/*.repo)$ curl -o /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo$ curl -o /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo$ cat <<EOF > /etc/yum.repos.d/kubernetes.repo[kubernetes]name=Kubernetesbaseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64enabled=1gpgcheck=0repo_gpgcheck=0gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpgEOF$ yum clean all && yum makecache
三、安装docker-ce(三台机)1、安装docker-ce社区版(指定了相应的版本)$ yum install docker-ce-cli-18.09.9-3.el7 docker-ce-18.09.9-3.el72、启动docker$ systemctl enable docker && systemctl start docker
四、部署kubernetes集群1、安装 kubeadm, kubelet 和 kubectl(三台机)$ yum install -y kubelet-1.16.2 kubeadm-1.16.2 kubectl-1.16.2 --disableexcludes=kubernetes$ systemctl enable kubelet备注:安装完好,可能通过以下命令验证$ kubeadmin version
2、初始化安装配置文件(master节点操作)$ kubeadm config print init-defaults > kubeadm.yaml$ cat kubeadm.yamlapiVersion: kubeadm.k8s.io/v1beta2bootstrapTokens:- groups: - system:bootstrappers:kubeadm:default-node-token token: abcdef.0123456789abcdef ttl: 24h0m0s usages: - signing - authenticationkind: InitConfigurationlocalAPIEndpoint: advertiseAddress: 10.3.153.200 # apiserver地址,因为单master,所以配置master的节点内网IP bindPort: 6443nodeRegistration: criSocket: /var/run/dockershim.sock name: k8s-master taints: - effect: NoSchedule key: node-role.kubernetes.io/master---apiServer: timeoutForControlPlane: 4m0sapiVersion: kubeadm.k8s.io/v1beta2certificatesDir: /etc/kubernetes/pkiclusterName: kubernetescontrollerManager: {}dns: type: CoreDNSetcd: local: dataDir: /var/lib/etcdimageRepository: registry.aliyuncs.com/google_containers # 修改成阿里镜像源kind: ClusterConfigurationkubernetesVersion: v1.16.0networking: dnsDomain: cluster.local podSubnet: 10.244.0.0/16 #指定pod运行的子网 serviceSubnet: 10.96.0.0/12scheduler: {}
3、根据初始化配置文件,初始化master节点$ kubeadm init --config kubeadm.yaml#以下命令待上面命令执行成功后执行:$ mkdir -p $HOME/.kube$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
$ kubeadm join 10.3.153.200:6443 --token abcdef.0123456789abcdef \ --discovery-token-ca-cert-hash sha256:a2300a66a90689f28b15e04a72b4c297158202fb5b37da638516a0b773be6b62备注:token为上面的master安装完后生成
5、查看k8s节点信息
备注:当前状态为NotReady,需安装网络插件flannel
$ wget https://raw.githubusercontent.com/coreos/flannel/2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/kube-flannel.yml$ vi kube-flannel.yml#修改配置,指定网卡名称,大概在文件的190行,添加一行配置:
执行安装flannel插件$ kubectl create -f kube-flannel.yml
六、再次确认节点状态七、集群验证1、创建一个pod$ kubectl run test-nginx --image=nginx:alpine