实验:minikuber上运行简单程序

本文通过实战演示了如何使用 Kubernetes 创建和管理 Pod。从克隆课程仓库开始,逐步介绍了部署 Pod、检查状态、暴露服务及访问应用的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 验证实验
[zhoujing@zhouj Code]$ git clone https://github.com/wardviaene/kubernetes-course
正克隆到 'kubernetes-course'...
remote: Counting objects: 90, done.
remote: Total 90 (delta 0), reused 0 (delta 0), pack-reused 90
展开对象中: 100% (90/90), 完成.
[zhoujing@zhouj Code]$ cd kubernetes-course/
[zhoujing@zhouj kubernetes-course]$ kubectl get node
NAME       STATUS    AGE       VERSION
minikube   Ready     1d        v1.7.0
[zhoujing@zhouj kubernetes-course]$ cat first-app/helloworld.yml
apiVersion: v1
kind: Pod
metadata:
  name: nodehelloworld.example.com
  labels:
    app: helloworld
spec:
  containers:
  - name: k8s-demo
    image: wardviaene/k8s-demo
    ports:
    - name: nodejs-port
      containerPort: 3000
  • 创建pod
[zhoujing@zhouj kubernetes-course]$ kubectl create -f first-app/helloworld.yml 
pod "nodehelloworld.example.com" created
[zhoujing@zhouj kubernetes-course]$ kubectl get pod
NAME                             READY     STATUS              RESTARTS   AGE
hello-minikube-180744149-r908g   1/1       Running             1          2h
nodehelloworld.example.com       0/1       ContainerCreating   0          2m
  • 描述pod相关信息
[zhoujing@zhouj kubernetes-course]$ kubectl describe pod nodehelloworld.example.com
Name:       nodehelloworld.example.com
Namespace:  default
Node:       minikube/192.168.99.100
Start Time: Wed, 02 Aug 2017 17:40:49 +0800
Labels:     app=helloworld
Annotations:    <none>
Status:     Pending
IP:     
Containers:
  k8s-demo:
    Container ID:   
    Image:      wardviaene/k8s-demo
    Image ID:       
    Port:       3000/TCP
    State:      Waiting
      Reason:       ContainerCreating
    Ready:      False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-23mkf (ro)
Conditions:
  Type      Status
  Initialized   True 
  Ready     False 
[zhoujing@zhouj kubernetes-course]$ kubectl describe pod nodehelloworld.example.com
Name:       nodehelloworld.example.com
Namespace:  default
Node:       minikube/192.168.99.100
Start Time: Wed, 02 Aug 2017 17:40:49 +0800
Labels:     app=helloworld
Annotations:    <none>
Status:     Pending
IP:     
Containers:
  k8s-demo:
    Container ID:   
    Image:      wardviaene/k8s-demo
    Image ID:       
    Port:       3000/TCP
    State:      Waiting
      Reason:       ContainerCreating
    Ready:      False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-23mkf (ro)
Conditions:
  Type      Status
  Initialized   True 
  Ready     False 
  • 显示本地端口,访问查看信息
[zhoujing@zhouj kubernetes-course]$ kubectl port-forward nodehelloworld.example.com 8081:3000
Forwarding from 127.0.0.1:8081 -> 3000
Forwarding from [::1]:8081 -> 3000
Handling connection for 8081

#在浏览器端口输入:localhost:8081,显示信息内容如下:
Hello World!
  • 暴露pod的端口;显示访问地址
[zhoujing@zhouj kubernetes-course]$ kubectl expose pod nodehelloworld.example.com  --type=NodePort
The Service "nodehelloworld.example.com" is invalid: metadata.name: Invalid value: "nodehelloworld.example.com": a DNS-1035 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name',  or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')
[zhoujing@zhouj kubernetes-course]$ kubectl expose pod nodehelloworld.example.com  --type=NodePort --name nodehelloworld-dervice
service "nodehelloworld-dervice" exposed
[zhoujing@zhouj kubernetes-course]$ minikube service nodehelloworld-dervice --url
http://192.168.99.100:30602
  浏览网页,显示内容如下:

Hello World!

  • 查看service信息
[zhoujing@zhouj kubernetes-course]$ kubectl get service
NAME                     CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
hello-minikube           10.0.0.38    <nodes>       8080:30299/TCP   2h
kubernetes               10.0.0.1     <none>        443/TCP          1d
nodehelloworld-dervice   10.0.0.247   <nodes>       3000:30602/TCP   3m
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值