K8S 集群安装(基于Centos7)

K8S 集群安装(基于Centos7)_第1张图片
部署规划.png

1~9 Master和Node机器都操作。

1.升级内核版本

yum update kernel -y

2.修改服务器名称

Master


/etc/sysconfig/network.png

/etc/hostname.png

/etc/hosts.png

Node01


/etc/sysconfig/network.png

/etc/hostname.png

/etc/hosts.png

Node02
/etc/sysconfig/network.png

/etc/hostname.png

/etc/hosts.png

3.设置固定IP(Mac版本)

  • 进入Mac终端
cat /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf
其中IP为本机的网关IP
netmask为子网掩码
K8S 集群安装(基于Centos7)_第2张图片
image.png
  • 然后到系统偏好设置中,选择网络-->高级-->DNS查看本机DNS


    K8S 集群安装(基于Centos7)_第3张图片
    image.png
  • 登陆虚拟机,修改/etc/sysconfig/network-scripts/ifcfg-ens33 配置


    K8S 集群安装(基于Centos7)_第4张图片
    image.png

    BOOTRPOTO:static表示静态IP
    IPADDR:当前虚拟机需要的固定IP
    NETMASK:子网掩码
    GATEWAY:网关IP
    DNS1:dnsIP

  • 重启网关
    systemctl restart network.service

4.关闭防火墙

systemctl stop firewalld
systemctl disable firewalld.service

5.关闭selinux

setenforce 0

6.关闭swap

swapoff -a    临时关闭
vim /etc/fstab  注释掉swap的行,永久关闭
free 查看swap是否关闭
image.png

7.添加阿里云YUM软件源

cat < /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
exclude=kube*
EOF

8.安装docker

yum update -y
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum list docker-ce --showduplicates | sort -r
yum install docker-ce-18.06.3.ce-3.el7 -y
systemctl start docker
systemctl enable docker

9.修改 sysctl 配置

vi /etc/sysctl.conf 

添加
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1

sysctl --system

10.安装kubelet kubeadm kubectl

yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

systemctl enable kubelet && systemctl start kubelet 

11.Master 节点安装

  • 列出需要的镜像
kubeadm config images list


k8s.gcr.io/kube-apiserver:v1.17.4
k8s.gcr.io/kube-controller-manager:v1.17.4
k8s.gcr.io/kube-scheduler:v1.17.4
k8s.gcr.io/kube-proxy:v1.17.4
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.4.3-0
k8s.gcr.io/coredns:1.6.5
  • 下载需要的镜像
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.17.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.17.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.17.4 
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.17.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.5
  • 将镜像tag为对应的镜像名
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.17.4 k8s.gcr.io/kube-apiserver:v1.17.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.17.4 k8s.gcr.io/kube-controller-manager:v1.17.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.17.4 k8s.gcr.io/kube-scheduler:v1.17.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.17.4 k8s.gcr.io/kube-proxy:v1.17.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0 k8s.gcr.io/etcd:3.4.3-0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.5 k8s.gcr.io/coredns:1.6.5
  • 初始化 Master
kubeadm init --pod-network-cidr=10.244.0.0/16

注意:--pod-network-cidr=10.244.0.0/16 是 k8s 的网络插件所需要用到的配置信息,用来给 node 分配子网段。然后我们这边用到的网络插件是 flannel,就是这么配。
  • 配置 kubectl
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
  • 安装 Pod 网络
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  • kubectl 命令自动补全
source <(kubectl completion bash)

如果命令补全报_get_comp_words_by_ref错误,执行以下命令
yum install bash-completion -y
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)

12.Node节点安装

  • 查看添加节点命令
kubeadm token create --print-join-command
image.png
  • 到node节点执行以上命令
kubeadm join 172.16.177.128:6443 --token pmdom9.wivf86fjhbvauxa5     --discovery-token-ca-cert-hash sha256:72dcb127e36568b2c445a7e9b8a436999fa310555803a9904ea145db36c69c45
  • 默认token过期为24小时,如果token过期,可以通过下面命令在master上生成
kubeadm create token (默认24小时过期)
kubeadm token create --ttl 0(永不过期)
然后在执行 kubeadm token create --print-join-command查看添加节点命令
  • node上还需要下载quay.io/coreos/flannel:v0.12.0-amd64,k8s.gcr.io/kube-proxy,k8s.gcr.io/pause镜像。后面两个镜像可通过kubeadm config images list查看版本

13.查看所有Node是否正常(master上)

kubectl get nodes -o wide
image.png

14.安装dashboard

  • 创建dashboard服务
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta4/aio/deploy/recommended.yaml
  • 修改Service,把type改成NodePort类型,nodePort修改成自己的端口


    K8S 集群安装(基于Centos7)_第5张图片
    l
  • 创建登录用户,创建dashboard-admin.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kube-system

kubectl create -f dashboard-admin.yaml
  • 查看admin-user账户的token,用Token登陆
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')

你可能感兴趣的:(K8S 集群安装(基于Centos7))