kubernetes集群安装2019最新

集群安装准备工作请参考https://segmentfault.com/a/1190000020119190

1.环境介绍

一共三台
CentOS Linux release 7.6.1810 (Core)
192.168.1.100 master
192.168.1.101 node1
192.168.1.102 node2

2.Master、Node节点安装、配置Docker

# 卸载原来的docker
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

# 安装依赖
sudo yum update -y && sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
  
#添加阿里云yum源(官网的源比较慢)
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    
# 安装docker的指定版本
查看版本
$ yum list docker-ce --showduplicates | sort -r
$ sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
例如:yum install docker-ce-18.09.5 docker-ce-cli-18.09.5 containerd.io -y

# 查看docker版本
docker --version

# 开机启动
systemctl enable --now docker

修改docker cgroup驱动,与k8s一致,使用systemd

# 修改docker cgroup驱动:native.cgroupdriver=systemd
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"
  ]
}
EOF

systemctl restart docker # 重启使配置生效

3.安装 kubelet kubeadm kubectl

master ,node节点都要安装
用阿里云yum源安装

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 install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

systemctl enable --now kubelet  # 开机启动kubelet

4.使用kubeadm创建集群

只在master节点执行

# 初始化Master(Master需要至少2核)此处会各种报错,异常...成功与否就在此
kubeadm init --apiserver-advertise-address 192.168.1.100 \
--pod-network-cidr 10.244.0.0/16 \
--image-repository=registry.aliyuncs.com/google_containers
# --kubernetes-version 1.14.1
# --apiserver-advertise-address 指定与其它节点通信的接口
# --pod-network-cidr 指定pod网络子网,使用fannel网络必须使用这个CIDR
# --image-repository=registry.aliyuncs.com/google_containers初始化时可以添加下载镜像的源地址。因为默认是google的,国内无法访问。这样前面就不要提前下载好镜像了。

5.用户设置权限(root用户也需要执行)

master 执行

mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

6.应用flannel网络

master 执行

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

7.node节点加入到master

# node1:
kubeadm join 192.168.1.100:6443 --token te0fvk.dbf7t1qsu4kpvxe2 \
    --discovery-token-ca-cert-hash sha256:dd74bd1b52313dd8664b8147cb6d18a6f8b25c6c5aa4debc3f2873ea2402b9a5 
# node2:
kubeadm join 192.168.1.100:6443 --token te0fvk.dbf7t1qsu4kpvxe2 \
    --discovery-token-ca-cert-hash sha256:dd74bd1b52313dd8664b8147cb6d18a6f8b25c6c5aa4debc3f2873ea2402b9a5

8.结果检查

node节点加入master输出信息如下:

[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.15" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

master 节点执行kubectl get nodes

[root@master ~]# kubectl get nodes
NAME     STATUS   ROLES    AGE     VERSION
master   Ready    master   14m     v1.15.2
node1    Ready    <none>   3m36s   v1.15.2
node2    Ready    <none>   101s    v1.15.2

至此安装完毕!
注意:您的环境跟我一样的话,一般不会出错。如果有不了解的可以私信,或关注我的公主号:运维大师兄。

相关推荐