使用kubeadm 安裝k8s單master
阿新 • • 發佈:2018-12-19
1.關閉selinux
setenforce 0
vim /etc/selinux/config
SELINUX=diabled
2.配置轉發引數
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
3.關掉防火牆
systemctl stop firewalld
systemctl diable firewalld
4.新增kubernetes源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo [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
5.rpm安裝kubeadm和docker
yum install kubeadm docker
systemctl start docker
systemctl start kubelet
systemctl enable docker kubelet
6.更改kubelet的啟動引數,主要是為了和docker的cgroup一致
vim /etc/systemd/system/kubelet.service.d/10-kubeadm.conf # Note: This dropin only works with kubeadm and kubelet v1.11+ [Service] Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --cgroup-driver=systemd" Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml" Environment="KUBELET_CGROUP_ARGS=--cgroup-driver=systemd --runtime-cgroups=/systemd/system.slice --kubelet-cgroups=/systemd/system.slice" # This is a file that "kubeadm init" and "kubeadm join" generates at runtime, populating the KUBELET_KUBEADM_ARGS variable dynamically EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env # This is a file that the user can use for overrides of the kubelet args as a last resort. Preferably, the user should use # the .NodeRegistration.KubeletExtraArgs object in the configuration files instead. KUBELET_EXTRA_ARGS should be sourced from this file. EnvironmentFile=-/etc/sysconfig/kubelet ExecStart= ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS $KUBELET_CGROUP_ARGS
7.無法訪問k8s.gcr.io的情況下,通過docker.io下載kubeadm必要的映象並修改tag
###kubeadm init初始化連線不到k8s.gcr.io會報錯,然後提示出需要的映象
docker pull mirrorgooglecontainers/kube-apiserver-amd64:v1.13.1
docker pull mirrorgooglecontainers/kube-controller-manager-amd64:v1.13.1
docker pull mirrorgooglecontainers/kube-scheduler-amd64:v1.13.1
docker pull mirrorgooglecontainers/kube-proxy-amd64:v1.13.1
docker pull mirrorgooglecontainers/pause:3.1
docker pull mirrorgooglecontainers/etcd-amd64:3.2.24
docker pull coredns/coredns:1.2.6
###tag修改
docker tag docker.io/mirrorgooglecontainers/kube-proxy-amd64:v1.13.1 k8s.gcr.io/kube-proxy:v1.13.1
docker tag docker.io/mirrorgooglecontainers/kube-scheduler-amd64:v1.13.1 k8s.gcr.io/kube-scheduler:v1.13.1
docker tag docker.io/mirrorgooglecontainers/kube-apiserver-amd64:v1.13.1 k8s.gcr.io/kube-apiserver:v1.13.1
docker tag docker.io/mirrorgooglecontainers/kube-controller-manager-amd64:v1.13.1 k8s.gcr.io/kube-controller-manager:v1.13.1
docker tag docker.io/mirrorgooglecontainers/etcd-amd64:3.2.24 k8s.gcr.io/etcd:3.2.24
docker tag docker.io/mirrorgooglecontainers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag docker.io/coredns/coredns:1.2.6 k8s.gcr.io/coredns:1.2.6
下面內容在master上單獨操作
8.初始化k8s
kubeadm init --kubernetes-version=v1.13.1 --pod-network-cidr=10.244.0.0/16
###根據提示繼續操作
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
####儲存以下內容,在安裝好後,在node節點上執行
kubeadm join 172.21.17.18:6443 --token rfe3jn.j3ceftk2n23rtbse --discovery-token-ca-cert-hash sha256:cf4276160c5fc2c09297bc20e4eb2bc3816cdeb359913f263f708242a591d363
9.安裝網路外掛,這裡注意檢視官方的文件,blog或者筆記有版本限制,導致出錯
#官方文件地址
https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/#before-you-begin
#yaml檔案地址
https://raw.githubusercontent.com/coreos/flannel/bc79dd1505b0c8681ece4de4c0d86c5cd2643275/Documentation/kube-flannel.yml
###根據yaml檔案安裝
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/bc79dd1505b0c8681ece4de4c0d86c5cd2643275/Documentation/kube-flannel.yml
10.檢視節點資訊
kubectl get pods --all-namespaces
11.所有pod顯示running表示成功,如果顯示pendding或者ContainerCreating,檢視詳細資訊
kubectl describe pods --namespace=kube-system pod-name
journalctl -f -xeu kubelet
12.節點加入忘了之前初始化提示的資訊,可使用以下命令建立token
kubeadm token create --print-join-command