官网链接为 https://kubernetes.io/docs/tutorials/kubernetes-basics/

基础操作环境为minikube

常见基础命令

查看基础的一些信息

# 查看版本
(oracle) ➜ cdss_db git:(dameng) kubectl version
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.0", GitCommit:"fc32d2f3698e36b93322a3465f63a14e9f0eaead", GitTreeState:"clean", BuildDate:"2018-03-26T16:55:54Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.0", GitCommit:"fc32d2f3698e36b93322a3465f63a14e9f0eaead", GitTreeState:"clean", BuildDate:"2018-03-26T16:44:10Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"} # 查看节点
(oracle) ➜ cdss_db git:(dameng) kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready master 23h v1.10.0

PODS/DEPLOYMENTS

创建一个pod(由于网络问题,需要提前将image导入到minikube的机器中)

# 创建pod
(oracle) ➜ cdss_db git:(dameng) kubectl run kubernetes-bootcamp --image=gcr.io/hello-minikube-zero-install/hello-node:latest --port=8080 --image-pull-policy=Never
deployment.apps "kubernetes-bootcamp" created # 查看pod
(oracle) ➜ cdss_db git:(dameng) kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-85459f8c6c-9jgfl 1/1 Running 0 5s # 查看deployment
(oracle) ➜ cdss_db git:(dameng) kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1 1 1 1 19m # 将服务暴露到外网(相对于k8s的内部网)环境
(oracle) ➜ cdss_db git:(dameng) kubectl proxy
Starting to serve on 127.0.0.1:8001 # 测试----查看相关的接口
➜ minikube curl 127.0.0.1:8001/version
{
"major": "1",
"minor": "10",
"gitVersion": "v1.10.0",
"gitCommit": "fc32d2f3698e36b93322a3465f63a14e9f0eaead",
"gitTreeState": "clean",
"buildDate": "2018-03-26T16:44:10Z",
"goVersion": "go1.9.3",
"compiler": "gc",
"platform": "linux/amd64"
}% # 测试----测试对应接口
➜ minikube curl 127.0.0.1:8001/api/v1/namespaces/default/pods/kubernetes-bootcamp-85459f8c6c-9jgfl/proxy/
Hello World!% # 中止proxy
ctrl+c 即可停止

查看pod的详细信息

# 查看pod信息
➜ ~ kubectl describe pods
Name: kubernetes-bootcamp-85459f8c6c-9jgfl
Namespace: default
Node: minikube/10.0.2.15
Start Time: Tue, 19 Mar 2019 14:04:18 +0800
Labels: pod-template-hash=4101594727
run=kubernetes-bootcamp
Annotations: <none>
Status: Running
IP: 172.17.0.5
Controlled By: ReplicaSet/kubernetes-bootcamp-85459f8c6c
Containers:
kubernetes-bootcamp:
Container ID: docker://9ad04cae7f054805acfe9af452210bca960c44b673c2e837e9c39cf70f40a717
Image: gcr.io/hello-minikube-zero-install/hello-node:latest
Image ID: docker://sha256:40da0f31e97619946d2a9ca662cb9ccef3f9d45508cd3de58a93542ac2108121
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 19 Mar 2019 14:04:20 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-c9lkr (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-c9lkr:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-c9lkr
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 26m default-scheduler Successfully assigned kubernetes-bootcamp-85459f8c6c-9jgfl to minikube
Normal SuccessfulMountVolume 26m kubelet, minikube MountVolume.SetUp succeeded for volume "default-token-c9lkr"
Normal Pulled 26m kubelet, minikube Container image "gcr.io/hello-minikube-zero-install/hello-node:latest" already present on machine
Normal Created 26m kubelet, minikube Created container
Normal Started 26m kubelet, minikube Started container # 查看pod日志
kubectl logs kubernetes-bootcamp-85459f8c6c-9jgfl # 查看pod环境变量
➜ ~ kubectl exec kubernetes-bootcamp-85459f8c6c-9jgfl env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=kubernetes-bootcamp-85459f8c6c-9jgfl
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
NODE_VERSION=6.14.2
YARN_VERSION=1.6.0
HOME=/root # 进入pod的shell环境
➜ ~ kubectl exec -it kubernetes-bootcamp-85459f8c6c-9jgfl bash
root@kubernetes-bootcamp-85459f8c6c-9jgfl:/# ls
bin dev home lib64 mnt proc run server.js sys usr
boot etc lib media opt root sbin srv tmp var

SERVICES

为pod/deployment创建service

# 创建service
➜ minikube kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
service "kubernetes-bootcamp" exposed # 查看deployment
➜ minikube kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1 1 1 1 41m # 查看service
➜ minikube kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h
kubernetes-bootcamp NodePort 10.111.228.62 <none> 8080:32383/TCP 1m # 查看service相关信息
➜ minikube kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: run=kubernetes-bootcamp
Annotations: <none>
Selector: run=kubernetes-bootcamp
Type: NodePort
IP: 10.111.228.62
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 32383/TCP
Endpoints: 172.17.0.5:8080
Session Affinity: None
External Traffic Policy: Cluster
Events: <none> # 观察到NodePort,为Node相关服务暴露的接口,配合node的ip,可以直接访问
➜ minikube curl 192.168.99.101:32383
Hello World!%

labels的使用

# 使用describe可以查看到对应的labels
➜ minikube kubectl describe deployment
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Tue, 19 Mar 2019 14:04:17 +0800
Labels: run=kubernetes-bootcamp
Annotations: deployment.kubernetes.io/revision=1
Selector: run=kubernetes-bootcamp
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Pod Template:
Labels: run=kubernetes-bootcamp
Containers:
kubernetes-bootcamp:
Image: gcr.io/hello-minikube-zero-install/hello-node:latest
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: kubernetes-bootcamp-85459f8c6c (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 44m deployment-controller Scaled up replica set kubernetes-bootcamp-85459f8c6c to 1 # 上面labels为run=kubernetes-bootcamp
# 查看labels对应的pod/service
➜ minikube kubectl get pods -l run=kubernetes-bootcamp
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-85459f8c6c-9jgfl 1/1 Running 0 47m
➜ minikube kubectl get services -l run=kubernetes-bootcamp
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-bootcamp NodePort 10.111.228.62 <none> 8080:32383/TCP 7m # 为pod打新label,使用方法类似于docker的tag命令
➜ minikube kubectl label pod kubernetes-bootcamp-85459f8c6c-9jgfl app=v1
pod "kubernetes-bootcamp-85459f8c6c-9jgfl" labeled
➜ minikube kubectl describe pods kubernetes-bootcamp-85459f8c6c-9jgfl
Name: kubernetes-bootcamp-85459f8c6c-9jgfl
Namespace: default
Node: minikube/10.0.2.15
Start Time: Tue, 19 Mar 2019 14:04:18 +0800
Labels: app=v1
pod-template-hash=4101594727
run=kubernetes-bootcamp # 查看label对应的pods
➜ minikube kubectl get pods -l app=v1
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-85459f8c6c-9jgfl 1/1 Running 0 50m

删除service

# 删除service,可以使用label来删除
➜ ~ kubectl delete service -l run=kubernetes-bootcamp
service "kubernetes-bootcamp" deleted # 查看service,可以看到被删除
➜ ~ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h # 服务已经不可用,但pod依然存在
➜ ~ curl 192.168.99.101:32383
curl: (7) Failed to connect to 192.168.99.101 port 32383: Connection refused ➜ ~ kubectl exec -it kubernetes-bootcamp-85459f8c6c-9jgfl bash
root@kubernetes-bootcamp-85459f8c6c-9jgfl:/# ➜ ~ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-85459f8c6c-9jgfl 1/1 Running 0 54m

动态调整

增加/调整分片

# 查看分片
➜ ~ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1 1 1 1 1h # 增加分片
➜ ~ kubectl scale deployments/kubernetes-bootcamp --replicas=3
deployment.extensions "kubernetes-bootcamp" scaled # 查看效果
➜ ~ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 3 3 3 3 1h # 查看各个pod的信息
➜ ~ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
kubernetes-bootcamp-85459f8c6c-4x8fd 1/1 Running 0 1m 172.17.0.6 minikube
kubernetes-bootcamp-85459f8c6c-9jgfl 1/1 Running 0 1h 172.17.0.5 minikube
kubernetes-bootcamp-85459f8c6c-rlzbp 1/1 Running 0 1m 172.17.0.7 minikube # 使用describe命令可以查看其变动(包含event)
➜ ~ kubectl describe deployments/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Tue, 19 Mar 2019 14:04:17 +0800
Labels: run=kubernetes-bootcamp
Annotations: deployment.kubernetes.io/revision=1
Selector: run=kubernetes-bootcamp
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Pod Template:
Labels: run=kubernetes-bootcamp
Containers:
kubernetes-bootcamp:
Image: gcr.io/hello-minikube-zero-install/hello-node:latest
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: kubernetes-bootcamp-85459f8c6c (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 2m deployment-controller Scaled up replica set kubernetes-bootcamp-85459f8c6c to 3 # 暴露services,随后查看详细信息
➜ ~ kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
service "kubernetes-bootcamp" exposed ➜ ~ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 1d
kubernetes-bootcamp NodePort 10.97.170.139 <none> 8080:30424/TCP 6s ➜ ~ kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: run=kubernetes-bootcamp
Annotations: <none>
Selector: run=kubernetes-bootcamp
Type: NodePort
IP: 10.97.170.139
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30424/TCP
Endpoints: 172.17.0.5:8080,172.17.0.6:8080,172.17.0.7:8080
Session Affinity: None
External Traffic Policy: Cluster
Events: <none> # 验证服务
➜ ~ curl 192.168.99.101:30424
Hello World!% # 可以重新调整分片
➜ ~ kubectl scale deployments/kubernetes-bootcamp --replicas=2
deployment.extensions "kubernetes-bootcamp" scaled ➜ ~ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 2 2 2 2 1h # 可以查看到变化
➜ ~ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
kubernetes-bootcamp-85459f8c6c-4x8fd 1/1 Running 0 7m 172.17.0.6 minikube
kubernetes-bootcamp-85459f8c6c-9jgfl 1/1 Running 0 1h 172.17.0.5 minikube
kubernetes-bootcamp-85459f8c6c-rlzbp 1/1 Terminating 0 7m 172.17.0.7 minikube ➜ ~ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-85459f8c6c-4x8fd 1/1 Running 0 7m
kubernetes-bootcamp-85459f8c6c-9jgfl 1/1 Running 0 1h
kubernetes-bootcamp-85459f8c6c-rlzbp 1/1 Terminating 0 7m

更新服务

# 更新为新的镜像(首先需要导入新的镜像,新镜像返回的内容从hello world变更为hello)
➜ minikube kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=node_hello:2
deployment.apps "kubernetes-bootcamp" image updated # 查看pods,可以看到过程
➜ minikube kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-85459f8c6c-4x8fd 1/1 Terminating 1 1h
kubernetes-bootcamp-b4d449bd-vlnpd 1/1 Running 0 17m # services没有发生变化
➜ minikube kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 1d
kubernetes-bootcamp NodePort 10.97.170.139 <none> 8080:30424/TCP 1h # describe可以看到镜像已经发生变化
➜ minikube kubectl describe pod kubernetes-bootcamp-b4d449bd-vlnpd
Name: kubernetes-bootcamp-b4d449bd-vlnpd
Namespace: default
Node: minikube/10.0.2.15
Start Time: Tue, 19 Mar 2019 16:07:19 +0800
Labels: pod-template-hash=60800568
run=kubernetes-bootcamp
Annotations: <none>
Status: Running
IP: 172.17.0.5
Controlled By: ReplicaSet/kubernetes-bootcamp-b4d449bd
Containers:
kubernetes-bootcamp:
Container ID: docker://fb59d3c98a1ca705640520954198d5bb336167a8fe1e40603e9281e714a997f4
Image: node_hello:2
Image ID: docker://sha256:74a687422f4cb90ac252a35b6e0bef78d5f4a546fa64b63afc672b763ce5e6b2
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 19 Mar 2019 16:07:20 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-c9lkr (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-c9lkr:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-c9lkr
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 17m default-scheduler Successfully assigned kubernetes-bootcamp-b4d449bd-vlnpd to minikube
Normal SuccessfulMountVolume 17m kubelet, minikube MountVolume.SetUp succeeded for volume "default-token-c9lkr"
Normal Pulled 17m kubelet, minikube Container image "node_hello:2" already present on machine
Normal Created 17m kubelet, minikube Created container
Normal Started 17m kubelet, minikube Started container

从上面可以看到服务已经更新,可以使用下面的方式验证一下

# 通过describe查看service的nodeport
➜ ~ kubectl describe service kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: run=kubernetes-bootcamp
Annotations: <none>
Selector: run=kubernetes-bootcamp
Type: NodePort
IP: 10.97.170.139
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30424/TCP
Endpoints: 172.17.0.5:8080
Session Affinity: None
External Traffic Policy: Cluster
Events: <none> # 返回内容已经变更为hello(之前为hello world)
➜ minikube curl 192.168.99.101:30424
Hello% # 也可以通过下面命令来验证更新结果
➜ ~ kubectl rollout status deployments/kubernetes-bootcamp
deployment "kubernetes-bootcamp" successfully rolled out

k8s官网 基础知识入门教程的更多相关文章

  1. Linux基础知识入门

    [Linux基础]Linux基础知识入门及常见命令.   前言:最近刚安装了Linux系统, 所以学了一些最基本的操作, 在这里把自己总结的笔记记录在这里. 1,V8:192.168.40.10V1: ...

  2. Hibernate入门1. Hibernate基础知识入门

    Hibernate入门1. Hibernate基础知识入门 20131127 前言: 之前学习过Spring框架的知识,但是不要以为自己就可以说掌握了Spring框架了.这样一个庞大的Spring架构 ...

  3. HTML基础知识自学教程

    HTML 是用来描述网页的一套标记标签,是我们在web前端开发中的基础.下面PHP程序员雷雪松主要结合自己的经验给大家分享下HTML的基础知识,以及在自学过程中一些比较常用的和重要的HTML知识点. ...

  4. redis最基础的入门教程

      Redis最基础入门教程 简介 Redis 简介 Redis 优势 Redis与其他key-value存储有什么不同? 字符串(Strings) 哈希(Hash) 列表(List) 集合(Sets ...

  5. vultr vps官网改版免费注册教程(最新优惠码)

    本站强烈推荐的,国外超高性价比vps服务器Vultr官方网站最近更新了,导致一些朋友心生茫然,今天写个简易教程给大家. 首先是打开vultr官网,免费注册一个vultr账号.填写邮箱地址.密码,非常简 ...

  6. 1 python大数据挖掘系列之基础知识入门

    preface Python在大数据行业非常火爆近两年,as a pythonic,所以也得涉足下大数据分析,下面就聊聊它们. Python数据分析与挖掘技术概述 所谓数据分析,即对已知的数据进行分析 ...

  7. Git教程(1)官网及官方中文教程

    1,Git官网 http://www.git-scm.com/ 2,官方中文教程 http://git-scm.com/book/zh/v2

  8. git官网和安装使用教程链接

    git官网 https://git-scm.com/download/win git安装教程 https://www.cnblogs.com/wj-1314/p/7993819.html

  9. Oracle 基础知识入门

    前记: 近来项目用到Oracle数据库,大学学了点,后面基本忘记得差不多了,虽然基本语法跟sql 差不多,但是oracle知识是非常多的. 这里简单说点基础知识,希望后面补上更多的关于ORacle知识 ...

随机推荐

  1. 利用Webpack+React(antd)+ES6+python(flask)实现代码转换

    之前的几篇博客是将flask 结合 antd本地化,但是这样使得antd无法按需加载(也不支持ES6的语法),而且在写的过程中还需要把每个组件都用antd对象,这样的做法虽然是实现了antd的本地化, ...

  2. Redis自学笔记:1.简介

    博主教材:李子骅.人民邮电出版社.<redis入门指南> 博主操作系统系统:虚拟机Ubuntu16.04 博主redis版本:3.0.6 第1章:简介 redis是一个开源的.高性能的.基 ...

  3. 2018-7-17-随笔-params和ref、out用法、事件访问器

    **************************************************************************************************** ...

  4. WebStorm中使用npm包管理器

    1.首先安装Node.js,安装之后添加NodeJs的安装路径到系统Path环境变量,在控制台之中测试node,与npm命令是否工作正常. 2.配置npm的源为国内的淘宝镜像,这里不推荐使用cnpm ...

  5. Java 关键字 static

    关键字static作用如下: 1. 为某个基本数据类型或对象分配单一的存储空间. 2. 实现某个属性或方法与类关联.在类被加载后类名可以直接调用静态成员方法(下面简称静态方法)或者访问静态成员变量(下 ...

  6. angular-环境搭建

    angular环境搭建(angular-cli) 1.安装nodejs http://nodejs.cn/download/ 2.Angular命令行工具安装 npm install -g @angu ...

  7. line-height 设置为 1

    https://stackoverflow.com/questions/1000398/what-is-line-height1   If no unit is supplied e.g. " ...

  8. 《软件性能测试与LoadRunner实战教程》喜马拉雅有声图书上线

    工作忙的同学们有福了,可以听书了. 于涌老师的<软件性能测试与LoadRunner实战教程>喜马拉雅有声图书上线.

  9. JS数字指定长度不足前补零的实现

    问题描述:         要求输出的数字长度是固定的,如长度为2,数字为1,则输出01,即不够位数就在之前补足0. 解决方法: 方法1 function fn1(num, length) { ret ...

  10. jQuery Distpicker插件 省市区三级联动 动态赋值修改地址

    在获取创建页面数据后需要在编辑页面调取之前提交的数据,在使用这个插件后发现无法动态赋值,查找资料后发现需要先销毁实例,$(’#target’).distpicker(‘destroy’); 第一步 引 ...