使用kubeadm拉起三主一從k8s集羣

初始化
首先和上次一樣,我們需要對就四個節點做初始化準備,我們需要手動修改hosts配置文件,ntp時間,剩下的腳本可以完成。

[root@node15 ~]# cat init.sh 
#!/bin/bash
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
modprobe br_netfilter
cat >> /etc/sysctl.conf  <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward = 1
EOF
sysctl -p
mkdir /var/log/journal
mkdir /etc/systemd/journald.conf.d
cat >  /etc/systemd/journald.conf.d/prophet.conf <<EOF
[Journal]
#持久化保存到磁盤
Storage=persistent

# 壓縮歷史日誌
Compress=yes

SyncIntervalSec=5m
RateLimitInterval=30s
RateLimitBurst=1000

# 最大佔用空間
SystemMaxUse=5G

# 單個日誌文件最大
SystemMaxFileSize=200M

#日誌保存時間
MaxRetentionSec=2week

#不講日誌轉發到syslog
ForwardToSyslog=no
EOF
systemctl restart systemd-journald

cat > /etc/yum.repos.d/docker-ce.repo << EOF
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=0
EOF

yum repolist
yum install -y docker-ce-17.03.2-1.el7
yum install -y docker-ce-selinux-17.03.2-1.el7

sed -i '11c ExecStart=/usr/bin/dockerd --storage-driver=overlay2 --storage-opt overlay2.override_kernel_check=1' /usr/lib/systemd/system/docker.service
mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": ["https://ol663cbd.mirror.aliyuncs.com"],
  "insecure-registries":[ "0.0.0.0/0"],
   "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF

systemctl daemon-reload
systemctl start docker
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack_ipv4

chmod +x /etc/rc.local
cat >> /etc/rc.local << EOF
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack_ipv4
EOF

cat > /etc/yum.repos.d/k8s.repo <<EOF
[k8s]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enable=1
gpgcheck=0
EOF

yum install -y kubeadm-1.15.1 kubectl-1.15.1 kubelet-1.15.1
systemctl enable kubelet.service
systemctl enable docker.service

配置keepailved和haproxy
我們有一個虛擬ip爲192.168.100.100
配置haproxy的監聽端口爲16443,轉發到100.12/13/14的6443端口,因爲這個是我們將會使用的apiserver的端口,不過需要先把13/14的轉發註釋掉,因爲我們在12上會先啓動apiserver,此時部分服務需要連接apiserver,會去連接100.100的16443接口,不可以讓轉發請求到13/14的apiserver接口,因爲此時還沒有啓動.

初始化主節點
首先生成一份配置文件,然後修改

[root@node12 ~]# cat  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:
  advertiseAddress: 192.168.100.12
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  name: node12.localhost.localdomain
  taints:
  - effect: NoSchedule
    key: node-role.kubernetes.io/master
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controlPlaneEndpoint: "192.168.100.100:16443"
controllerManager: {}
dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd
kind: ClusterConfiguration
kubernetesVersion: v1.15.1
imageRepository: registry.cn-hangzhou.aliyuncs.com/oyd
networking:
  dnsDomain: cluster.local
  podSubnet: 10.244.0.0/16
  serviceSubnet: 10.96.0.0/12
scheduler: {}
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
featureGates:
  SupportIPVSProxyMode: true
mode: ipvs

啓動:

[root@node12 ~]# kubeadm init --experimental-upload-certs --config=init.default.yaml | tee kubeadm.log
Flag --experimental-upload-certs has been deprecated, use --upload-certs instead
[init] Using Kubernetes version: v1.15.1
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [node12.localhost.localdomain kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.100.12 192.168.100.100]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [node12.localhost.localdomain localhost] and IPs [192.168.100.12 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [node12.localhost.localdomain localhost] and IPs [192.168.100.12 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[endpoint] WARNING: port specified in controlPlaneEndpoint overrides bindPort in the controlplane address
[kubeconfig] Writing "admin.conf" kubeconfig file
[endpoint] WARNING: port specified in controlPlaneEndpoint overrides bindPort in the controlplane address
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[endpoint] WARNING: port specified in controlPlaneEndpoint overrides bindPort in the controlplane address
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[endpoint] WARNING: port specified in controlPlaneEndpoint overrides bindPort in the controlplane address
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 22.537245 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.15" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Storing the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
[upload-certs] Using certificate key:
6c5b0d5f4f6d32201b76706151590f886be5b29ea4b0f9d95fbd1019cc60ff0d
[mark-control-plane] Marking the node node12.localhost.localdomain as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node node12.localhost.localdomain as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: abcdef.0123456789abcdef
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[endpoint] WARNING: port specified in controlPlaneEndpoint overrides bindPort in the controlplane address
[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!

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/

You can now join any number of the control-plane node running the following command on each as root:

  kubeadm join 192.168.100.100:16443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:fc038e1906fd3b9f64b397fba9e909b3b5054302fac12bb848697dd3f79a2768 \
    --control-plane --certificate-key 6c5b0d5f4f6d32201b76706151590f886be5b29ea4b0f9d95fbd1019cc60ff0d

Please note that the certificate-key gives access to cluster sensitive data, keep it secret!
As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use 
"kubeadm init phase upload-certs --upload-certs" to reload certs afterward.

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.100.100:16443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:fc038e1906fd3b9f64b397fba9e909b3b5054302fac12bb848697dd3f79a2768 

我們可以看到和原來單節點的不同,這個時候如果需要加入爲主節點,需要運行上面的join,從節點就是下面的join,我們在13/14上執行第一個join,在15上執行第二個join

添加網絡插件

[root@node12 ~]# kubectl apply -f kube-flannel.yml 
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds-amd64 created

查看信息:此時我們就有了一個三主一從的k8s集羣。

[root@node12 ~]# kubectl get pod -n kube-system
NAME                                                   READY   STATUS    RESTARTS   AGE
coredns-59d56d6dd7-6qv2h                               1/1     Running   0          17m
coredns-59d56d6dd7-8444b                               1/1     Running   0          17m
etcd-node12.localhost.localdomain                      1/1     Running   0          16m
etcd-node13.localhost.localdomain                      1/1     Running   0          16m
etcd-node14.localhost.localdomain                      1/1     Running   0          16m
kube-apiserver-node12.localhost.localdomain            1/1     Running   0          17m
kube-apiserver-node13.localhost.localdomain            1/1     Running   0          16m
kube-apiserver-node14.localhost.localdomain            1/1     Running   0          15m
kube-controller-manager-node12.localhost.localdomain   1/1     Running   1          17m
kube-controller-manager-node13.localhost.localdomain   1/1     Running   0          16m
kube-controller-manager-node14.localhost.localdomain   1/1     Running   0          15m
kube-flannel-ds-amd64-22j7h                            1/1     Running   0          15m
kube-flannel-ds-amd64-2rdkt                            1/1     Running   0          15m
kube-flannel-ds-amd64-lmhb7                            1/1     Running   0          15m
kube-flannel-ds-amd64-rcpsp                            1/1     Running   0          15m
kube-proxy-7sxxp                                       1/1     Running   0          15m
kube-proxy-dmv8n                                       1/1     Running   0          17m
kube-proxy-t7pmh                                       1/1     Running   0          17m
kube-proxy-vl6pz                                       1/1     Running   0          16m
kube-scheduler-node12.localhost.localdomain            1/1     Running   1          17m
kube-scheduler-node13.localhost.localdomain            1/1     Running   0          16m
kube-scheduler-node14.localhost.localdomain            1/1     Running   0          15m
[root@node12 ~]# kubectl get node
NAME                           STATUS   ROLES    AGE   VERSION
node12.localhost.localdomain   Ready    master   18m   v1.15.1
node13.localhost.localdomain   Ready    master   16m   v1.15.1
node14.localhost.localdomain   Ready    master   16m   v1.15.1
node15.localhost.localdomain   Ready    <none>   17m   v1.15.1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章