Kubernetes学习之路(九)之kubernetes命令式快速创建应用
1、使用命令kubectl run创建应用
语法:
kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool]
[--overrides=inline-json] [--command] -- [COMMAND] [args...] [options]
实用举例:
[root@k8s-master ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port= --replicas= #创建一个nginx的应用,副本数为1
deployment.apps/nginx-deploy created [root@k8s-master ~]# kubectl get deployment #获取应用信息,查看应用是否符合预期状态
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-deploy 40s [root@k8s-master ~]# kubectl get pods #获取pod信息
NAME READY STATUS RESTARTS AGE
nginx-deploy-5b595999-44zwq / Running 1m [root@k8s-master ~]# kubectl get pods -o wide #查看pod运行在哪个节点上
NAME READY STATUS RESTARTS AGE IP NODE
nginx-deploy-5b595999-44zwq / Running 1m 10.244.2.2 k8s-node02
从上面创建的应用可以得知,nginx-deploy应用的pod的ip为10.244.2.2,这是一个pod ip,仅仅可以在集群内部访问,如下:
[root@k8s-master ~]# curl 10.244.2.2 -I
HTTP/1.1 OK
Server: nginx/1.14.
Date: Thu, Feb :: GMT
Content-Type: text/html
Content-Length:
Last-Modified: Fri, Jul :: GMT
Connection: keep-alive
ETag: "5b3f9e97-264"
Accept-Ranges: bytes [root@k8s-node01 ~]# curl 10.244.2.2 -I
HTTP/1.1 OK
Server: nginx/1.14.
Date: Thu, Feb :: GMT
Content-Type: text/html
Content-Length:
Last-Modified: Fri, Jul :: GMT
Connection: keep-alive
ETag: "5b3f9e97-264"
Accept-Ranges: bytes [root@k8s-node02 ~]# curl 10.244.2.2 -I
HTTP/1.1 OK
Server: nginx/1.14.
Date: Thu, Aug :: GMT
Content-Type: text/html
Content-Length:
Last-Modified: Fri, Jul :: GMT
Connection: keep-alive
ETag: "5b3f9e97-264"
Accept-Ranges: bytes
这里要注意的是pod的客户端有2类,1类是其他pod,1类是集群外部客户端,那么集群外部的客户端如何访问到pod呢?pod的地址是随时变化的,假设先删除创建的pod:
[root@k8s-master ~]# kubectl delete pods nginx-deploy-5b595999-44zwq
pod "nginx-deploy-5b595999-44zwq" deleted
要明白pod是通过控制器进行管理的,当控制器发现pod的状态不满足预期的状态时,将会重新创建一个pod
[root@k8s-master ~]# kubectl get pods -o wide #由于在node01节点上没有镜像,需要重新下载
NAME READY STATUS RESTARTS AGE IP NODE
nginx-deploy-5b595999-872c7 / ContainerCreating 24s <none> k8s-node01
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-deploy-5b595999-872c7 / Running 56s 10.244.1.2 k8s-node01
此时可以看到新建的pod的ip地址已经更改了,并且本次创建的pod是在node01节点上,这样就需要提供一个固定端点,给集群外部客户端进行访问。这个固定端点就是service:
语法如下:
kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP] [--target-port=number-or-name]
[--name=name] [--external-ip=external-ip-of-service] [--type=type] [options] [root@k8s-master ~]# kubectl expose deployment nginx-deploy --name=nginx --port= --target-port= --protocol=TCP #创建一个nginx的service
service/nginx exposed [root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> /TCP 1d
nginx ClusterIP 10.106.162.254 <none> /TCP 19s [root@k8s-master ~]# curl 10.106.162.254 -I #通过ClusterIP进行访问nginx pod
HTTP/1.1 OK
Server: nginx/1.14.
Date: Thu, Aug :: GMT
Content-Type: text/html
Content-Length:
Last-Modified: Fri, Jul :: GMT
Connection: keep-alive
ETag: "5b3f9e97-264"
Accept-Ranges: bytes
10.106.162.254这网段依然是集群内部的网段,只能被集群内部所能访问,外部是无法通过service的ip进行访问的。那么针对pod的客户端除了通过service ip访问还可以通过service的名称进行访问,但是前提是需要对service的名称能够进行解析。而解析时是依赖coredns服务的,而我们本地的dns指向并非coredns,如下:
[root@k8s-master ~]# curl nginx
curl: () Could not resolve host: nginx; Unknown error
[root@k8s-master ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 8.8.8.8
nameserver 114.114.114.114
下面查看一下coredns的ip地址:
[root@k8s-master ~]# kubectl get pods -n kube-system -o wide
NAME READY STATUS RESTARTS AGE IP NODE
coredns-78fcdf6894-nmcmz 1/1 Running 0 1d 10.244.0.3 k8s-master
coredns-78fcdf6894-p5pfm 1/1 Running 0 1d 10.244.0.2 k8s-master
etcd-k8s-master / Running 1d 192.168.56.11 k8s-master
kube-apiserver-k8s-master / Running 1d 192.168.56.11 k8s-master
kube-controller-manager-k8s-master / Running 1d 192.168.56.11 k8s-master
kube-flannel-ds-n5c86 / Running 1d 192.168.56.11 k8s-master
kube-flannel-ds-nrcw2 / Running 5h 192.168.56.13 k8s-node02
kube-flannel-ds-pgpr7 / Running 1d 192.168.56.12 k8s-node01
kube-proxy-glzth / Running 5h 192.168.56.13 k8s-node02
kube-proxy-rxlt7 / Running 1d 192.168.56.11 k8s-master
kube-proxy-vxckf / Running 1d 192.168.56.12 k8s-node01
kube-scheduler-k8s-master / Running 1d 192.168.56.11 k8s-master
而一般,也不会直接通过coredns的这个pod ip地址进行访问,而是通过service进行访问,查看一下coredns的service:
[root@k8s-master ~]# kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.10 <none> /UDP,/TCP 1d
那么就可以通过这个service ip:10.96.0.10进行解析上面的nginx服务,如下:
[root@k8s-master ~]# yum install -y bind-utils [root@k8s-master ~]# dig -t A nginx.default.svc.cluster.local @10.96.0.10 #这里需要使用完整的服务名称,否则会因为dns搜索域的问题而导致无法解析成功 ; <<>> DiG 9.9.-RedHat-9.9.-.el7 <<>> -t A nginx.default.svc.cluster.local @10.96.0.10
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id:
;; flags: qr aa rd ra; QUERY: , ANSWER: , AUTHORITY: , ADDITIONAL: ;; OPT PSEUDOSECTION:
; EDNS: version: , flags:; udp:
;; QUESTION SECTION:
;nginx.default.svc.cluster.local. IN A ;; ANSWER SECTION:
nginx.default.svc.cluster.local. 5 IN A 10.106.162.254 #这样就可以正常解析出nginx的service ip了 ;; Query time: msec
;; SERVER: 10.96.0.10#(10.96.0.10)
;; WHEN: Thu Aug :: EDT
;; MSG SIZE rcvd:
那么再演示通过pod 客户端进行访问:
[root@k8s-master ~]# kubectl run client --image=busybox --replicas= -it --restart=Never #创建pod
[root@k8s-master ~]# kubectl exec -it client /bin/sh #首次创建如果没进入到容器,可以使用这命令进入 / # cat /etc/resolv.conf #查看dns,这里就是自动指向coredns
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots: / # wget -O - -q http://nginx:80 #请求解析nginx
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>
这就是service提供给pod的固定访问端点的使用,而pod的增删改查,并不会影响通过service进行访问,可以通过以下命令来查看service的详细信息:
[root@k8s-master ~]# kubectl describe svc nginx
Name: nginx
Namespace: default
Labels: run=nginx-deploy
Annotations: <none>
Selector: run=nginx-deploy
Type: ClusterIP
IP: 10.106.162.254
Port: <unset> /TCP
TargetPort: /TCP
Endpoints: 10.244.1.5:80 #pod 的ip,会根据资源变化改变,但是实际访问的service 依旧有效
Session Affinity: None
Events: <none>
那么pod的增删改,service又是如何确定对pod的访问呢?这就需要通过标签选择器进行选定,无论pod的ip如何变化,但是标签不会变化,从而达到固定端点的访问效果,查看一下pod的标签:
[root@k8s-master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client / Running 21h run=client
nginx-deploy-5b595999-872c7 / Running 22h pod-template-hash=,run=nginx-deploy
run=nginx-deploy就是这个应用的标签,所以当pod的改变,并不会影响service的访问。
2、应用副本的动态伸缩
语法如下:
kubectl scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME) (1)创建应用myapp
[root@k8s-master ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=
deployment.apps/myapp created [root@k8s-master ~]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
myapp 15s
nginx-deploy 22h (2)查看pod详细信息
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
client / Running 21h 10.244.2.3 k8s-node02
client2 / Running 48m 10.244.1.6 k8s-node01
client3 / Running 27m 10.244.2.4 k8s-node02
myapp-848b5b879b-bdp7t / Running 26s 10.244.1.7 k8s-node01
myapp-848b5b879b-swt2c / ErrImagePull 26s 10.244.2.5 k8s-node02
nginx-deploy-5b595999-872c7 / Running 22h 10.244.1.5 k8s-node01
(3)配置service端点
[root@k8s-master ~]# kubectl expose deployment myapp --name=myapp --port=
service/myapp exposed
(4)查看服务信息
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> /TCP 2d
myapp ClusterIP 10.106.67.242 <none> /TCP 14s
nginx ClusterIP 10.106.162.254 <none> /TCP 21h
(5)Pod客户端访问
/ # wget -O - -q http://myapp:80
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
(6)副本增加到5
[root@k8s-master ~]# kubectl scale --replicas= deployment myapp
deployment.extensions/myapp scaled [root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client / Running 21h
client2 / Running 51m
client3 / Running 30m
myapp-848b5b879b-6p6ml / Running 1m
myapp-848b5b879b-7xmnj / ImagePullBackOff 1m
myapp-848b5b879b-bdp7t / Running 3m
myapp-848b5b879b-swt2c / ImagePullBackOff 3m
myapp-848b5b879b-zlvl2 / Running 1m
nginx-deploy-5b595999-872c7 / Running 22h
(7)副本收缩到3
[root@k8s-master ~]# kubectl scale --replicas= deployment myapp
deployment.extensions/myapp scaled
3、应用的版本升级
语法如下:
kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 ... CONTAINER_NAME_N=CONTAINER_IMAGE_N
(1)版本升级为v2
[root@k8s-master ~]# kubectl set image deployment myapp myapp=ikubernetes/myapp:v2
deployment.extensions/myapp image updated
(2)查看升级过程
[root@k8s-master ~]# kubectl rollout status deployment myapp #查看更新过程
Waiting for deployment "myapp" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: old replicas are pending termination...
Waiting for deployment "myapp" rollout to finish: old replicas are pending termination...
deployment "myapp" successfully rolled out (3)获取pod信息
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client / Running 21h
client2 / Running 53m
client3 / Running 33m
myapp-74c94dcb8c-2djgg / Running 1m
myapp-74c94dcb8c-92d9p / Running 28s
myapp-74c94dcb8c-nq7zt / Running 25s
nginx-deploy-5b595999-872c7 / Running 22h [root@k8s-master ~]# kubectl describe pods myapp-74c94dcb8c-2djgg
(4)pod客户端测试访问,可以看到是v2版本
/ # wget -O - -q http://myapp:80
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
4、应用的版本回滚
语法如下:
kubectl rollout undo (TYPE NAME | TYPE/NAME) [flags] [options]
[root@k8s-master ~]# kubectl rollout undo deployment myapp #不指定版本直接回滚到上一个版本
deployment.extensions/myapp / # wget -O - -q http://myapp:80
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
5、实现外部访问service
[root@k8s-master ~]# kubectl edit svc myapp
TYPE:CLUSTER-IP改为
TYPE:NodePort [root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> /TCP 2d
myapp NodePort 10.106.67.242 <none> :32432/TCP 18m
nginx ClusterIP 10.106.162.254 <none> /TCP 22h
这里再查看service信息,可以看到myapp进行了端口映射,将myapp的80端口映射到本地32432端口,则可以使用http://192.168.56.11:32432进行访问。如图:
Kubernetes学习之路(九)之kubernetes命令式快速创建应用的更多相关文章
- Kubernetes学习之路目录
Kubernetes基础篇 环境说明 版本说明 系统环境 Centos 7.2 Kubernetes版本 v1.11.2 Docker版本 v18.09 Kubernetes学习之路(一)之概念和架构 ...
- Kubernetes学习之路(十九)之Kubernetes dashboard认证访问
Dashboard:https://github.com/kubernetes/dashboard 一.Dashboard部署 由于需要用到k8s.gcr.io/kubernetes-dashboar ...
- Kubernetes学习之路(十五)之Ingress和Ingress Controller
目录 一.什么是Ingress? 1.Pod 漂移问题 2.端口管理问题 3.域名分配及动态更新问题 二.如何创建Ingress资源 三.Ingress资源类型 1.单Service资源型Ingres ...
- Kubernetes学习之路(四)之Node节点二进制部署
K8S Node节点部署 1.部署kubelet (1)二进制包准备 [root@linux-node1 ~]# cd /usr/local/src/kubernetes/server/bin/ [r ...
- Kubernetes学习之路(二)之ETCD集群二进制部署
ETCD集群部署 所有持久化的状态信息以KV的形式存储在ETCD中.类似zookeeper,提供分布式协调服务.之所以说kubenetes各个组件是无状态的,就是因为其中把数据都存放在ETCD中.由于 ...
- Kubernetes学习之路(三)之Mater节点二进制部署
K8S Mater节点部署 1.部署Kubernetes API服务部署 apiserver提供集群管理的REST API接口,包括认证授权.数据校验以及集群状态变更等. 只有API Server才能 ...
- Kubernetes学习之路(五)之Flannel网络二进制部署和测试
一.K8S的ip地址 Node IP:节点设备的IP,如物理机,虚拟机等容器宿主的实际IP. Pod IP:Pod的IP地址,是根据docker0网络IP段进行分配的. Cluster IP:Serv ...
- Kubernetes学习之路(七)之Coredns和Dashboard二进制部署
一.CoreDNS部署 在 Cluster 中,除了可以通过 Cluster IP 访问 Service,Kubernetes 还提供了更为方便的 DNS 访问. (1)编辑coredns.yaml文 ...
- Kubernetes学习之路(八)之Kubeadm部署集群
一.环境说明 节点名称 ip地址 部署说明 Pod 网段 Service网段 系统说明 k8s-master 192.168.56.11 docker.kubeadm.kubectl.kubelet ...
- Kubernetes学习之路(十)之资源清单定义
一.Kubernetes常用资源 以下列举的内容都是 kubernetes 中的 Object,这些对象都可以在 yaml 文件中作为一种 API 类型来配置. 类别 名称 工作负载型资源对象 Pod ...
随机推荐
- [UI] 精美UI界面欣赏[3]
精美UI界面欣赏[3]
- 将字典或者数组转换成JSON数据或者字符串
将字典或者数组转换成JSON数据或者字符串 源码: NSDictionary+JSON.h 与 NSDictionary+JSON.m // // NSDictionary+JSON.h // Cat ...
- $.ajax 在请求没有完成,是可以往下继续执行js代码的
$.ajax({ url:url, data:{}, success:function(arr) { var varHtml='<option value="" checke ...
- Lua脚本语法说明(转):
Lua脚本语法说明(增加lua5.1部份特性) 转自:http://www.cnblogs.com/ly4cn/archive/2006/08/04/467550.html Lua 的语法比较简单,学 ...
- [2018HN省队集训D5T1] 沼泽地marshland
[2018HN省队集训D5T1] 沼泽地marshland 题意 给定一张 \(n\times n\) 的棋盘, 对于位置 \((x,y)\), 若 \(x+y\) 为奇数则可能有一个正权值. 你可以 ...
- 【Alpha】Daily Scrum Meeting——blog1
团队成员 吴吉键 201421122007(组长) 魏修祺 201421122008 孙劲林 201421122022 1. 会议当天照片 忘记拍了,能补上不? 2. 每个人的工作 3. 燃尽图
- grep命令.md
grep命令 简介 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Prin ...
- 1202. [HNOI2005]狡猾的商人【贪心 或 并查集】
Description 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的.账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i=1,2,3...n-1,n), .当 ...
- Docker实战(十)之分布式处理与大数据平台
分布式系统和大数据处理平台是目前业界关注的热门技术. 1.RabbitMQ RabbitMQ是一个支持AMQP的开源消息队列实现,由Erlang编写,因以高性能.高可用以及可伸缩性出名.它支持多种客户 ...
- Python 把数据库的数据导出到excel表
import io,xlwt def export_excel(request): """导出数据到excel表""" list_obj = ...