最近Kubernetes v1.16.3 剛剛發(fā)布,由于公司要上線業(yè)務(wù)图云,所以就先在線下自己搭環(huán)境測(cè)試惯悠。由于之前的數(shù)據(jù)中心在國(guó)外,所以安裝Kubernetes去gcr.io琼稻,k8s.gcr.io拉取鏡像的時(shí)候沒有問題吮螺。 但是現(xiàn)在的公司VM在阿里云上,眾所周知帕翻,由于一些不可抗力國(guó)內(nèi)無法直接拉取google上的任何鏡像,這就包括gcr.io萝风。
之前用了一些做法嘀掸,就是先找一臺(tái)可以科學(xué)上網(wǎng)的機(jī)器,把鏡像先拉下來规惰,然后重新打tag睬塌。或者利用shell腳本自動(dòng)pull歇万,tag揩晴,然后push到private registry,然后安裝的時(shí)候指定自己的私庫贪磺。
但是由于版本更迭硫兰,有時(shí)候tag就變了,需要改腳本重新測(cè)試寒锚,所以感覺一直不完美劫映。
后來發(fā)現(xiàn)國(guó)內(nèi)可以直接用azure.cn的鏡像加速,直接拉取k8s需要的所有鏡像刹前。之前用過阿里云的鏡像加速站點(diǎn)泳赋,也是可以的。Azure.cn基本和google同步喇喉,速度可能更快一些祖今,個(gè)人喜歡用azure.cn的鏡像。下面就是Azure鏡像的常用站點(diǎn):
docker hub 鏡像:
docker pull dockerhub.azk8s.cn/xxx/yyy:tag
gcr.io 鏡像:
docker pull gcr.azk8s.cn/xxx/yyy:tag
k8s.gcr.io 鏡像:
對(duì)于kubernetes相關(guān)的鏡像,我們會(huì)使用到k8s.gcr.io開頭的鏡像千诬。
k8s.gcr.io等價(jià)于gcr.io/google-containers撒踪,因此同上也可以使用中科大鏡像或者Azure中國(guó)鏡像。
docker pull gcr.azk8s.cn/google-containers/xxx:yyy
quay.io鏡像:
docker pull quay.azk8s.cn/xxx/yyy:zzz
下面開始Kubernetes v1.16.3的正式安裝:
系統(tǒng)要求:
? ? One or more machines running one of:
? ? ? ? Ubuntu 16.04+
? ? ? ? Debian 9+
? ? ? ? CentOS 7
? ? ? ? Red Hat Enterprise Linux (RHEL) 7
? ? ? ? Fedora 25+
? ? ? ? HypriotOS v1.0.1+
? ? ? ? Container Linux (tested with 1800.6.0)
? ? 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. See here for more details.
? ? Certain ports are open on your machines. See here for more details.
? ? Swap disabled. You MUST disable swap in order for the kubelet to work properly.
安裝環(huán)境:
VMwareWorkstation or VirtualBOX:
1臺(tái)master:4 core 8GB
2臺(tái)worker:4 core 8GB
自動(dòng)化工具:
ansible
Kubernetes安裝工具:
kubeadm
利用kubeadm安裝集群
首先大渤,準(zhǔn)備好3臺(tái)VM
master IP:192.168.199.200
worker1:192.168.199.201
worker2:192.168.199.202
然后做一些初始化工作:
CentOS/RHEL:
? ? 關(guān)閉swap分區(qū)
? ? 關(guān)閉selinux
? ? 關(guān)閉NetworkManager
? ? 關(guān)閉firewalld
? ? 添加從master到worker節(jié)點(diǎn)的免密鑰登錄
? ? 由于沒有DNS服務(wù)器制妄,需要配置主機(jī)上的/etc/hosts,把master和worker節(jié)點(diǎn)對(duì)應(yīng)添加進(jìn)去
下面正式開始:
1 登錄到master節(jié)點(diǎn)泵三,安裝ansible
? ? #CentOS/RHEL:
? ? yum install epel-release
? ? yum install -y ansible python2-pip.noarch
? ? pip install --upgrade ansible
2 編寫ansible inventory文件
vim ~/inventory
? ? [all]
? ? k8s-master node_ip=192.168.199.200
? ? node1 node_ip=192.168.199.201
? ? node2 node_ip=192.168.199.202
3 編寫playbook (下面的playbook是以Centos7為例耕捞,如果是Ubuntu需要把包管理器從yum改為apt,rpm_key改為apt_key)
vim ~/host-prepare.yml
? ? ---
? ? - hosts: all
? ? ? become: true
? ? ? tasks:
? ? ? - name: Disable NetworkManager
? ? ? ? service:
? ? ? ? ? name: NetworkManager
? ? ? ? ? state: stopped
? ? ? ? ? enabled: false
? ? ? - name: Disable firewalld
? ? ? ? service:
? ? ? ? ? name: firewalld
? ? ? ? ? state: stopped
? ? ? ? ? enabled: false
? ? ? - name: Install docker and its dependecies
? ? ? ? yum:
? ? ? ? ? name: docker
? ? ? ? ? state: latest
? ? ? ? ? update_cache: yes
? ? ? - name: Start docker
? ? ? ? service:
? ? ? ? ? name: docker
? ? ? ? ? state: started
? ? ? ? ? enabled: yes
? ? ? - name: Remove swapfile from /etc/fstab
? ? ? ? mount:
? ? ? ? ? name: "{{ item }}"
? ? ? ? ? fstype: swap
? ? ? ? ? state: absent
? ? ? ? with_items:
? ? ? ? ? - swap
? ? ? ? ? - none
? ? ? - name: Disable swap
? ? ? ? command: swapoff -a
? ? ? ? when: ansible_swaptotal_mb > 0
? ? ? - name: Disable Selinux
? ? ? ? selinux:
? ? ? ? ? state: disabled
? ? ? - name: Add kubernetes repository for stable version
? ? ? ? yum_repository:
? ? ? ? ? description: kubernetes
? ? ? ? ? name: kubernetes
? ? ? ? ? baseurl: https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
? ? ? ? ? enabled: yes
? ? ? ? ? gpgcheck: yes
? ? ? ? ? repo_gpgcheck: yes
? ? ? ? ? gpgkey: https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
? ? ? ? ? state: present
? ? ? - name: Add an yum signing key for Kubernetes
? ? ? ? rpm_key:
? ? ? ? ? key: https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
? ? ? ? ? state: present
? ? ? - name: Add an rpm signing key for Kubernetes
? ? ? ? rpm_key:
? ? ? ? ? key: https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
? ? ? ? ? state: present
? ? ? - name: Install Kubernetes binaries
? ? ? ? package:
? ? ? ? ? name: "{{ packages }}"
? ? ? ? ? state: present
? ? ? ? ? use: yum
? ? ? ? vars:
? ? ? ? ? packages:
? ? ? ? ? ? - kubelet
? ? ? ? ? ? - kubeadm
? ? ? ? ? ? - kubectl
? ? ? - name: Configure node ip
? ? ? ? lineinfile:
? ? ? ? ? path: /etc/sysconfig/kubelet
? ? ? ? ? line: KUBELET_EXTRA_ARGS=--node-ip={{ node_ip }}
? ? ? - name: Restart kubelet
? ? ? ? service:
? ? ? ? ? name: kubelet
? ? ? ? ? daemon_reload: yes
? ? ? ? ? state: restarted
? ? ? ? ? enabled: true
4 運(yùn)行playbook
首先在master上生成ssh密鑰:
ssh-keygen
ssh-copy-id root@k8s-master
ssh-copy-id root@node1
ssh-copy-id root@node2
sed -i s/#host_key_checking/host_key_checking/ /etc/ansible/ansible.cfg
ansible-playbook -i ~/inventory ~/host-prepare.yml
5 在master節(jié)點(diǎn)運(yùn)行kubeadm安裝kubernetes:
這里標(biāo)紅的參數(shù),就是我指定image倉庫的地址烫幕,默認(rèn)為gcr.io俺抽,這里我改為Azure.cn的地址
kubeadm init --node-name k8s-master --apiserver-advertise-address <your master node IP> --pod-network-cidr 192.168.0.0/16 --image-repository gcr.azk8s.cn/google-containers
根據(jù)機(jī)器配置和網(wǎng)速,等待時(shí)間不等较曼,大概5分鐘磷斧,可以看到master節(jié)點(diǎn)就成功安裝了〗萦蹋看到如下提示弛饭,就可以根據(jù)最后一條命令加入worker node了。
? ? 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:
? ? ? /docs/concepts/cluster-administration/addons/
? ? You can now join any number of machines by running the following on each node
? ? as root:
? ? kubeadm join 192.168.199.200:6443 --token 95u7da.08yb7leugjunvg3s? ? --discovery-token-ca-cert-hash sha256:cc40687e79b5ea1486d3f1ea066789578758822a46d1d54516cc8d9ff28cf774
加入worker node之前萍歉,我們按照提示運(yùn)行:
? mkdir -p $HOME/.kube
? sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
? sudo chown $(id -u):$(id -g) $HOME/.kube/config
我們還需要安裝網(wǎng)絡(luò)組件侣颂,這里我選擇的是Calico,所以在master上運(yùn)行以下命令即可安裝Calico:
kubectl apply -f https://docs.projectcalico.org/v3.8/manifests/calico.yaml
然后利用kubectl查看集群健康狀態(tài)(如果沒運(yùn)行上一步枪孩,安裝CNI的過程憔晒,這里master的STATUS將會(huì)是NotReady):
[root@k8s-master ~]# kubectl get node
NAME? ? ? ? STATUS? ? ROLES? ? AGE? ? VERSION
k8s-master? Ready? master? 3m50s? v1.16.3
這里我們看到集群master部署完成了,而且可以正常連接蔑舞。如果master狀態(tài)還是NotReady拒担,說明沒有部署Calico,重新部署Calico之后,狀態(tài)會(huì)變成Ready
[root@k8s-master ~]# kubectl get pod -A
NAMESPACE? ? NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? READY? STATUS? ? RESTARTS? AGE
kube-system? calico-kube-controllers-55754f75c-zgxgv? 1/1? ? Running? 0? ? ? ? ? 38s
kube-system? calico-node-vsk9v? ? ? ? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 38s
kube-system? coredns-58ffd68966-4lf5s? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 4m56s
kube-system? coredns-58ffd68966-5xpvf? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 4m56s
kube-system? etcd-k8s-master? ? ? ? ? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 4m5s
kube-system? kube-apiserver-k8s-master? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 4m12s
kube-system? kube-controller-manager-k8s-master? ? ? ? 1/1? ? Running? 0? ? ? ? ? 3m55s
kube-system? kube-proxy-hmkv2? ? ? ? ? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 4m56s
kube-system? kube-scheduler-k8s-master? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 3m46s
我們可以看到master節(jié)點(diǎn)已經(jīng)正常了攻询,各組件也正常啟動(dòng)从撼,沒問題。
6 最后一步蜕窿,加入worker node
我們?cè)趦膳_(tái)worker節(jié)點(diǎn)上按照提示運(yùn)行:
kubeadm join 192.168.199.200:6443 --token 95u7da.08yb7leugjunvg3s? ? --discovery-token-ca-cert-hash sha256:cc40687e79b5ea1486d3f1ea066789578758822a46d1d54516cc8d9ff28cf774
這里注意谋逻,上面命令的token和hash是你自己機(jī)器生成的,一定要用你自己的token
最終桐经,我們可以查看集群已經(jīng)裝好了:
[root@k8s-master ~]# kubectl get node
NAME? ? ? ? STATUS? ROLES? ? AGE? ? VERSION
k8s-master? Ready? ? master? 24m? ? v1.16.3
node1? ? ? ? Ready? ? <none>? 2m51s? v1.16.3
node2? ? ? ? Ready? ? <none>? 84s? ? v1.16.3
[root@k8s-master ~]# kubectl get pod -A
NAMESPACE? ? NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? READY? STATUS? ? RESTARTS? AGE
kube-system? calico-kube-controllers-55754f75c-zgxgv? 1/1? ? Running? 0? ? ? ? ? 19m
kube-system? calico-node-rn95j? ? ? ? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 2m47s
kube-system? calico-node-rsgdb? ? ? ? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 80s
kube-system? calico-node-vsk9v? ? ? ? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 19m
kube-system? coredns-58ffd68966-4lf5s? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 23m
kube-system? coredns-58ffd68966-5xpvf? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 23m
kube-system? etcd-k8s-master? ? ? ? ? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 22m
kube-system? kube-apiserver-k8s-master? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 22m
kube-system? kube-controller-manager-k8s-master? ? ? ? 1/1? ? Running? 0? ? ? ? ? 22m
kube-system? kube-proxy-44c7r? ? ? ? ? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 80s
kube-system? kube-proxy-bsjn2? ? ? ? ? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 2m47s
kube-system? kube-proxy-hmkv2? ? ? ? ? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 23m
kube-system? kube-scheduler-k8s-master? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 22m