Ubuntu18安装部署Kubernetes并完成Nginx-deployment的安装、升级、扩容、删除操作demo

【采用docker方式进行】

【1】在虚拟机安装kuberctl

1.下载最新版:curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
	要下载最新版,请用特定版本替换$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)命令部分。
2.使下载的这个kubectl获得二进制可执行权限:chmod +x ./kubectl
3. 将二进制文件移到PATH中:sudo mv ./kubectl /usr/local/bin/kubectl
4.测试以确保您安装的是最新版:kubectl version --client
	我的显示:Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.6", GitCommit:"dff82dc0de47299ab66c83c626e08b245ab19037", GitTreeState:"clean", BuildDate:"2020-07-15T16:58:53Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}

【2】查看OS类型是否为linux,可以使用docker命令:docker info --format ‘{{.OSType}}’ 可以看到是linux

【3】在网上下载minikube:

https://github.com/kubernetes/minikube/releases/download/v1.10.1/minikube_latest_amd64.deb

然后在这个文件目录下执行:sudo dpkg -i minikube_latest_amd64.deb
	1.要是它警告说:Warning: /**/bin was not found in your $PATH.。。。意味着你需要将这个路径加入到你的$PATH中去
		可以使用echo $PATH查看一下,没有就使用export PATH=$PATH:/**/bin 加入$PATH

【4】提前下载镜像:docker pull anjone/kicbase

【5】启动

sudo minikube start --registry-mirror=https://bmtb46e4.mirror.aliyuncs.com --vm-driver=docker --base-image=“anjone/kicbase” --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers

	1.如果出现Sorry, Kubernetes v1.18.0 requires conntrack to be installed in root's path
		请执行:sudo apt install conntrack然后再启动一下即可

执行终端日志输出:
root@deployment:/home/ljo/kubernetes# minikube start --registry-mirror=https://bmtb46e4.mirror.aliyuncs.com --driver=none --base-image=“anjone/kicbase” --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers

😄  minikube v1.10.1 on Ubuntu 18.04
✨  Using the none driver based on existing profile
👍  Starting control plane node minikube in cluster minikube
🏃  Updating the running none "minikube" bare metal machine ...
ℹ️  OS release is Ubuntu 18.04.3 LTS
🐳  Preparing Kubernetes v1.18.2 on Docker 19.03.5 ...
    ▪ kubelet.resolv-conf=/run/systemd/resolve/resolv.conf
🤹  Configuring local host environment ...

❗  The 'none' driver is designed for experts who need to integrate with an existing VM
💡  Most users should use the newer 'docker' driver instead, which does not require root!
📘  For more information, see: https://minikube.sigs.k8s.io/docs/reference/drivers/none/

❗  kubectl and minikube configuration will be stored in /root
❗  To use kubectl or minikube commands as your own user, you may need to relocate them. For example, to overwrite your own settings, run:

    ▪ sudo mv /root/.kube /root/.minikube $HOME
    ▪ sudo chown -R $USER $HOME/.kube $HOME/.minikube

💡  This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
🌟  Enabled addons: default-storageclass, storage-provisioner
🏄  Done! kubectl is now configured to use "minikube"
root@deployment:/home/ljo/kubernetes#
【这中间可能会等一段时间,它在后台下载东西,不要以为是卡住了】

【6】查看状态:minikube status

root@deployment:/home/ljo# minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
【没有启动的话就是下面这个】
root@deployment:/home/ljo# minikube status
minikube
type: Control Plane
host: Stopped
kubelet: Stopped
apiserver: Stopped
kubeconfig: Stopped

【7】查看当前配置 : kubectl config current-context

显示:minikube

【8】查看集群状态:kubectl cluster-info

root@deployment:/home/ljo# kubectl cluster-info
Kubernetes master is running at https://10.112.26.131:8443
KubeDNS is running at https://10.112.26.131:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

【9】查看节点:kubectl get no

root@deployment:/home/ljo# kubectl get no
NAME         STATUS   ROLES    AGE     VERSION
deployment   Ready    master   8m55s   v1.18.2

【10】打开dashboard:minikube dashboard

我这里总是启动这个失败,有点烦。
root@deployment:/home/ljo# minikube dashboard
🔌 Enabling dashboard …
🤔 Verifying dashboard health …
🚀 Launching proxy …
🤔 Verifying proxy health …
💣 http://127.0.0.1:37527/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ is not accessible: Temporary Error: unexpected response code: 503

通过该命令查看:kubectl get pods --all-namespaces -o wide,发现我的kubernetes-dashboard-xxx有点不正常。

root@deployment:/home/ljo# kubectl get pods --all-namespaces -o wide
NAMESPACE              NAME                                         READY   STATUS             RESTARTS   AGE   IP              NODE         NOMINATED NODE   READINESS GATES
kube-system            coredns-546565776c-gwzht                     0/1     Running            0          22m   172.17.0.3      deployment   <none>           <none>
kube-system            coredns-546565776c-nstdz                     0/1     Running            0          22m   172.17.0.2      deployment   <none>           <none>
kube-system            etcd-deployment                              1/1     Running            0          23m   10.112.26.131   deployment   <none>           <none>
kube-system            kube-apiserver-deployment                    1/1     Running            0          23m   10.112.26.131   deployment   <none>           <none>
kube-system            kube-controller-manager-deployment           1/1     Running            0          23m   10.112.26.131   deployment   <none>           <none>
kube-system            kube-proxy-zbvtr                             1/1     Running            0          22m   10.112.26.131   deployment   <none>           <none>
kube-system            kube-scheduler-deployment                    1/1     Running            0          23m   10.112.26.131   deployment   <none>           <none>
kube-system            storage-provisioner                          1/1     Running            0          22m   10.112.26.131   deployment   <none>           <none>
kubernetes-dashboard   dashboard-metrics-scraper-84bfdf55ff-4rzb6   1/1     Running            0          13m   172.17.0.4      deployment   <none>           <none>
kubernetes-dashboard   kubernetes-dashboard-696dbcc666-94q2p        0/1     CrashLoopBackOff   6          13m   172.17.0.5      deployment   <none>           <none>
root@deployment:/home/ljo# kubectl get services --all-namespaces
NAMESPACE              NAME                        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
default                kubernetes                  ClusterIP   10.96.0.1       <none>        443/TCP                  25m
kube-system            kube-dns                    ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP,9153/TCP   25m
kubernetes-dashboard   dashboard-metrics-scraper   ClusterIP   10.100.36.203   <none>        8000/TCP                 15m
kubernetes-dashboard   kubernetes-dashboard        ClusterIP   10.111.135.14   <none>        80/TCP                   15m

不管这个了,看下一个,直接玩起来。

如果要在本地访问dashboard,我们需要创建一个安全通道,可运行如下命令
root@deployment:/home/ljo/kubernetes# kubectl proxy
Starting to serve on 127.0.0.1:8001

【11】提交一个 nginx deployment;

kubectl apply -f https://k8s.io/examples/application/deployment.yaml

root@deployment:/home/ljo# kubectl apply  -f  https://k8s.io/examples/application/deployment.yaml
deployment.apps/nginx-deployment created
root@deployment:/home/ljo# kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   1/2     2            1           90s
root@deployment:/home/ljo# kubectl describe deployment nging-deployment
Error from server (NotFound): deployments.apps "nging-deployment" not found
root@deployment:/home/ljo# kubectl describe deployment nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Wed, 29 Jul 2020 05:57:46 +0000
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=nginx
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.14.2
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-6b474476c4 (2/2 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  2m55s  deployment-controller  Scaled up replica set nginx-deployment-6b474476c4 to 2

【12】升级 nginx deployment;

kubectl apply -f https://k8s.io/examples/application/deployment-update.yaml

root@deployment:/home/ljo# kubectl apply -f  https://k8s.io/examples/application/deployment-update.yaml
deployment.apps/nginx-deployment configured
root@deployment:/home/ljo# kubectl describe deployment nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Wed, 29 Jul 2020 05:57:46 +0000
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 2
Selector:               app=nginx
Replicas:               2 desired | 1 updated | 3 total | 2 available | 1 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.16.1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    ReplicaSetUpdated
OldReplicaSets:  nginx-deployment-6b474476c4 (2/2 replicas created)
NewReplicaSet:   nginx-deployment-7b45d69949 (1/1 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  4m9s  deployment-controller  Scaled up replica set nginx-deployment-6b474476c4 to 2
  Normal  ScalingReplicaSet  13s   deployment-controller  Scaled up replica set nginx-deployment-7b45d69949 to 1

【13】扩容 nginx deployment。

kubectl apply -f https://k8s.io/examples/application/deployment-scale.yaml

root@deployment:/home/ljo# kubectl apply -f  https://k8s.io/examples/application/deployment-scale.yaml
deployment.apps/nginx-deployment configured
root@deployment:/home/ljo# kubectl describe deployment nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Wed, 29 Jul 2020 05:57:46 +0000
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 3
Selector:               app=nginx
Replicas:               4 desired | 2 updated | 5 total | 2 available | 3 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.14.2
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      False   MinimumReplicasUnavailable
  Progressing    True    ReplicaSetUpdated
OldReplicaSets:  nginx-deployment-7b45d69949 (3/3 replicas created)
NewReplicaSet:   nginx-deployment-6b474476c4 (2/2 replicas created)
Events:
  Type    Reason             Age                  From                   Message
  ----    ------             ----                 ----                   -------
  Normal  ScalingReplicaSet  2m30s                deployment-controller  Scaled up replica set nginx-deployment-7b45d69949 to 1
  Normal  ScalingReplicaSet  41s                  deployment-controller  Scaled down replica set nginx-deployment-6b474476c4 to 1
  Normal  ScalingReplicaSet  41s                  deployment-controller  Scaled up replica set nginx-deployment-7b45d69949 to 2
  Normal  ScalingReplicaSet  38s                  deployment-controller  Scaled down replica set nginx-deployment-6b474476c4 to 0
  Normal  ScalingReplicaSet  12s                  deployment-controller  Scaled up replica set nginx-deployment-7b45d69949 to 4
  Normal  ScalingReplicaSet  11s (x2 over 6m26s)  deployment-controller  Scaled up replica set nginx-deployment-6b474476c4 to 2
  Normal  ScalingReplicaSet  11s                  deployment-controller  Scaled up replica set nginx-deployment-6b474476c4 to 1
  Normal  ScalingReplicaSet  11s                  deployment-controller  Scaled down replica set nginx-deployment-7b45d69949 to 3

【14】删除deployment

root@deployment:/home/ljo# kubectl delete deployment nginx-deployment
deployment.apps "nginx-deployment" deleted
root@deployment:/home/ljo# kubectl get deployment
No resources found in default namespace.
root@deployment:/home/ljo#

【15】停止运行的minikube(minikube)

root@deployment:/home/ljo# minikube stop
✋  Stopping "minikube" in none ...
🛑  Node "minikube" stopped.

【16】删除本地的minikube集群

root@deployment:/home/ljo# minikube delete

在这里插入图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐