2018-02-06 使用kubeadm启动kubenetes集群

1. Prepare VM 

2. Prepare OS: CentOS7

参考link: https://kubernetes.io/docs/setup/independent/install-kubeadm/

Before you begin


OS: CentOS 7

2 GB or more of RAM per machine (any less will leave little room for your apps)(安装之前配置)

2 CPUs or more(安装之前配置)

Full network connectivity between all machines in the cluster (public or private network is fine)

Unique hostname, MAC address, and product_uuid for every node

Certain ports are open on your machines. See the section below for more details

Swap disabled. You MUST disable swap in order for the kubelet to work properly

每个node要有不同的host_name,MAC address和Product_UUID


两台虚拟机

1. Master: 带界面

2. Node

方法一:在安装CentOS过程中,打开network 设置,在这里设置上述属性

方法二:可在shell中更改

>>hostname       查询主机名

node安装docker

所有的机器以root账户运行

>>yum install -y docker

如果出现无法联网,解决方法如下:

方法一、

1、打开 vi /etc/sysconfig/network-scripts/ifcfg-eth0(每个机子都可能不一样,但格式会是“ifcfg-eth数字”),把ONBOOT=no,改为ONBOOT=yes

2、重启网络:service network restart

方法二、

1、打开 vi /etc/resolv.conf,增加 nameserver 8.8.8.8

2、重启网络: service network restart

配置docker的group (可选?在我的实践中并没有进行这个配置,但是并没有出现问题)

cat << EOF > /etc/docker/daemon.json

{

"exec-opts": ["native.cgroupdriver=systemd"]

}

EOF

systemctl enable docker && systemctl start docker

安装 kubeadm, kubelet 和 kubectl

kubeadm the command to bootstrap the cluster

kubelet the component that run on all of the machines in your cluster and does things like starting pods and containters

kubectl the command line util to talk to your cluster

安装kubeadm, kubelet和kubect前,需做如下配置

配置Kubernetes repo,为了能使用yum来安装kubenetes

cat < 

[kubernetes]

name=Kubernetes

baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64

enabled=1

gpgcheck=1

repo_gpgcheck=1

gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg

https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg

EOF

setenforce 0 关闭SELinux  -- 这个需要修改/etc/selinux/config来彻底关闭SELinux,否则无法正常启动服务,修改了/etc/selinux/config以后需要重启机器来使配置修改生效

yum install -y kubelet kubeadm kubectl 

systemctl enable kubelet && systemctl start kubelet

关闭swap 在每个vi 机器上运行 swapoff -a

systemctl stop firewalld.service

运行 创建 cluster

kubeadm init 进行初始化


kubeadm reset: 重置kubeadm状态

根据选择的pod network 选择使用一个相应的flag, 对于Flannel 需要使用 --pod-network-cidr=10.244.0.0/16 这是一个固定式无需修改


kubeadm init --pod-network-cidr=10.244.0.0/16

kubeadm 再次运行之前需要先 tearing down cluster: kubeadm reset

如果有两个网卡的话,需要指定network interface --apiserver-advertise-address=

由于使用了Flannel所有这个命令要变种为 kubeadm init --apiserver-advertise-address=192.168.56.102 --pod-network-cidr=10.244.0.0/16 --- 这个命令仅使用与我自己的配置了双网卡的机器

加载配置

root 用户

export KUBECONFIG=/etc/kubernetes/admin.conf

非 root 用户po

pod network --- 使DNS服务启动  

这里我们用 Flannel配置 iptable sysctl net.bridge.bridge-nf-call-iptables=1

应用 kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml  

节点:

Note节点直接运行 kubuadm init输出的那个kubeadm join 命令来加入master节点

注意Node节点上也需要运行 systemctl start kubelet

前面一直到初始化,操作都一致

查看命令

kubectl get nodes

kubectl get pods --all-namespaces    

安装Dashboard 服务

kubectl apply -fhttps://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml

运行 kubectl proxy

在master机器上访问http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/

运行Dashboard的时候可能需要只让系统保留一个网卡运行,之前两个网卡的时候打不开dashboard,用了一个网卡的环境可以打开

dashborad网页打开需要输入token

参看网页https://github.com/kubernetes/dashboard/wiki/Creating-sample-user

创建admin-user, 创建一个yaml文件并输入以下内容,并运行命令kubectl create -f xxx.yaml

apiVersion: v1 

 kind: ServiceAccount 

 metadata: 

     name: admin-user

     namespace: kube-system


创建 ClusterRoleBinding,创建一个yaml文件并输入以下内容,并运行命令kubectl create -f xxx.yaml

apiVersion: rbac.authorization.k8s.io/v1beta1 

    kind: ClusterRoleBinding

metadata: 

    name: admin-user 

roleRef:

    apiGroup: rbac.authorization.k8s.io 

    kind: ClusterRole 

    name: cluster-admin 

subjects: 

  -  kind: ServiceAccount 

    name: admin-user

    namespace: kube-system


kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')

Others:

进入 heapster-1.5.0/deploy/kube-config/influxdb 修改grafana.yaml 中的 GF_SERVER_ROOT_URL uncomments apiservier设置

运行sh heapster-1.5.0/deploy/kube.sh start

kubectl get pods --all-namespaces 查看所有运行的服务的状态

在使用ss 代理的情况下,需要先把需要的image pull下来

docker pull k8s.gcr.io/kube-apiserver-amd64:v1.9.1

docker pull k8s.gcr.io/kube-controller-manager-amd64:v1.9.1

docker pull k8s.gcr.io/kube-scheduler-amd64:v1.9.1

docker pull k8s.gcr.io/kube-proxy-amd64:v1.9.1

docker pull k8s.gcr.io/etcd-amd64:3.1.10

docker pull k8s.gcr.io/pause-amd64:3.0

docker pull k8s.gcr.io/k8s-dns-sidecar-amd64:1.14.7

docker pull k8s.gcr.io/k8s-dns-kube-dns-amd64:1.14.7

docker pull k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64:1.14.7

在work节点上也需要pull 镜像,只需要pull docker pull k8s.gcr.io/kube-proxy-amd64:v1.9.1

要是服务能够整个的运行需要彻底关闭SELinux,这需要设置 /etc/selinux/config

问题速查:

docker logs 查docker运行过程中的问题

https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-init/

中文参考

参考https://mritd.me/2016/10/29/set-up-kubernetes-cluster-by-kubeadm/#21%E5%AE%89%E8%A3%85%E5%8C%85%E4%BB%8E%E5%93%AA%E6%9D%A5

运行init成功的例子:

Your Kubernetes master has initialized successfully!

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 --token cb327c.f171b1e736a5184e 192.168.56.102:6443 --discovery-token-ca-cert-hash sha256:392545adad830f474ee0409691d1cb9a6d8f2499decdd903c788ccb60e2cb247

额外参考: https://www.jianshu.com/p/9c7e1c957752

你可能感兴趣的:(2018-02-06 使用kubeadm启动kubenetes集群)