K8S入門(二) 單機部署

dunne21發表於2021-09-09

目的

搭建一個單機環境學習實踐用,既做Master節點也做Node節點,透過實際操作深入理解k8s中的基礎概念

配置與執行Master

直接從模板機複製一臺虛擬機器作為本次單機環境的機器,參考

初始化Master

透過命令列引數

  • 宿主機可能無法訪問虛擬機器NAT地址,需要配置apiserver地址,用來從宿主機訪問apiserver

  • kubeadm預設會向伺服器查詢版本號,而查詢介面很有可能無法訪問,同時模板虛擬機器中的版本是確定的,所以也指定了版本號

# 192.168.56.102需要改成你自己的虛擬機器地址$ sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.56.102 --kubernetes-version=v1.11.1[init] using Kubernetes version: v1.11.1[preflight] running pre-flight checks
I0823 16:53:38.690945   15374 kernel_validator.go:81] Validating kernel version
I0823 16:53:38.691271   15374 kernel_validator.go:96] Validating kernel config
        [WARNING SystemVerification]: docker version is greater than the most recently validated version. Docker version: 18.06.0-ce. Max validated version: 17.03[preflight/images] Pulling images required for setting up a Kubernetes cluster
[preflight/images] This might take a minute or two, depending on the speed of your internet connection
[preflight/images] You can also perform this action in beforehand using 'kubeadm config images pull'[kubelet] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"[kubelet] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"[preflight] Activating the kubelet service
[certificates] Generated ca certificate and key.
[certificates] Generated apiserver certificate and key.
[certificates] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.56.102]
[certificates] Generated apiserver-kubelet-client certificate and key.
[certificates] Generated sa key and public key.
[certificates] Generated front-proxy-ca certificate and key.
[certificates] Generated front-proxy-client certificate and key.
[certificates] Generated etcd/ca certificate and key.
[certificates] Generated etcd/server certificate and key.
[certificates] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [127.0.0.1 ::1]
[certificates] Generated etcd/peer certificate and key.
[certificates] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.56.102 127.0.0.1 ::1]
[certificates] Generated etcd/healthcheck-client certificate and key.
[certificates] Generated apiserver-etcd-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"[controlplane] wrote Static Pod manifest for component kube-apiserver to "/etc/kubernetes/manifests/kube-apiserver.yaml"[controlplane] wrote Static Pod manifest for component kube-controller-manager to "/etc/kubernetes/manifests/kube-controller-manager.yaml"[controlplane] wrote Static Pod manifest for component kube-scheduler to "/etc/kubernetes/manifests/kube-scheduler.yaml"[etcd] Wrote Static Pod manifest for a local etcd instance to "/etc/kubernetes/manifests/etcd.yaml"[init] waiting for the kubelet to boot up the control plane as Static Pods from directory "/etc/kubernetes/manifests"[init] this might take a minute or longer if the control plane images have to be pulled
[apiclient] All control plane components are healthy after 47.013260 seconds
[uploadconfig] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.11" in namespace kube-system with the configuration for the kubelets in the cluster
[markmaster] Marking the node k8s-master as master by adding the label "node-role.kubernetes.io/master=''"[markmaster] Marking the node k8s-master as master by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "k8s-master" as an annotation
[bootstraptoken] using token: lkpkfd.ymx618h27m75aa56
[bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstraptoken] creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes master 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:
  

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

  kubeadm join 192.168.56.102:6443 --token lkpkfd.ymx618h27m75aa56 --discovery-token-ca-cert-hash sha256:ba896f324da35303f50e996c3b76f4dd6e428fc1381714e792f19f4fd38069b2

根據提示執行以下命令

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

透過配置檔案

可以在配置檔案中更改映象源,這樣就不會被牆了

參考

安裝Pod網路外掛

$ sudo sysctl net.bridge.bridge-nf-call-iptables=1$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.extensions/kube-flannel-ds created

確認節點狀態

確認Master節點已經處於Ready

$ kubectl get nodes
NAME         STATUS    ROLES     AGE       VERSION
k8s-master   Ready     master    4m        v1.11.1

Master Isolation

預設情況下,Master節點不會被安排執行Pods,需要修改預設配置

$ kubectl taint nodes --all node-role.kubernetes.io/master-
node/k8s-master untainted

安裝Dashboard

  • 部署Dashboard

$ kubectl apply -f 
serviceaccount/kubernetes-dashboard created
role.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
deployment.apps/kubernetes-dashboard created
service/kubernetes-dashboard created
  • 確認Pods狀態

$ kubectl get pods --all-namespacesNAMESPACE     NAME                                    READY     STATUS    RESTARTS   AGEkube-system   coredns-78fcdf6894-2jswr                1/1       Running   0          14mkube-system   coredns-78fcdf6894-ftbzj                1/1       Running   0          14mkube-system   etcd-k8s-master                         1/1       Running   0          14mkube-system   kube-apiserver-k8s-master               1/1       Running   0          13mkube-system   kube-controller-manager-k8s-master      1/1       Running   0          14mkube-system   kube-flannel-ds-lj5f5                   1/1       Running   0          10mkube-system   kube-proxy-gbjbl                        1/1       Running   0          14mkube-system   kube-scheduler-k8s-master               1/1       Running   0          13mkube-system   kubernetes-dashboard-6d4bc79449-g6klp   1/1       Running   0          6m
  • 配置訪問許可權

編輯dashboard-admin.yaml

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: kubernetes-dashboard
  labels:
    k8s-app: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: kubernetes-dashboard  namespace: kube-system
  • 應用配置

$ kubectl create -f dashboard-admin.yaml
clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
  • 執行代理

$ kubectl proxy --address 0.0.0.0 --accept-hosts '.*'Starting to serve on [::]:8001

服務地址如下,請將192.168.56.102改成你自己的地址
http://192.168.56.102:8001/api/v1/namespaces/kube-system/services/kubernetes-dashboard/proxy/#!/overview?namespace=default

  • 在宿主機瀏覽器中訪問dashboard

圖片描述

Dashboard預覽



作者:核子飛彈
連結:


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1600/viewspace-2820928/,如需轉載,請註明出處,否則將追究法律責任。

相關文章