K8S 之 通过kubeadmin安装K8S集群

一、集群信息

K8S 之 通过kubeadmin安装K8S集群

二、安装前准备

1、设置hostname(三台机)

k8s-master:
$ hostnamectl set-hostname k8s-master && bash
k8s-slave1:
$ hostnamectl set-hostname k8s-slave1 && bash
k8s-slave2:
$ hostnamectl set-hostname k8s-slave2 && bash

2、设置hosts解释(三台机)

[[email protected] ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.3.153.200 k8s-master
10.3.153.201 k8s-slave1
10.3.153.202 k8s-slave2

3、调整系统参数(三台机)

1、关闭swap使用
$ swapoff -a
$ sed -i '/ swap / s/^(.*)$/#\1/g' /etc/fstab

2、关闭selinux和防火墙
$ sed -ri 's#(SELINUX=).*#\1disabled#' /etc/selinux/config setenforce 0 
$ systemctl disable firewalld && systemctl stop firewalld

3、修改内核参数
vi /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1 
net.bridge.bridge-nf-call-iptables = 1 
net.ipv4.ip_forward=1 
vm.max_map_count=262144

4、内核参数生效
$ modprobe br_netfilter && sysctl -p /etc/sysctl.d/k8s.conf

5、设置相应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=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
        http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
$ 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.el7

2、启动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.yaml
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 10.3.153.200      # apiserver地址,因为单master,所以配置master的节点内网IP
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  name: k8s-master
  taints:
  - effect: NoSchedule
    key: node-role.kubernetes.io/master
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers   # 修改成阿里镜像源
kind: ClusterConfiguration
kubernetesVersion: v1.16.0
networking:
  dnsDomain: cluster.local
  podSubnet: 10.244.0.0/16                  #指定pod运行的子网
  serviceSubnet: 10.96.0.0/12
scheduler: {}

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

K8S 之 通过kubeadmin安装K8S集群
K8S 之 通过kubeadmin安装K8S集群

4、slave1与slave2 节点加入集群

$ kubeadm join 10.3.153.200:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:a2300a66a90689f28b15e04a72b4c297158202fb5b37da638516a0b773be6b62

备注:token为上面的master安装完后生成

5、查看k8s节点信息

K8S 之 通过kubeadmin安装K8S集群
备注:当前状态为NotReady,需安装网络插件flannel

五、安装flannel插件(只需在master节点)

$ wget https://raw.githubusercontent.com/coreos/flannel/2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/kube-flannel.yml
$ vi kube-flannel.yml
#修改配置,指定网卡名称,大概在文件的190行,添加一行配置:

K8S 之 通过kubeadmin安装K8S集群

执行安装flannel插件
$ kubectl create -f kube-flannel.yml

六、再次确认节点状态

K8S 之 通过kubeadmin安装K8S集群

七、集群验证

1、创建一个pod

$ kubectl run  test-nginx --image=nginx:alpine

K8S 之 通过kubeadmin安装K8S集群

猜你喜欢

转载自blog.51cto.com/12965094/2633806

相关文章