ubuntu16.04 kubeadm快速搭建kubernetes环境

刚开始百度到一些很复杂的教程,不太方便入门。

由于kebernetes的源放在google,所以,需要科学上网。

kubernetes的基础知识请自行找教程学习。

主要参考:
https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/
http://blog.csdn.net/ximenghappy/article/details/70157361

step1:安装相应软件

apt-get update 
apt-get install -y curl apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF


apt-get update
#如果没有安装docker的需要先安装docker,我用官网最新版本的docker遇到版本不匹配的情况,所以还是按照官方教程做比较好
apt-get install -y docker.io 

apt-get install -y kubelet kubeadm kubectl kubernetes-cni

step2:使用kubeadm 初始化 kubernetes 集群

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

这个时候,kubeadm会初始化各种环境配置,包括部署证书(private ca)和密钥,然后调用docker pull相应的镜像

以下是log输出

[kubeadm] WARNING: kubeadm is in beta, please do not use it for production clusters.
[init] Using Kubernetes version: v1.6.6
[init] Using Authorization modes: [RBAC]
[preflight] Running pre-flight checks
[certificates] Generated CA certificate and key.
[certificates] Generated API server certificate and key.
[certificates] API Server serving cert is signed for DNS names [c-pc kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.217.135]
[certificates] Generated API server kubelet client certificate and key.
[certificates] Generated service account token signing key and public key.
[certificates] Generated front-proxy CA certificate and key.
[certificates] Generated front-proxy client certificate and key.
[certificates] Valid certificates and keys now exist in "/etc/kubernetes/pki"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/admin.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/kubelet.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/controller-manager.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/scheduler.conf"
[apiclient] Created API client, waiting for the control plane to become ready
[apiclient] All control plane components are healthy after 710.502147 seconds
[token] Using token: c64554.15b62d72ae89cbb3
[apiconfig] Created RBAC rules
[addons] Applied essential addon: kube-proxy
[addons] Applied essential addon: kube-dns

Your Kubernetes master has initialized successfully!

To start using your cluster, you need to run (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:
  http://kubernetes.io/docs/admin/addons/

You can now join any number of machines by running the following on each node
as root:

  kubeadm join --token c64554.15b62d72ae89cbb3 192.168.217.135:6443

提示:在log看到这句输出的时候,docker就开始pull对应的镜像,比较耗时,耗时取决于你的网速
[apiclient] Created API client, waiting for the control plane to become ready

pull完镜像后,可以看到

root@c-pc:~# docker images
REPOSITORY                                               TAG                 IMAGE ID            CREATED             SIZE
gcr.io/google_containers/kube-proxy-amd64                v1.6.6              b01133efa4f0        2 weeks ago         109 MB
gcr.io/google_containers/kube-controller-manager-amd64   v1.6.6              a1b8bfeff845        2 weeks ago         132.9 MB
gcr.io/google_containers/kube-apiserver-amd64            v1.6.6              eaedf2552acd        2 weeks ago         150.7 MB
gcr.io/google_containers/kube-scheduler-amd64            v1.6.6              b662c4a72c62        2 weeks ago         76.75 MB
gcr.io/google_containers/etcd-amd64                      3.0.17              243830dae7dd        4 months ago        168.9 MB
gcr.io/google_containers/pause-amd64                     3.0                 99e59f495ffa        14 months ago       746.9 kB


kubectl查看node和pod的状态

根据log提示,需要配置kubectl的配置,

初始化kubectl配置

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

需要先执行以上操作,否则会遇到以下错误提示

root@c-pc:~# kubectl get nodes
The connection to the server localhost:8080 was refused - did you specify the right host or port?

最新的kubectl链接api server不是通过8080端口连接的,而是通过6443端口,可以在admin.conf查看

...
server: https://192.168.217.135:6443
...

初始化kubectl配置之后

root@c-pc:~# kubectl get nodes
The connection to the server localhost:8080 was refused - did you specify the right host or port?
root@c-pc:~# mkdir -p $HOME/.kube
root@c-pc:~# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
root@c-pc:~# chown $(id -u):$(id -g) $HOME/.kube/config
root@c-pc:~# kubectl get nodes
NAME      STATUS     AGE       VERSION
c-pc      NotReady   3h        v1.7.0

居然是NotReady状态!!
查看以下pods的状态,发现kube-dns 还在挂起,怪不得docker images的时候没有发现kube-dns镜像

root@c-pc:~# kubectl get pods --all-namespaces
NAMESPACE     NAME                           READY     STATUS    RESTARTS   AGE
kube-system   etcd-c-pc                      1/1       Running   0          3h
kube-system   kube-apiserver-c-pc            1/1       Running   0          3h
kube-system   kube-controller-manager-c-pc   1/1       Running   0          3h
kube-system   kube-dns-2838158301-v0srq      0/3       Pending   0          3h
kube-system   kube-proxy-ql2g0               1/1       Running   0          3h
kube-system   kube-scheduler-c-pc            1/1       Running   0          3h

按照官网教程,需要配置pod的网络,这里使用了flannel网络,依次执行

kubectl create -f https://github.com/coreos/flannel/raw/master/Documentation/kube-flannel-rbac.yml

kubectl create -f  https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

查看以下pods的状态

root@c-pc:~# kubectl get pods --all-namespaces
NAMESPACE     NAME                           READY     STATUS              RESTARTS   AGE
kube-system   etcd-c-pc                      1/1       Running             0          3h
kube-system   kube-apiserver-c-pc            1/1       Running             0          3h
kube-system   kube-controller-manager-c-pc   1/1       Running             0          3h
kube-system   kube-dns-2838158301-v0srq      0/3       Pending             0          3h
kube-system   kube-flannel-ds-vp7mt          0/2       ContainerCreating   0          12s
kube-system   kube-proxy-ql2g0               1/1       Running             0          3h
kube-system   kube-scheduler-c-pc            1/1       Running             0          3h

这个时候开始创建pull flannel的image了,稍等一下,

root@c-pc:~# kubectl get nodes
NAME      STATUS    AGE       VERSION
c-pc      Ready     3h        v1.7.0
root@c-pc:~# kubectl get pods --all-namespaces
NAMESPACE     NAME                           READY     STATUS    RESTARTS   AGE
kube-system   etcd-c-pc                      1/1       Running   0          3h
kube-system   kube-apiserver-c-pc            1/1       Running   0          3h
kube-system   kube-controller-manager-c-pc   1/1       Running   0          3h
kube-system   kube-dns-2838158301-v0srq      3/3       Running   0          3h
kube-system   kube-flannel-ds-vp7mt          2/2       Running   0          6m
kube-system   kube-proxy-ql2g0               1/1       Running   0          3h
kube-system   kube-scheduler-c-pc            1/1       Running   0          3h

node的状态变成ready,good!

在另外一台机器上
kubeadm join –token c64554.15b62d72ae89cbb3 192.168.217.135:6443
在master主机上可以看到

root@c-pc:~# kubectl get nodes
NAME      STATUS    AGE       VERSION
c-pc      Ready     4h        v1.7.0
c-pc2     Ready     27s       v1.7.0

你可能感兴趣的:(DevOps)