34 【kubernetes】安裝手冊

全文參考了兩篇中文文檔:html

1,https://www.cnblogs.com/RainingNight/p/using-kubeadm-to-create-a-cluster.htmlnode

2,http://running.iteye.com/blog/2322634nginx

 

注意:web

運行命令是必定要區分是在master節點仍是在pods節點上運行的,有些命令只能在master節點執行,有些命令只能在pods節點執行。這個要區分。docker

運行命令必定要區分清用戶是誰,是root仍是普通用戶。bootstrap

 

大步驟:ubuntu

1,在master節點和pods節點上安裝軟件;api

2,在master節點上啓動kubernetes軟件,並初始化master節點;網絡

3,在從節點上啓動kubernetes軟件,並鏈接到master節點進行註冊;app

4,經過master啓動一個pods,執行一個應用程序(nginx爲例);

5,經過master啓動一個服務,將剛纔的應用程序關聯到這個服務項上;

6,測試master的scale能力,根據服務名瞬間啓動一個相同的pods;

 

1,在master節點和pods節點上安裝軟件

sudo apt-get update && sudo apt-get install -y apt-transport-https
curl -s http://packages.faasx.com/google/apt/doc/apt-key.gpg | sudo apt-key add -
sudo cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://mirrors.ustc.edu.cn/kubernetes/apt/ kubernetes-xenial main
EOF
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl

 

2,在master節點上啓動kubernetes軟件,並初始化master節點;

2.1,在master節點初始化一個cluster

因爲網絡緣由,咱們須要提早拉取k8s初始化須要用到的Images,並添加對應的k8s.gcr.io標籤:

## 拉取鏡像
docker pull reg.qiniu.com/k8s/kube-apiserver-amd64:v1.10.2
docker pull reg.qiniu.com/k8s/kube-controller-manager-amd64:v1.10.2
docker pull reg.qiniu.com/k8s/kube-scheduler-amd64:v1.10.2
docker pull reg.qiniu.com/k8s/kube-proxy-amd64:v1.10.2
docker pull reg.qiniu.com/k8s/etcd-amd64:3.1.12
docker pull reg.qiniu.com/k8s/pause-amd64:3.1

## 添加Tag
docker tag reg.qiniu.com/k8s/kube-apiserver-amd64:v1.10.2 k8s.gcr.io/kube-apiserver-amd64:v1.10.2
docker tag reg.qiniu.com/k8s/kube-scheduler-amd64:v1.10.2 k8s.gcr.io/kube-scheduler-amd64:v1.10.2
docker tag reg.qiniu.com/k8s/kube-controller-manager-amd64:v1.10.2 k8s.gcr.io/kube-controller-manager-amd64:v1.10.2
docker tag reg.qiniu.com/k8s/kube-proxy-amd64:v1.10.2 k8s.gcr.io/kube-proxy-amd64:v1.10.2
docker tag reg.qiniu.com/k8s/etcd-amd64:3.1.12 k8s.gcr.io/etcd-amd64:3.1.12
docker tag reg.qiniu.com/k8s/pause-amd64:3.1 k8s.gcr.io/pause-amd64:3.1

## 在Kubernetes 1.10 中,增長了CoreDNS,若是使用CoreDNS(默認關閉),則不須要下面三個鏡像。
docker pull reg.qiniu.com/k8s/k8s-dns-sidecar-amd64:1.14.10
docker pull reg.qiniu.com/k8s/k8s-dns-kube-dns-amd64:1.14.10
docker pull reg.qiniu.com/k8s/k8s-dns-dnsmasq-nanny-amd64:1.14.10

docker tag reg.qiniu.com/k8s/k8s-dns-sidecar-amd64:1.14.10 k8s.gcr.io/k8s-dns-sidecar-amd64:1.14.10
docker tag reg.qiniu.com/k8s/k8s-dns-kube-dns-amd64:1.14.10 k8s.gcr.io/k8s-dns-kube-dns-amd64:1.14.10
docker tag reg.qiniu.com/k8s/k8s-dns-dnsmasq-nanny-amd64:1.14.10 k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64:1.14.10

2.2,初始化cluster

<master-node>: sudo kubeadm init --pod-network-cidr=192.168.0.0/16
注意這裏的輸出最好能記錄在text中,由於後面會用到

2.3,將kubernetes的配置項放到普通用戶目錄下

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

這樣kubectl會自動尋址到config文件,不用依賴一個嚴格的root可讀的config

2.4,安裝各類軟件(https://docs.projectcalico.org/v3.3/getting-started/kubernetes/)

2.4.1 安裝etcd
kubectl apply -f \
https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/etcd.yaml

2.4.2 安裝rbac
kubectl apply -f \
https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/rbac.yaml

2.4.3 安裝calico
kubectl apply -f \
https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/calico.yaml

2.4.4 確認安裝成功
watch kubectl get pods --all-namespaces
結束後Ctrl + C

2.4.5 再次確認
kubectl get nodes -o wide

2.5 在從節點啓動kubernetes軟件

在從節點上執行:
sudo kubeadm join 192.168.0.8:6443 --token vtyk9m.g4afak37myq3rsdi --discovery-token-ca-cert-hash sha256:19246ce11ba3fc633fe0b21f2f8aaaebd7df9103ae47138dc0dd615f61a32d99 
這裏的命令要和2.2的輸出保持一致(幾個參數可能不一致,按照本身的輸出自行修改便可)

若是2.2的輸出已經無法找到,能夠用如下命令再次獲得join語句。
在主節點上執行:
kubeadm token create --print-join-command

而後再在從節點上執行以上獲得的join語句

 

2.6,確認主從節點已經完成啓動,須要等幾分鐘:
主節點上執行
kubectl get nodes

 


3,建立可用的pod
3.1,建立一個nginx的鏡像當作pod內的應用程序
主節點上執行:
kubectl run my-nginx --image=nginx --replicas=1 --port=80

 

3.2,確認pod已經生成
主節點上執行:

kubectl get pods

 

3.3,將該pods發佈到kubernetes上,做爲一個服務
luwenwei@localhost:~/download/k8s$ kubectl expose deployment my-nginx --port=8080 --target-port=80 
service/my-nginx exposed

kubectl expose deployment <run-deploy-name> --port=<port> --target-port=<target-port> --name=<service-name>
把已經發布的應用<run-deploy-name>暴露成一種服務,原始端口爲<port>,對外服務的端口爲<target-port>,暴露出來的service名稱爲<service-name>

 3.4,查看服務是否已經生成

luwenwei@localhost:~$ kubectl get services
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1     <none>        443/TCP    108m
my-nginx     ClusterIP   10.98.38.80   <none>        8080/TCP   91m

 

3.5,訪問該服務
luwenwei@localhost:~$ kubectl describe service/my-nginx
Name:              my-nginx
Namespace:         default
Labels:            run=my-nginx
Annotations:       <none>
Selector:          run=my-nginx
Type:              ClusterIP
IP:                10.98.38.80
Port:              <unset>  8080/TCP
TargetPort:        80/TCP
Endpoints:         192.168.244.65:80
Session Affinity:  None
Events:            <none>

 

獲取到IP和port
從節點上執行:
luwenwei@ubuntu:~$ curl 10.98.38.80:8080 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>

 



4,擴容該service的pods數
4.1,查看擴容前的pods數
luwenwei@localhost:~$ kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-756f645cd7-mg45n   1/1     Running   0          98m

 

4.2,執行擴容

luwenwei@localhost:~$ kubectl scale deployment my-nginx --replicas=2
deployment.extensions/my-nginx scaled

 

 

4.3,查看擴容後的pods信息

luwenwei@localhost:~$ kubectl get pods
NAME                        READY   STATUS              RESTARTS   AGE
my-nginx-756f645cd7-dww7g   0/1     ContainerCreating   0          6s
my-nginx-756f645cd7-mg45n   1/1     Running             0          98m

 

 

4.4,查看service的信息

luwenwei@localhost:~$ kubectl describe service/my-nginx
Name:              my-nginx
Namespace:         default
Labels:            run=my-nginx
Annotations:       <none>
Selector:          run=my-nginx
Type:              ClusterIP
IP:                10.98.38.80
Port:              <unset>  8080/TCP
TargetPort:        80/TCP
Endpoints:         192.168.244.65:80,192.168.244.66:80
Session Affinity:  None
Events:            <none>

 

4.4,反向驗證pods和service的對應

luwenwei@localhost:~$ kubectl describe pods | grep IP
IP:                 192.168.244.66
IP:                 192.168.244.65

 

4.5,訪問新的service

luwenwei@ubuntu:~$ curl 10.98.38.80:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

 

 

5,使用完畢後清理cluster信息

5.1,清除node數據

主節點上執行
kubectl drain <node name> --delete-local-data --force --ignore-daemonsets

 

5.2,刪除node節點

主節點上執行
kubectl delete node <node name>

 

5.3,收回cluster信息

在要移除的節點上,執行:
sudo kubeadm reset

 

輸出結果:
luwenwei@ubuntu:~$ sudo kubeadm reset
[sudo] password for luwenwei: 
[reset] WARNING: changes made to this host by 'kubeadm init' or 'kubeadm join' will be reverted.
[reset] are you sure you want to proceed? [y/N]: y
[preflight] running pre-flight checks
[reset] stopping the kubelet service
[reset] unmounting mounted directories in "/var/lib/kubelet"
[reset] no etcd manifest found in "/etc/kubernetes/manifests/etcd.yaml". Assuming external etcd
[reset] please manually reset etcd to prevent further issues
[reset] deleting contents of stateful directories: [/var/lib/kubelet /etc/cni/net.d /var/lib/dockershim /var/run/kubernetes]
[reset] deleting contents of config directories: [/etc/kubernetes/manifests /etc/kubernetes/pki]
[reset] deleting files: [/etc/kubernetes/admin.conf /etc/kubernetes/kubelet.conf /etc/kubernetes/bootstrap-kubelet.conf /etc/kubernetes/controller-manager.conf /etc/kubernetes/scheduler.conf
相關文章
相關標籤/搜索