kubeadm之ha (1)

  • 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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,258评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,335评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,225评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,126评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,140评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,098评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,018评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,857评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,298评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,518评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,678评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,400评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,993评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,638评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,801评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,661评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,558评论 2 352

推荐阅读更多精彩内容