查看名称空间:
[root@hdss7-21 ~]# kubectl get namespace # 等同于kubectl get ns
NAME STATUS AGE
default Active 3d22h
kube-node-lease Active 3d22h
kube-public Active 3d22h
kube-system Active 3d22h
查看defalut名称空间下的所有资源:
[root@hdss7-21 ~]# kubectl get all -n default # 等同于kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-ds-pqszs 1/1 Running 0 2d12h
pod/nginx-ds-vnlx9 1/1 Running 0 2d12h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 3d23h
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
daemonset.apps/nginx-ds 0 0 0 0 0 <none> 2d12h
创建名称空间:
[root@hdss7-21 ~]# kubectl create namespace app
namespace/app created
[root@hdss7-21 ~]# kubectl get ns
NAME STATUS AGE
app Active 45s
default Active 3d23h
kube-node-lease Active 3d23h
kube-public Active 3d23h
kube-system Active 3d23h
删除名称空间:
[root@hdss7-21 ~]# kubectl delete namespace app
namespace "app" deleted
[root@hdss7-21 ~]# kubectl get ns
NAME STATUS AGE
default Active 3d23h
kube-node-lease Active 3d23h
kube-public Active 3d23h
kube-system Active 3d23h
创建deployment资源:
# 在名称为kube-public的名称空间创建名称为nginx-db资源进行pod控制
[root@hdss7-21 ~]# kubectl create deployment nginx-dp --image=harbor.od.com/public/nginx:v1.7.9 -n kube-public
deployment.apps/nginx-db created
[root@hdss7-21 ~]# kubectl get deploy -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-db 0/1 1 0 2m28s
[root@hdss7-21 ~]# kubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-db-7b4dd9978d-8wzdl 0/1 Pending 0 2m52s
查看 kube-public名称空间下的Pod
[root@hdss7-21 ~]# kubectl get pods -n kube-public -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-db-7b4dd9978d-8wzdl 0/1 Pending 0 8m15s <none> <none> <none> <none>
# 根据pod名称可以获得对应的容器,一般包含2个容器,分别是基础容器和业务容器
查看详细信息:
[root@hdss7-21 ~]# kubectl describe deployment nginx-db -n kube-public
Name: nginx-db
Namespace: kube-public
CreationTimestamp: Wed, 01 Jul 2020 10:58:43 +0800
Labels: app=nginx-db
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx-db
Replicas: 1 desired | 1 updated | 1 total | 0 available | 1 unavailable
StrategyType: RollingUpdate # 发布策略:滚动升级
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx-db
Containers:
nginx:
Image: harbor.od.com/public/nginx:v1.7.9
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available False MinimumReplicasUnavailable
Progressing False ProgressDeadlineExceeded
OldReplicaSets: <none>
NewReplicaSet: nginx-db-7b4dd9978d (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 17m deployment-controller Scaled up replica set nginx-db-7b4dd9978d to 1
# kubectl发出的指令先找apiserver通信确认,apiserver再找scheduler确认,scheduler在最合适的节点通过kubelet来启动容器
进入pod资源
[root@hdss7-21 ~]# kubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
pod/nginx-dp-5dfc689474-fc6z9 1/1 Running 0 4s
# kubectl exec可以跨主机进入不同的pod
[root@hdss7-21 ~]# kubectl exec -it nginx-dp-5dfc689474-fc6z9 /bin/bash
root@nginx-ds-pqszs:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:07:15:02 brd ff:ff:ff:ff:ff:ff
inet 172.7.21.3/24 brd 172.7.21.255 scope global eth0
valid_lft forever preferred_lft forever
删除pod资源:删除后,pod控制器会立即重启一个新的pod资源,是重启Pod的重要方法
[root@hdss7-21 ~]# kubectl delete pod nginx-db-7b4dd9978d-8wzdl -n kube-public
pod "nginx-db-7b4dd9978d-8wzdl" deleted
# 强制删除
[root@hdss7-21 ~]# kubectl delete pod nginx-db-7b4dd9978d-8wzdl -n kube-public --force --grace-period=0
删除deployment资源
[root@hdss7-21 ~]# kubectl delete deployment nginx-db -n kube-public
deployment.extensions "nginx-db" deleted
[root@hdss7-21 ~]# kubectl get deployment -n kube-public
No resources found.
[root@hdss7-21 ~]# kubectl get pods -n kube-public
No resources found.
[root@hdss7-21 ~]#
创建service资源
# 暴露80端口
[root@hdss7-21 ~]# kubectl expose deployment nginx-dp --port=80 -n kube-public
service/nginx-dp exposed
[root@hdss7-21 ~]# kubectl get all -n kube-public
NAME READY STATUS RESTARTS AGE
pod/nginx-dp-5dfc689474-4dfsq 1/1 Running 0 44m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-dp ClusterIP 192.168.103.156 <none> 80/TCP 29m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-dp 2/2 2 2 44m
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-dp-5dfc689474 2 2 2 44m
查看端口是否可访问
# 当无法curl时,检查宿主机的内核中是否开启了ipv4转发功能
[root@hdss7-21 ~]# cat /proc/sys/net/ipv4/ip_forward
0
[root@hdss7-21 ~]# echo "1" >!$
echo "1" >/proc/sys/net/ipv4/ip_forward
[root@hdss7-21 ~]# cat /proc/sys/net/ipv4/ip_forward
1
[root@hdss7-21 ~]# curl 192.168.103.156
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 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>
# 访问成功
# 查看lvs服务信息
[root@hdss7-21 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.1:443 nq
-> 10.4.7.21:6443 Masq 1 0 0
-> 10.4.7.22:6443 Masq 1 0 0
TCP 192.168.103.156:80 nq
-> 172.7.22.2:80 Masq 1 0 0
对deployment资源扩容:默认创建时只有一个pod资源
[root@hdss7-21 ~]# kubectl scale deployment nginx-dp --replicas=2 -n kube-public
deployment.extensions/nginx-dp scaled
# 查看扩容后的信息
[root@hdss7-21 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.1:443 nq
-> 10.4.7.21:6443 Masq 1 0 0
-> 10.4.7.22:6443 Masq 1 0 0
TCP 192.168.103.156:80 nq
-> 172.7.22.2:80 Masq 1 0 0
-> 172.7.22.3:80 Masq 1 0 0
查看svc详细信息
[root@hdss7-21 ~]# kubectl describe svc nginx-dp -n kube-public
Name: nginx-dp # service资源名称
Namespace: kube-public
Labels: app=nginx-dp
Annotations: <none>
Selector: app=nginx-dp # pod标签名称,将service与pod关联起来
Type: ClusterIP
IP: 192.168.103.156 # 该IP网段对集群内部使用
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 172.7.21.2:80,172.7.22.2:80
Session Affinity: None
Events: <none>