K8S系列-通过YAML文件/命令创建一个Nginx服务(pod)
开始在完成K8S集群部署之后,我们可以小试牛刀,先创建一个简单应用进行测试通过YAML创建K8S支持创建YAML文件,我们可以通过K8S自动创建的YAML内容进行编辑,创建YAML内容方法如下:kubectl run myapp --image=nginx --dry-run -o yaml执行结果:root@debian:~# kubectl run myapp --image=nginx --
·
开始
在完成K8S集群部署之后,我们可以小试牛刀,先创建一个简单应用进行测试
通过YAML创建
K8S支持创建YAML文件,我们可以通过K8S自动创建的YAML内容进行编辑,创建YAML内容方法如下:
kubectl run myapp --image=nginx --dry-run -o yaml
执行结果:
root@debian:~# kubectl run myapp --image=nginx --dry-run -o yaml
W0609 17:30:23.266011 62036 helpers.go:557] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: myapp
name: myapp
spec:
containers:
- image: nginx
name: myapp
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
然后把反馈的内容复制到一个yaml文件进行修改即可,例如:
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
将修改完的内容输入到一个文件之后,使用apply -f
进行一个应用即可创建,过程如下:
root@master:~# cat nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
root@master:~# kubectl get pod
No resources found in default namespace.
root@master:~# kubectl apply -f nginx.yaml
pod/nginx created
root@master:~# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 0/1 ContainerCreating 0 2s
root@master:~# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 0/1 ContainerCreating 0 10s
root@master:~# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 28s
root@master:~#
可以看到,此时这个Nginx的Pod就起来了
通过终端命令创建
同样的,kubectl也提供了类似docker run
的一个运行方法,例如:
kubectl run --image=nginx:alpine nginx-app --port=80
执行效果
root@master:~# kubectl run --image=nginx nginx-app --port=80
pod/nginx-app created
root@master:~# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app 0/1 ContainerCreating 0 8s
root@master:~# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app 0/1 ContainerCreating 0 14s
root@master:~# kubectl logs nginx-app
/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
2021/06/17 01:20:21 [notice] 1#1: using the "epoll" event method
2021/06/17 01:20:21 [notice] 1#1: nginx/1.21.0
2021/06/17 01:20:21 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/06/17 01:20:21 [notice] 1#1: OS: Linux 5.4.0-74-generic
2021/06/17 01:20:21 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/06/17 01:20:21 [notice] 1#1: start worker processes
2021/06/17 01:20:21 [notice] 1#1: start worker process 33
2021/06/17 01:20:21 [notice] 1#1: start worker process 34
2021/06/17 01:20:21 [notice] 1#1: start worker process 35
2021/06/17 01:20:21 [notice] 1#1: start worker process 36
root@master:~# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app 1/1 Running 0 48s
root@master:~#
更多推荐
已为社区贡献7条内容
所有评论(0)