國內(nèi)(墻內(nèi))部署最新版本kubernetes(k8s) v1.16.3 最簡單方式,無需手動拉取鏡像,打tag

最近Kubernetes v1.16.3 剛剛發(fā)布,由于公司要上線業(yè)務(wù),所以就先在線下自己搭環(huán)境測試。由于之前的數(shù)據(jù)中心在國外,所以安裝Kubernetes去gcr.io,k8s.gcr.io拉取鏡像的時候沒有問題。 但是現(xiàn)在的公司VM在阿里云上,眾所周知,由于一些不可抗力國內(nèi)無法直接拉取google上的任何鏡像,這就包括gcr.io。

之前用了一些做法,就是先找一臺可以科學(xué)上網(wǎng)的機器,把鏡像先拉下來,然后重新打tag?;蛘呃胹hell腳本自動pull,tag,然后push到private registry,然后安裝的時候指定自己的私庫。

但是由于版本更迭,有時候tag就變了,需要改腳本重新測試,所以感覺一直不完美。

后來發(fā)現(xiàn)國內(nèi)可以直接用azure.cn的鏡像加速,直接拉取k8s需要的所有鏡像。之前用過阿里云的鏡像加速站點,也是可以的。Azure.cn基本和google同步,速度可能更快一些,個人喜歡用azure.cn的鏡像。下面就是Azure鏡像的常用站點:

docker hub 鏡像:

docker pull dockerhub.azk8s.cn/xxx/yyy:tag

gcr.io 鏡像:

docker pull gcr.azk8s.cn/xxx/yyy:tag

k8s.gcr.io 鏡像:

對于kubernetes相關(guān)的鏡像,我們會使用到k8s.gcr.io開頭的鏡像。

k8s.gcr.io等價于gcr.io/google-containers,因此同上也可以使用中科大鏡像或者Azure中國鏡像。

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臺master:4 core 8GB

2臺worker:4 core 8GB

自動化工具:

ansible

Kubernetes安裝工具:

kubeadm

利用kubeadm安裝集群

首先,準備好3臺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é)點的免密鑰登錄

? ? 由于沒有DNS服務(wù)器,需要配置主機上的/etc/hosts,把master和worker節(jié)點對應(yīng)添加進去

下面正式開始:

1 登錄到master節(jié)點,安裝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 運行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é)點運行kubeadm安裝kubernetes:

這里標紅的參數(shù),就是我指定image倉庫的地址,默認為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ù)機器配置和網(wǎng)速,等待時間不等,大概5分鐘,可以看到master節(jié)點就成功安裝了??吹饺缦绿崾荆涂梢愿鶕?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之前,我們按照提示運行:

? 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上運行以下命令即可安裝Calico:

kubectl apply -f https://docs.projectcalico.org/v3.8/manifests/calico.yaml

然后利用kubectl查看集群健康狀態(tài)(如果沒運行上一步,安裝CNI的過程,這里master的STATUS將會是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)會變成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é)點已經(jīng)正常了,各組件也正常啟動,沒問題。

6 最后一步,加入worker node

我們在兩臺worker節(jié)點上按照提示運行:

kubeadm join 192.168.199.200:6443 --token 95u7da.08yb7leugjunvg3s? ? --discovery-token-ca-cert-hash sha256:cc40687e79b5ea1486d3f1ea066789578758822a46d1d54516cc8d9ff28cf774

這里注意,上面命令的token和hash是你自己機器生成的,一定要用你自己的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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容