主機 | 類型 | 操做系統 |
---|---|---|
192.168.124.180 | Master | Centos 7 |
192.168.124.181 | Node | Centos 7 |
192.168.124.182 | Node | Centos 7 |
hostnamectl set-hostname master hostnamectl set-hostname node1 hostnamectl set-hostname node2 # 修改 /etc/hosts 192.168.124.180 master 192.168.124.181 node1 192.168.124.182 node2
# 禁用主機SELinux,讓容器能夠讀取主機文件系統 setenforce 0 # 關閉防火牆 systemctl disable firewalld systemctl stop firewalld # #實時動態關閉交換分區 swapoff -a # 註釋 swap 行 vim /etc/fstab
cd /etc/yum.repos.d && \ sudo mv CentOS-Base.repo CentOS-Base.repo.bak && \ sudo wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \ yum clean all && \ yum makecache
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
Docker 安裝html
# 換源 mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://registry.docker-cn.com"], "exec-opts": ["native.cgroupdriver=systemd"] } EOF service docker restart
yum install kubelet kubeadm kubectl
kubeadm config images list
kubeadm config images list |sed -e 's/^/docker pull /g' -e 's#k8s.gcr.io#registry.cn-hangzhou.aliyuncs.com/google_containers#g' |sh -x docker images |grep registry.cn-hangzhou.aliyuncs.com/google_containers |awk '{print "docker tag ",$1":"$2,$1":"$2}' |sed -e 's#registry.cn-hangzhou.aliyuncs.com/google_containers#k8s.gcr.io#2' |sh -x docker images |grep registry.cn-hangzhou.aliyuncs.com/google_containers |awk '{print "docker rmi ", $1":"$2}' |sh -x
systemctl enable docker && systemctl start docker systemctl enable kubelet && systemctl start kubelet
kubeadm config print init-defaults > init.default.yaml kubeadm init --config=init.default.yaml
apiVersion: kubeadm.k8s.io/v1beta2 bootstrapTokens: - groups: - system:bootstrappers:kubeadm:default-node-token token: abcdef.0123456789abcdef ttl: 24h0m0s usages: - signing - authentication kind: InitConfiguration localAPIEndpoint: # 修改成master的ip地址 advertiseAddress: 192.168.124.180 bindPort: 6443 nodeRegistration: criSocket: /var/run/dockershim.sock name: k8s-master taints: - effect: NoSchedule key: node-role.kubernetes.io/master --- apiServer: timeoutForControlPlane: 4m0s apiVersion: kubeadm.k8s.io/v1beta2 certificatesDir: /etc/kubernetes/pki clusterName: kubernetes controllerManager: {} dns: type: CoreDNS etcd: local: dataDir: /var/lib/etcd imageRepository: k8s.gcr.io kind: ClusterConfiguration # 修改版本爲v1.17.2 kubernetesVersion: v1.17.2 networking: dnsDomain: cluster.local # 添加pod網段 podSubnet: "10.224.0.0/16" serviceSubnet: 10.96.0.0/12 scheduler: {} ### 使用ipvs ### yum install ipvsadm ### 查看 ipvsadm -Ln --- apiVersion: kubeproxy.config.k8s.io/v1alpha1 kind: KubeProxyConfiguration featureGates: SupportIPVSProxyMode: true mode: ipvs
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.124.180:6443 --token abcdef.0123456789abcdef \ --discovery-token-ca-cert-hash sha256:58056cdd9dfd9cc91da880adc63a2a7e9e7594eeda1bf7c2bcdceb0fbbf255ad
kubectl create -f kube-flannel.yml
# 查看node仍是處於NoReay狀態 kubectl get node # NAME STATUS ROLES AGE VERSION # k8s-master NotReady master 5h41m v1.17.2
# NAME STATUS ROLES AGE VERSION # k8s-master Ready master 5h59m v1.17.2
kubeadm join 192.168.124.183:6443 --token abcdef.0123456789abcdef \ --discovery-token-ca-cert-hash sha256:58056cdd9dfd9cc91da880adc63a2a7e9e7594eeda1bf7c2bcdceb0fbbf255ad
kubectl get node kubectl get pods --all-namespaces
虛擬機配置比較低,因此會出現一些錯誤,咱們能夠按照提示忽略【非生產環境】
node
[ERROR NumCPU CPU] 必須超過2個,把虛擬機的處理器設置一下便可。
[ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables] 解決:echo "1" > /proc/sys/net/bridge/bridge-nf-call-iptables
[ERROR DirAvailable--var-lib-etcd]: /var/lib/etcd is not empty 解決:清空/var/lib/etcd 目錄
[ERROR FileContent--proc-sys-net-ipv4-ip_forward]: /proc/sys/net/ipv4/ip_forward contents are not set to 1 解決: echo '1' > /proc/sys/net/ipv4/ip_forwardlinux