課堂資料
安裝 Kubernetes
所有機器安裝步驟
環境下加裝常用工具
yum install wget net-tools nano -y
為叢集設置主機名稱以便 nodes 間的相互溝通,XXX 分別對應自己的角色 Hostname
hostnamectl set-hostname XXX
禁用 Selinux 與 swap,
kubernetes 基於性能理由建議關閉 swap、更改 fstab 檔案註解 swap 分區以避免重開機後再次作用
sed -i 's@SELINUX=enforcing@SELINUX=disabled@' /etc/sysconfig/selinux
註解 swap 後須重新開機並使用free
檢查swap是否關閉
ptables相關功能或模組的啟用與停用,後續kubernetes會接手管理這部分的網路功能
systemctl disable firewalld && systemctl stop firewalld
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.conf
modprobe br_netfilter
echo "br_netfilter" > /etc/modules-load.d/br_netfilter.conf
lsmod | grep br_netfilter
添加 docker-ce 與 kubernetes 的 yum 源、並重整 yum 倉庫
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
vim /etc/yum.repos.d/kubernetes.repo
進入編輯後輸入以下文檔
[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
yum clean all && yum repolist
安裝 docker-ce 與 kubernetes、指定套件的安裝版本並設置為開機啟動
yum install docker-ce-18.09.8 --nogpgcheck
systemctl enable docker && systemctl start docker
yum install kubelet-1.15.2 kubectl-1.15.2 kubeadm-1.15.2 --nogpgcheck --disableexcludes=kubernetes
systemctl enable kubelet.service
Master機器初始化:以vm1 為Master
初始化 master,api-server-advertise 指定為 master 的內部 IP,pod-network-cidr 與 service-cidr 都採預設值,也可以變更為其他
kubeadm init --apiserver-advertise-address=[ master IP ] --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --kubernetes-version=v1.15.2 --cri-socket="/var/run/dockershim.sock"
紅字的前三行是在說明作為 master 角色還需手動輸入提示的指令,才能順利運作這個叢集,而最下方的kubeadm 範例則是提示 worker nodes 欲加入叢集所需輸入的指令
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
安裝通用的 flannel容器網路介面CNI(Container Network Interface)元件
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.8.129:6443 --token e09bz5.lwdqen1ezp4wif1a \
--discovery-token-ca-cert-hash sha256:982152b77b1cd7e7104d8cc15527020238c9654832c34014540c4f581cab34d0
將結果複製去 vm2、vm3
kubectl create deployment httpd-app --image=httpd
kubectl expose deployment httpd-app --type=NodePort --name=httpd-service --port=80
檢查是否連接:IP 位址:[ httpd-service's port(s) ]
kubectl describe node [ master ]
kubectl get deployment
kubectl scale deployment httpd-app --replicas 2
kubectl get deployment
kubectl create deployment web1 --image=httpd
kubectl get deployment
kubectl describe deployment web1
kubectl create deployment web2 --image=http1d
kubectl get deployment
kubectl describe deployment web1
kubectl describe pod web2-d9b9cf459-flh2r
kubectl delete deployment web2
kubectl get deployment
kubectl get deployment
kubectl get deployment web1 -o yaml
kubectl get deployment web1 -o yaml > myweb.yml
kubectl apply -f myweb.yml
kubectl get deployment
kubectl get pod -o wide