本篇內容主要講解“怎么安裝Kubernetes”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么安裝Kubernetes”吧!
成都創(chuàng)新互聯(lián)主營龍湖網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,成都App定制開發(fā),龍湖h5小程序設計搭建,龍湖網(wǎng)站營銷推廣歡迎龍湖等地區(qū)企業(yè)咨詢
安裝過程基本包括下載軟件,下載鏡像,主機配置,啟動 Master 節(jié)點,配置網(wǎng)絡,啟動 Node 節(jié)點。
這個步驟有翻墻能力的人可以直接按照步驟來,沒有的話可以使用我打包好的1.6.2軟件包。
首先在自己的翻墻的主機配置K8S源。
cat </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 EOF
配置好后,下載軟件包
yum install -y -downloadonly kubelet kubeadm kubectl kubernetes-cni
將下載好的所有 RPM 打包,發(fā)回到本地。這樣就完成了K8S軟件包的下載了。
下載好的 RPM:
https://pan.baidu.com/s/1clIpjC cp6h
下載鏡像可以直接使用我提供的腳本,前提是要能翻墻,你懂的。我自己也已經下載過一份了。提供給大家使用。
#!/usr/bin/env bash images=( kube-proxy-amd64:v1.6.2 kube-controller-manager-amd64:v1.6.2 kube-apiserver-amd64:v1.6.2 kube-scheduler-amd64:v1.6.2 kubernetes-dashboard-amd64:v1.6.0 k8s-DNS-sidecar-amd64:1.14.1 k8s-dns-kube-dns-amd64:1.14.1 k8s-dns-dnsmasq-nanny-amd64:1.14.1 etcd-amd64:3.0.17 pause-amd64:3.0 ) for imageName in ${images[@]} ; do docker pull gcr.io/google_containers/$imageName docker tag gcr.io/google_containers/$imageName registry.cn-beijing.aliyuncs.com/bbt_k8s/$imageName docker push registry.cn-beijing.aliyuncs.com/bbt_k8s/$imageName done quay.io/coreos/flannel:v0.7.0-amd64 docker tag quay.io/coreos/flannel:v0.7.0-amd64 registry.cn-beijing.aliyuncs.com/bbt_k8s/flannel:v0.7.0-amd64 docker push registry.cn-beijing.aliyuncs.com/bbt_k8s/flannel:v0.7.0-amd64
關于這個腳本我解釋一下。這個腳本是下載常用的鏡像,然后回傳到國內的源上,可以將registry.cn-beijing.aliyuncs.com/bbt_k8s改成你自己的地址,必須先用docker login 進行登錄,否則可能會出現(xiàn)權限認證錯誤。推薦使用阿里云,網(wǎng)易的服務。如果使用自己的地址,請注意下面的配置,根據(jù)自己的情況進行修改,不在累贅了。
鏡像版本號說明:
軟件 | 版本 | 說明 | 備注 |
---|---|---|---|
kube-proxy-amd64 kube-controller-manager-amd64 kube-apiserver-amd64 kube-scheduler-amd64 | v1.6.2 | 這幾個鏡像一般跟著 K8S 的版本走,例如我安裝的是 K8S 的1.6.2,那么版本號就是 v1.6.2 | |
kubernetes-dashboard-amd64 | v1.6.0 | 這個是 K8S 的控制臺(雖然并不好用,單但是最起碼很適合新手),一般跟著 K8S 的大版本好走,例如我安裝 K8S 的1.6.2,大版本是1.6,所以版本號是 v1.6.0 | |
k8s-dns-sidecar-amd64 k8s-dns-kube-dns-amd64 k8s-dns-dnsmasq-nanny-amd64 | 1.14.1 | 這個是 DNS 服務,一般不跟隨 K8S 進行升級,具體版本可以參考https://kubernetes.io/docs/getting-started-guides/kubeadm/ | |
etcd-amd64 | 3.0.17 | 這個是 etcd 服務,一般不跟隨 K8S 進行升級,具體版本可以參考https://kubernetes.io/docs/getting-started-guides/kubeadm/ | |
pause-amd64 | 3.0 | 一般不跟隨 K8S 進行升級,具體版本可以參考https://kubernetes.io/docs/getting-started-guides/kubeadm/ | 已經很長時間版本都是3.0了。 |
flannel | v0.7.0-amd64 | 網(wǎng)絡組件,我這里使用的flannel,當然也可以使用其它的。具體版本信息參考對應的網(wǎng)絡組件,例如flannel是https://github.com/coreos/flannel/tree/master/Documentation |
OK,這些鏡像下載完成就 OK 了,沒有翻墻工具的,就直接跳過吧。
上面的內容下載好后,我們就可以安裝了。
沒什么可以介紹的。
yum update -y
K8S 的1.6.x 版本僅僅在 Docker 1.12上測試過,雖然最新版本 Docker 也可以運行,但是不推薦安裝最新版本,免得遇到什么問題。
curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh /dev/stdin 1.12.6
安裝完成后,禁用 Docker 的更新,禁用方式為,在/etc/yum.conf添加
exclude=docker-engine*
主要是配置一些加速器,避免自己下載鏡像速度太慢。
修改/etc/docker/daemon.json 添加如下內容:
{ "registry-mirrors": ["https://自己的加速地址"] }
之后就是啟動Docker 的服務,
systemctl daemon-reload systemctl enable docker systemctl start docker
主要是開啟橋接相關支持,這個是 flannel 需要的配置,具體是否需要,看自己的網(wǎng)絡組件選擇的是什么。
修改/usr/lib/sysctl.d/00-system.conf,將net.bridge.bridge-nf-call-iptables改成1.之后修改當前內核狀態(tài)
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
上傳RPM 包,到自己的服務器上,然后執(zhí)行
yum install -y *.rpm
之后開啟kubelet的開機啟動
systemctl enable kubelet
然后配置 kubelet,修改/etc/systemd/system/kubelet.service.d/10-kubeadm.conf成如下文件
[Service] Environment="KUBELET_KUBECONFIG_ARGS=--kubeconfig=/etc/kubernetes/kubelet.conf --require-kubeconfig=true" Environment="KUBELET_SYSTEM_PODS_ARGS=--pod-manifest-path=/etc/kubernetes/manifests --allow-privileged=true" Environment="KUBELET_NETWORK_ARGS=--network-plugin=cni --cni-conf-dir=/etc/cni/net.d --cni-bin-dir=/opt/cni/bin" Environment="KUBELET_DNS_ARGS=--cluster-dns=10.96.0.10 --cluster-domain=cluster.local" Environment="KUBELET_AUTHZ_ARGS=--authorization-mode=Webhook --client-ca-file=/etc/kubernetes/pki/ca.crt" Environment="KUBELET_ALIYUN_ARGS=--pod-infra-container-image=registry-vpc.cn-beijing.aliyuncs.com/bbt_k8s/pause-amd64:3.0" ExecStart= ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_SYSTEM_PODS_ARGS $KUBELET_NETWORK_ARGS $KUBELET_DNS_ARGS $KUBELET_AUTHZ_ARGS $KUBELET_EXTRA_ARGS $KUBELET_ALIYUN_ARGS
在這里主要修正2個問題,一個是將 POD 的基礎容器修改為我們自己源里面的,另外一個是最新版本的 K8S 的資源管理和 Docker 默認的資源管理方式有沖突,這里把這塊給刪除了。具體可以參考https://github.com/kubernetes/release/issues/306
然后重新 reload 服務。
systemctl daemon-reload
這樣就完成了主機環(huán)境的初始化,如果是使用虛擬機,拷貝3份就可以了。如果是實體機,3臺都按照這個步驟來一遍就好了。然后為每一臺主機根據(jù)類型設置好 HostName,K8S會把 HostName 當做主機標識。
配置完主機后,我們就可以啟動我們的 Master 節(jié)點了,通常 Master 節(jié)點推薦2-3個,本地測試我們就簡單一些,一個節(jié)點就可以了。
export KUBE_REPO_PREFIX="registry-vpc.cn-beijing.aliyuncs.com/bbt_k8s" export KUBE_ETCD_IMAGE="registry-vpc.cn-beijing.aliyuncs.com/bbt_k8s/etcd-amd64:3.0.17" kubeadm init --kubernetes-version=v1.6.2 --pod-network-cidr=10.96.0.0/12
前面2個環(huán)境變量配置,是讓 kubeadm 初始化的時候,使用我們的鏡像源下載鏡像。
最后 kubeadm init 是初始化 Master 節(jié)點。其中需要配置的參數(shù)我說明一下。
參數(shù) | 意義 | 備注 |
---|---|---|
--kubernetes-version | K8S 的版本號,根據(jù)自己下載的鏡像和 RPM 版本選擇。 | 我這里使用的1.6.2,所以版本為v1.6.2. |
--pod-network-cidr | POD 的網(wǎng)絡,只要不和主機網(wǎng)絡沖突就可以,我這里使用的是10.96.0.0/12 | 這個和上面/etc/systemd/system/kubelet.service.d/10-kubeadm.conf里面聲明的KUBELET_DNS_ARGS掛鉤,請一同修改。 |
執(zhí)行完畢后,稍等一陣,就完成了。
kubeadm init --kubernetes-version=v1.6.2 --pod-network-cidr=10.96.0.0/12 [kubeadm] WARNING: kubeadm is in beta, please do not use it for production clusters. [init] Using Kubernetes version: v1.6.2 [init] Using Authorization mode: RBAC [preflight] Running pre-flight checks [preflight] Starting the kubelet service [certificates] Generated CA certificate and key. [certificates] Generated API server certificate and key. [certificates] API Server serving cert is signed for DNS names [node0 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.61.41] [certificates] Generated API server kubelet client certificate and key. [certificates] Generated service account token signing key and public key. [certificates] Generated front-proxy CA certificate and key. [certificates] Generated front-proxy 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" [apiclient] Created API client, waiting for the control plane to become ready [apiclient] All control plane components are healthy after 14.583864 seconds [apiclient] Waiting for at least one node to register [apiclient] First node has registered after 6.008990 seconds [token] Using token: e7986d.e440de5882342711 [apiconfig] Created RBAC rules [addons] Created essential addon: kube-proxy [addons] Created essential addon: kube-dns Your Kubernetes master has initialized successfully! To start using your cluster, you need to run (as a regular user): sudo cp /etc/kubernetes/admin.conf $HOME/ sudo chown $(id -u):$(id -g) $HOME/admin.conf export KUBECONFIG=$HOME/admin.conf You should now deploy a pod network to the cluster. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: http://kubernetes.io/docs/admin/addons/ You can now join any number of machines by running the following on each node as root: kubeadm join --token 1111.1111111111111 *.*.*.*:6443
安裝完成后,有一個內容非常重要,查看安裝日志,拷貝類似于下面的語句,這條語句用來初始化之后的節(jié)點。
kubeadm join --token 11111.11111111111111 *.*.*.*:6443
接下來我們就可以去看看我們 K8S 的狀態(tài)。我這使用的是 Mac。 Linux 和 Windows 的同學自行處理。
安裝 kubectl
brew install kubectl
然后拷貝 Master 節(jié)點上的/etc/kubernetes/admin.conf文件到本機的~/.kube/config
之后執(zhí)行kebectl get node。我這里已經安裝完畢了,所以有全部信息,只要能看到節(jié)點,就算是成功了。
接下來我們安裝網(wǎng)絡組件,我這里使用的是flannel。創(chuàng)建2個文件
kube-flannel-rbac.yml
# Create the clusterrole and clusterrolebinding: # $ kubectl create -f kube-flannel-rbac.yml # Create the pod using the same namespace used by the flannel serviceaccount: # $ kubectl create --namespace kube-system -f kube-flannel.yml --- kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: flannel rules: - apiGroups: - "" resources: - pods verbs: - get - apiGroups: - "" resources: - nodes verbs: - list - watch - apiGroups: - "" resources: - nodes/status verbs: - patch --- kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: flannel roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: flannel subjects: - kind: ServiceAccount name: flannel namespace: kube-system
kube-flannel-ds.yaml
--- apiVersion: v1 kind: ServiceAccount metadata: name: flannel namespace: kube-system --- kind: ConfigMap apiVersion: v1 metadata: name: kube-flannel-cfg namespace: kube-system labels: tier: node app: flannel data: cni-conf.json: | { "name": "cbr0", "type": "flannel", "delegate": { "isDefaultGateway": true } } net-conf.json: | { "Network": "10.96.0.0/12", "Backend": { "Type": "vxlan" } } --- apiVersion: extensions/v1beta1 kind: DaemonSet metadata: name: kube-flannel-ds namespace: kube-system labels: tier: node app: flannel spec: template: metadata: labels: tier: node app: flannel spec: hostNetwork: true nodeSelector: beta.kubernetes.io/arch: amd64 tolerations: - key: node-role.kubernetes.io/master operator: Exists effect: NoSchedule serviceAccountName: flannel containers: - name: kube-flannel image: registry.cn-beijing.aliyuncs.com/bbt_k8s/flannel:v0.7.0-amd64 command: [ "/opt/bin/flanneld", "--ip-masq", "--kube-subnet-mgr" ] securityContext: privileged: true env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace volumeMounts: - name: run mountPath: /run - name: flannel-cfg mountPath: /etc/kube-flannel/ - name: install-cni image: registry.cn-beijing.aliyuncs.com/bbt_k8s/flannel:v0.7.0-amd64 command: [ "/bin/sh", "-c", "set -e -x; cp -f /etc/kube-flannel/cni-conf.json /etc/cni/net.d/10-flannel.conf; while true; do sleep 3600; done" ] volumeMounts: - name: cni mountPath: /etc/cni/net.d - name: flannel-cfg mountPath: /etc/kube-flannel/ volumes: - name: run hostPath: path: /run - name: cni hostPath: path: /etc/cni/net.d - name: flannel-cfg configMap: name: kube-flannel-cfg
然后使用使用命令進行配置。
kubectl create -f kube-flannel-rbac.yml kubectl create -f kube-flannel-ds.yaml
分別在2個Node 節(jié)點,執(zhí)行下面的命令。
export KUBE_REPO_PREFIX="registry-vpc.cn-beijing.aliyuncs.com/bbt_k8s" export KUBE_ETCD_IMAGE="registry-vpc.cn-beijing.aliyuncs.com/bbt_k8s/etcd-amd64:3.0.17" kubeadm join --token 1111.111111111111 *.*.*.*:6443
其中 kubeadm join 請參考啟動 Master 節(jié)點中的內容。
理論上我們安裝到這里,K8S 就已經可以使用了。接下來主要是K8S的 Dashboard 的安裝,僅供參考,不一定要安裝。
創(chuàng)建文件kubernetes-dashboard.yaml
# Copyright 2015 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Configuration to deploy release version of the Dashboard UI compatible with # Kubernetes 1.6 (RBAC enabled). # # Example usage: kubectl create -fapiVersion: v1 kind: ServiceAccount metadata: labels: app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system --- apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRoleBinding metadata: name: kubernetes-dashboard labels: app: kubernetes-dashboard roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount name: kubernetes-dashboard namespace: kube-system --- kind: Deployment apiVersion: extensions/v1beta1 metadata: labels: app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system spec: replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: app: kubernetes-dashboard template: metadata: labels: app: kubernetes-dashboard spec: containers: - name: kubernetes-dashboard image: registry.cn-beijing.aliyuncs.com/bbt_k8s/kubernetes-dashboard-amd64:v1.6.0 imagePullPolicy: Always ports: - containerPort: 9090 protocol: TCP args: # Uncomment the following line to manually specify Kubernetes API server Host # If not specified, Dashboard will attempt to auto discover the API server and connect # to it. Uncomment only if the default does not work. # - --apiserver-host=http://my-address:port livenessProbe: httpGet: path: / port: 9090 initialDelaySeconds: 30 timeoutSeconds: 30 serviceAccountName: kubernetes-dashboard # Comment the following tolerations if Dashboard must not be deployed on master tolerations: - key: node-role.kubernetes.io/master effect: NoSchedule --- kind: Service apiVersion: v1 metadata: labels: app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system spec: type: NodePort ports: - port: 80 targetPort: 9090 selector: app: kubernetes-dashboard
創(chuàng)建文件dashboard-rbac.yaml
kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: dashboard-admin roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount name: default namespace: kube-system
之后執(zhí)行
kubectl create -f dashboard-rbac.yml kubectl create -f kubernetes-dashboard.yaml
之后用下面的命令,獲取到對應端口號。主要是看 NodePort:
kubectl describe --namespace kube-system service kubernetes-dashboard
到此,這份安裝教程就到這里結束了。最后奉上一份安裝后的截圖。
到此,相信大家對“怎么安裝Kubernetes”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!