官网链接为 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. 安装淘宝镜像cnpm时出现问题及解决方案

    问题: 解决方案: 安装完成:

  2. RabbitMQ路由模式

    生产者 import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import utils.Connecti ...

  3. Git——常用场景解析

    总结:本篇文章从初识GitHub.Git,实践GitHub的五种常用场景,分别是:git for windows安装,git配置,克隆远程代码到本地,上传本地代码到远程以及Git的常用指令.相信Jam ...

  4. 报错Error configuring application listener of class org.springframework.web.context.ContextConfigLocation

    错误内容是ClassNotFoundException: org.springframework.web.context.ContextConfigLocationdao导致一运行项目就是404 是因 ...

  5. 一个label两种颜色,一个label两种字体

    -(void)addLabel{ UILabel *label = [[UILabel alloc]init]; label.backgroundColor = [UIColor grayColor] ...

  6. 微信小程序-添加手机联系人

    仅供参考 1,wxml: <view bindlongtap="phoneNumTap">{{phoneNum}}</view> 2,js: data = ...

  7. C. Playing Piano 动态规划

    题目意思是给你一个n长度的数字串为a,让你构造一个n长度的数字串b值都为1-5满足以下条件: 正常的dfs暴力构造会超时,我试过了.. 可以开一个二维数组dp[i][j]用来表示b的第i个数字为j是否 ...

  8. [原创]RedisDesktopManager工具使用介绍

    [原创]RedisDesktopManager工具使用介绍 1 RedisDesktopManager简介 一款能够跨平台使用的开源性redis可视化工具.redis desktop manager主 ...

  9. linux > 和 >> 、< 区别

    linux中经常会用到将内容输出到某文件当中,只需要在执行命令后面加上>或者>>号即可进入操作. 大于号:将一条命令执行结果(标准输出,或者错误输出,本来都要打印到屏幕上面的)重定向 ...

  10. Lua MD5加密字符串

    function md5_sumhexa(k) local md5_core = require "md5.core" k = md5_core.sum(k) return (st ...