master 节点 (高可用)
kube-apiserver: 无状态,直接前面挂负载均衡
kube-controller-manager: 有状态,集群只能有一个负责处理事务,通过选举leader的方式实现高可用(三台)
kube-scheduler: 有状态,同kube-controller-manager(三台)etcd
基于raft算法实现数据存储高可用 (三台)worker节点
基于多个worker节点实现业务高可用,无需额外设置
集群自带etcd高可用部署,参考 https://kubernetes.io/docs/setup/independent/high-availability/
1. 三个节点分别安装kubeadm,kubelet
root@master-192:/etc/apt# apt-get install kubeadm kubelet
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
root@master-192:/etc/apt# dpkg -l|grep kube
ii kubeadm 1.13.1-00 amd64 Kubernetes Cluster Bootstrapping Tool
ii kubectl 1.13.1-00 amd64 Kubernetes Command Line Tool
ii kubelet 1.13.1-00 amd64 Kubernetes Node Agent
ii kubernetes-cni 0.6.0-00 amd64 Kubernetes CNI
修改kubelet参数
root@master-192:/etc/apt# vi /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
...
Environment="KUBELET_POD_INFRA_CONTAINER=--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.0"
ExecStart=/usr/bin/kubelet $KUBELET_POD_INFRA_CONTAINER $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS
设置kubelet开机启动
root@master-192:/etc/apt# systemctl daemon-reload
root@master-192:/etc/apt# systemctl enable kubelet
2.配置haproxy
#---------------my k8s proxy--------------------
...
listen k8s_admin[root@localhost ~]# cat /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
bind 172.30.81.199:6443
mode tcp
server node194 172.30.81.194:6443
server node193 172.30.81.193:6443
server node192 172.30.81.192:6443
启动haproxy
[root@localhost ~]# netstat -antp|grep 6443
tcp 0 0 172.30.81.199:6443 0.0.0.0:* LISTEN 1630/haproxy
3.初始集群
root@master-194:/etc/apt/sources.list.d# cat /root/ha/kubeadm.conf
apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
kubernetesVersion: v1.13.0
apiServer:
certSANs:
- "172.30.81.199"
controlPlaneEndpoint: "172.30.81.199:6443"
networking:
dnsDomain: cluster.local
podSubnet: "192.168.0.0/16"
serviceSubnet: 10.96.0.0/12
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
root@master-194:/etc/apt/sources.list.d# kubeadm init --config /root/ha/kubeadm.conf
[init] Using Kubernetes version: v1.13.0
[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 [master-194 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.30.81.194 172.30.81.199 172.30.81.199]
[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 [master-194 localhost] and IPs [172.30.81.194 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [master-194 localhost] and IPs [172.30.81.194 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"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[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 23.012330 seconds
[uploadconfig] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.13" in namespace kube-system with the configuration for the kubelets in the cluster
[patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "master-194" as an annotation
[mark-control-plane] Marking the node master-194 as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node master-194 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: so9886.5lxjilqqrkl1d2z0
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstraptoken] creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
Your Kubernetes master 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 machines by running the following on each node
as root:
kubeadm join 172.30.81.199:6443 --token so9886.5lxjilqqrkl1d2z0 --discovery-token-ca-cert-hash sha256:bbcddbf66dbefeb645598614e28c1557fb04fb4fbd288b36edc18816250f9e77
查看节点情况
root@master-194:/etc/apt/sources.list.d# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master-194 NotReady master 81s v1.13.1
root@master-194:/etc/apt/sources.list.d# kubectl get po -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-89cc84847-hznhd 0/1 Pending 0 88s
coredns-89cc84847-r2w2g 0/1 Pending 0 88s
etcd-master-194 1/1 Running 0 52s
kube-apiserver-master-194 1/1 Running 0 46s
kube-controller-manager-master-194 1/1 Running 0 39s
kube-proxy-72844 1/1 Running 0 88s
kube-scheduler-master-194 1/1 Running 0 36s
初始化网络
下载地址 https://github.com/xiaotech/calico
root@master-194:~/adds/calico# kubectl create -f .
configmap/calico-config created
secret/calico-etcd-secrets created
daemonset.extensions/calico-node created
serviceaccount/calico-node created
deployment.extensions/calico-kube-controllers created
serviceaccount/calico-kube-controllers created
daemonset.extensions/calico-etcd created
service/calico-etcd created
clusterrole.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrolebinding.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrole.rbac.authorization.k8s.io/calico-node created
clusterrolebinding.rbac.authorization.k8s.io/calico-node cr
网络部署完成后,查看状态
root@master-194:~/adds/calico# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master-194 Ready master 8m38s v1.13.1
root@master-194:~/adds/calico# kubectl get po -n kube-system
NAME READY STATUS RESTARTS AGE
calico-etcd-dfzht 1/1 Running 0 6m7s
calico-kube-controllers-7df47b8cbc-dnq4l 1/1 Running 0 6m15s
calico-node-8gqw2 2/2 Running 1 6m15s
coredns-89cc84847-hznhd 1/1 Running 0 8m27s
coredns-89cc84847-r2w2g 1/1 Running 0 8m27s
etcd-master-194 1/1 Running 0 7m51s
kube-apiserver-master-194 1/1 Running 0 7m45s
kube-controller-manager-master-194 1/1 Running 0 7m38s
kube-proxy-72844 1/1 Running 0 8m27s
kube-scheduler-master-194 1/1 Running 0 7m35s
4.其余master节点的加入
首先拷贝证书文件到其他节点
master_nodes="172.30.81.192 172.30.81.193"
for i in $master_nodes
do
ssh $i mkdir /etc/kubernetes/pki/etcd -p
scp /etc/kubernetes/pki/ca.crt ${i}:/etc/kubernetes/pki/
scp /etc/kubernetes/pki/ca.key ${i}:/etc/kubernetes/pki/
scp /etc/kubernetes/pki/sa.key ${i}:/etc/kubernetes/pki/
scp /etc/kubernetes/pki/sa.pub ${i}:/etc/kubernetes/pki/
scp /etc/kubernetes/pki/front-proxy-ca.crt ${i}:/etc/kubernetes/pki/
scp /etc/kubernetes/pki/front-proxy-ca.key ${i}:/etc/kubernetes/pki/
scp /etc/kubernetes/pki/etcd/ca.crt ${i}:/etc/kubernetes/pki/etcd/
scp /etc/kubernetes/pki/etcd/ca.key ${i}:/etc/kubernetes/pki/etcd/
scp /etc/kubernetes/admin.conf ${i}:/etc/kubernetes/
done
join节点
root@master-193:~# kubeadm join 172.30.81.199:6443 --token so9886.5lxjilqqrkl1d2z0 --discovery-token-ca-cert-hash sha256:bbcddbf66dbefeb645598614e28c1557fb04fb4fbd288b36edc18816250f9e77 --experimental-control-plane
[preflight] Running pre-flight checks
[WARNING Hostname]: hostname "master-193" could not be reached
[WARNING Hostname]: hostname "master-193": lookup master-193 on 114.114.114.114:53: no such host
[discovery] Trying to connect to API Server "172.30.81.199:6443"
[discovery] Created cluster-info discovery client, requesting info from "https://172.30.81.199:6443"
[discovery] Requesting info from "https://172.30.81.199:6443" again to validate TLS against the pinned public key
[discovery] Cluster info signature and contents are valid and TLS certificate validates against pinned roots, will use API Server "172.30.81.199:6443"
[discovery] Successfully established connection with API Server "172.30.81.199:6443"
[join] Reading configuration from the cluster...
[join] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[join] Running pre-flight checks before initializing the new control plane instance
[WARNING Hostname]: hostname "master-193" could not be reached
[WARNING Hostname]: hostname "master-193": lookup master-193 on 114.114.114.114:53: no such host
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [master-193 localhost] and IPs [172.30.81.193 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [master-193 localhost] and IPs [172.30.81.193 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [master-193 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.30.81.193 172.30.81.199 172.30.81.199]
[certs] Generating "front-proxy-client" certificate and key
[certs] valid certificates and keys now exist in "/etc/kubernetes/pki"
[certs] Using the existing "sa" key
[kubeconfig] Using existing up-to-date kubeconfig file: "/etc/kubernetes/admin.conf"
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[etcd] Checking Etcd cluster health
[kubelet] Downloading configuration for the kubelet from the "kubelet-config-1.13" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[tlsbootstrap] Waiting for the kubelet to perform the TLS Bootstrap...
[patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "master-193" as an annotation
[etcd] Announced new etcd member joining to the existing etcd cluster
[etcd] Wrote Static Pod manifest for a local etcd instance to "/etc/kubernetes/manifests/etcd.yaml"
[uploadconfig] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[mark-control-plane] Marking the node master-193 as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node master-193 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
This node has joined the cluster and a new control plane instance was created:
* Certificate signing request was sent to apiserver and approval was received.
* The Kubelet was informed of the new secure connection details.
* Master label and taint were applied to the new node.
* The Kubernetes control plane instances scaled up.
* A new etcd member was added to the local/stacked etcd cluster.
To start administering your cluster from this node, 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
Run 'kubectl get nodes' to see this node join the cluster.
查看节点情况
root@master-194:/home# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master-192 Ready master 2m32s v1.13.1
master-193 Ready master 4m49s v1.13.1
master-194 Ready master 24m v1.13.1
root@master-194:/home# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
calico-etcd-72cfp 1/1 Running 0 22s
calico-etcd-dfzht 1/1 Running 0 21m
calico-etcd-ptzz4 1/1 Running 0 4m25s
calico-kube-controllers-7df47b8cbc-dnq4l 1/1 Running 0 22m
calico-node-46hc6 2/2 Running 2 5m11s
calico-node-6qczp 2/2 Running 0 2m54s
calico-node-8gqw2 2/2 Running 1 22m
coredns-89cc84847-hznhd 1/1 Running 0 24m
coredns-89cc84847-r2w2g 1/1 Running 0 24m
etcd-master-192 1/1 Running 1 2m52s
etcd-master-193 1/1 Running 0 5m10s
etcd-master-194 1/1 Running 0 23m
kube-apiserver-master-192 1/1 Running 2 2m53s
kube-apiserver-master-193 1/1 Running 0 5m10s
kube-apiserver-master-194 1/1 Running 0 23m
kube-controller-manager-master-192 1/1 Running 2 2m53s
kube-controller-manager-master-193 1/1 Running 0 5m10s
kube-controller-manager-master-194 1/1 Running 1 23m
kube-proxy-2t6r4 1/1 Running 0 5m11s
kube-proxy-72844 1/1 Running 0 24m
kube-proxy-vjr8k 1/1 Running 1 2m54s
kube-scheduler-master-192 1/1 Running 2 2m53s
kube-scheduler-master-193 1/1 Running 0 5m10s
kube-scheduler-master-194 1/1 Running 1 23m