在CentOS7环境下,一步步傻瓜式安装Kubernetes单主节点集群环境,Kubernetes为最新版本v1.8.6。
安装docker 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 yum install -y yum-utils device-mapper-persistent-data lvm2 yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo yum install -y docker-ce docker-ce-cli containerd.io mkdir /etc/docker cat <<EOF | sudo tee /etc/docker/daemon.json { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2", "storage-opts": [ "overlay2.override_kernel_check=true" ], "registry-mirrors": [ "https://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn", "https://mirror.ccs.tencentyun.com", "https://reg-mirror.qiniu.com", "https://registry.docker-cn.com" ] } EOF mkdir -p /etc/systemd/system/docker.service.d systemctl enable docker systemctl daemon-reload systemctl start docker
安装kubernetes
新建 /etc/yum.repos.d/kubernetes.repo,内容为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 # Add kubernetes repo cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo [kubernetes] name=kubernetes baseurl=https://mirrors.tuna.tsinghua.edu.cn/kubernetes/yum/repos/kubernetes-el7-\$basearch enabled=1 gpgcheck=0 EOF # Disable swap swapoff -a # Letting iptables see bridged traffic cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl --system # Set SELinux in permissive mode (effectively disabling it) setenforce 0 sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes systemctl enable --now kubelet
主节点初始化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 export K8S_VERSION=v1.18.6 export MASTER_IP=10.0.1.7 export APISERVER_NAME=k8s-m01 export POD_SUBNET=10.100.0.1/16 echo "${MASTER_IP} ${APISERVER_NAME}" >> /etc/hosts cat <<EOF > ./kubeadm-config.yaml apiVersion: kubeadm.k8s.io/v1beta2 kind: InitConfiguration nodeRegistration: name: ${APISERVER_NAME} --- apiVersion: kubeadm.k8s.io/v1beta2 kind: ClusterConfiguration kubernetesVersion: ${K8S_VERSION} imageRepository: registry.aliyuncs.com/k8sxio controlPlaneEndpoint: "${APISERVER_NAME}:6443" networking: serviceSubnet: "10.96.0.0/16" podSubnet: "${POD_SUBNET}" dnsDomain: "cluster.local" EOF kubeadm init --config=kubeadm-config.yaml --upload-certs mkdir -p $HOME/.kube cp -i /etc/kubernetes/admin.conf $HOME/.kube/config chown $(id -u):$(id -g) $HOME/.kube/config
如果kube镜像下载不下来可用备选方案
1 2 3 4 5 6 7 8 9 10 # plan b images=( kube-apiserver:v1.18.6 kube-controller-manager:v1.18.6 kube-scheduler:v1.18.6 kube-proxy:v1.18.6 ) for imageName in ${images[@]} ; do curl -s https://zhangguanzhang.github.io/bash/pull.sh | bash -s -- k8s.gcr.io/$imageName done
安装网络
1 2 curl https://docs.projectcalico.org/manifests/calico.yaml -o calico.yaml kubectl apply -f calico.yaml
1 KUBECONFIG=/etc/kubernetes/admin.conf kubectl apply -f https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/kubeadm-kuberouter.yaml
验证安装结果
1 2 watch kubectl get pod -n kube-system -o wide kubectl get nodes -o wide
加入主节点
在主节点上执行命令
1 kubeadm token create --print-join-command
例如,输出以下内容
kubeadm join k8s-m01:6443 –token egc33s.h7xd65n5asw9enya –discovery-token-ca-cert-hash sha256:8b662c0ce7e5e0c49c37ea9a40b8ea4a6e99b6df6ec0e881a368ed8002420ace
在工作节点上配置hosts
1 echo "192.168.0.146 k8s-w01" >> /etc/hosts
复制主节点上的命令输出结果,在工作节点上执行加入主节点命令
1 kubeadm join k8s-m01:6443 --token egc33s.h7xd65n5asw9enya --discovery-token-ca-cert-hash sha256:8b662c0ce7e5e0c49c37ea9a40b8ea4a6e99b6df6ec0e881a368ed8002420ace --node-name=k8s-w01
在客户端电脑安装 kubectl
安装kubectl
在本机上创建~/.kube/config文件,并直接复制主节点的/etc/kubernetes/admin.conf内容
配置hosts文件
验证安装结果,执行命令
输出全部节点都是Reday状态即可
NAME STATUS ROLES AGE VERSION k8s-m01 Ready master 4h15m v1.18.6 k8s-w01 Ready 4h5m v1.18.6 k8s-w02 Ready 59m v1.18.6