目录
一、NameSpace
二、Pod
【1】命令式对象管理: 直接使用命令去操作kubernetes资源
【2】命令式对象配置: 通过命令配置和配置文件去操作作kubernetes资源
1、写一个ymal文件
2、然后运行yaml文件(提前创建好了命名空间string)
3、用bashboard创建pod
4、一个pod里面运行多个容器
一、NameSpace
- 命名空间,用来对集群资源进行隔离划分。默认只隔离资源,不隔离网络
[root@master ~]# kubectl create ns dev
namespace/dev created
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 23h
dev Active 6s
kube-node-lease Active 23h
kube-public Active 23h
kube-system Active 23h
kubernetes-dashboard Active 77m
二、Pod
- 运行中的一组容器,Pod是kuberneres中应用的最小单位
【1】命令式对象管理: 直接使用命令去操作kubernetes资源
[root@master ~]# kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-56c7cdffc6-trxkj 1/1 Running 1 23h
kube-system calico-node-8zsdw 1/1 Running 1 22h
kube-system calico-node-khp6k 1/1 Running 1 22h
kube-system calico-node-vkqhc 1/1 Running 1 23h
kube-system coredns-7f89b7bc75-65jnr 1/1 Running 1 23h
kube-system coredns-7f89b7bc75-lrtqq 1/1 Running 1 23h
kube-system etcd-master 1/1 Running 1 23h
kube-system kube-apiserver-master 1/1 Running 1 23h
kube-system kube-controller-manager-master 1/1 Running 1 23h
kube-system kube-proxy-9k8ps 1/1 Running 1 23h
kube-system kube-proxy-nvzmj 1/1 Running 1 22h
kube-system kube-proxy-zph7s 1/1 Running 1 22h
kube-system kube-scheduler-master 1/1 Running 1 23h
kubernetes-dashboard dashboard-metrics-scraper-79c5968bdc-l4fjp 1/1 Running 0 70m
kubernetes-dashboard kubernetes-dashboard-658485d5c7-dj8qn 1/1 Running 0 70m
[root@master ~]# kubectl run mynginx --image=nginx
pod/mynginx created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mynginx 0/1 ContainerCreating 0 10s
[root@master ~]# kubectl describe pod mynginx
Name: mynginx
Namespace: default
Priority: 0
Node: node02/172.31.0.2
Start Time: Fri, 06 Jan 2023 23:27:42 +0800
Labels: run=mynginx
Annotations: cni.projectcalico.org/containerID: 2eec547318e2d978dfbbf589915cd684fbd2f8450609583cfe20e43c3d5e39df
cni.projectcalico.org/podIP: 192.168.140.71/32
cni.projectcalico.org/podIPs: 192.168.140.71/32
Status: Running
IP: 192.168.140.71
IPs:
IP: 192.168.140.71
Containers:
mynginx:
Container ID: docker://8f0e73fc980847280fe363558dd6dbae3fd1f803a3823a982208e32d87827f8b
Image: nginx
Image ID: docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Port: <none>
Host Port: <none>
State: Running
Started: Fri, 06 Jan 2023 23:28:52 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-dcw8j (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-dcw8j:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-dcw8j
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 86s default-scheduler Successfully assigned default/mynginx to node02
Normal Pulling 85s kubelet Pulling image "nginx"
Normal Pulled 20s kubelet Successfully pulled image "nginx" in 1m5.956007089s
Normal Created 16s kubelet Created container mynginx
Normal Started 16s kubelet Started container mynginx
进入pod
[root@master ~]# kubectl exec -it mynginx -- /bin/bash
root@mynginx:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
查看log 加上f表示实时查看
[root@master ~]# kubectl logs mynginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/01/09 09:25:47 [notice] 1#1: using the "epoll" event method
2023/01/09 09:25:47 [notice] 1#1: nginx/1.21.5
2023/01/09 09:25:47 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/01/09 09:25:47 [notice] 1#1: OS: Linux 3.10.0-1127.el7.x86_64
2023/01/09 09:25:47 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/01/09 09:25:47 [notice] 1#1: start worker processes
2023/01/09 09:25:47 [notice] 1#1: start worker process 32
2023/01/09 09:25:47 [notice] 1#1: start worker process 33
192.168.219.64 - - [09/Jan/2023:09:33:45 +0000] "GET / HTTP/1.1" 200
【2】命令式对象配置: 通过命令配置和配置文件去操作作kubernetes资源
1、写一个ymal文件
apiVersion: v1 # API版本号,注意:具有多个,不同的对象可能会使用不同API
kind: Pod # 对象类型,pod
metadata: # 元数据
name: mynginx # POD名称
spec: # specification of the resource content(资源内容的规范)
containers: # 容器列表
- name: mynginx # 容器名称
image: nginx # 容器镜像
2、然后运行yaml文件(提前创建好了命名空间string)
[root@master ~]# vi pod.yaml
[root@master ~]# kubectl apply -f pod.yaml
pod/mynginx created
[root@master ~]# kubectl get pod -n string
NAME READY STATUS RESTARTS AGE
mynginx 1/1 Running 0 27s
[root@master ~]# kubectl describe pod mynginx -n string
Name: mynginx
Namespace: string
Priority: 0
Node: node02/172.31.0.2
Start Time: Mon, 09 Jan 2023 17:10:22 +0800
Labels: <none>
Annotations: cni.projectcalico.org/containerID: 5a1eefb7be0f61fc7cceca2c8bfe67e72fe104db89a13b9f58921804278a2904
cni.projectcalico.org/podIP: 192.168.140.74/32
cni.projectcalico.org/podIPs: 192.168.140.74/32
Status: Running
IP: 192.168.140.74
IPs:
IP: 192.168.140.74
Containers:
mynginx:
Container ID: docker://613b31a4f4736788af136f9187bf78d64ac0f6e0b3960a34087ed481ebed7847
Image: nginx
Image ID: docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Port: <none>
Host Port: <none>
State: Running
Started: Mon, 09 Jan 2023 17:10:23 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-tv5f7 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-tv5f7:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-tv5f7
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 52s default-scheduler Successfully assigned string/mynginx to node02
Normal Pulling 51s kubelet Pulling image "nginx"
Normal Pulled 51s kubelet Successfully pulled image "nginx" in 429.471972ms
Normal Created 51s kubelet Created container mynginx
Normal Started 51s kubelet Started container mynginx
[root@master ~]# kubectl delete -f pod2.yaml
pod "mynginx" deleted
3、用bashboard创建pod
4、一个pod里面运行多个容器
先创建一个yaml
apiVersion: v1 # API版本号,注意:具有多个,不同的对象可能会使用不同API
kind: Pod # 对象类型,pod
metadata: # 元数据
name: mynginx # POD名称
namespace: string # 所属的命名空间
spec: # specification of the resource content(资源内容的规范)
containers: # 容器列表
- name: mynginx # 容器名称
image: nginx # 容器镜像
- name: mytomcat
image: tomcat
运行容器
[root@master ~]# kubectl apply -f pod.yaml
pod/mynginx created
[root@master ~]# kubectl get pod -n string
NAME READY STATUS RESTARTS AGE
mynginx 0/2 ContainerCreating 0 23s
[root@master ~]# kubectl get pod -n string -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mynginx 0/2 ContainerCreating 0 33s <none> node01 <none> <none>
[root@master ~]# kubectl get pod mynginx -n string -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mynginx 2/2 Running 0 7m40s 192.168.196.139 node01 <none> <none>
进入容器查看
[root@master ~]# kubectl exec -it mynginx -n string -- /bin/bash
Defaulting container name to mynginx.
Use 'kubectl describe pod/mynginx -n string' to see all of the containers in this pod.
root@mynginx:/# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
root@mynginx:/# curl 127.0.0.1:8080
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/10.0.14</h3></body></html>root@mynginx:/#
!!注意在一个pod里不能有多个应用都用同一个端口