CentOS7 搭建kubernetes集群

环境准备

三台机器:
192.168.0.101 k8s-node1
192.168.0.102 k8s-node2
192.168.0.103 k8s-node3

master节点需要2个cpu,4g内存,否则可能报错
以下操作,三台机器都需要执行
Ⅰ 关闭swap
vim /etc/fstab 注释掉最后一行/dev/mapper/centos-swap,reboot生效

#
# /etc/fstab
# Created by anaconda on Wed Apr 28 21:50:03 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=94704751-6631-4501-8a2f-b64291566eea /boot                   xfs     defaults        0 0
#/dev/mapper/centos-swap swap                    swap    defaults        0 0

执行free命令,查看是否关闭成功

[root@k8s-node1 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        629M        1.6G         12M        1.5G        2.8G
Swap:            0B          0B          0B

swap都为0,即成功
Ⅱ 关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

Ⅲ 关闭selinux
SELINUX=enforcing 修改为 SELINUX=disabled

vi /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

Ⅳ 添加ip与hostname对应关系
vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6


192.168.0.101  k8s-node1
192.168.0.102  k8s-node2
192.168.0.103  k8s-node3

Ⅴ 将桥接的IPV4流量传递到iptables 的链

[root@k8s-node1 ~]# cat > /etc/sysctl.d/k8s.conf << EOF
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.bridge-nf-call-iptables = 1
> EOF
[root@k8s-node1 ~]# sysctl --system

Ⅵ 下载安装docker 18.06
可能需要配置域名解析,使用google的免费域名解析

[root@k8s-node1 ~]# vim /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
[root@k8s-node1 ~]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O/etc/yum.repos.d/docker-ce.repo
[root@k8s-node1 ~]# yum -y install docker-ce-18.06.1.ce-3.el7

设置开机自启动

[root@k8s-node1 ~]# systemctl enable docker && systemctl start docker

Ⅶ 添加阿里云yum源

[root@k8s-node1 ~]# 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=1
> repo_gpgcheck=1
> gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
> https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
> EOF

Ⅷ 安装kubectl、kubelet、kubeadm

[root@k8s-node1 ~]# yum install -y kubectl-1.14.0 --nogpgcheck
[root@k8s-node1 ~]# yum install -y kubelet-1.14.0 --nogpgcheck
[root@k8s-node1 ~]# yum install -y kubeadm-1.14.0 --nogpgcheck

设置kubelet自启动

[root@k8s-node1 ~]# systemctl enable kubelet

部署Master节点

以下只需在Master节点执行
apiserver-advertise-address参数为master节点的ip

[root@k8s-node1 ~]# [root@k8s-node1 ~]# kubeadm init --apiserver-advertise-address=192.168.0.101 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.14.0 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16

执行成功之后,会出现以下内容

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

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.0.101:6443 --token erqhfv.iq242lfyl554xql4 \
    --discovery-token-ca-cert-hash sha256:34fb3145a0f18908e83b727cc0bcbe9376ec786cc3c2e5555f18b43968d6c88c

按照提示,执行以下命令

[root@k8s-node1 ~]# mkdir -p $HOME/.kube
[root@k8s-node1 ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@k8s-node1 ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config

以下命令用于node加入集群,注意保存

kubeadm join 192.168.0.101:6443 --token erqhfv.iq242lfyl554xql4 \
    --discovery-token-ca-cert-hash sha256:34fb3145a0f18908e83b727cc0bcbe9376ec786cc3c2e5555f18b43968d6c88c

安装网络插件

[root@k8s-node1 ~]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml

执行命令,查看pod是否部署成功

[root@k8s-node1 ~]# kubectl get pod -n kube-system
NAME                                READY   STATUS    RESTARTS   AGE
coredns-8686dcc4fd-7sr78            1/1     Running   0          89s
coredns-8686dcc4fd-kq2j2            1/1     Running   0          89s
etcd-k8s-node1                      1/1     Running   0          49s
kube-apiserver-k8s-node1            1/1     Running   0          28s
kube-controller-manager-k8s-node1   1/1     Running   0          43s
kube-flannel-ds-amd64-hcc75         1/1     Running   0          30s
kube-proxy-lm68n                    1/1     Running   0          89s
kube-scheduler-k8s-node1            1/1     Running   0          42s

执行命令查看node状态

[root@k8s-node1 ~]# kubectl get node
NAME        STATUS   ROLES    AGE     VERSION
k8s-node1   Ready    master   2m21s   v1.14.0

到这里Master节点就部署成功了,接下来我们部署node节点

部署node节点

以下只需在node节点执行
执行kubeadm join命令加入集群

[root@k8s-node3 ~]# kubeadm join 192.168.0.101:6443 --token erqhfv.iq242lfyl554xql4 \
    --discovery-token-ca-cert-hash sha256:34fb3145a0f18908e83b727cc0bcbe9376ec786cc3c2e5555f18b43968d6c88c

在Master节点执行命令,查看节点是否加入集群

[root@k8s-node1 ~]# kubectl get node
NAME        STATUS   ROLES    AGE   VERSION
k8s-node1   Ready    master   18m   v1.14.0
k8s-node2   Ready    <none>   70s   v1.14.0
k8s-node3   Ready    <none>   31s   v1.14.0

如果在非Master节点上执行上述命令,会出现以下错误

[root@k8s-node2 ~]# kubectl get node
The connection to the server localhost:8080 was refused - did you specify the right host or port?

解决方案:

  1. 将Master节点/etc/kubernetes/admin.conf文件拷到node节点相同目录下
scp  /etc/kubernetes/admin.conf 192.168.0.102:/etc/kubernetes/admin.conf
  1. node节点配置环境变量
    vi ~/.bash_profile 增加一行内容"export KUBECONFIG=/etc/kubernetes/admin.conf"
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
export KUBECONFIG=/etc/kubernetes/admin.conf
  1. 使文件生效
source ~/.bash_profile
  1. 执行kubectl get node验证
[root@k8s-node2 ~]# kubectl get node
NAME        STATUS   ROLES    AGE     VERSION
k8s-node1   Ready    master   28m     v1.14.0
k8s-node2   Ready    <none>   10m     v1.14.0
k8s-node3   Ready    <none>   9m51s   v1.14.0
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐