这个工具能通过两条指令完成一个kubernetes集群的部署:
# 创建一个 Master 节点
kubeadm init
# 将一个 Node 节点加入到当前集群中
kubeadm join <Master节点的IP和端口 >
在开始之前,部署Kubernetes集群机器需要满足以下几个条件:
关闭防火墙:
# systemctl stop firewalld
# systemctl disable firewalld
关闭selinux:
# sed -i ‘s/enforcing/disabled/‘ /etc/selinux/config
# setenforce 0
关闭swap:
# swapoff -a # 临时
# vim /etc/fstab # 永久
添加主机名与IP对应关系):
# cat /etc/hosts
192.168.31.61 k8s-master
192.168.31.62 k8s-node1
192.168.31.63 k8s-node2
设置主机名:
# hostnamectl set-hostname k8s-master
将桥接的IPv4流量传递到iptables的链:
# cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# sysctl --system
Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker。
每台机器上安装Docker,建议使用18.09版本。
# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
# yum list docker-ce --showduplicates | sort -r
# yum -y install docker-ce-18.09.9-3.el7
设置cgroup驱动,推荐systemd:
# cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
# systemctl enable docker
# systemctl start docker
镜像下载加速:
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
[root@master ~]# cat /etc/docker/daemon.json
{"registry-mirrors": ["http://f1361db2.m.daocloud.io"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {"max-size": "100m"
},
"storage-driver": "overlay2"
}
# 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=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
由于版本更新频繁,这里指定版本号部署:
# yum install -y kubelet-1.16.0 kubeadm-1.16.0 kubectl-1.16.0
# systemctl enable kubelet
在192.168.31.61(Master)执行。
# kubeadm init --apiserver-advertise-address=192.168.2.91 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.16.0 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16
由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址。
记住返回值,后面要用到
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.2.91:6443 --token 7hqdrp.kopv0g7cphxgoo3z \
--discovery-token-ca-cert-hash sha256:59532acfbeebb14a5e392cf8aef8bcd49f347e874ab81a2bf29e0b28de320816
使用kubectl工具:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# kubectl get nodes
# kubectl apply -f kube-flannel.yaml
# kubectl get pods -n kube-system
确保能够访问到quay.io这个registery。
如果下载失败,可以改成这个镜像地址:lizhenliang/flannel:v0.11.0-amd64
在192.168.31.62/63(Node)执行。
向集群添加新节点,执行在kubeadm init输出的kubeadm join命令:
```(上面init初始化kubeadm时候的输出)
--discovery-token-ca-cert-hash sha256:00603a05805807501d7181c3d60b478788408cfe6cedefedb1f97569708be9c5
默认token有效期为24小时,当过期之后,该token就不可用了。这时就需要重新创建token,操作如下:
63bca849e0e01691ae14eab449570284f0c3ddeea590f8da988c07fe2729e924
<https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-join/>
## 8. 测试kubernetes集群
在Kubernetes集群中创建一个pod,验证是否正常运行:
访问地址:http://NodeIP:Port
## 9. 部署 Dashboard
访问地址:http://NodeIP:30001
创建service account并绑定默认cluster-admin管理员集群角色:
使用输出的token登录Dashboard。
原文:https://blog.51cto.com/13420391/2540288